Get EntityPlayer Name from PacketType.Play.Client.USE_ENTITY

Discussion in 'Spigot Plugin Development' started by _zMattyPower_, Aug 7, 2018.

  1. Hi, I created a Entity Player, and for this the only method to get that you right click or click a fake player is with packets, and i used ProtocolLib, but i don't know how to get only the fake player and not all the players that you right click. This is the code:

    Code (Java):
        public static void npcInteract() {
            ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(Main.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY) {
                public void onPacketReceiving(PacketEvent e) {
                    if (e.getPacketType() == PacketType.Play.Client.USE_ENTITY) {
                        try {
                            PacketContainer packet = e.getPacket();
                            packet.getIntegers().read(0);
                            Player p = e.getPlayer();
                            if (!interactCooldown.contains(p)) {
                                interactCooldown.add(p);
                                p.sendMessage("hi");
                                Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
                                    public void run() {
                                        interactCooldown.remove(p);
                                    }
                                }, 40L);
                            }
                        } catch (Exception ex) {
                        }
                    }
                }
            });
        }

    And i have another problem, sometimes I get this error, I think is ProtocolLib, but i'm not sure.

    Code (Text):
    [16:16:34 FATAL]: Handling task
    java.util.concurrent.ExecutionException: java.lang.NullPointerException
            at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_162]
            at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_162]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:697) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_162]
    Caused by: java.lang.NullPointerException
            at net.minecraft.server.v1_8_R1.WorldServer.sendParticles(WorldServer.java:1161) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.EntityLiving.a(EntityLiving.java:150) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.EntityPlayer.a(EntityPlayer.java:612) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:444) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.PacketPlayInFlying.a(SourceFile:126) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.PacketPlayInPositionLook.a(SourceFile:18) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_162]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_162]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            ... 4 more
     
  2. 1. Save the entityID of the fake player
    2. When receiving the packet check if the entityID is equal to the saved one
     
  3. How I can get the entityID of the fake player? I'm new to packets/ProtocolLib
     
  4. Ok, I got this to work, but every time a player joins the id will change, and It will not work anymore. (only for the new player)
     
  5. The id is a temporary value that depends on the current instance and is incremented by one for each new entity (Players included). You would need to get the UUID and save that.
     
  6. How i can get the fake player uuid from packets?
     
  7. ↑.
    You would need some way to save them in some way for getting them back later. If they are not created by your plugin, maybe there is an API available for that plugin?