Adding unsafe enchantments to item

Discussion in 'Plugin Development' started by Honeyjuice14, Dec 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Honeyjuice14

    As the anvil limits you to level 5 enchants, my server gives players 6-7 level books but they cant be used. My idea for a work around is they right click the book, then right click what they want to enchant at the cost of levels.
    Here is my code so far:
    Code:
        public boolean state = false;
        public EnchantmentStorageMeta meta;
        @EventHandler
        public void onRC(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (p.getItemInHand().getType() == Material.ENCHANTED_BOOK) {
                ItemStack book = p.getItemInHand();
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) book
                        .getItemMeta();
                p.sendMessage(ChatColor.GOLD
                        + "Right click the item you would like to apply "
                        + ChatColor.AQUA + meta + ChatColor.GOLD + " to.");
                state = true;
            }
            if ((state == true) && (p.getItemInHand().getType() == Material.DIAMOND_AXE)) {
                ItemStack resultItem = p.getItemInHand();
                resultItem.addUnsafeEnchantments(meta.getStoredEnchants());
            }
        }
    There are a couple of problems i am having with the code
    1. when it sends them a message its not just the name of the enchant, but a weird {enchant_type: durability...} type thing, how do i get just the name
    2. when they right click the tool, the enchant is not put on the tool and i get a null pointer in console (on the line where the unsafe enchant is applied)
     
  2. Offline

    TwerkinCraft

    There are a few things wrong with your code, first, when the message is sent to the player, you are putting in the toString() representation of the meta rather than a human readable version of it. Second, the state boolean is not saved per player so if one player were to click the book, and another player were to click their sword, the second player would steal the first players enchantment. Third, the book isn't removed from the player's inventory after enchantment, not sure if this is intended or not. Lastly, it returns a null pointer because you are initializing a new EnchantmentStorageMeta inside of the if statement rather than setting the global one outside of the void (which should be per player also) to the items meta, thus keeping it a null value when you are ready to enchant. Hope this helps :D
     
Thread Status:
Not open for further replies.

Share This Page