From 11d3d8ff08a703ea6ab32d5288619d293ce5e3d7 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 12 Apr 2021 18:14:46 +0200 Subject: [PATCH 1/5] Moar Packets --- .../spectatesystem/PacketProcessor.java | 23 +++++++++++++++- .../spectatesystem/elements/REntity.java | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/spectatesystem/PacketProcessor.java b/src/de/steamwar/spectatesystem/PacketProcessor.java index f2a53ec..bf5b5d5 100644 --- a/src/de/steamwar/spectatesystem/PacketProcessor.java +++ b/src/de/steamwar/spectatesystem/PacketProcessor.java @@ -305,6 +305,22 @@ class PacketProcessor { packetCounter.clear(); } + private void bow() { + //TODO implement Bow + } + + private void damage() throws IOException { + int entityId = source.rInt(); + + execSync(() -> REntity.getEntity(entityId).damage()); + } + + private void fireTick() throws IOException { + int entityId = source.rInt(); + + execSync(() -> REntity.getEntity(entityId).setOnFire()); + } + private void process(){ try{ @@ -343,6 +359,11 @@ class PacketProcessor { case 0x09: fireballSpawn(); break; + case 0x0A: + case 0x0B: + damage(); + case 0x0C: + fireTick(); case 0x30: block(); break; @@ -392,7 +413,7 @@ class PacketProcessor { tickFinished = true; break; default: - Bukkit.getLogger().log(Level.SEVERE, "Unknown packet recieved, closing"); + Bukkit.getLogger().log(Level.SEVERE, "Unknown packet recieved, closing: " + packetType); source.close(); } } diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index fc11e99..17a98a9 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -104,6 +104,8 @@ public abstract class REntity { } public void sneak(boolean sneaking) { + entity.setSneaking(sneaking); + DataWatcher dataWatcher = new DataWatcher(null); DataWatcherObject dataWatcherObject = new DataWatcherObject<>(6, DataWatcherRegistry.s); @@ -117,6 +119,30 @@ public abstract class REntity { } } + public void setOnFire() { + entity.setOnFire(21, false); + + DataWatcher dataWatcher = new DataWatcher(null); + + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); + dataWatcher.register(dataWatcherObject, (byte) 1); + dataWatcher.markDirty(dataWatcherObject); + + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); + + for(Player player : Bukkit.getOnlinePlayers()){ + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + } + + public void damage() { + PacketPlayOutEntityStatus packet = new PacketPlayOutEntityStatus(entity, (byte) 2); + + for(Player player : Bukkit.getOnlinePlayers()){ + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + } + public void animation(byte animation) { PacketPlayOutAnimation packet = new PacketPlayOutAnimation(entity, animation); for(Player player : Bukkit.getOnlinePlayers()){ From 2aad8cba5873fd6a61ed446960a3ff9a4ad8b1d5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 12 Apr 2021 23:26:59 +0200 Subject: [PATCH 2/5] Functional Bow, Damage and Fire! --- .../spectatesystem/PacketProcessor.java | 15 ++++++++++++--- .../spectatesystem/elements/REntity.java | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/spectatesystem/PacketProcessor.java b/src/de/steamwar/spectatesystem/PacketProcessor.java index bf5b5d5..fe14625 100644 --- a/src/de/steamwar/spectatesystem/PacketProcessor.java +++ b/src/de/steamwar/spectatesystem/PacketProcessor.java @@ -305,8 +305,12 @@ class PacketProcessor { packetCounter.clear(); } - private void bow() { - //TODO implement Bow + private void bow() throws IOException { + int entityId = source.rInt(); + boolean drawn = source.rBoolean(); + int hand = source.rInt(); + + execSync(() -> REntity.getEntity(entityId).setBowDrawn(drawn, hand)); } private void damage() throws IOException { @@ -317,8 +321,9 @@ class PacketProcessor { private void fireTick() throws IOException { int entityId = source.rInt(); + boolean perma = source.rBoolean(); - execSync(() -> REntity.getEntity(entityId).setOnFire()); + execSync(() -> REntity.getEntity(entityId).setOnFire(perma)); } private void process(){ @@ -360,10 +365,14 @@ class PacketProcessor { fireballSpawn(); break; case 0x0A: + bow(); + break; case 0x0B: damage(); + break; case 0x0C: fireTick(); + break; case 0x30: block(); break; diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index 17a98a9..f2f651f 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -119,8 +119,9 @@ public abstract class REntity { } } - public void setOnFire() { - entity.setOnFire(21, false); + public void setOnFire(boolean perma) { + if(!perma) + entity.setOnFire(21, false); DataWatcher dataWatcher = new DataWatcher(null); @@ -135,6 +136,20 @@ public abstract class REntity { } } + public void setBowDrawn(boolean drawn, int hand) { + DataWatcher dataWatcher = new DataWatcher(null); + + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(7, DataWatcherRegistry.a); + dataWatcher.register(dataWatcherObject, (byte) ((drawn?1:0) + (hand * 2))); + dataWatcher.markDirty(dataWatcherObject); + + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); + + for(Player player : Bukkit.getOnlinePlayers()){ + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + } + public void damage() { PacketPlayOutEntityStatus packet = new PacketPlayOutEntityStatus(entity, (byte) 2); From aacff6b895184932730b41b2d7f870bde669c6f8 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 13 Apr 2021 21:29:56 +0200 Subject: [PATCH 3/5] Reordering a lot of Stuff --- .../spectatesystem/PacketProcessor.java | 1 + .../spectatesystem/elements/REntity.java | 74 ++++++++++++++----- .../spectatesystem/elements/RPlayer.java | 1 - 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/de/steamwar/spectatesystem/PacketProcessor.java b/src/de/steamwar/spectatesystem/PacketProcessor.java index fe14625..971afe7 100644 --- a/src/de/steamwar/spectatesystem/PacketProcessor.java +++ b/src/de/steamwar/spectatesystem/PacketProcessor.java @@ -418,6 +418,7 @@ class PacketProcessor { break; case (byte) 0xFF: //Tick + execSync(() -> REntity.tickFire()); if(!source.async()) tickFinished = true; break; diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index f2f651f..ed0abd5 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -31,12 +31,32 @@ import java.util.Map; public abstract class REntity { + public static void tickFire() { + getEntities().forEach((integer, entity) -> { + if(entity.fireTick > 0) + entity.fireTick--; + if(entity.fireTick == 0) { + DataWatcher dataWatcher = new DataWatcher(null); + + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); + dataWatcher.register(dataWatcherObject, (byte) 0); + dataWatcher.markDirty(dataWatcherObject); + entity.sendDataWatcher(dataWatcher); + } + }); + } + protected static final WorldServer world = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); private static final Map entities = new HashMap<>(); + private static Map getEntities() { + return entities; + } + private final int internalId; protected final Entity entity; + private int fireTick; protected REntity(int internalId, Entity entity){ this.internalId = internalId; @@ -55,6 +75,27 @@ public abstract class REntity { for(REntity entity : entities.values()){ entity.sendToPlayer(player); entity.sendLocation(player); + if(entity.fireTick != 0) { + DataWatcher dataWatcher = new DataWatcher(null); + + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); + dataWatcher.register(dataWatcherObject, (byte) 1); + dataWatcher.markDirty(dataWatcherObject); + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.entity.getId(), dataWatcher, false); + + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + if(entity.entity.isSneaking()) { + DataWatcher dataWatcher = new DataWatcher(null); + + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(6, DataWatcherRegistry.s); + dataWatcher.register(dataWatcherObject, EntityPose.CROUCHING); + dataWatcher.markDirty(dataWatcherObject); + + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.entity.getId(), dataWatcher, false); + + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } } } } @@ -112,16 +153,15 @@ public abstract class REntity { dataWatcher.register(dataWatcherObject, sneaking?EntityPose.CROUCHING:EntityPose.STANDING); dataWatcher.markDirty(dataWatcherObject); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); - - for(Player player : Bukkit.getOnlinePlayers()){ - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); - } + sendDataWatcher(dataWatcher); } public void setOnFire(boolean perma) { - if(!perma) - entity.setOnFire(21, false); + if(!perma) { + fireTick = 21; + } else { + fireTick = -1; + } DataWatcher dataWatcher = new DataWatcher(null); @@ -129,11 +169,7 @@ public abstract class REntity { dataWatcher.register(dataWatcherObject, (byte) 1); dataWatcher.markDirty(dataWatcherObject); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); - - for(Player player : Bukkit.getOnlinePlayers()){ - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); - } + sendDataWatcher(dataWatcher); } public void setBowDrawn(boolean drawn, int hand) { @@ -143,11 +179,7 @@ public abstract class REntity { dataWatcher.register(dataWatcherObject, (byte) ((drawn?1:0) + (hand * 2))); dataWatcher.markDirty(dataWatcherObject); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); - - for(Player player : Bukkit.getOnlinePlayers()){ - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); - } + sendDataWatcher(dataWatcher); } public void damage() { @@ -186,4 +218,12 @@ public abstract class REntity { protected void spawnEntity(PlayerConnection connection){ connection.sendPacket(new PacketPlayOutSpawnEntity(entity)); } + + private void sendDataWatcher(DataWatcher dataWatcher) { + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); + + for(Player player : Bukkit.getOnlinePlayers()){ + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + } } diff --git a/src/de/steamwar/spectatesystem/elements/RPlayer.java b/src/de/steamwar/spectatesystem/elements/RPlayer.java index 0276fa8..8f83a4b 100644 --- a/src/de/steamwar/spectatesystem/elements/RPlayer.java +++ b/src/de/steamwar/spectatesystem/elements/RPlayer.java @@ -54,7 +54,6 @@ public class RPlayer extends REntity { public RPlayer(UUID uuid, String name, int internalId){ super(internalId, createPlayer(uuid, name)); - //TODO Damage team.addEntry(entity.getName()); } From f532dce64b6669f9e420d471a20d017552560ae7 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 14 Apr 2021 20:37:44 +0200 Subject: [PATCH 4/5] Reordering a lot of Stuff --- .../steamwar/spectatesystem/elements/REntity.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index ed0abd5..a7dc873 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -33,15 +33,16 @@ public abstract class REntity { public static void tickFire() { getEntities().forEach((integer, entity) -> { - if(entity.fireTick > 0) + if(entity.fireTick > 0) { entity.fireTick--; - if(entity.fireTick == 0) { - DataWatcher dataWatcher = new DataWatcher(null); + if(entity.fireTick == 0) { + DataWatcher dataWatcher = new DataWatcher(null); - DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); - dataWatcher.register(dataWatcherObject, (byte) 0); - dataWatcher.markDirty(dataWatcherObject); - entity.sendDataWatcher(dataWatcher); + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); + dataWatcher.register(dataWatcherObject, (byte) 0); + dataWatcher.markDirty(dataWatcherObject); + entity.sendDataWatcher(dataWatcher); + } } }); } From a2a4b96b44d7ef97af78d7e97a50345701d44695 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 17 Apr 2021 19:42:29 +0200 Subject: [PATCH 5/5] Reduce Code Duplication --- src/de/steamwar/spectatesystem/elements/REntity.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index a7dc873..ccc5171 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -82,9 +82,7 @@ public abstract class REntity { DataWatcherObject dataWatcherObject = new DataWatcherObject<>(0, DataWatcherRegistry.a); dataWatcher.register(dataWatcherObject, (byte) 1); dataWatcher.markDirty(dataWatcherObject); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.entity.getId(), dataWatcher, false); - - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + entity.sendDataWatcher(dataWatcher); } if(entity.entity.isSneaking()) { DataWatcher dataWatcher = new DataWatcher(null); @@ -92,10 +90,7 @@ public abstract class REntity { DataWatcherObject dataWatcherObject = new DataWatcherObject<>(6, DataWatcherRegistry.s); dataWatcher.register(dataWatcherObject, EntityPose.CROUCHING); dataWatcher.markDirty(dataWatcherObject); - - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.entity.getId(), dataWatcher, false); - - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + entity.sendDataWatcher(dataWatcher); } } }