Checking if the item in hand is a specific potion

Discussion in 'Plugin Development' started by Hoolean, Sep 22, 2012.

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

    Hoolean

    The title says it all :D

    e.g. I want to find out if the player is holding a 'Water' potion
     
  2. Offline

    Kodfod

    So Like mundane potion, Awkward potion, thick potion?
     
  3. Offline

    Hoolean

    YAH!

    Wait, would this do it?
    Code:java
    1. Potion wBottle = new Potion(PotionType.WATER);
    2. if(p.getItemInHand().equals(wBottle))


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

    Kodfod

    Not sure water wuld but mundane and thick should work like that
     
  5. Offline

    Hoolean

    Hmm... Eclipse hasn't complained so hopefully it will work!
     
    Kodfod likes this.
  6. Offline

    Kodfod

    If it works, say so.

    Might Help people down the road =)
     
  7. did you figure it out?
    also, i don't only want to check the potion, but also set the item in hand to be a certain potion.
     
  8. Offline

    Hoolean

    Yes, see the code in the post above!
     
  9. but how would i set that item to his hand?
     
  10. Offline

    Jake230599

    player.getInventory().setItemInHand(args)
     
  11. don't work. can't set an potion to be an item. error:
    The method setiteminhand(itemstack) in the type playerinventory is not applicable for the arguments(potion)
    needs to be an itemstack.
     
  12. setiteminhand(potion.toItemStack(0))
     
  13. Ah, nice, thanks!
     
  14. Offline

    M0rse

    I tried to combine the checking if a player has a potion in their inventory with the making the potion something different when they left click:
    Code:
    public class potionswitcher implements Listener{
        @EventHandler
     
        public void potionchange(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action action = event.getAction();
            if (player.getItemInHand().getTypeId()==Material.POTION.getId()){
             
         
            if (action == Action.LEFT_CLICK_AIR){
                Potion poison = new Potion(PotionType.POISON,1);
                Potion harm = new Potion(PotionType.INSTANT_DAMAGE,2);
                Potion weakness = new Potion(PotionType.WEAKNESS,2);
                Potion regen = new Potion(PotionType.REGEN,1);
             
             
                if(player.getItemInHand().equals(poison)){
                    player.setItemInHand(harm.toItemStack(1));
                }
             
                if(player.getItemInHand().equals(harm)){
                    player.setItemInHand(weakness.toItemStack(1));
                }
             
                if(player.getItemInHand().equals(weakness)){
                    player.setItemInHand(regen.toItemStack(1));
                }
     
    Ect...
    i was hoping this code would change the item in their hand to a different potion depending on which was in their hand. Eclipse gives me no errors, but when I try and run this in the game while say holding a non-splash, poison 1, potion, rather than changing it to a harm potion, it just stays there when i left click... see where i went wrong?
     
  15. Offline

    xa112

    you need to make the 3 "if" statements to "else if" because other wise this happens:

    bob has poison, left clicks, now bob has harm, then weakness then regen. All because the next if statements are fulfilled if the item in hand is changed then checked again. Another fix would be to replace all the player.getItemInHand() parts with a variable you create at the beggining, pHand = player.getItemInHand,
    if(pHand.equals(something)){



    something}.
     
  16. Offline

    MayoDwarf

    Grave digger...
     
  17. Offline

    xa112

    What do you mean by Grave Digger?
     
  18. Offline

    Hoolean

    xa112

    I think he means you dug up a thread that had been buried in the forums for 5 months untouched (metaphorically) :)
     
    MayoDwarf likes this.
Thread Status:
Not open for further replies.

Share This Page