From 033d5ae49a17c7930ea96bce5ba17b5181258a3e Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 17 Apr 2024 22:39:08 +0200 Subject: [PATCH] Handle removed item conversion in component rewriter --- .../api/minecraft/item/data/BlockPredicate.java | 4 ++-- .../rewriter/ComponentRewriter1_20_5.java | 16 +++++++++++----- .../template/protocols/Protocol1_99To_98.java | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java index c6ac278a1..149cea6b4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java @@ -58,7 +58,7 @@ public final class BlockPredicate { private final StatePropertyMatcher[] propertyMatchers; private final CompoundTag tag; - public BlockPredicate(@Nullable final HolderSet holderSet, @Nullable final StatePropertyMatcher[] propertyMatchers, @Nullable final CompoundTag tag) { + public BlockPredicate(@Nullable final HolderSet holderSet, final StatePropertyMatcher @Nullable [] propertyMatchers, @Nullable final CompoundTag tag) { this.holderSet = holderSet; this.propertyMatchers = propertyMatchers; this.tag = tag; @@ -68,7 +68,7 @@ public final class BlockPredicate { return holderSet; } - public @Nullable StatePropertyMatcher[] propertyMatchers() { + public StatePropertyMatcher @Nullable [] propertyMatchers() { return propertyMatchers; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java index 275bd936a..349d42bb6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java @@ -226,7 +226,7 @@ public class ComponentRewriter1_20_5 extends ComponentRewriter, StructuredData> data) { + protected CompoundTag toTag(final Map, StructuredData> data, final boolean empty) { final CompoundTag tag = new CompoundTag(); for (final Map.Entry, StructuredData> entry : data.entrySet()) { final StructuredDataKey key = entry.getKey(); @@ -247,13 +247,19 @@ public class ComponentRewriter1_20_5 extends ComponentRewriter value = entry.getValue(); if (value.isEmpty()) { + if (empty) { + // Theoretically not needed here, but we'll keep it for consistency + tag.put("!" + key.identifier(), new CompoundTag()); + continue; + } throw new IllegalArgumentException("Empty structured data: " + key.identifier()); } //noinspection unchecked final Tag valueTag = rewriters.get(key).convert(value.value()); - if (valueTag == null) continue; - + if (valueTag == null) { + continue; + } tag.put(key.identifier(), valueTag); } return tag; @@ -863,7 +869,7 @@ public class ComponentRewriter1_20_5 extends ComponentRewriter, StructuredData> components = item.structuredData().data(); - tag.put("components", toTag(components)); + tag.put("components", toTag(components, true)); } protected void convertFilterableString(final CompoundTag tag, final FilterableString string, final int min, final int max) { diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java index 529d7a7a4..4998fb24c 100644 --- a/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java @@ -87,7 +87,7 @@ public final class Protocol1_99To_98 extends AbstractProtocol