From d8825f0262cbcb15649e3dd1c5bea2482ff5f454 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 15 Jan 2023 18:32:46 +0100 Subject: [PATCH] Fix SpawnPacketPosition Reflection --- .../de/steamwar/core/BountifulWrapper8.java | 8 ++--- .../de/steamwar/core/BountifulWrapper9.java | 8 ++--- .../de/steamwar/core/BountifulWrapper.java | 2 +- .../src/de/steamwar/entity/REntity.java | 30 ++++++++++++++----- .../src/de/steamwar/entity/RPlayer.java | 2 +- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java b/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java index 3a0ee7a..8a74a0f 100644 --- a/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java @@ -66,10 +66,10 @@ public class BountifulWrapper8 implements BountifulWrapper.IBountifulWrapper { } @Override - public BountifulWrapper.PositionSetter getPositionSetter(Class packetClass, int fieldOffset8) { - Reflection.FieldAccessor posX = Reflection.getField(packetClass, int.class, fieldOffset8); - Reflection.FieldAccessor posY = Reflection.getField(packetClass, int.class, fieldOffset8+1); - Reflection.FieldAccessor posZ = Reflection.getField(packetClass, int.class, fieldOffset8+2); + public BountifulWrapper.PositionSetter getPositionSetter(Class packetClass, int fieldOffset) { + Reflection.FieldAccessor posX = Reflection.getField(packetClass, int.class, fieldOffset); + Reflection.FieldAccessor posY = Reflection.getField(packetClass, int.class, fieldOffset +1); + Reflection.FieldAccessor posZ = Reflection.getField(packetClass, int.class, fieldOffset +2); return (packet, x, y, z) -> { posX.set(packet, MathHelper.floor(x * 32)); diff --git a/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java b/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java index dc7c774..97fe828 100644 --- a/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java +++ b/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java @@ -56,10 +56,10 @@ public class BountifulWrapper9 implements BountifulWrapper.IBountifulWrapper { } @Override - public BountifulWrapper.PositionSetter getPositionSetter(Class packetClass, int fieldOffset8) { - Reflection.FieldAccessor posX = Reflection.getField(packetClass, double.class, 0); - Reflection.FieldAccessor posY = Reflection.getField(packetClass, double.class, 1); - Reflection.FieldAccessor posZ = Reflection.getField(packetClass, double.class, 2); + public BountifulWrapper.PositionSetter getPositionSetter(Class packetClass, int fieldOffset) { + Reflection.FieldAccessor posX = Reflection.getField(packetClass, double.class, fieldOffset); + Reflection.FieldAccessor posY = Reflection.getField(packetClass, double.class, fieldOffset+1); + Reflection.FieldAccessor posZ = Reflection.getField(packetClass, double.class, fieldOffset+2); return (packet, x, y, z) -> { posX.set(packet, x); diff --git a/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java b/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java index f71d234..a0625a2 100644 --- a/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java @@ -38,7 +38,7 @@ public class BountifulWrapper { Object getDataWatcherObject(int index, Class type); Object getDataWatcherItem(Object dataWatcherObject, Object value); - PositionSetter getPositionSetter(Class packetClass, int fieldOffset8); + PositionSetter getPositionSetter(Class packetClass, int fieldOffset); UUIDSetter getUUIDSetter(Class packetClass); } diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntity.java b/SpigotCore_Main/src/de/steamwar/entity/REntity.java index e4e88d2..6c5bf80 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntity.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntity.java @@ -178,8 +178,24 @@ public class REntity { server.removeEntity(this); } - private static final Function livingSpawnPacketGenerator = entitySpawnPacketGenerator(ProtocolWrapper.spawnLivingPacket, 2); - private static final Function spawnPacketGenerator = entitySpawnPacketGenerator(ProtocolWrapper.spawnPacket, 1); + private static int spawnPacketOffset() { + switch (Core.getVersion()) { + case 8: + case 18: + return 1; + case 9: + case 10: + case 12: + case 14: + case 15: + return 0; + case 19: + default: + return 2; + } + } + private static final Function spawnPacketGenerator = entitySpawnPacketGenerator(ProtocolWrapper.spawnPacket, spawnPacketOffset()); + private static final Function livingSpawnPacketGenerator = Core.getVersion() >= 19 ? spawnPacketGenerator : entitySpawnPacketGenerator(ProtocolWrapper.spawnLivingPacket, Core.getVersion() == 8 ? 2 : 0); void spawn(Consumer packetSink) { if(entityType.isAlive()) { packetSink.accept(livingSpawnPacketGenerator.apply(this)); @@ -274,7 +290,7 @@ public class REntity { private static final Class teleportPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport"); private static final Reflection.FieldAccessor teleportEntity = Reflection.getField(teleportPacket, int.class, 0); - private static final BountifulWrapper.PositionSetter teleportPosition = BountifulWrapper.impl.getPositionSetter(teleportPacket, 1); + private static final BountifulWrapper.PositionSetter teleportPosition = BountifulWrapper.impl.getPositionSetter(teleportPacket, Core.getVersion() == 8 ? 1 : 0); private static final Reflection.FieldAccessor teleportYaw = Reflection.getField(teleportPacket, byte.class, 0); private static final Reflection.FieldAccessor teleportPitch = Reflection.getField(teleportPacket, byte.class, 1); private Object getTeleportPacket(){ @@ -307,9 +323,9 @@ public class REntity { return packet; } - private static Function entitySpawnPacketGenerator(Class spawnPacket, int posOffset8) { + private static Function entitySpawnPacketGenerator(Class spawnPacket, int posOffset) { BountifulWrapper.UUIDSetter uuid = BountifulWrapper.impl.getUUIDSetter(spawnPacket); - Function packetGenerator = spawnPacketGenerator(spawnPacket, posOffset8); + Function packetGenerator = spawnPacketGenerator(spawnPacket, posOffset); return entity -> { Object packet = packetGenerator.apply(entity); @@ -319,9 +335,9 @@ public class REntity { }; } - protected static Function spawnPacketGenerator(Class spawnPacket, int posOffset8) { + protected static Function spawnPacketGenerator(Class spawnPacket, int posOffset) { Reflection.FieldAccessor entityId = Reflection.getField(spawnPacket, int.class, 0); - BountifulWrapper.PositionSetter position = BountifulWrapper.impl.getPositionSetter(spawnPacket, posOffset8); + BountifulWrapper.PositionSetter position = BountifulWrapper.impl.getPositionSetter(spawnPacket, posOffset); return entity -> { Object packet = Reflection.newInstance(spawnPacket); diff --git a/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java b/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java index 5ae5cb5..c5a080b 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java @@ -115,7 +115,7 @@ public class RPlayer extends REntity { } private static final Class namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn"); - private static final Function namedSpawnPacketGenerator = spawnPacketGenerator(namedSpawnPacket, 1); + private static final Function namedSpawnPacketGenerator = spawnPacketGenerator(namedSpawnPacket, Core.getVersion() == 8 ? 1 : 0); private static final Reflection.FieldAccessor namedSpawnUUID = Reflection.getField(namedSpawnPacket, UUID.class, 0); private Object getNamedSpawnPacket() { Object packet = namedSpawnPacketGenerator.apply(this);