Custom anvil inventory not all allowing item rename

Discussion in 'Plugin Development' started by Banjer_HD, Jun 11, 2017.

Thread Status:
Not open for further replies.
  1. Hello there,

    My code only comes to: "Its the slot" and never reaches: "Its the item!".
    It looks like the event is canceling because if I click the renamed name tag it cancels and I cant drag it anymore.
    Can someone help? Thank you in advance!

    Code:
    Code:
    public class SomeClass implements Listener {
          
        @EventHandler
        public void onPlace(BlockPlaceEvent e) {
            if(e.getBlock().getType().equals(Material.BEACON)) {
               
                Inventory nameinv = Bukkit.createInventory(null, InventoryType.ANVIL, ChatColor.RED + "Name your team");
               
                ItemStack nametag = new ItemStack(Material.NAME_TAG, 1);
                ItemMeta nametagmeta = nametag.getItemMeta();
               
                nametagmeta.setDisplayName("Name your Team");
               
                nametag.setItemMeta(nametagmeta);
                nameinv.setItem(0, nametag);
               
                e.getPlayer().openInventory(nameinv);
               
            }
        }
       
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
           
            if(e.getClickedInventory().getType().equals(InventoryType.ANVIL) && ChatColor.stripColor(e.getClickedInventory().getName()).equals("Name your team")) {
                Bukkit.broadcastMessage("Its the inv!");
               
               
                InventoryView view = e.getView();
                int rawSlot = e.getRawSlot();
               
                if(rawSlot == view.convertSlot(rawSlot) && rawSlot == 2) {
                   
                    Bukkit.broadcastMessage("Its the slot!");
                   
                   
                    ItemStack item = e.getCurrentItem();
                   
                    if(item != null && item.getType().equals(Material.NAME_TAG)){
                       
                        Bukkit.broadcastMessage("Its the item!");
                       
                       
                        ItemMeta meta = item.getItemMeta();
                       
                       
                       
                        if(meta.hasDisplayName()) {
                           
                           
                            String displayName = meta.getDisplayName();
                            Player p = (Player) e.getWhoClicked();
                           
                            Bukkit.broadcastMessage("WORKING!" + displayName);
                            //DO SOMETHING
                                   
                                   
                                   
                                   
                        }
                               
                               
                               
                    }   
                }
            }
           
        }
       
       
       
    }
    
    
     
  2. Offline

    Zombie_Striker

    Something is false in this statement. Do you know if rawSlot is equal to 2? Are you sure that rowslot is equal to the number that converslot returns?
     
  3. My code only comes to: "Its the slot" and never reaches: "Its the item!".
    So that code works, in an anvil you have 3 slots:
    0 = input, 1 = input 2, 2 = output
     
  4. Offline

    Zombie_Striker

    @Banjer_HD
    Then your issue is that the current item is not a nametag. Can you print out what it is equal to?
     
    Banjer_HD likes this.
  5. @Zombie_Striker Thanks for your reply!
    Code:
    if(e.getCurrentItem() != null) {
        Bukkit.broadcastMessage(e.getCurrentItem().getType().toString());
    }else {
        Bukkit.broadcastMessage("Item is null");
    }
    
    It returns null, weird, do you know why?

    It looks like it cancels the click: https://gifyu.com/images/java.gif
     
  6. Offline

    Zombie_Striker

    @Banjer_HD
    Most likely, the event is being called after you pick it up. Try replacing getCurrentItem with getCursor.
     
Thread Status:
Not open for further replies.

Share This Page