1.14.4 InventoryClickEvent Anvil does not do anything

Discussion in 'Spigot Plugin Development' started by AVCK, Jan 13, 2020.

  1. I'm trying to make an anvil credit system, where without credit, you will not be able to use the anvil. But when I tried using this method, the game does not print anything, no errors nor any of these messages. Any help?
    Code (Java):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if (e.getInventory().getType() == InventoryType.ANVIL && e.getCurrentItem() == e.getInventory().getItem(2)){
                if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL)> 4) && e.getInventory().getItem(0) != null
                        && e.getInventory().getItem(1) != null){


                    Player p = (Player) e.getWhoClicked();

                    if (Bindenchant.globalMap.get(p) == 0){
                        p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                        e.setCancelled(true);
                    } else if (Bindenchant.globalMap.get(p)>0){
                        int a = Bindenchant.globalMap.get(p)-1;
                        Bindenchant.globalMap.put(p, a);
                        p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired. Your remaining credit is " + a + ".");
                    } else if (Bindenchant.globalMap.get(p) == null){
                        p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                        e.setCancelled(true);
                        Bindenchant.globalMap.put(p, 0);
                    }

                }
            }
        }
    }
     
  2. MiMiMiEnot

    MiMiMiEnot Previously Artem1997

    Are you registry event in onEnable?

    UPD:

    Code (Text):
    if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                   || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL)> 4) && e.getInventory().getItem(0) != null
                        && e.getInventory().getItem(1) != null)
    You use || and && in one if, maybe that is causer of problem.

    Try create two "if" blocks. One with "Ors" data and one with "and" data.
    Like
    Code (Text):
    if(X || Y || Z){
    if(C && V && B){
    }
    }
     
  3. how do I register the event on onEnable?
     
  4. MiMiMiEnot

    MiMiMiEnot Previously Artem1997

    in your onEnable method


    Code (Text):
    Bukkit.getPluginManager().registerEvents(this, new <Name of listener where is a Event>());
     
  5. this produced an error. I tried
    Code (Java):
       Bukkit.getPluginManager().registerEvents(new ListenerName(), this);
    and this seems to not produce an error. Is this correct?
     
    • Like Like x 1
  6. yes he just had it backwards. reading the javadocs would tell you more accurately
    /e do note that the event class has to implement Listener as well
     
  7. If this ia your actual code, then no. (Unless the class is named ListenerName, which is probably not)

    Post the stacktrace.
     
  8. I've added my actual listener name in there, but it still doesn't work. Here's my full code if anyone will tell me something is wrong. It still does not do anything.
    Code (Java):
    package me.joseph.bindenchant.bindenchant.enchants;

    import me.joseph.bindenchant.bindenchant.Bindenchant;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.inventory.PrepareAnvilEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Anvil implements Listener {

        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent event) {
            //Get item in first slot
            ItemStack firstSlot = event.getInventory().getItem(0);
            //Get resulting item of inventory
            ItemStack resultItem = event.getResult();

            //Check if resulting item is valid (you should probably also do this for firstSlot)
            if(resultItem.getType() != Material.AIR && resultItem.hasItemMeta()) {
                ItemMeta meta = resultItem.getItemMeta();
                if(resultItem.getItemMeta().hasDisplayName()) {

                    //Set the display name
                    meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());
                    resultItem.setItemMeta(meta);
                    //Set the resulting item
                    event.setResult(resultItem);
                }
            }
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory().getType() == InventoryType.ANVIL && e.getCurrentItem() == e.getInventory().getItem(2)) {
                if (e.getInventory().getItem(0) != null) {
                    if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL)> 4)){


                        Player p = (Player) e.getWhoClicked();
                        ItemStack finalitem = e.getInventory().getItem(2);

                        if (Bindenchant.globalMap.get(p) == 0){
                            p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                            e.setCancelled(true);
                        } else if (Bindenchant.globalMap.get(p)>0){
                            int a = Bindenchant.globalMap.get(p)-1;
                            Bindenchant.globalMap.put(p, a);
                            p.setItemOnCursor(finalitem);
                            p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired. Your remaining credit is " + a + ".");
                        } else if (Bindenchant.globalMap.get(p) == null){
                            p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                            e.setCancelled(true);
                            Bindenchant.globalMap.put(p, 0);
                        }

                    }
                }
            }
        }
    }

     
     
  9. system.out'ing first line of each event to see if theyre called?

    /e to save you trouble, just debug your methods. put a debug at the beginning and after every if statement, changing each one, so you know how far your code gets.
     
  10. I tried this, and my code now looks like this:
    Code (Java):


    public class Anvil implements Listener {

        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent event) {
            //Get item in first slot
            ItemStack firstSlot = event.getInventory().getItem(0);
            //Get resulting item of inventory
            ItemStack resultItem = event.getResult();

            //Check if resulting item is valid (you should probably also do this for firstSlot)
            if(resultItem.getType() != Material.AIR && resultItem.hasItemMeta()) {
                ItemMeta meta = resultItem.getItemMeta();
                if(resultItem.getItemMeta().hasDisplayName()) {

                    //Set the display name
                    meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());
                    resultItem.setItemMeta(meta);
                    //Set the resulting item
                    event.setResult(resultItem);
                }
            }
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory().getType() == InventoryType.ANVIL && e.getCurrentItem() == e.getInventory().getItem(2)) {
                if (e.getInventory().getItem(0) != null) {
                    if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                            || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL)> 4)){


                        Player p = (Player) e.getWhoClicked();
                        ItemStack finalitem = e.getInventory().getItem(2);
                        Bukkit.broadcastMessage("1");

                        if (Bindenchant.globalMap.get(p) == 0){
                            p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                            e.setCancelled(true);
                            Bukkit.broadcastMessage("2");
                        } else if (Bindenchant.globalMap.get(p)>0){
                            int a = Bindenchant.globalMap.get(p)-1;
                            Bindenchant.globalMap.put(p, a);
                            p.setItemOnCursor(finalitem);
                            Bukkit.broadcastMessage("3");
                            p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired. Your remaining credit is " + a + ".");
                        } else if (Bindenchant.globalMap.get(p) == null){
                            p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                            e.setCancelled(true);
                            Bukkit.broadcastMessage("4");
                            Bindenchant.globalMap.put(p, 0);
                        }

                    }
                }
            }
        }
    }

     

    None of these get triggered. Is this an issue with this statement?
    Code (Java):
            if (e.getInventory().getType() == InventoryType.ANVIL && e.getCurrentItem() == e.getInventory().getItem(2)
    How can I fix it?
     
  11. yes. i dont believe you can use == to compare those item stacks. it creates a new instance every time, wrapping the handle of the nms itemstack. youll want to use #equals or find the right method that compares the items, like #isSimilar (altho not sure what class thats attached to)
     
  12. I just want this code to check if the player is clicking the second slot of the anvil. Is there a correct way to check this?
     
  13. the slot of the result in the anvil is definitely 2, and I ran the code with the raw id to check if it was indeed 2. Now my code looks like this:

    Code (Java):
     @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory().getType() == InventoryType.ANVIL ) {
                if (e.getInventory().getItem(0) != null) {

                    if (e.getCurrentItem() == e.getInventory().getItem(2)) {
                        String n = String.valueOf(e.getRawSlot());
                        Bukkit.broadcastMessage(n);
                        if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 4)) {


                            Player p = (Player) e.getWhoClicked();
                            ItemStack finalitem = e.getInventory().getItem(2);
                            Bukkit.broadcastMessage("1");

                            if (Bindenchant.globalMap.get(p) == 0) {
                                p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                                e.setCancelled(true);
                                Bukkit.broadcastMessage("2");
                            } else if (Bindenchant.globalMap.get(p) > 0) {
                                int a = Bindenchant.globalMap.get(p) - 1;
                                Bindenchant.globalMap.put(p, a);
                                p.setItemOnCursor(finalitem);
                                Bukkit.broadcastMessage("3");
                                p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired. Your remaining credit is " + a + ".");
                            } else if (Bindenchant.globalMap.get(p) == null) {
                                p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                                e.setCancelled(true);
                                Bukkit.broadcastMessage("4");
                                Bindenchant.globalMap.put(p, 0);
                            }

                        }
                    }
                }
            }

    This is definitely the problem of the if statement checking which slot is the item clicked is in. I've checked and it is 2, but when I input 2 in the code, it never works. HELP!!!
     
  14. Try checking by using e.getRawSlot() == 2 and also check if clicked item is not AIR
     
  15. thats what im saying. dont compare the items, i dont think that will ever return true. just check the slot that was clicked, rather than checking the item in the slot that was clicked. if you just do what hammodrex said, you achieve the same thing
     
  16. Thanks, now it works. But I'm also trying to make the renaming with color thing work. It should rename based on the renamed name on the anvil and it does visually, but when I click the result item, the repair is finished but then the item on my cursor is just the name before the rename. Lets say I had a weapon with a name of [Name], and i want to rename it to [Rename]. The final slot of the anvil is visually showing [Rename] but when I click it and drag it to my inventory, it is set back to [Name]. Can you help me figure this out?
    Code (Java):
    public class Anvil implements Listener {

        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent event) {
            //Get item in first slot
            ItemStack firstSlot = event.getInventory().getItem(0);
            //Get resulting item of inventory
            ItemStack resultItem = event.getResult();

            //Check if resulting item is valid (you should probably also do this for firstSlot)
            if(resultItem.getType() != Material.AIR && resultItem.hasItemMeta()) {
                ItemMeta meta = resultItem.getItemMeta();
                if(resultItem.getItemMeta().hasDisplayName()) {

                    //Set the display name
                    meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());
                    resultItem.setItemMeta(meta);
                    //Set the resulting item
                    event.setResult(resultItem);
                }
            }
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory().getType() == InventoryType.ANVIL ) {
                if (e.getInventory().getItem(0) != null) {

                    if (e.getRawSlot() == 2) {
                        String n = String.valueOf(e.getRawSlot());
                        Bukkit.broadcastMessage(n);
                        if ((e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                                || e.getInventory().getItem(2).getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 4)) {


                            Player p = (Player) e.getWhoClicked();

                            Bukkit.broadcastMessage("1");

                            if (Bindenchant.globalMap.get(p) == 0) {
                                p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                                e.setCancelled(true);
                                Bukkit.broadcastMessage("2");
                            } else if (Bindenchant.globalMap.get(p) > 0) {
                                int a = Bindenchant.globalMap.get(p) - 1;
                                Bindenchant.globalMap.put(p, a);
                                ItemStack finalitem = e.getInventory().getItem(2);
                                p.setItemOnCursor(finalitem);

                                ItemStack air = new ItemStack(Material.AIR);
                                e.getInventory().setItem(0,air);
                                e.getInventory().setItem(1,air);
                                e.getInventory().setItem(2,air);
                                Bukkit.broadcastMessage("3");
                                p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired. Your remaining credit is " + a + ".");
                            } else if (Bindenchant.globalMap.get(p) == null) {
                                p.sendMessage(ChatColor.DARK_RED + "You don't have repair credits!");
                                e.setCancelled(true);
                                Bukkit.broadcastMessage("4");
                                Bindenchant.globalMap.put(p, 0);
                            }

                        }
                    }
                }
            }

        }
    }