Solved Test for item name

Discussion in 'Plugin Development' started by Jumb_1907, Feb 10, 2017.

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

    Jumb_1907

    Hey,

    It's not working,
    I want to check for item names.
    It works without this line of code :
    Code:
          if (blazerod.getItemMeta().getDisplayName().equals("test")){

    Code:
    package com.jumbo1907.empirewand1.spells;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.util.Vector;
    
    public class Tnt implements Listener{
       
       
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
           
            Action action = e.getAction();
            Player player = e.getPlayer();
            ItemStack blazerod = new ItemStack(Material.BLAZE_ROD);
           
           
           
        if (action == Action.LEFT_CLICK_AIR) {
        if (player.getInventory().getItemInHand().equals(blazerod)) {
            if (blazerod.getItemMeta().getDisplayName().equals("test")){
            Player p = e.getPlayer();
            Location loc1 = p.getLocation();
            Vector vector = p.getEyeLocation().getDirection();
            vector.setX(vector.getX() * 2);
            vector.setY(vector.getY() * 2);
            vector.setZ(vector.getZ() * 2);
            e.getPlayer().getWorld().spawnEntity(loc1, EntityType.PRIMED_TNT).setVelocity(vector);
                        }
                    }
        }
    }
    }
    
     
  2. Offline

    JanTuck

    .... The blazerod has no displayname... I cant write a in depth description on everything here you are doing kindof wrong but im sure. @ZombieStriker would like to.

    Sendt fra min ALE-L21 med Tapatalk
     
  3. Offline

    xelatercero

    "equals(String)" is used to compare strings use "==" instead


    You are cheking if blazerot have display name, and thats is not true
     
  4. Offline

    ipodtouch0218

    @xelatercero

    No. You are sooooo wrong. "==" is used to compare memory space. equals(Object obj) is used to compare object values.

    @Jumb_1907
    You're right for using .equals(), however, using ItemStack#isSimilar disregards Item Amounts, so if there are more in a stack it will still activate.
    As @JanTuck said, you never specify "blazerod"s name.
    Code:
    ItemStack blazerod = new ItemStack(Material.BLAZE_ROD)
     
    mine-care and Rayzr522 like this.
  5. Offline

    xelatercero

    @ipodtouch0218 then why always i used equals() to compare itemstacks , other people like timtower, zombieStriker, said me use == instead of equals?
     
  6. Offline

    ipodtouch0218

  7. Offline

    mine-care

    @xelatercero
    Aha! One of my favourite topics :D

    Object#equals(Object param) vs Object == Object:
    The operator '==' compares two items and in the case of Objects, returns true if and only if the two pointers point to the same Object.
    For example:
    Code:
    AnObject a = new AnObject();
    AnObject b = a;
    
    boolean thisIsTrue =  a==b;
    
    The boolean is true because b and a point to the same Object.
    However:
    Code:
    AnObject a = new AnObject("SameText");
    AnObject b = new AnObject("SameText");
    
    boolean thisIsFalse = a==b;
    Here although the operands apear to be the same because they contain the same values (lets suppose), the == operator gives false because they point to different Objects.

    now the method .equals() which exists for all Objects (As it is defined in the mother of all, Object) does indeed check if the pointers point at the same Object, however it does not do just that. It also examines if the two operands are the same in the sence of values contained etc.
    Therefore in the second piece of code i provided above, although == is false, .equals() would return true.

    == is generaly faster than .equals() but the difference in such aplications is minor. Where it actually counts is when you are talking about massively repetitive aplications where the slightest extra delay translates to hours or in efficiency-sensitive enviroments.

    So, when should i use == to compare two operants?
    You may use it:
    • When comparing primitive datatypes.
    • When comparing enum constants.
    • When checking if pointers point to the same Object
     
  8. Offline

    Rayzr522

    @Jumb_1907 You'll want to check if getItemInHand().getType() is BLAZE_ROD, and for that you can use ==, but before you do that you need quite a few checks. First off, getItemInHand() is sometimes null, so you'll want to make sure it isn't null first. If you don't you'll get NPEs. Secondly, you'll want to check if the item meta hasDisplayName(), because again, this will cause NPEs if it doesn't have one. It's stupid, yes, go blame Bukkit, but it's a necessary precaution.

    Also, just a note, in your original code you're not checking the display name of the item in the player's hand, you're checking the display name of the blazerod variable you created, which doesn't even need to exist in the first place. Just check the display name of the item in the player's hand, but after you do the various null checks.
     
  9. Offline

    Zombie_Striker

    You do not own jumno1907.com. Do not use domains you do not own. Instead, either use your email adress, a domain you do own, or use the format "me.<name>.<project>"

    Since this is only used once, you do not need it as a variable. Remove this, and replace all instances of action with e.getAction()

    Since you just want to check if the player is holding an itemstack, remove the first line, null check the item in hand (if the player is not holding anything, the second line will throw an NPE,) and then check if the type is equal to Material.BLAZEROD.

    You create a second instance of the player that is exactly the same as 'player'. Remove this line.

    You can replace all three lines here with "vector.multiply(2);"

    From a PM.

    To check if an player is holding an item with a custom name, use
    Code:
    if(player.getItemInHand()!=null){
    //Check if the item is not null
     if(player.getItemInHand().getType()==Material.BLAZE_ROD){
     // Check if the type is equal to blaze rod
        if(player.getItemInHand().getItemMeta().hasDisplayName()&&player.getItemInHand().getItemMeta().getDisplayName().equals("THE NAME")){
        //Checks if the item has a name, and if so, checks if it is equal to "THE NAME"
       }
     }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 10, 2017
    Rayzr522 likes this.
  10. Offline

    Jumb_1907

    I still got no idea how to do this
     
  11. Offline

    Jumb_1907

    It worked, Thanks
     
  12. Offline

    Zombie_Striker

    @Jumb_1907
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page