ProtocolLib icon indicating copy to clipboard operation
ProtocolLib copied to clipboard

Difference between documentation and server error for PacketType.Play.Server.SPAWN_ENTITY

Open TheCalypso opened this issue 1 year ago • 1 comments

  • [X] This issue is not solved in a development build

Describe the bug Hello, I am in the process of updating my NPC plugin. I also make Fake players appear on my map. Before I used NAMED_ENTITY_SPAWN and apparently I have to use SPAWN_ENTITY from now on since version 1.20.4.

By following the documentation that you can find here: https://wiki.vg/Protocol#Spawn_Entity it seems that index 2 is now a VarInt corresponding to https://wiki.vg/Entity_metadata#Player.

Expected behavior So I wrote the code below:

public static PacketContainer createPacketNES(UUID uuid, int entityID, Location location) {
    PacketContainer nesPacket = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
    nesPacket.getModifier().writeDefaults();
    var spawnPacketModifier = nesPacket.getModifier();

    spawnPacketModifier.write(0, entityID);
    spawnPacketModifier.write(1, uuid);
    spawnPacketModifier.write(2, 122); // ID for player
    spawnPacketModifier.write(3, location.getX());
    spawnPacketModifier.write(4, location.getY());
    spawnPacketModifier.write(5, location.getZ());
    spawnPacketModifier.write(6, getCompressedAngle(location.getYaw()));
    spawnPacketModifier.write(7, getCompressedAngle(location.getPitch()));

    return nesPacket;
}

However, here is the error I have:

[16:40:17 WARN]: [GaspeziaNPCs] Plugin GaspeziaNPCs v1.9.0 generated an exception while executing task 882
java.lang.IllegalStateException: Unable to set value of field private final net.minecraft.world.entity.EntityTypes net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity.e
        at com.comphenix.protocol.reflect.accessors.DefaultFieldAccessor.set(DefaultFieldAccessor.java:44) ~[ProtocolLib.jar:?]
        at com.comphenix.protocol.reflect.StructureModifier.writeInternal(StructureModifier.java:358) ~[ProtocolLib.jar:?]
        at com.comphenix.protocol.reflect.StructureModifier.write(StructureModifier.java:321) ~[ProtocolLib.jar:?]
        at fr.hotmail.regisgaia.gnpcs.packets.NPCsPC.createPacketNES(NPCsPC.java:101) ~[GNPCs-1.9.0.jar:?]
        at fr.hotmail.regisgaia.gnpcs.packets.SenderPacketsPlayer.sendSpawnPackets(SenderPacketsPlayer.java:75) ~[GNPCs-1.9.0.jar:?]
        at fr.hotmail.regisgaia.gnpcs.entities.FakePlayerData.spawn(FakePlayerData.java:73) ~[GNPCs-1.9.0.jar:?]
        at fr.hotmail.regisgaia.gnpcs.entities.NPCsManager.spawnNPCforPlayer(NPCsManager.java:189) ~[GNPCs-1.9.0.jar:?]
        at fr.hotmail.regisgaia.gnpcs.entities.NPCsManager.spawnNPCs(NPCsManager.java:160) ~[GNPCs-1.9.0.jar:?]
        at fr.hotmail.regisgaia.gnpcs.player.PlayerManager.lambda$sendNPCs$0(PlayerManager.java:37) ~[GNPCs-1.9.0.jar:?]
        at org.bukkit.craftbukkit.v1_20_R3.scheduler.CraftTask.run(CraftTask.java:101) ~[paper-1.20.4.jar:git-Paper-454]
        at org.bukkit.craftbukkit.v1_20_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:57) ~[paper-1.20.4.jar:git-Paper-454]
        at com.destroystokyo.paper.ServerSchedulerReportingWrapper.run(ServerSchedulerReportingWrapper.java:22) ~[paper-1.20.4.jar:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.ClassCastException: Cannot cast java.lang.Integer to net.minecraft.world.entity.EntityTypes
        at java.lang.Class.cast(Class.java:3889) ~[?:?]
        at com.comphenix.protocol.reflect.accessors.DefaultFieldAccessor.set(DefaultFieldAccessor.java:41) ~[ProtocolLib.jar:?]
        ... 14 more

It seems that the expected type is not a VarInt but in EntityTypes. I didn't manage to go any further.

If anyone could help me I would be very grateful.

I use the Paper version git-Paper-454 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT) (Git: 99a6416) with the last build of Protocolibs 5.2.0-SNAPSHOT-679.

PS: my old function :

public static PacketContainer createPacketNES(UUID uuid, int entityID, Location location) {
	PacketContainer nesPacket = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
	nesPacket.getModifier().writeDefaults();
        var spawnPacketModifier = nesPacket.getModifier();

        spawnPacketModifier.write(0, entityID);
        spawnPacketModifier.write(1, uuid);
        spawnPacketModifier.write(2, location.getX());
        spawnPacketModifier.write(3, location.getY());
        spawnPacketModifier.write(4, location.getZ());
        spawnPacketModifier.write(5, getCompressedAngle(location.getYaw()));
        spawnPacketModifier.write(6, getCompressedAngle(location.getPitch()));

        return nesPacket;
    }

Version Info https://pastebin.com/gWkE15q9

Additional context Like every time I file a ticket, I want to thank the developers and the community for the work you do <3

TheCalypso avatar Mar 22 '24 18:03 TheCalypso

Hi, I still haven't found a solution to this problem. If anyone can help me I would be very grateful.

TheCalypso avatar Apr 15 '24 09:04 TheCalypso

Hi, maybe use the following: nesPacket.getEntityTypeModifier().write(0, entity.getType()); (where entity is any player...) and don't write drectly to "2"

Jeppa avatar May 11 '24 13:05 Jeppa

Thank you, it works really well!

TheCalypso avatar May 31 '24 11:05 TheCalypso