From d05bdd9edac85ec3b1cf0fea74c3c9ab3436372a Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Fri, 10 Jan 2025 17:43:12 +0100 Subject: [PATCH] Keep empty items in CONTAINER in 1.20.3->1.20.5 (#4338) --- .../BlockItemPacketRewriter1_20_5.java | 42 ++++++++++++++++--- .../rewriter/ComponentRewriter1_20_5.java | 21 +++++++--- gradle.properties | 2 +- sponge/build.gradle.kts | 0 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 sponge/build.gradle.kts diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java index ad76c438a..f69a81fb9 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java @@ -101,11 +101,13 @@ import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.Either; import com.viaversion.viaversion.util.Key; +import com.viaversion.viaversion.util.MathUtil; import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.UUIDUtil; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -557,9 +559,9 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter dataKey, final boolean allowEmpty) { + final String key, final StructuredDataKey dataKey) { final ListTag itemsTag = tag.getListTag(key, CompoundTag.class); if (itemsTag != null) { final Item[] items = itemsTag.stream() .limit(256) .map(item -> itemFromTag(connection, item)) - .filter(item -> allowEmpty || !item.isEmpty()) + .filter(item -> !item.isEmpty()) .toArray(Item[]::new); data.set(dataKey, items); } @@ -1486,8 +1488,36 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter itemsTag = tag.getListTag("Items", CompoundTag.class); + if (itemsTag != null) { + int highestSlot = 0; + + for (int i = 0, size = Math.min(itemsTag.size(), 256); i < size; i++) { + final CompoundTag itemTag = itemsTag.get(i); + final Item item = itemFromTag(connection, itemTag); + if (item.isEmpty()) { + continue; + } + + final int slot = itemTag.getByte("Slot"); + highestSlot = MathUtil.clamp(slot, highestSlot, 255); + } + + final Item[] filteredItems = new Item[highestSlot + 1]; + Arrays.fill(filteredItems, StructuredItem.empty()); + for (final CompoundTag itemTag : itemsTag) { + final Item item = itemFromTag(connection, itemTag); + if (item.isEmpty()) { + continue; + } + + final int slot = itemTag.getByte("Slot"); + if (slot >= 0 && slot < filteredItems.length) { + filteredItems[slot] = item; + } + } + + data.set(StructuredDataKey.CONTAINER1_20_5, filteredItems); addBlockEntityId(tag, "shulker_box"); // Won't happen to the others and doesn't actually have to be correct otherwise } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java index f6c82fdc8..f28f4ae7b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java @@ -849,12 +849,18 @@ public class ComponentRewriter1_20_5 extends Co protected ListTag convertContainer(final UserConnection connection, final Item[] value) { final ListTag tag = new ListTag<>(CompoundTag.class); - final ListTag items = convertItemArray(connection, value); - for (int i = 0; i < items.size(); i++) { + for (int i = 0; i < value.length; i++) { + final Item item = value[i]; + if (item.isEmpty()) { + continue; + } + + final CompoundTag slotTag = new CompoundTag(); final CompoundTag itemTag = new CompoundTag(); - itemTag.putInt("slot", i); - itemTag.put("item", items.get(i)); - tag.add(itemTag); + convertItem(connection, itemTag, item); + slotTag.putInt("slot", i); + slotTag.put("item", itemTag); + tag.add(slotTag); } return tag; } @@ -967,7 +973,10 @@ public class ComponentRewriter1_20_5 extends Co tag.putInt("count", 1); } final Map, StructuredData> components = item.dataContainer().data(); - tag.put("components", toTag(connection, components, true)); + final CompoundTag componentsTag = toTag(connection, components, true); + if (!componentsTag.isEmpty()) { + tag.put("components", componentsTag); + } } protected void convertFilterableString(final CompoundTag tag, final FilterableString string, final int max) { diff --git a/gradle.properties b/gradle.properties index 3c81cde94..f671121a5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ projectVersion=5.2.2-SNAPSHOT # Smile emoji -mcVersions=1.21.4,1.21.3,1.21.2,1.21.1,1.21,1.20.6,1.20.5,1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10, 1.9.4, 1.9.3, 1.9.2, 1.9.1, 1.9, 1.8.9 +mcVersions=1.21.4, 1.21.3, 1.21.2, 1.21.1, 1.21, 1.20.6, 1.20.5, 1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10, 1.9.4, 1.9.3, 1.9.2, 1.9.1, 1.9, 1.8.9 mcVersionRange=1.8-1.21.4 velocityVersion=3.4 diff --git a/sponge/build.gradle.kts b/sponge/build.gradle.kts new file mode 100644 index 000000000..e69de29bb