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 58b602e550258c1062ee940bc46538dac95d8979..3ad5950d05fed7ff8810a8a7969fcb58547b1021 100644 --- a/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java +++ b/src/main/java/net/minecraft/network/syncher/SynchedEntityData.java @@ -165,15 +165,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 @@ -215,6 +229,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; @@ -339,11 +373,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() { @@ -373,6 +410,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 ae188ae314336d971303023c7b7b8ecf32bae253..506e4d80e47723a0764703578370a45ad2f126fd 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -380,7 +380,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 bee42880274cf36c57abc5f633e6f14f41a0ea2b..12fa631aa176e67dcda522dca6376a69ca5b34d8 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -3388,7 +3388,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S 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/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index f1459e5929f178cdab864506d1a485df61fde035..6e7a3acb40ef3ed062c3e6cc2c5bf5cd35f2d2d6 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -3167,7 +3167,7 @@ public abstract class LivingEntity extends Entity implements Attackable { } public boolean equipmentHasChanged(ItemStack stack, ItemStack stack2) { - return !ItemStack.isSame(stack2, stack); + return !ItemStack.isSameItem(stack2, stack); } private void handleHandSwap(Map equipmentChanges) { 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 2a6a85519c63bd3f879a8dbe146a9d05310cee3c..d6cdb7c3314004ee25cbed96eb289d420999c01c 100644 --- a/src/main/java/net/minecraft/world/entity/player/Player.java +++ b/src/main/java/net/minecraft/world/entity/player/Player.java @@ -644,7 +644,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 82eb22d1539951cd04267d7269b1447ebc4ee2c6..d95d08fd519ca633c1c126982f3720e7c3aabe28 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -2733,7 +2733,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 = this.getMaxHealth(); }