From cea2ffdac6298f6c0c3210c0704c6010d1e436bb Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 9 Jan 2023 17:35:00 +0100 Subject: [PATCH] WIP CoreREntity --- .../de/steamwar/core/ProtocolWrapper18.java | 6 ++ .../de/steamwar/core/ProtocolWrapper19.java | 5 ++ .../de/steamwar/core/BountifulWrapper8.java | 34 +++----- .../de/steamwar/core/ProtocolWrapper8.java | 9 ++ .../de/steamwar/core/BountifulWrapper9.java | 35 +++----- .../de/steamwar/core/BountifulWrapper.java | 14 ++- .../src/de/steamwar/core/ProtocolWrapper.java | 1 + .../src/de/steamwar/entity/REntity.java | 86 +++++++++++-------- 8 files changed, 107 insertions(+), 83 deletions(-) diff --git a/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java b/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java index a09d9e9..d0ecebc 100644 --- a/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java +++ b/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java @@ -37,6 +37,7 @@ public class ProtocolWrapper18 implements ProtocolWrapper { } private static final Reflection.FieldAccessor spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, EntityTypes.class, 0); + private static final Reflection.FieldAccessor spawnLivingType = Reflection.getField(ProtocolWrapper.spawnLivingPacket, int.class, 1); @Override public void setSpawnPacketType(Object packet, EntityType type) { switch(type) { @@ -49,6 +50,11 @@ public class ProtocolWrapper18 implements ProtocolWrapper { case FIREBALL: spawnType.set(packet, EntityTypes.S); break; + case ARMOR_STAND: + spawnLivingType.set(packet, 1); + break; + default: + throw new IllegalArgumentException(type.name() + " is not implemented"); } } diff --git a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java index d672c97..c2d25eb 100644 --- a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java +++ b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java @@ -49,6 +49,11 @@ public class ProtocolWrapper19 implements ProtocolWrapper { case FIREBALL: spawnType.set(packet, EntityTypes.V); break; + case ARMOR_STAND: + spawnType.set(packet, EntityTypes.d); + break; + default: + throw new IllegalArgumentException(type.name() + " is not implemented"); } } diff --git a/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java b/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java index 0659c10..3a0ee7a 100644 --- a/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/BountifulWrapper8.java @@ -31,7 +31,6 @@ import org.bukkit.entity.Player; import java.util.HashMap; import java.util.Map; -import java.util.UUID; public class BountifulWrapper8 implements BountifulWrapper.IBountifulWrapper { @@ -66,30 +65,21 @@ public class BountifulWrapper8 implements BountifulWrapper.IBountifulWrapper { return watchableObjectConstructor.invoke(watchableDatatypes.get(value.getClass()), dwo, value); } - private static final Class teleportPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport"); - private static final Reflection.FieldAccessor teleportX = Reflection.getField(teleportPacket, int.class, 1); - private static final Reflection.FieldAccessor teleportY = Reflection.getField(teleportPacket, int.class, 2); - private static final Reflection.FieldAccessor teleportZ = Reflection.getField(teleportPacket, int.class, 3); @Override - public void setTeleportPacketPosition(Object packet, double x, double y, double z) { - teleportX.set(packet, MathHelper.floor(x * 32)); - teleportY.set(packet, MathHelper.floor(y * 32)); - teleportZ.set(packet, MathHelper.floor(z * 32)); + 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); + + return (packet, x, y, z) -> { + posX.set(packet, MathHelper.floor(x * 32)); + posY.set(packet, MathHelper.floor(y * 32)); + posZ.set(packet, MathHelper.floor(z * 32)); + }; } @Override - public void setSpawnPacketUUID(Object packet, UUID uuid) { - // field not present - } - - private static final Class namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn"); - private static final Reflection.FieldAccessor namedSpawnX = Reflection.getField(namedSpawnPacket, int.class, 0); - private static final Reflection.FieldAccessor namedSpawnY = Reflection.getField(namedSpawnPacket, int.class, 1); - private static final Reflection.FieldAccessor namedSpawnZ = Reflection.getField(namedSpawnPacket, int.class, 2); - @Override - public void setNamedSpawnPosition(Object packet, double x, double y, double z) { - namedSpawnX.set(packet, MathHelper.floor(x * 32)); - namedSpawnY.set(packet, MathHelper.floor(y * 32)); - namedSpawnZ.set(packet, MathHelper.floor(z * 32)); + public BountifulWrapper.UUIDSetter getUUIDSetter(Class packetClass) { + return (packet, uuid) -> {}; } } diff --git a/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java b/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java index 08123bf..a12c6fb 100644 --- a/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java @@ -43,21 +43,25 @@ public class ProtocolWrapper8 implements ProtocolWrapper { } private static final Reflection.FieldAccessor spawnType; + private static final Reflection.FieldAccessor spawnLivingType = Reflection.getField(ProtocolWrapper.spawnLivingPacket, int.class, 1); private static final Object tnt; private static final Object arrow; private static final Object fireball; + private static final int armorStand; static { if(Core.getVersion() < 14) { spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, Core.getVersion() > 8 ? 6 : 9); tnt = 50; arrow = 60; fireball = 63; + armorStand = 30; } else { Class entityTypes = Reflection.getClass("{nms.world.entity}.EntityTypes"); spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, entityTypes, 0); tnt = Reflection.getField(entityTypes, "TNT", entityTypes).get(null); arrow = Reflection.getField(entityTypes, "ARROW", entityTypes).get(null); fireball = Reflection.getField(entityTypes, "FIREBALL", entityTypes).get(null); + armorStand = 1; } } @Override @@ -72,6 +76,11 @@ public class ProtocolWrapper8 implements ProtocolWrapper { case FIREBALL: spawnType.set(packet, fireball); break; + case ARMOR_STAND: + spawnLivingType.set(packet, armorStand); + break; + default: + throw new IllegalArgumentException(type.name() + " is not implemented"); } } diff --git a/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java b/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java index c16300c..dc7c774 100644 --- a/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java +++ b/SpigotCore_9/src/de/steamwar/core/BountifulWrapper9.java @@ -55,32 +55,23 @@ public class BountifulWrapper9 implements BountifulWrapper.IBountifulWrapper { return itemConstructor.invoke(dwo, value); } - private static final Class teleportPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport"); - private static final Reflection.FieldAccessor teleportX = Reflection.getField(teleportPacket, double.class, 0); - private static final Reflection.FieldAccessor teleportY = Reflection.getField(teleportPacket, double.class, 1); - private static final Reflection.FieldAccessor teleportZ = Reflection.getField(teleportPacket, double.class, 2); @Override - public void setTeleportPacketPosition(Object packet, double x, double y, double z) { - teleportX.set(packet, x); - teleportY.set(packet, y); - teleportZ.set(packet, z); + 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); + + return (packet, x, y, z) -> { + posX.set(packet, x); + posY.set(packet, y); + posZ.set(packet, z); + }; } - private static final Class spawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntity"); - private static final Reflection.FieldAccessor spawnUUID = Reflection.getField(spawnPacket, UUID.class, 0); @Override - public void setSpawnPacketUUID(Object packet, UUID uuid) { - spawnUUID.set(packet, uuid); - } + public BountifulWrapper.UUIDSetter getUUIDSetter(Class packetClass) { + Reflection.FieldAccessor uuidField = Reflection.getField(packetClass, UUID.class, 0); - private static final Class namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn"); - private static final Reflection.FieldAccessor namedSpawnX = Reflection.getField(namedSpawnPacket, double.class, 0); - private static final Reflection.FieldAccessor namedSpawnY = Reflection.getField(namedSpawnPacket, double.class, 1); - private static final Reflection.FieldAccessor namedSpawnZ = Reflection.getField(namedSpawnPacket, double.class, 2); - @Override - public void setNamedSpawnPosition(Object packet, double x, double y, double z) { - namedSpawnX.set(packet, x); - namedSpawnY.set(packet, y); - namedSpawnZ.set(packet, z); + return uuidField::set; } } diff --git a/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java b/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java index 6d5bc69..f71d234 100644 --- a/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/BountifulWrapper.java @@ -37,8 +37,16 @@ public class BountifulWrapper { Object getDataWatcherObject(int index, Class type); Object getDataWatcherItem(Object dataWatcherObject, Object value); - void setTeleportPacketPosition(Object packet, double x, double y, double z); - void setSpawnPacketUUID(Object packet, UUID uuid); - void setNamedSpawnPosition(Object packet, double x, double y, double z); + + PositionSetter getPositionSetter(Class packetClass, int fieldOffset8); + UUIDSetter getUUIDSetter(Class packetClass); + } + + public interface PositionSetter { + void set(Object packet, double x, double y, double z); + } + + public interface UUIDSetter { + void set(Object packet, UUID uuid); } } diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index b569428..9e811e5 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -27,6 +27,7 @@ public interface ProtocolWrapper { Class itemStack = Reflection.getClass("{nms.world.item}.ItemStack"); Class spawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntity"); + Class spawnLivingPacket = Core.getVersion() > 18 ? ProtocolWrapper.spawnPacket : Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntityLiving"); Class equipmentPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityEquipment"); // 0: hand, 1: offhand, 2: feet, 3: legs, 4: chest, 5: head diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntity.java b/SpigotCore_Main/src/de/steamwar/entity/REntity.java index 98b8fef..dac3e7f 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntity.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntity.java @@ -74,6 +74,7 @@ public class REntity { private boolean invisible; private boolean sneaks; + private boolean bowDrawn; private int fireTick; private String displayName; private final Map itemSlots; @@ -157,24 +158,29 @@ public class REntity { public void sneak(boolean sneaking) { sneaks = sneaking; - server.updateEntity(this, getDataWatcherPacket(sneakingDataWatcher, FlatteningWrapper.impl.getPose(sneaking))); //TODO entityStatusData in 1.8! + if(Core.getVersion() > 12) { + server.updateEntity(this, getDataWatcherPacket(sneakingDataWatcher, FlatteningWrapper.impl.getPose(sneaking))); + } else { + server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); + } } public void setOnFire(boolean perma) { fireTick = perma ? -1 : 21; - server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, (byte) 1)); + server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); } public void setInvisible(boolean invisible) { this.invisible = invisible; - server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, (byte) 0x20)); + server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); } public void setBowDrawn(boolean drawn, boolean offHand) { + bowDrawn = drawn; if(Core.getVersion() > 8){ server.updateEntity(this, getDataWatcherPacket(bowDrawnWatcher, (byte) ((drawn ? 1 : 0) + (offHand ? 2 : 0)))); }else{ - server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, (byte)0x10)); + server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); } } @@ -217,7 +223,7 @@ public class REntity { server.updateEntity(this, getEquipmentPacket(slot, stack)); } - public void close() { + public void die() { server.removeEntity(this); } @@ -229,30 +235,27 @@ public class REntity { packetSink.accept(getEquipmentPacket(entry.getKey(), entry.getValue())); } packetSink.accept(getDataWatcherPacket(skinPartsDataWatcher, (byte) 0x7F)); //TODO multiversioning - } else if(entityType == EntityType.ARMOR_STAND) { + } else if(entityType.isAlive()) { packetSink.accept(getSpawnLivingEntityPacket()); - packetSink.accept(getDataWatcherPacket(sizeWatcher, (byte) 0x10)); // small size //TODO multiversioning + packetSink.accept(getDataWatcherPacket(sizeWatcher, (byte) 0x10)); // small size //TODO multiversioning TODO only on ArmorStands for (Map.Entry entry : itemSlots.entrySet()) { packetSink.accept(getEquipmentPacket(entry.getKey(), entry.getValue())); } } else { packetSink.accept(getSpawnEntityPacket()); } - packetSink.accept(getTeleportPacket()); //TODO teleport necessary + //packetSink.accept(getTeleportPacket()); packetSink.accept(getHeadRotationPacket()); //TODO head rotation for all types? //TODO merge MetadataPacket (DataWatcherPackets) - if(sneaks) { - packetSink.accept(getDataWatcherPacket(entityStatusWatcher, FlatteningWrapper.impl.getPose(true))); + if(Core.getVersion() > 12 && sneaks) { + packetSink.accept(getDataWatcherPacket(sneakingDataWatcher, FlatteningWrapper.impl.getPose(true))); } - if(fireTick != 0) { - packetSink.accept(getDataWatcherPacket(sneakingDataWatcher, getDataWatcherPacket(entityStatusWatcher, (byte)1))); - } - - if(invisible) { - packetSink.accept(getDataWatcherPacket(entityStatusWatcher, (byte) 0x20)); + byte status = getEntityStatus(); + if(status != 0) { + packetSink.accept(getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); } if(displayName != null) { @@ -265,7 +268,7 @@ public class REntity { if(fireTick > 0) { fireTick--; if(fireTick == 0) { - server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, (byte)0)); + server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus())); } } } @@ -297,6 +300,21 @@ public class REntity { return z; } + private byte getEntityStatus() { + byte status = 0; + + if(fireTick != 0) + status |= 1; + if(Core.getVersion() <= 12 && sneaks) + status |= 2; + if(Core.getVersion() == 8 && bowDrawn) + status |= 0x10; + if(invisible) + status |= 0x20; + + return status; + } + private static final Class metadataPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityMetadata"); private static final Reflection.FieldAccessor metadataEntity = Reflection.getField(metadataPacket, int.class, 0); private static final Reflection.FieldAccessor metadataMetadata = Reflection.getField(metadataPacket, List.class, 0); @@ -316,12 +334,13 @@ 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 Reflection.FieldAccessor teleportYaw = Reflection.getField(teleportPacket, byte.class, 0); private static final Reflection.FieldAccessor teleportPitch = Reflection.getField(teleportPacket, byte.class, 1); private Object getTeleportPacket(){ Object packet = Reflection.newInstance(teleportPacket); teleportEntity.set(packet, entityId); - BountifulWrapper.impl.setTeleportPacketPosition(packet, x, y, z); + teleportPosition.set(packet, x, y, z); teleportYaw.set(packet, yaw); teleportPitch.set(packet, pitch); return packet; @@ -366,42 +385,37 @@ public class REntity { private static final Class namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn"); private static final Reflection.FieldAccessor namedSpawnEntity = Reflection.getField(namedSpawnPacket, int.class, 0); private static final Reflection.FieldAccessor namedSpawnUUID = Reflection.getField(namedSpawnPacket, UUID.class, 0); + private static final BountifulWrapper.PositionSetter namedSpawnPosition = BountifulWrapper.impl.getPositionSetter(namedSpawnPacket, 1); private Object getNamedSpawnPacket() { Object packet = Reflection.newInstance(namedSpawnPacket); namedSpawnEntity.set(packet, entityId); namedSpawnUUID.set(packet, uuid); - BountifulWrapper.impl.setNamedSpawnPosition(packet, x, y, z); + namedSpawnPosition.set(packet, x, y, z); FlatteningWrapper.impl.setNamedSpawnPacketDataWatcher(packet); return packet; } - private static final Class spawnLivingPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntityLiving"); //TODO not existing in 1.19 - private static final Reflection.ConstructorInvoker spawnLivingPacketConstructor = Reflection.getConstructor(spawnLivingPacket); //TODO multiversioning - private static final Reflection.FieldAccessor spawnLivingEntityId = Reflection.getField(spawnLivingPacket, int.class, 0); //TODO multiversioning - private static final Reflection.FieldAccessor spawnLivingUUID = Reflection.getField(spawnLivingPacket, UUID.class, 0); //TODO not existing in 1.8 - private static final Reflection.FieldAccessor spawnLivingEntityType = Reflection.getField(spawnLivingPacket, int.class, 1); //TODO multiversioning - private static final Reflection.FieldAccessor spawnLivingEntityX = Reflection.getField(spawnLivingPacket, double.class, 0); //TODO int in 1.8 - private static final Reflection.FieldAccessor spawnLivingEntityY = Reflection.getField(spawnLivingPacket, double.class, 1); //TODO int in 1.8 - private static final Reflection.FieldAccessor spawnLivingEntityZ = Reflection.getField(spawnLivingPacket, double.class, 2); //TODO int in 1.8 + private static final Reflection.FieldAccessor spawnLivingEntityId = Reflection.getField(ProtocolWrapper.spawnLivingPacket, int.class, 0); + private static final BountifulWrapper.UUIDSetter livingSpawnUUID = BountifulWrapper.impl.getUUIDSetter(ProtocolWrapper.spawnLivingPacket); + private static final BountifulWrapper.PositionSetter livingSpawnPosition = BountifulWrapper.impl.getPositionSetter(ProtocolWrapper.spawnLivingPacket, 2); //TODO only diff to spawnEntity, simplify code private Object getSpawnLivingEntityPacket() { - Object packet = spawnLivingPacketConstructor.invoke(); + Object packet = Reflection.newInstance(ProtocolWrapper.spawnLivingPacket); spawnLivingEntityId.set(packet, entityId); - spawnLivingUUID.set(packet, uuid); - spawnLivingEntityType.set(packet, 1); //TODO set correct type - spawnLivingEntityX.set(packet, x); - spawnLivingEntityY.set(packet, y); - spawnLivingEntityZ.set(packet, z); + livingSpawnUUID.set(packet, uuid); + ProtocolWrapper.impl.setSpawnPacketType(packet, entityType); + livingSpawnPosition.set(packet, x, y, z); return packet; } - private static final Reflection.FieldAccessor spawnEntity = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, 0); + private static final BountifulWrapper.UUIDSetter spawnUUID = BountifulWrapper.impl.getUUIDSetter(ProtocolWrapper.spawnPacket); + private static final BountifulWrapper.PositionSetter spawnPosition = BountifulWrapper.impl.getPositionSetter(ProtocolWrapper.spawnPacket, 1); private Object getSpawnEntityPacket() { Object packet = Reflection.newInstance(ProtocolWrapper.spawnPacket); spawnEntity.set(packet, entityId); - BountifulWrapper.impl.setSpawnPacketUUID(packet, uuid); + spawnUUID.set(packet, uuid); ProtocolWrapper.impl.setSpawnPacketType(packet, entityType); - //TODO set position + spawnPosition.set(packet, x, y, z); return packet; }