[1.10] Clicking fake armorstand not firing any interaction events

Discussion in 'Spigot Plugin Development' started by uksspy, Oct 7, 2016.

  1. I am trying to listen for player left clicks and right clicks while they are holding an arrow. It works perfectly fine until they right click a location that contains an invisible EntityArmorStand I spawned via packets.

    With regular entities the fact that clicking an entity doesn't fire PlayerInteractEvent is fine because you can just listen for PlayerInteractAtEntityEvent. The issue that I am having is not even that event is fired when clicking the fake armorstand.

    Debugging code I used:
    Code (Java):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            event.getPlayer().sendMessage("PlayerInteract: " + event.getAction());
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(event.getPlayer().getInventory().getItemInMainHand().getType() == Material.ARROW) event.getPlayer().openInventory(inv);
            }
        }
       
        @EventHandler
        public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event){
            event.getPlayer().sendMessage("PlayerInteractAtEntity: " + event.getRightClicked().getType());
        }
       
        @EventHandler
        public void onEntityDamageEntityEvent(EntityDamageByEntityEvent event){
            if(event.getDamager() instanceof Player){
                ((Player) event.getDamager()).sendMessage("EntityDamageByEntity: " + event.getEntity().getType());
            }
        }
    Note that I did not receive any messages when clicking a invisible armorstand.
     
  2. Did you register you events in an onEnable?
    Because clicking a fake armor stand shouldn't trigger a PlayerInteractAtEntityEvent, but still trigger a PlayerInteractEvent.
     
  3. The server is probably looking for that entity you clicked, not finding it and therefore not throwing that event as there is no value to populate the getClicked() field with, and that returning null would raise a lot of NPEs.
    -> intercept the packet and handle it yourself.
     
  4. Yep. I didn't expect clicking a fake armor stand to trigger a PlayerInteractAtEntityEvent but I wanted to check just to make sure. Like you said, the real issue is it blocking the PlayerInteractEvent.

    @Jo_Jo_2000 Makes sense, the main problem is PlayerInteractEvent not being fired. I haven't worked with intercepting packets but I'm sure I can figure it out. I'm guessing it is this packet I should be listening for, right?
     
  5. That would be a good packet to listen to. And also the UseItem packet which is sent regardless if they are looking at an entity and then you can check if they are looking at your packet-sent entity.
    A couple good packet listening strategies would be using ProtocolLib or following this tutorial and using netty.
    Another alternative to prevent messy packet listening or anything like that is to spawn the entity so spigot knows it's there (aka without packets) and then send a PacketPlayOutEntityDestroy to the players you don't want to see the entity. This would have to be done right after spawning the entity so you don't see the entity flash quickly (although you still might)