Solved Unknown problem with player.Action

Discussion in 'Plugin Development' started by TheStarkPvP, Jul 17, 2016.

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

    TheStarkPvP

    Hey there! I'm creating a spells plugin and it will use mana, so I'm doing mana potions and I have a problem. My code:

    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(p.getItemInHand() == null){
                    return;
                }
                if(p.getItemInHand().getItemMeta() == null){
                    return;
                }
                if(p.getItemInHand().getItemMeta().getLore() == null){
                    return;
                }
                if(p.getItemInHand().getItemMeta().getDisplayName() == null){
                    return;
                }
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Poção de mana I")){
                    if(p.getItemInHand().getItemMeta().getLore().get(0).equals("§7Ao beber restaura 32 de mana")){
                        p.setItemInHand(null);
                        p.getInventory().addItem(Mana.mana(new ItemStack(Material.INK_SACK, 32, (short) 12)));
                    }
                }
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Poção de mana II")){
                    if(p.getItemInHand().getItemMeta().getLore().get(0).equals("§7Ao beber restaura 64 de mana")){
                        p.setItemInHand(null);
                        p.getInventory().addItem(Mana.mana(new ItemStack(Material.INK_SACK, 64, (short) 12)));
                    }
                }
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Poção de mana III")){
                    if(p.getItemInHand().getItemMeta().getLore().get(0).equals("§7Ao beber restaura 128 de mana")){
                        p.getInventory().removeItem(p.getItemInHand());
                        p.getInventory().addItem(Mana.mana(new ItemStack(Material.INK_SACK, 128, (short) 12)));
                    }
                }
            }
        }
    Problem:
    With the potion I, I right click air and nothing happens, and when right click block I get 32 mana.
    With the potion II, I right click air and nothing happens too, and when right click block I get 64 mana.
    With the potion III, I right click air and get 64 mana, but still am with the pot in the hand, and when I right click block i get 128 mana, but 64 of this are invisible until I open one inventory or click in the slot.

    I want to get the amount of mana of each potion when right click block or air, and make the potion disappear, but that happens, someone may help me?
    Thanks!
     
  2. Offline

    ArsenArsen

    PlayerInteractEvent triggers only if there is something to interact with. Says that in the docs.
     
  3. I have seen a lot of posters ask these questions lately, and I myself have become very curious about this. Are there any events which fire when a player clicks in the air?
     
  4. Offline

    thapengwin

    But...
    Code:
    public class PlayerInteractEvent
    extends PlayerEvent
    implements Cancellable
    Called when a player interacts with an object or air.
    This event will fire as cancelled if the vanilla behavior is to do nothing (e.g interacting with air)
    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/PlayerInteractEvent.html
     
  5. Offline

    ArsenArsen

    @thapengwin @AlvinB ignoreCanclled = true

    I think so.
     
Thread Status:
Not open for further replies.

Share This Page