From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cryptite Date: Tue, 6 Jun 2023 07:51:32 -0500 Subject: [PATCH] Packet obfuscation and reduction diff --git a/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java b/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java index ff7ba7a161cfed7521354bc6e3f21ba0f17f3760..b5f4af655fdc6a9150802254811ed743cc61074c 100644 --- a/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java +++ b/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java @@ -139,15 +139,29 @@ public class SynchedEntityData { } public void set(EntityDataAccessor key, T value, boolean force) { + // Slice start + this.set(key, value, force, null); + } + + public void set(EntityDataAccessor key, T value, @Nullable T foreignValue) { + this.set(key, value, false, foreignValue); + } + + public void set(EntityDataAccessor key, T value, boolean force, @Nullable T foreignValue) { // Slice end SynchedEntityData.DataItem datawatcher_item = this.getItem(key); + // Slice start + if (foreignValue != null && ObjectUtils.notEqual(foreignValue, datawatcher_item.getForeignValue())) { + datawatcher_item.setForeignValue(foreignValue); + } + // Slice end + if (force || ObjectUtils.notEqual(value, datawatcher_item.getValue())) { datawatcher_item.setValue(value); this.entity.onSyncedDataUpdated(key); datawatcher_item.setDirty(true); this.isDirty = true; } - } // CraftBukkit start - add method from above @@ -189,6 +203,26 @@ public class SynchedEntityData { return list; } + // Slice start + @Nullable + public List> packForeignDirty() { + List> list = null; + + for (DataItem dataItem : this.itemsById.values()) { + if (dataItem.isDirty(true)) { + dataItem.setForeignDirty(false); + if (list == null) { + list = new ArrayList(); + } + + list.add(dataItem.foreignValue != null ? dataItem.foreignValue() : dataItem.value()); + } + } + + return list; + } + // Slice end + @Nullable public List> getNonDefaultValues() { List> list = null; @@ -296,11 +330,14 @@ public class SynchedEntityData { T value; private final T initialValue; private boolean dirty; + private @Nullable T foreignValue = null; // Slice + private boolean foreignDirty; // Slice public DataItem(EntityDataAccessor data, T value) { this.accessor = data; this.initialValue = value; this.value = value; + this.foreignDirty = true; //Slice } public EntityDataAccessor getAccessor() { @@ -330,6 +367,35 @@ public class SynchedEntityData { public SynchedEntityData.DataValue value() { return SynchedEntityData.DataValue.create(this.accessor, this.value); } + + // Slice start + public SynchedEntityData.DataValue foreignValue() { + return SynchedEntityData.DataValue.create(this.accessor, this.foreignValue); + } + + public void setForeignValue(T foreignValue) { + this.foreignValue = foreignValue; + this.foreignDirty = true; + } + + public @Nullable T getForeignValue() { + return foreignValue; + } + + public boolean isDirty(boolean foreign) { + if (foreign) { + //There must be a foreign value in order for this to be dirty, otherwise we consider this a normal + //value and check the normal dirty flag. + return foreignValue == null || this.foreignDirty; + } + + return this.dirty; + } + + public void setForeignDirty(boolean dirty) { + this.foreignDirty = dirty; + } + // Slice end } public static record DataValue(int id, EntityDataSerializer serializer, T value) { // CraftBukkit - decompile error diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java index d86836541f5eaa654d3dc452fe7cc15eafff4dfa..5de01ea9b5bd6d4d67024cd045a68a730365a6fa 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -378,7 +378,20 @@ public class ServerEntity { if (list != null) { this.trackedDataValues = datawatcher.getNonDefaultValues(); - this.broadcastAndSend(new ClientboundSetEntityDataPacket(this.entity.getId(), list)); +// this.broadcastAndSend(new ClientboundSetEntityDataPacket(this.entity.getId(), list)); + // Slice start + ClientboundSetEntityDataPacket dataPacket = new ClientboundSetEntityDataPacket(this.entity.getId(), list); + if (this.entity instanceof ServerPlayer serverPlayer) { + serverPlayer.connection.send(dataPacket); + } + + //Get the packedData that the original packet has, and then determine if any of those are changed in + //the foreign version. If null, nothing to notify foreign trackers about. + List> dirtyItems = datawatcher.packForeignDirty(); + if (dirtyItems != null) { + this.broadcast.accept(new ClientboundSetEntityDataPacket(this.entity.getId(), dirtyItems)); + } + // Slice end } if (this.entity instanceof LivingEntity) { diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java index 66bcacfafe25f2543d40eb959ef251da400a2c73..94a3d39f9802be7b71adf378985f132fd7f5c0fd 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -3151,7 +3151,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { this.entityData.markDirty(Entity.DATA_AIR_SUPPLY_ID); return; } - this.entityData.set(Entity.DATA_AIR_SUPPLY_ID, event.getAmount()); + this.entityData.set(Entity.DATA_AIR_SUPPLY_ID, event.getAmount(), getMaxAirSupply()); // Slice // CraftBukkit end } diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java index 28125492ceed99b244c5e177edb6739a9c2d68d9..8821d57e1f111fb4528d7645ba635c7d8687c8a9 100644 --- a/src/main/java/net/minecraft/world/entity/player/Player.java +++ b/src/main/java/net/minecraft/world/entity/player/Player.java @@ -641,7 +641,7 @@ public abstract class Player extends LivingEntity { public void increaseScore(int score) { int j = this.getScore(); - this.entityData.set(Player.DATA_SCORE_ID, j + score); + this.entityData.set(Player.DATA_SCORE_ID, j + score, 0); // Slice } public void startAutoSpinAttack(int riptideTicks) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 1b58330a0eff27eb920a37dad0b436049fe0bce6..4ae992fd174135eda153015236c6a3b884af3dd8 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -2515,7 +2515,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { this.sendHealthUpdate(); } } - this.getHandle().getEntityData().set(net.minecraft.world.entity.LivingEntity.DATA_HEALTH_ID, (float) this.getScaledHealth()); + this.getHandle().getEntityData().set(net.minecraft.world.entity.LivingEntity.DATA_HEALTH_ID, (float) this.getScaledHealth(), isDead() ? 0f : 20f); // Slice this.getHandle().maxHealthCache = getMaxHealth(); }