Solved What am I missing here?

Discussion in 'Plugin Development' started by RAFA_GATO_FOFO, May 24, 2014.

Thread Status:
Not open for further replies.
  1. Hey,

    So I'm making a simple region select wand using flint as an item.

    This first wandMeta() method returns the meta displayed below.
    Code:java
    1. public static ItemMeta wandMeta(){
    2. ItemStack wand = new ItemStack(Material.FLINT);
    3. ItemMeta wandMeta = wand.getItemMeta();
    4. List<String> loreList = new ArrayList<String>();
    5. loreList.add(Core.TAG+"Use this to select your Arena");
    6. wandMeta.setDisplayName(Core.TAG+"Wand ยง8(Left/Right Click)");
    7. wandMeta.setLore(loreList);
    8. return wandMeta;
    9. }
    10.  


    This method will give a Flint to the player with the above method's meta.
    Code:java
    1.  
    2.  
    3. public void giveWand(Player player){
    4. if(player.getInventory().firstEmpty() == -1){
    5. player.sendMessage(Core.TAG+"Your backpack is full!");
    6. return;
    7. }
    8. ItemStack wand = new ItemStack(Material.FLINT);
    9. wand.setItemMeta(wandMeta());
    10. player.getInventory().addItem(wand);
    11. player.sendMessage(Core.TAG+"Left Click to select Pos1");
    12. player.sendMessage(Core.TAG+"Right Click to select Pos2");
    13. return;
    14. }


    But my issue is that when I check for the meta on the item like so it doesn't work:
    (This is just a test command)
    Code:java
    1. if (args[0].equalsIgnoreCase("test")){
    2. if (player.getInventory().getItemInHand().getItemMeta() == wandMeta()){
    3. player.sendMessage("This is a wand.");
    4. return true;
    5. }
    6. player.sendMessage("This is not a wand.");
    7. return true;
    8. }


    It always says "This is not a wand". Any suggestions?

    Regards.
     
  2. Code:java
    1. if (player.getInventory().getItemInHand().getItemMeta().equals(wandMeta())){

    Should work.
     
  3. Offline

    xTigerRebornx

    RAFA_GATO_FOFO As michiletsplay said, you'll want to use ItemMeta#equals() to compare the 2 ItemMetas (ItemMeta overrides Object#equals() and does a check on its components such as name, lore, enchants, and others)
     
  4. Offline

    NonameSL

    You should also check if the item in hands item meta isn't null.
    Code:java
    1. if(player.getItemInHand().getItemMeta()!=null){
     
  5. Offline

    xTigerRebornx

    NonameSL I don't believe ItemStack#getItemMeta() can return null (see CraftItemStack), it will make a new instance if it doesn't exist when ItemStack#getItemMeta() is called.
     
  6. michiletsplay
    That works fine, thanks a lot!

    NonameSL
    If a player decides to add a name to a flint it'll have Item Meta even though it's not a wand, so that's not the optimal way to go for me. Only because I'm not using permissions as of now.
     
  7. Offline

    AoH_Ruthless

    RAFA_GATO_FOFO
    I think he meant as an extra check (first check if item meta is not null, then check if it's equal to your wand meta). But I think xTigerRebornx is also correct in saying that itemmeta can't be null.
     
  8. Offline

    NonameSL

    I said ADD the boolean to the if's, not replace it. Plus, a flint or any itemstack always has itemmeta, the check is just if the item is null.
     
  9. NonameSL
    This was fixed on Saturday, what's with you.
     
Thread Status:
Not open for further replies.

Share This Page