Solved How do I get a specific slot?

Discussion in 'Plugin Development' started by webbhead, Jul 3, 2015.

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

    webbhead

    I am wondering how to get a specific item slot in a players inventory. Here is my current code:

    Whole event:
    Code:
        @EventHandler
        public void addJoin(PlayerJoinEvent e) {
            ItemStack i = new ItemStack(Material.DIAMOND_PICKAXE, 1);
            i.addUnsafeEnchantment(Enchantment.DURABILITY, 100);
            i.addEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 1);
            i.addEnchantment(Enchantment.DIG_SPEED, 1);
            ItemMeta m = i.getItemMeta();
            m.setDisplayName(pickname);
            m.setLore(Arrays.asList("ยง7Right-Click to upgrade pickaxe."));
            i.setItemMeta(m);
            Player p = e.getPlayer();
            if (!(p.getInventory().getItem(0).getItemMeta().getDisplayName() == i.getItemMeta().getDisplayName())) {
                p.getInventory().setItem(0, i);
            }
        }
    Checking Slot (Didn't Work):
    Code:
            if (!(p.getInventory().getItem(0).getItemMeta().getDisplayName() == i.getItemMeta().getDisplayName())) {
                p.getInventory().setItem(0, i);
    }
    Can anyone help me out here?

    I really need this resolved. I need it to make my plugin work, this is literally the last thing I need...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. You don't use "==" operator for an Object unless it is a null check, use .equals(), which does a value check rather than "==" which does a reference check (not what you want). Also make sure the item meta is not null and the item display name is not null before using the .equals() method on it.

    More information: perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html

    In general, there are a few things wrong with this code:

    Firstly, you're creating a new ItemStack every time a player joins, so why not just make it a global variable? It's more efficient that way.

    Secondly, what if a player doesn't have an item in Slot 1 (0)? I believe it'll throw a NullPointerException, so first make sure to check if the slot ItemStack is not null.

    Thirdly, points in first paragraph about item meta being null or display name null.
     
    Last edited: Jul 3, 2015
  3. Offline

    webbhead

    Ahh!! Sorry I literally just figured out and came to put this to solved. But thanks for the advice... I always thought == and .equals() were the same o_o
     
Thread Status:
Not open for further replies.

Share This Page