Bukkit.getEntity(uuid) returning null (ProtocolLib)

Discussion in 'Spigot Plugin Development' started by Sploon, Jun 4, 2018.

  1. I'm just starting to use ProtocolLib:
    Code (Text):
    // in new PacketAdapter
    @Override
    public void onPacketSending(PacketEvent event) { // event's packet is Packet.Play.Server.SPAWN_ENTITY
        for(Entity entity : event.getPlayer().getWorld().getEntities()) {
          if(entity.getUniqueId().equals(event.getPacket().getUUIDs().read(0))) {
             Bukkit.broadcastMessage("123");
          }
       }
       Bukkit.broadcastMessage(String.valueOf(Bukkit.getEntity(event.getPacket().getUUIDs().read(0))));
    }
    123 gets broadcasted fine - the entity with the UUID is found somewhere in the world. However, Bukkit.getEntity(uuid) returns null.
    I can use the for-loop to find the entity I'm looking for, but I'd much rather use Bukkit.getEntity(uuid) (which keeps track of the uuid->entity mapping with a HashMap, instead of just iterating through everything).
    Did I make some sort of stupid mistake? Or is Bukkit.getEntity returning null because the packet gets fired before the entry gets added to the map?
     
  2. Choco

    Moderator

    You have to remember that while the entity "exists", it doesn't actually exist to the server. The server is sending the client a packet telling it that it exists, thus why it can render it, but the server doesn't actually have that entity. It does not exist on the server. It's not registered in the entity tracker and thus cannot be retrieved with Bukkit#getEntity().
     
  3. This isn't my own custom entity, though, it's one spawned with:
    Code (Text):
    enchantHolder.launchProjectile(Snowball.class);
     
  4. Choco

    Moderator

    Ah, in that case, there's no real reason to use protocol lib as #launchProjectile() returns a <T extends Projectile> (in this case, Snowball). You can do "Snowball snowball = enchantHolder.launchProjectile(Snowball.class);"
     
  5. This is just me trying to mess around with things - I'm going to do more with ProtocolLib in the future, but I haven't been able to find a whole lot of documentation, so I've just been spending some time trying to figure things out.

    My end goal is to change the snowball into an ItemStack.
     
  6. It is possible that, I am not sure, the projectile doesn't directly get registered - try a tick delay maybe? Alternatively I would suggest to check your decompiled spigot jar to check out why it wont be found.