Adding a new enchantment removes my custom enchantment

Discussion in 'Plugin Development' started by Swompd, Jul 11, 2022.

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

    Swompd

    I created a custom enchantment that when you mine iron/gold ore it automatically smelts it, but whenever I add unbreaking or efficiency to the pickaxe my custom enchant doesn't work. Does anyone know how to solve this?

    Here's my code:

    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
          
            if (p.getInventory().getItemInMainHand() != null &&
                    p.getInventory().getItemInMainHand().getEnchantments().containsKey(Enchantment.getByKey(NEMain.r.getKey()))) {
              
                if (p.getGameMode()== GameMode.SURVIVAL) {
                    if (p.getInventory().getItemInMainHand().getType()== Material.IRON_PICKAXE || p.getInventory().getItemInMainHand().getType()== Material.DIAMOND_PICKAXE) {
                        if (e.getBlock().getType()== Material.IRON_ORE) {
                            e.setDropItems(false);
                          
                            e.isDropItems();
                          
                            p.getInventory().addItem(new ItemStack(Material.IRON_INGOT));
                        }
                      
                        if (e.getBlock().getType()== Material.GOLD_ORE) {
                            e.setDropItems(false);
                          
                            e.isDropItems();
                          
                            p.getInventory().addItem(new ItemStack(Material.GOLD_INGOT));
                        }
                    }
                }
              
            }
        }
    I fixed it by simply looping through all the lines of lore and checking if it had my custom enchant name in one of the lines.

    Well now whenever I craft my item with Unbreaking on it it just completely removes the Unbreaking.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 21, 2022
  2. Offline

    Strahan

    What MC version? If you are coding for newer versions (I think 1.14+) I'd just use a PersistentDataContainer tag.
     
  3. Offline

    man_in_matrix


    Hmm, maybe instead of using a kvp try to add the enchantment as a piece of lore to the item then check if the item is correct using a method which takes in the lore of an item as an arraylist of type string, as well as the enchant's name as a string.

    something like

    public static boolean hasEnch(List<String> lore, String enchantment) {}

    then loop through each of the strings of the lore and check if that string contains(enchantment) id use tolowercase to make life easier.
     
Thread Status:
Not open for further replies.

Share This Page