203 lines
11 KiB
Diff
203 lines
11 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Cryptite <cryptite@gmail.com>
|
|
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<ClientGamePacketLis
|
|
this.slots = equipmentList;
|
|
}
|
|
|
|
+ public ClientboundSetEquipmentPacket(int id, List<Pair<EquipmentSlot, ItemStack>> equipmentList, net.minecraft.world.entity.LivingEntity entity, String tag) {
|
|
+ this.entity = id;
|
|
+ slots = new java.util.ArrayList<>(equipmentList.size());
|
|
+ for (Pair<EquipmentSlot, ItemStack> 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 97dbcb6f527bd5c567e77200d7f6cbc1abb15792..248a4af1b9c6684502570c879efae20a36558414 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
|
|
@@ -325,27 +325,8 @@ public class ServerEntity {
|
|
sender.accept(new ClientboundSetEntityMotionPacket(this.entity.getId(), this.ap));
|
|
}
|
|
|
|
- if (this.entity instanceof LivingEntity) {
|
|
- List<Pair<EquipmentSlot, ItemStack>> 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).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 3c453de303422f96abf3aa67fa23523dd71327cb..416d71d91addc7b59c292377459f04512c5d9db7 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
@@ -262,6 +262,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<String, ItemStack, ItemStack> equipmentPacketCache = com.google.common.collect.HashBasedTable.create(); // Slice
|
|
+ public java.util.Map<String, net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket> cachedEquipmentMap = new java.util.HashMap<>(); // Slice
|
|
|
|
@Override
|
|
public float getBukkitYaw() {
|
|
@@ -3110,6 +3112,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);
|
|
}
|
|
}
|
|
@@ -3202,7 +3205,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
|
|
@@ -4543,4 +4564,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<net.minecraft.network.protocol.Packet<net.minecraft.network.protocol.game.ClientGamePacketListener>> 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<Pair<EquipmentSlot, ItemStack>> 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);
|
|
+ }
|
|
+
|
|
+ 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 1f1ef68a9a449a4a90c284f34a397ab4b6d905f6..3efaa46456a38eac7431552f92761ed1c9b76f5f 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
@@ -1118,4 +1118,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
|
|
}
|