Check when player hold an item with a specific name

Discussion in 'Plugin Development' started by Eusart, Jul 26, 2017.

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

    Eusart

    Hello! to all, Im trying to make a " Factions Claim Wands" Its suppose to claim and unclaim when you click, there is two versions of it(2 wands), the problem is when I try to check if an item in inventory it's equal to the ItemStack, then the [if] isn't working, the plugin goes directly to the code inside the [if] and there is no the iteminmainhand = to the itemstack e.

    here's my code:
    Code:
    package net.eusart.FactionsClaimWands;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public final class firsto extends JavaPlugin implements Listener {
    /** INIT DE CODE >:X
    */
    
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            getLogger().info(ChatColor.AQUA + "**WORKS");
            Player p = event.getPlayer();
            //
            ItemStack itemx = new ItemStack(Material.STICK);
            ItemMeta im = itemx.getItemMeta();
            im.setDisplayName (ChatColor.AQUA + "Claim Wand");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_AQUA + "- Click to claim");
            im.setLore(lore);
            itemx.setItemMeta(im);
    
            ////////////////////////////////////////////////////////////
           
            if(p.getInventory().getItemInMainHand() == (itemx));
            {
                p.performCommand("f claim");
                p.sendMessage(ChatColor.DARK_AQUA + "> Clicks!)");
            }
                ///////////////
                ItemStack itemz = new ItemStack(Material.STICK);
                ItemMeta imz = itemz.getItemMeta();
                imz.setDisplayName (ChatColor.RED + "Wand of Unclaiming");
                List<String> lorez = new ArrayList<String>();
                lorez.add(ChatColor.DARK_AQUA + "- Click to unclaim");
                imz.setLore(lorez);
                itemz.setItemMeta(imz);           
            ////////////// ITEMstack of unclaiming
    
            if(p.getInventory().getItemInMainHand().equals(itemz));
            {
                p.performCommand("f unclaim");
                p.sendMessage(ChatColor.DARK_AQUA + "> Click!)");
              }
           
           
           
           
           
             }
       
       
       
           
    /**
    * END INT Code    
    */
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info(ChatColor.AQUA + "**FactionsClaimWands has been turned ON");
        }
    
        @Override
        public void onDisable() {
            getLogger().info(ChatColor.RED +"FactionsClaimWands has been turned OFF");
        }
    }
     
  2. Offline

    RRGamingZ

    I think you should check if the item has meta, if it has then check if the meta is same as whatever you are trying to compare it to...
     
    Eusart likes this.
  3. Offline

    Machine Maker

    @Eusart
    1. Do not use == to compare objects. Use .equals() instead. The == operator compares the memory space of each object, .equals() compares the values.

    2. Compare the type of the Material Type of each item stack and then check the meta display name.
     
    Eusart likes this.
  4. Offline

    Eusart

    the {if} changed to >

    Code:
    if(p.getInventory().getItemInMainHand().getItemMeta().getLore().equals(itemz));
            {
                p.performCommand("f unclaim");
                p.sendMessage(ChatColor.DARK_AQUA + "> Click!)");
              }
    Okey, now is working only if I've the item in had, but the second if executes in anycase if the item isnt in the hand
    (new code link)
     
  5. Offline

    Zombie_Striker

    @Eusart
    1. Do not log your own plugins. Bukkit does this for you. Remove the logger lines.
    2. If statements cannot have semicolons after them. That just invalidates the if statement.
    3. Check if the types and displaynames are the same.
    4. Do not create a new instance of the itemstack every time a player clicks on anything. Instead, only do in in the onEnable and store the itemstacks as a field.
     
    Eusart likes this.
  6. Offline

    Eusart

    2 -> It has its if semicolons after them
    - Example how store the itemstacks as a fields?
     
  7. Offline

    Zombie_Striker

    @Eusart
    Did you remove the semi-colons, or do you have a question about it?

    Fields are are just variables outside of methods, stored in the class instance. Here is how you would set up the itemstacks
    Code:
    Itemstack is1 = null;
    
    public void onEnable(){
      is1 = new ItemStack(....); 
    }
    from there, you can just copy and paste the code for setting the displayname and lore into the onEnable.
     
    Eusart likes this.
Thread Status:
Not open for further replies.

Share This Page