diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java index 75cc0d7af..94d0119c2 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java @@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.util.Unit; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import java.util.Map; +import java.util.function.Function; import org.checkerframework.checker.nullness.qual.Nullable; public final class StructuredDataContainer { @@ -69,6 +70,26 @@ public final class StructuredDataContainer { return data != null && data.isPresent() ? data : null; } + /** + * Returns structured data by id if not empty, or creates it. + * + * @param key serializer id + * @param mappingFunction function to create structured data if not present + * @param data type + * @return structured data if not empty + */ + public StructuredData computeIfAbsent(final StructuredDataKey key, final Function, T> mappingFunction) { + final StructuredData data = this.getNonEmpty(key); + if (data != null) { + return data; + } + + final int id = serializerId(key); + final StructuredData empty = StructuredData.of(key, mappingFunction.apply(key), id); + this.data.put(key, empty); + return empty; + } + public void set(final StructuredDataKey key, final T value) { final int id = serializerId(key); if (id != -1) { diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Enchantments.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Enchantments.java index 87180420e..05731586b 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Enchantments.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Enchantments.java @@ -62,6 +62,10 @@ public final class Enchantments { this.showInTooltip = showInTooltip; } + public Enchantments(final boolean showInTooltip) { + this(new Int2IntOpenHashMap(), showInTooltip); + } + public Int2IntMap enchantments() { return enchantments; } @@ -73,4 +77,20 @@ public final class Enchantments { public boolean showInTooltip() { return showInTooltip; } + + public void add(final int id, final int level) { + enchantments.put(id, level); + } + + public void remove(final int id) { + enchantments.remove(id); + } + + public void clear() { + enchantments.clear(); + } + + public int getLevel(final int id) { + return enchantments.getOrDefault(id, -1); + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java index e9649d4c2..ec8758a06 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java @@ -116,9 +116,9 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter(StringTag.class)); wolfVariantsPacket.write(Type.REGISTRY_ENTRY_ARRAY, new RegistryEntry[]{new RegistryEntry("minecraft:pale", paleWolf)}); wolfVariantsPacket.send(Protocol1_20_5To1_20_3.class);