1.16.5 How to check if player right clicks with custom item

Discussion in 'Spigot Plugin Development' started by Duke12345, Jun 27, 2021.

  1. So this problem is driving me crazy. I want to check if a player right clicks a block with a custom sword I made but it's not working. It works with vannila items though if I switch what item it is looking for. Here is the code I am using:

    Code (Java):
    public class PlayerInteractBlock implements Listener {

        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
           


            if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {


                Player player = event.getPlayer();

                if (event.getItem() == null) {
                    return;
                }
                if (event.getItem().getItemMeta().equals(WoodSwords.woodenV.getItemMeta())) {
                   
                    Block block = event.getClickedBlock();
                    World world = player.getWorld();
                    Location location = block.getLocation();
                    Entity entity = world.spawnEntity(location, EntityType.COW);
                    entity.setCustomName("Moo");
                    entity.setCustomNameVisible(true);
                }



            }

        }

    }
     
    I know the sword is working because I coded a command to give it to a player which works.
    I also tried doing this with by using player.getInventory.getItemInMainHand() which also didn't work.

    Any help would be appreciated thanks!
     
  2. The ItemMeta will not be equals() to your item when it has been damaged already.

    Just set a value on the Item‘s PersistentDataContainer to identify your custom sword.
     
  3. Sorry I'm new to coding how would you do this.

    So far I have:
    Code (Java):
    PersistentDataContainer data = meta.getPersistentDataContainer();
            data.

    I put this in the class were I created to item
     
  4. For example:

    Code (Java):

    // Set the value
    ItemMeta meta = item.getItemMeta();
    PersistentDataContainer pdc = meta.getPersistentDataContainer();
    pdc.set(new NamespacedKey(yourPluginInstance, „mycustomsword“), PersistentDataType.BYTE, (byte) 1);
    item.setItemMeta(meta);

    // Get the value
    boolean isMyCustomSword = false;
    if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(yourPluginInstance, „mycustomsword“), PersistentDataType.BYTE)) {
      isMyCustomSword = true;
    }
    Could contain typos, I wrote this from the phone
     
  5. It didn't work.

    My code in the class were I created the item:

    Code (Java):
    PersistentDataContainer pdc = meta.getPersistentDataContainer();
            //key I used is "wtv"
            pdc.set(new NamespacedKey(Main.getPlugin(Main.class), key), PersistentDataType.BYTE, (byte) 1);
            item.setItemMeta(meta);

    My code in event:

    Code (Java):
    public class PlayerInteractBlock implements Listener {

        @EventHandler
        public void onInteract(PlayerInteractEvent event) {



            if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {


                Player player = event.getPlayer();
                player.sendMessage("y");

                if (event.getItem() == null) {
                    return;
                }

                boolean isMyCustomSword = false;
                if(event.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(Main.getPlugin(Main.class), "wtv"), PersistentDataType.BYTE)) {
                    isMyCustomSword = true;
                }
                if (isMyCustomSword) {

                    Block block = event.getClickedBlock();
                    World world = player.getWorld();
                    Location location = block.getLocation();
                    Entity entity = world.spawnEntity(location, EntityType.COW);
                    entity.setCustomName("Moo");
                    entity.setCustomNameVisible(true);
                }



            }

        }

    }
     
  6. Are you sure the value of „key“ is „wtv“?
     
  7. ye I tried hardcoding it in and I double checked
     
  8. Using persistantDataContainer would probably work, but if you don't want to use it then you can use the clone function on the item the player is holding to make a copy of it, then set that clone's durability to full, then use .equals function to compare it to the known custom item.
     
  9. I would assume that checking the display name and/or lore would be enough, why the entire meta?
     
  10. I'm with @Sigong - PersistantDataContainer is your safest bet.
    It saves headaches and makes sure it's not an item someone randomly got - it only uses a very specific NBT tag that is (almost exclusively) accessed by plugins.
    This allows you to make special keywords to make sure it's an item you want without having to worry about too many specifics (durability, amount, color, etc.) and is a much safer use to other archaic checks that could have EASILY been changed by literally anything else (name, lore, etc.).
    You've got no idea how easy it has made the lives of plugin makers since its introduction in 1.14...
    So yeah, use the PersistantDataContainer.