Solved PlayerInteractEntityEvent problem

Discussion in 'Spigot Plugin Development' started by Flaquito, Mar 12, 2020.

Thread Status:
Not open for further replies.
  1. Hello guys, I have this problem:

    Code (Java):
    @EventHandler
        public void playerInteractWithEntity (PlayerInteractAtEntityEvent e) {
            Player player = e.getPlayer();
            Entity entity = e.getRightClicked();
            if(player.getInventory().getItemInMainHand().getType().equals(Material.STICK)) {
                if(entity.getType().equals(EntityType.COW)) {
                    if(player.getPassengers().isEmpty()) {
                        player.addPassenger(entity);
                        player.sendMessage("Te has §asubido§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes bajártelo con el mismo Palo dándole clic izquierdo.");
                    }else {
                        player.removePassenger(entity);
                        player.sendMessage("Te has §cbajado§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes subírtelo con el mismo Palo dándole clic izquierdo.");
                    }
                }
            }
    }
    When I clic on a cow, the "if" statement (entity.getType().equals(EntityType.COW)) works, but both. I mean, that the event is getting the clic two times, so the conditional works from "player.addPassenger(entity);" to "player.addPassenger(entity);". Why is doing that?
     
  2. The event fires once for each of the player's hands. Check that PlayerInteractAtEntityEvent#getHand is EquipmentSlot.OFF_HAND.
     
  3. I tryed now and "it works" 'cause I got it but, I need that it works with no item on hand and, it's working with whatever item on hand.
     
  4. getItemInMainHand returns an ItemStack, which theoretically would be null if there is no item, so check if it is null and then execute the following code
     
  5. I checked it but not, it doesn't return null. I try and the code doesn't execute.
     
  6. It returns "AIR". The problem with the event is that execute two times when I click on a entity.
     
  7. Show code
     
  8. Code (Java):
    @EventHandler
        public void playerInteractWithEntity (PlayerInteractAtEntityEvent e) {
            Player player = e.getPlayer();
            Entity entity = e.getRightClicked();
            player.sendMessage(""+player.getInventory().getItemInMainHand());
            if(player.getInventory().getItemInMainHand() == null) {
                if(entity.getType().equals(EntityType.COW)) {
                    if(player.getPassengers().isEmpty()) {
                        player.addPassenger(entity);
                        player.sendMessage("Te has §asubido§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes bajártelo con el mismo Palo dándole clic izquierdo.");
                    }else {
                        player.removePassenger(entity);
                        player.sendMessage("Te has §cbajado§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes subírtelo con el mismo Palo dándole clic izquierdo.");
                    }
                }
            }
        }
     
  9. event.getHand returns the hand (once for main and once for offhand). Compare it to whatever hand you want to use and check if the item in that hand is Material.AIR
     
  10. It should work with == null...
    try this
    Code (Java):
    if(player.getInventory().getItemInMainHand().getTypê() == Material.AIR && entity.getType() == EntityType.COW) {
                    if(player.getPassengers().isEmpty()) {
                        player.addPassenger(entity);
                        player.sendMessage("Te has §asubido§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes bajártelo con el mismo Palo dándole clic izquierdo.");
                    }else {
                        player.removePassenger(entity);
                        player.sendMessage("Te has §cbajado§r un/a §e"+entity.getName()+"§r al hombro.");
                        player.sendMessage("Puedes subírtelo con el mismo Palo dándole clic izquierdo.");
                    }
                }
     
  11. I want with right clic, just that. How can I cancel the other hand in that.
     
  12. It doesn't work. The code execute two times; the first "if" and then the "else".
     
  13. No. What executes twice is the event. The player has two hands and the event fires once per hand. As I said before, you have to check which hand the event was fired by.

    Code (Java):
    // Main hand
    if(event.getHand() == EquipmentSlot.HAND){
          // do something
    }
     
  14. I tried this and it works:

    Code (Java):
    @EventHandler
        public void playerInteractWithEntity (PlayerInteractAtEntityEvent e) {
            Player player = e.getPlayer();
            Entity entity = e.getRightClicked();
            player.sendMessage(""+player.getInventory().getItemInMainHand());
            if(e.getHand() == EquipmentSlot.OFF_HAND) {
                if(player.getInventory().getItemInMainHand().getType() == Material.AIR) {
                    if(entity.getType().equals(EntityType.COW)) {
                        if(player.getPassengers().isEmpty()) {
                            player.addPassenger(entity);
                            player.sendMessage("Te has §asubido§r un/a §e"+entity.getName()+"§r al hombro.");
                            player.sendMessage("Puedes bajártelo con el mismo Palo dándole clic izquierdo.");
                        }else {
                            player.removePassenger(entity);
                            player.sendMessage("Te has §cbajado§r un/a §e"+entity.getName()+"§r al hombro.");
                            player.sendMessage("Puedes subírtelo con el mismo Palo dándole clic izquierdo.");
                        }
                    }
                }
            }
        }
     

  15. Yeah, thank you. I was understanding that you said before. Now I got it.

    Thanks too to everyone who help me a lot.
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.