PacketReceiving firing twice?

Discussion in 'Spigot Plugin Development' started by MarkIsCool, Jun 28, 2018.

  1. Code (Text):

    protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Client.USE_ENTITY) {
                @Override
                public void onPacketReceiving(PacketEvent event) {
                    if (event.getPacketType() == PacketType.Play.Client.USE_ENTITY) {
                        PacketContainer packet = event.getPacket();
                        if (packet.getIntegers().read(0).equals(npcId)) {
                           
                            event.setCancelled(true);
                            Bukkit.broadcastMessage("It worked!");
                            Player player = event.getPlayer();
                            sendPlayerMessage(player);
                           
                        }
                    }
                }
            });
     
    It broadcasts "It worked!" twice when I interact with my npc.
    Anyway to make it so that it only sends once?
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    The client sends the packet twice, don't really know why.
    If you check the packet on wiki.vg: http://wiki.vg/Protocol#Use_Entity
    You'll notice the "type" row. I believe it sends two, one for interact and another for interact at. Simply determining if the packet is "interact" (not interact at) and only handling it then should solve your problem.
     
    • Like Like x 1
  3. I understand what you mean, but..
    How would I do that?
     
    #3 MarkIsCool, Jun 28, 2018
    Last edited: Jun 28, 2018
  4. Retrieve the field "type", it should return an integer and based on that you should be able to find the "interact" packet.
     
  5. It fires twice because you have two hands. ;)
     
  6. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Confusing bit is only one hand is interacting :/
     
  7. Doesn't matter. Both hands report to the server during any interaction event.

    This is how you can hold shears and wheat at the same time to feed and shear sheep in a single click, etc.

    Edit: I think the "confusing" bit is the fact that a lot of people intuitively expect/think each mouse buttons controls each hand individually, but that couldn't be further from reality. Get passed that, and it all makes sense. ;)
     
    #7 BillyGalbreath, Jun 29, 2018
    Last edited: Jun 29, 2018
  8. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Of course, the offhand and main hand are triggered at the same time with right click, aren't they. Makes sense now :D
     
  9. Using 1.8 ;)
     
  10. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Ah yes, probably connecting from a 1.12 client eh?

    Same thing still applies, just make sure the packet is similar or the same for 1.8. Yes, you will have to do extra work, and seriously just drop support for outdated and unsupported versions of Spigot.