From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cryptite Date: Mon, 12 Jun 2023 14:14:48 -0500 Subject: [PATCH] Equipment Packet Caching diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java index 5a8f850b447fc3a4bd0eb0c505bbdfc8be7115e8..34d74735b7a7d258c6bd14bb7e5406934a208a31 100644 --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java @@ -18,6 +18,15 @@ public class ClientboundSetEquipmentPacket implements Packet> equipmentList, net.minecraft.world.entity.LivingEntity entity, String tag) { + this.entity = id; + slots = new java.util.ArrayList<>(equipmentList.size()); + for (Pair pair : equipmentList) { + EquipmentSlot slot = pair.getFirst(); + slots.add(Pair.of(slot, entity.getOrCreateCachedEquipmentItem(tag, slot, pair.getSecond()))); + } + } + public ClientboundSetEquipmentPacket(FriendlyByteBuf buf) { this.entity = buf.readVarInt(); EquipmentSlot[] equipmentSlots = EquipmentSlot.values(); diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java index e3e0f306ebed31ce4742e7d2f15ab102b93a9dff..4f4ea6dcef4e1960ac69271fbd771dd3ad6af275 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -331,27 +331,8 @@ public class ServerEntity { sender.accept(new ClientboundSetEntityMotionPacket(this.entity.getId(), this.ap)); } - if (this.entity instanceof LivingEntity) { - List> list = Lists.newArrayList(); - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); - int i = aenumitemslot.length; - - for (int j = 0; j < i; ++j) { - EquipmentSlot enumitemslot = aenumitemslot[j]; - ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(enumitemslot); - - if (!itemstack.isEmpty()) { - // Paper start - prevent oversized data - final ItemStack sanitized = LivingEntity.sanitizeItemStack(itemstack.copy(), false); - list.add(Pair.of(enumitemslot, ((LivingEntity) this.entity).stripMeta(sanitized, false))); // Paper - Hide unnecessary item meta - // Paper end - prevent oversized data - } - } - - if (!list.isEmpty()) { - sender.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list)); - } - ((LivingEntity) this.entity).detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending + if (this.entity instanceof LivingEntity livingEntity) { + livingEntity.sendEquipment(sender, player); // Slice } if (!this.entity.getPassengers().isEmpty()) { diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index 23570a0b1227a840b9c1e6ae326827ea655bb5f7..e2b6d539395fbc59116cfce56189e0f129a34e0d 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -143,7 +143,6 @@ import org.bukkit.event.entity.EntityTeleportEvent; import org.bukkit.event.player.PlayerItemConsumeEvent; // CraftBukkit end -import co.aikar.timings.MinecraftTimings; // Paper public abstract class LivingEntity extends Entity implements Attackable { @@ -264,6 +263,11 @@ public abstract class LivingEntity extends Entity implements Attackable { public boolean silentDeath = false; // Paper - mark entity as dying silently for cancellable death event public net.kyori.adventure.util.TriState frictionState = net.kyori.adventure.util.TriState.NOT_SET; // Paper - Friction API + // Slice start + private static final java.util.Map> equipmentPacketCache = new java.util.HashMap<>(); + protected final java.util.Map cachedEquipmentPacket = new java.util.HashMap<>(); + // Slice end + @Override public float getBukkitYaw() { return this.getYHeadRot(); @@ -3133,6 +3137,7 @@ public abstract class LivingEntity extends Entity implements Attackable { if (map != null) { this.handleHandSwap(map); if (!map.isEmpty()) { + cachedEquipmentPacket.clear(); // Slice - Must invalidate cached equipment map if we have changes this.handleEquipmentChanges(map); } } @@ -3225,7 +3230,25 @@ public abstract class LivingEntity extends Entity implements Attackable { } }); - ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list)); + + // Slice start + net.minecraft.server.level.ChunkMap.TrackedEntity entityTracker = ((ServerLevel) this.level()).getChunkSource().chunkMap.entityMap.get(getId()); + if (entityTracker != null) { + ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(this.getId(), list); + for (net.minecraft.server.network.ServerPlayerConnection playerConnection : entityTracker.seenBy) { + ServerPlayer player = playerConnection.getPlayer(); + org.bukkit.event.player.PlayerReceiveEquipmentEvent event = new org.bukkit.event.player.PlayerReceiveEquipmentEvent(player.getBukkitEntity(), getBukkitEntity()); + level().getCraftServer().getPluginManager().callEvent(event); + + String tag = event.getTag(); + if (tag != null) { + playerConnection.send(new ClientboundSetEquipmentPacket(this.getId(), list, this, tag)); + } else { + playerConnection.send(packet); + } + } + } + // Slice end } // Paper start - Hide unnecessary item meta @@ -4565,4 +4588,76 @@ public abstract class LivingEntity extends Entity implements Attackable { public static record Fallsounds(SoundEvent small, SoundEvent big) { } + + // Slice start + public ItemStack getOrCreateCachedEquipmentItem(String tag, EquipmentSlot slot, ItemStack itemStack) { + return equipmentPacketCache.computeIfAbsent(tag, s -> com.google.common.cache.CacheBuilder.newBuilder().expireAfterWrite(15, java.util.concurrent.TimeUnit.MINUTES).build()) + .asMap() + .computeIfAbsent(itemStack, i -> { + String name = slot.name(); + + //How neat is this. + if (name.equals("MAINHAND")) { + name = "HAND"; + } else if (name.equals("OFFHAND")) { + name = "OFF_HAND"; + } + + org.bukkit.event.entity.EntityEquipmentItemLookup event = new org.bukkit.event.entity.EntityEquipmentItemLookup(getBukkitEntity(), tag, org.bukkit.inventory.EquipmentSlot.valueOf(name), CraftItemStack.asBukkitCopy(i)); + this.level().getCraftServer().getPluginManager().callEvent(event); + org.bukkit.inventory.ItemStack eventItem = event.getItemStack(); + return CraftItemStack.asNMSCopy(eventItem); + }); + } + // Slice end + + // Slice start + public void sendEquipment(java.util.function.Consumer> consumer, ServerPlayer p) { + org.bukkit.event.player.PlayerReceiveEquipmentEvent event = new org.bukkit.event.player.PlayerReceiveEquipmentEvent(p.getBukkitEntity(), getBukkitEntity()); + level().getCraftServer().getPluginManager().callEvent(event); + + boolean sendEquipment = !event.isCancelled(); + String tag = event.getTag(); + if (sendEquipment && this instanceof ServerPlayer player && tag != null) { + ClientboundSetEquipmentPacket equipmentPacket = player.cachedEquipmentPacket.get(tag); + if (equipmentPacket != null) { + //Event says use a tag, and our tag exists; so we simply used our entire cached packet + consumer.accept(equipmentPacket); + return; + } + } + + if (sendEquipment) { + EquipmentSlot[] equipmentSlots = EquipmentSlot.values(); + List> list = new ArrayList<>(equipmentSlots.length); + + for (EquipmentSlot enumitemslot : equipmentSlots) { + ItemStack itemstack = getItemBySlot(enumitemslot); + + if (!itemstack.isEmpty()) { + // Paper start - prevent oversized data + final ItemStack sanitized = LivingEntity.sanitizeItemStack(itemstack.copy(), false); + ItemStack strippedItem = stripMeta(sanitized, false); + + if (tag != null) { + strippedItem = getOrCreateCachedEquipmentItem(tag, enumitemslot, strippedItem); + } + + list.add(Pair.of(enumitemslot, strippedItem)); // Paper - remove unnecessary item meta + // Paper end - prevent oversized data + } + } + + if (!list.isEmpty()) { + ClientboundSetEquipmentPacket equipmentPacket = new ClientboundSetEquipmentPacket(getId(), list); + if (tag != null) { + cachedEquipmentPacket.put(tag, equipmentPacket); + } + consumer.accept(equipmentPacket); + } + + detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending + } + } + // Slice end } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 3f952d1409118535f325f614f1a6507c40efa4d6..3e36b1e140c5f82018d603db84eaab2e1c80f6da 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -1156,4 +1156,14 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { getHandle().knockback(strength, directionX, directionZ); }; // Paper end + + // Slice start + @Override + public void sendEquipment(Player p) { + if (entity instanceof net.minecraft.world.entity.LivingEntity livingEntity) { + net.minecraft.server.level.ServerPlayer serverPlayer = ((CraftPlayer) p).getHandle(); + livingEntity.sendEquipment(packet -> serverPlayer.connection.send(packet), serverPlayer); + } + } + // Slice end }