[1.16.5] How to set spawned entity's yaw and pitch
abcdef-007 opened this issue · 9 comments
Make sure you're doing the following
- You're using the latest build for your server version
- This isn't an issue caused by another plugin
- You've checked for duplicate issues
- You didn't use
/reload
I'm using the latest build of ProtocolLib 5.2.0 on both the plugin and the server, and I've tried 5.3.0 too. My problem is that, when spawning an entity, I get this error: FieldAccessException: Field index 0 is out of bounds for length 0
. The error is called in the line I commented in the code (.write(0, rotationByte(yaw))
)
PacketContainer packet= new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
packet.getIntegers().write(0, entityID);
packet.getUUIDs().write(0, entityUUID);
packet.getEntityTypeModifier().write(0, EntityType.PLAYER);
spawnPacket.getDoubles().write(0, x).write(1, y).write(2, z);
// ERROR IN THIS LINE
spawnPacket.getBytes().write(0, rotationByte(yaw)).write(1, rotationByte(pitch));
sendPacket(player, packet);
I actually wrote a lib for that, the code to spawn a player entity is here: https://github.com/juliarn/npc-lib/blob/v3/bukkit/src/main/java/com/github/juliarn/npclib/bukkit/protocol/ProtocolLibPacketAdapter.java#L256 - just grab what you need :)
The packet your looking for is called
NAMED_ENTITY_SPAWN
which got removed in 1.20.2 and looks like this:public class PacketPlayOutNamedEntitySpawn implements Packet<PacketListenerPlayOut> { private int a; private UUID b; private double c; private double d; private double e; private byte f; private byte g; public PacketPlayOutNamedEntitySpawn(EntityHuman var0) { this.a = var0.getId(); this.b = var0.getProfile().getId(); this.c = var0.locX(); this.d = var0.locY(); this.e = var0.locZ(); this.f = (byte) ((int) (var0.yaw * 256.0F / 360.0F)); this.g = (byte) ((int) (var0.pitch * 256.0F / 360.0F)); } // ... }
But I'm using 1.16.5... What should I do then? How do I fix the error?
Just use that because it got removed in 1.20.2+ which is way newer than 1.16.5 so it will work for your server: change PacketType.Play.Server.SPAWN_ENTITY
to PacketType.Play.Server.NAMED_ENTITY_SPAWN
Probably, but to be honest, I haven't tried to spawn fake players since version 1.17, so I'm not entirely sure. However, considering that I don't see any other packets for spawning entities in version 1.20.2 and later, I believe that is the correct method.
Just use that because it got removed in 1.20.2+ which is way newer than 1.16.5 so it will work for your server: change
PacketType.Play.Server.SPAWN_ENTITY
toPacketType.Play.Server.NAMED_ENTITY_SPAWN
Okay, thanks! How would it be done in 1.20.2 though? Using SPAWN_ENTITY and setting the yaw with getIntegers().write(5, yaw) like here?
its probably just an issue with protocollib tryna do something with args that don't exist or aren't compatible or sum
its probably just an issue with protocollib tryna do something with args that don't exist or aren't compatible or sum
So how do I fix it?
The packet your looking for is called NAMED_ENTITY_SPAWN
which got removed in 1.20.2 and looks like this:
public class PacketPlayOutNamedEntitySpawn implements Packet<PacketListenerPlayOut> {
private int a;
private UUID b;
private double c;
private double d;
private double e;
private byte f;
private byte g;
public PacketPlayOutNamedEntitySpawn(EntityHuman var0) {
this.a = var0.getId();
this.b = var0.getProfile().getId();
this.c = var0.locX();
this.d = var0.locY();
this.e = var0.locZ();
this.f = (byte) ((int) (var0.yaw * 256.0F / 360.0F));
this.g = (byte) ((int) (var0.pitch * 256.0F / 360.0F));
}
// ...
}