How to detect item in hand at any given time? [Solved]

Discussion in 'Plugin Development' started by Lanuk, Sep 10, 2012.

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

    Lanuk

    Basically I want to make it so if a player is holding a certain item (in this case a sword with an enchantment) at any given time, something will happen. In my code, I set it so the player is sent a message saying "Success" when they hold an item. The problem is I only get the "Success" message AFTER I switch back to another item.

    So if I am holding an enchanted sword, I won't get the success message until I switch the item in my hand. I want to get the message right when I select the enchanted sword.

    Here is my code:

    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerItemHeld(PlayerItemHeldEvent event){
            Player p = event.getPlayer();
            ItemStack i = p.getItemInHand();
     
            if(i.getTypeId() == 267
                    || p.getItemInHand().getTypeId() == 268
                    || p.getItemInHand().getTypeId() == 276
                    || p.getItemInHand().getTypeId() == 283){
     
                Map<Enchantment, Integer> Enchants = p.getInventory().getItemInHand().getEnchantments();
                if (Enchants.containsKey(Enchantment.OXYGEN)) {
                    Integer level = Enchants.get(Enchantment.OXYGEN);
                    if (level == 1) {
                        p.sendMessage("Success!");
                    }
                }
            }
        }
    Sorry if it's weird, I'm not very experienced. Thanks for any help!
     
  2. Offline

    DocRedstone

    I would suggest that instead of doing a listener you do a aSyncRepeatingTask. This way you can make it constantly looking at the item in hand instead of waiting until the item that is held has been changed.
     
  3. Offline

    JayzaSapphire

    Use event.getNewSlot().getItem() or something like that.
     
  4. Offline

    Lanuk

    Oh, thanks! I am looking at it right now, but I am a noob coder, and am having trouble figuring it out. I will post again if I do

    I tried that, but you can't get the item of the new slot (at least not with the .getItem() method)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  5. Offline

    skore87

    Getting the item from the given slot doesn't change between the getOldSlot and getNewSlot?

    player.getInventory().getItem(slot number)
     
  6. Offline

    Lanuk

    Using that method is just a more complicated way of using the player.getInventory().getItemHeld() method (since I am looking for the item in the player's hand), and I explained the issue with that above, so unfortunately, I don't think that will work either :(
     
  7. Offline

    Firefly

    So when do you need to check what the item in hand is?
     
  8. Offline

    Lanuk

    Basically, if a player switches the item in their hand, and the item in their hand has a certain enchantment on it, at that exact moment, while the enchanted item is still selected, I would like to send the player a message.

    I know that the PlayerItemHeld event is fired whenever a player switches their inventory slot, but even so, I do not get the message the player should be send when they select the enchanted item. Instead, they have to select the enchanted item, then DE-select it to get the message.

    Sorry if that didn't make much sense :/
     
  9. Offline

    Giant

    If you were to get the itemInHand() in the current Player object, that should return the correct item should it not?
     
  10. Offline

    Lanuk

    Oh, if anyone would actually be awesome enough to come on my server to take a look at the plugin firsthand, the ip is:

    tempserver.zapto.org

    EDIT: Well.. ip doesn't seem to be working for whatever reason atm..

    Well, that is what I am doing (unless you see something wrong with my code), but when on I go on my server and select the correct item, it will only send me a message after I deselect the item. So it sort of works.. but I need to to send the player the message right when it selects the item in hand.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  11. Offline

    Firefly

    So if you select something else, then select the enchanted item, it doesn't work?
     
  12. Offline

    Lanuk

    If I select the enchanted item, THEN select another item, it works. Nothing happens when I select the enchanted item. It appears that my ip posted above now works if you would like to join and see. It is sort of hard to explain
     
  13. Offline

    Firefly

    My question was if you are testing this when you are already holding the enchanted sword, or if you are holding something else THEN switching to the enchanted sword.
     
  14. Offline

    Zidkon

    I understand what you mean. The thing is the event is trigger when the player is ABOUT to change, not trigger when he changed already, so getting the item in the hand will not work, you must use the event to get the item he will have on his hand after the change.

    So I just checked the documentation on jd.bukkit.org and I found this http://jd.bukkit.org/doxygen/d7/dbe/PlayerItemHeldEvent_8java_source.html#l00011

    Is pretty interesting because you can see from wich slot TO wich slot the player is changing.

    So you can do:
    Code:JAVA
    1.  
    2. //Getting the New slot player is changing to.
    3. itemId = event.getNewSlot();
    4. //Getting the ItemStack the player WILL have on his hand after the event is done.
    5. ItemStack iStack = event.getPlayer().getInventory().getItem(itemId);
    6.  


    I guess from here with the ItemStack you can get the Id of the item and check your stuff.

    Good Luck.
     
    Lanuk likes this.
  15. Offline

    Lanuk

    Aweesomee! Thanks. I will try this now

    EDIT: It works. Thank you so much!
     
    Zidkon likes this.
  16. Offline

    Zidkon

    I forgot, itemID is an Integer xD

    Btw, if u liked my post like it ;), and tell me ur results later I will try help u.
     
    WaterNode likes this.
Thread Status:
Not open for further replies.

Share This Page