Intercept Packets

Discussion in 'Plugin Development' started by Irantwomiles, May 23, 2017.

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

    Irantwomiles

    I am currently working on a Pracitce PvP plugin that allows multiple fights to happen inside of 1 arena. This becomes complicated because I need to hide splash particles from potions and sounds, along with many other things. Luckily there is a simple class (EntityHider) that the authors of ProtocolLib have written that simplify a lot of the steps, but there is one step that I can't figure out.

    I've been trying to hide the splash and sound effects from potions or any entity really using ProtocolLib. I understand that I need to intercept the packet that the server is sending to the player and cancel it. But how exactly do I do this? Should I even be using ProtocolLib? I heard someone say I should use TinyProtocol but there is almost no documentation for that and I'm not very experienced with packet interception/receiving to be digging deep into that. Any insight, examples, or just anything that will help would be greatly appreciated as I've pulled out too many hairs out of my head.
     
  2. Offline

    Mahtaran

    From the ProtocolLib documentation:
    Code:
    // Disable all sound effects
    protocolManager.addPacketListener(
      new PacketAdapter(this, ListenerPriority.NORMAL,
              PacketType.Play.Server.NAMED_SOUND_EFFECT) {
        @Override
        public void onPacketSending(PacketEvent event) {
            // Item packets (id: 0x29)
            if (event.getPacketType() ==
                    PacketType.Play.Server.NAMED_SOUND_EFFECT) {
                event.setCancelled(true);
            }
        }
    });
    And yes, you should use ProtocolLib for this. You also should look into only cancelling the packets from players that aren't in the same fight, and send packets to the players that are. Just look at the link that is under the ProtocolLib documentation.
     
  3. Offline

    Irantwomiles

    Hey thanks for your response.

    So I have actually been using this and testing with it, but I came across a couple problems which didn't suit for my needs.

    1) As you said, this cancels all sound effects, but I need to cancel it for everyone but the two players that are fighting. So If there are for example 3 fights going on in one arena and there are 2 players per fight, the 2 people that are fighting each other should only hear/see the things that the other person is doing and nothing that the other 4 fighters are doing.

    2) When this event gets cancelled, it stays canceled unless the same PacketEvent is called and the cancellation is set to false. So if one person triggers it and it becomes cancelled, it remains cancelled.

    So my question is: Is there a way to tell who is sending the packet to the server that is being received by other players. In the code you posted you can actually get the player using event.getPlayer() which returns the player receiving the packet (I believe) but there is nothing (that I've found) that gives information about the original source of the packet sent and how to cancel just that one.
     
  4. Offline

    Mahtaran

    After some research, I found out that this would be pretty difficult. Do they really need to be in the same arena? Can't you copy paste the arena over or create a new world from a zip file?
     
  5. Offline

    Irantwomiles

    This is what I'm practically doing, but what I'm suggesting is becoming the norm and a lot of the people using my plugin are asking for this feature. What exacly about it is difficult?
     
  6. Offline

    Mahtaran

    Well, knowing from which player the packet is coming. However, you can get the location the sound is coming from and check which player that would be for some sounds.
     
  7. Offline

    Irantwomiles

    Well, there could be the chance that someone throws a potion at the same location as someone else which could cause problems. Thanks anyways, maybe someone who has more experience in this could help.
     
  8. Offline

    Mahtaran

    For that you will need to cancel the potion sound, listen to the PotionSplashEvent and play the sound to the correct players. I'm assuming you already are listening to that class for ignoring damage from another duel. I however do think it's a bad idea to have multiple duels in the same arena, as you would have to get around block placing, but also being able to hit your opponent even if someone else is standing between you, and let ender pearls be able to pass through other duelists.
     
  9. Offline

    Irantwomiles

    So how would I cancel the potion sound? Canceling the packet all together won't let you send the packet anymore.
     
  10. Offline

    Mahtaran

    I honestly don't know now, but maybe @Comphenix knows something.
     
  11. Offline

    Irantwomiles

    He hasn't been on since 2015 D:
     
  12. Offline

    Mahtaran

    You can always try :D. Also, have you tried cancelling PotionSplashEvent, adding the potion to the affected players that are in the same duel as the shooter (event.getEntity().getShooter()) and play the sound to them?
     
Thread Status:
Not open for further replies.

Share This Page