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 516fcc67aa83a870b74372742b81d0acef242655..07febce429ebd15656e8483d2d2af9e93c13de19 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -328,27 +328,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 - remove unnecessary item meta - // Paper end - } - } - - if (!list.isEmpty()) { - sender.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list)); - } - ((LivingEntity) this.entity).detectEquipmentUpdates(); // 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 b7c9876eaff4365d136a9af20c1a5845b3094336..a5165d76d8495242708093f7e4449f6f67386ad7 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -263,6 +263,8 @@ public abstract class LivingEntity extends Entity implements Attackable { public org.bukkit.craftbukkit.entity.CraftLivingEntity getBukkitLivingEntity() { return (org.bukkit.craftbukkit.entity.CraftLivingEntity) super.getBukkitEntity(); } // Paper 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 + private final com.google.common.collect.Table equipmentPacketCache = com.google.common.collect.HashBasedTable.create(); // Slice + public java.util.Map cachedEquipmentMap = new java.util.HashMap<>(); // Slice @Override public float getBukkitYaw() { @@ -3071,6 +3073,7 @@ public abstract class LivingEntity extends Entity implements Attackable { if (map != null) { this.handleHandSwap(map); if (!map.isEmpty()) { + cachedEquipmentMap.clear(); // Slice - Must invalidate cached equipment map if we have changes this.handleEquipmentChanges(map); } } @@ -3163,7 +3166,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 @@ -4450,4 +4471,74 @@ 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.row(tag).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.cachedEquipmentMap.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); + sendEquipment = false; + } + } + + 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 + } + } + + if (!list.isEmpty()) { + ClientboundSetEquipmentPacket equipmentPacket = new ClientboundSetEquipmentPacket(getId(), list); + if (tag != null) { + cachedEquipmentMap.put(tag, equipmentPacket); + } + consumer.accept(equipmentPacket); + } + + detectEquipmentUpdates(); // 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 6095d43e08bc1ffc4e9a4796fc7adbd07d69d716..8c29e292b8e495ca64668c6a22b9765ffe836a08 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -1099,4 +1099,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 }