diff --git a/leaf-server/minecraft-patches/features/0108-Only-broadcast-carried-item-if-changed.patch b/leaf-server/minecraft-patches/features/0108-Only-broadcast-carried-item-if-changed.patch new file mode 100644 index 00000000..4c6d05ba --- /dev/null +++ b/leaf-server/minecraft-patches/features/0108-Only-broadcast-carried-item-if-changed.patch @@ -0,0 +1,57 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Taiyou06 +Date: Thu, 20 Feb 2025 20:58:30 +0100 +Subject: [PATCH] Only broadcast carried item if changed + + +diff --git a/net/minecraft/world/inventory/AbstractContainerMenu.java b/net/minecraft/world/inventory/AbstractContainerMenu.java +index 6033f629ac457472ad10f8e346732a596aea52d9..ba37e6b52ceb5f73e9c18cc555e768f43857e549 100644 +--- a/net/minecraft/world/inventory/AbstractContainerMenu.java ++++ b/net/minecraft/world/inventory/AbstractContainerMenu.java +@@ -225,24 +225,38 @@ public abstract class AbstractContainerMenu { + } + + public void broadcastChanges() { ++ // Leaf start - Only broadcast carried item if changed ++ // Avoid allocating supplier if no changes ++ boolean hasChanges = false; ++ + for (int i = 0; i < this.slots.size(); i++) { + ItemStack item = this.slots.get(i).getItem(); +- Supplier supplier = Suppliers.memoize(item::copy); +- this.triggerSlotListeners(i, item, supplier); +- this.synchronizeSlotToRemote(i, item, supplier); ++ ItemStack lastItem = this.lastSlots.get(i); ++ ++ // Fast path - reference equality check first ++ if (item != lastItem && !ItemStack.matches(lastItem, item)) { ++ hasChanges = true; ++ Supplier supplier = Suppliers.memoize(item::copy); ++ this.triggerSlotListeners(i, item, supplier); ++ this.synchronizeSlotToRemote(i, item, supplier); ++ } + } + +- this.synchronizeCarriedToRemote(); ++ // Only synchronize carried item if there were slot changes ++ if (hasChanges) { ++ this.synchronizeCarriedToRemote(); ++ } + ++ // Data slot synchronization + for (int i = 0; i < this.dataSlots.size(); i++) { + DataSlot dataSlot = this.dataSlots.get(i); +- int i1 = dataSlot.get(); ++ int value = dataSlot.get(); // OBFHELPER + if (dataSlot.checkAndClearUpdateFlag()) { +- this.updateDataSlotListeners(i, i1); ++ this.updateDataSlotListeners(i, value); + } +- +- this.synchronizeDataSlotToRemote(i, i1); ++ this.synchronizeDataSlotToRemote(i, value); + } ++ // Leaf end - Only broadcast carried item if changed + } + + public void broadcastFullState() {