From 658364b643033535f092eb12c6bf6bed92542d70 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 12 Mar 2024 11:00:07 +0100 Subject: [PATCH] Add a few structured data -> nbt converters --- .../data/Enchantments1_20_3.java | 4 +- .../BlockItemPacketRewriter1_20_5.java | 8 +- .../rewriter/StructuredDataConverter.java | 83 ++++++++++++++++++- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Enchantments1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Enchantments1_20_3.java index 3d7aedcf0..f3b8121ce 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Enchantments1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Enchantments1_20_3.java @@ -64,11 +64,11 @@ public final class Enchantments1_20_3 { "vanishing_curse" ); - public static @Nullable String enchantment(final int id) { + public static @Nullable String idToKey(final int id) { return ENCHANTMENTS.idToKey(id); } - public static int id(final String enchantment) { + public static int keyToId(final String enchantment) { return ENCHANTMENTS.keyToId(enchantment); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java index bbf523f6d..46e85e69c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java @@ -416,8 +416,8 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter, Rewriter> REWRITERS = new Reference2ObjectOpenHashMap<>(); static { - // TODO - register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data.value()))); + register(StructuredDataKey.DAMAGE, (data, tag) -> tag.putInt("Damage", data)); + register(StructuredDataKey.UNBREAKABLE, (data, tag) -> { + tag.putBoolean("Unbreakable", true); + if (!data.showInTooltip()) { + putHideFlag(tag, 0x04); + } + }); + register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data))); + register(StructuredDataKey.LORE, (data, tag) -> { + final ListTag lore = new ListTag<>(StringTag.class); + for (final Tag loreEntry : data) { + lore.add(new StringTag(ComponentUtil.tagToJsonString(loreEntry))); + } + tag.put("Lore", lore); + }); + register(StructuredDataKey.ENCHANTMENTS, StructuredDataConverter::convertEnchantments); + register(StructuredDataKey.STORED_ENCHANTMENTS, StructuredDataConverter::convertEnchantments); + //register(StructuredDataKey.CAN_PLACE_ON, (data, tag) -> ); // TODO + //register(StructuredDataKey.CAN_BREAK, (data, tag) -> ); // TODO + register(StructuredDataKey.ATTRIBUTE_MODIFIERS, (data, tag) -> { + final ListTag modifiers = new ListTag<>(CompoundTag.class); + for (final AttributeModifier modifier : data.modifiers()) { + final String identifier = Attributes1_20_3.idToKey(modifier.attribute()); + if (identifier == null) { + continue; + } + + final CompoundTag modifierTag = new CompoundTag(); + modifierTag.putString("AttributeName", identifier); + modifierTag.putString("Name", modifier.modifier().name()); + modifierTag.putDouble("Amount", modifier.modifier().amount()); + modifierTag.putInt("Slot", modifier.slot()); + modifierTag.putInt("Operation", modifier.modifier().operation()); + modifiers.add(modifierTag); + } + tag.put("AttributeModifiers", modifiers); + + if (!data.showInTooltip()) { + putHideFlag(tag, 0x02); + } + }); + register(StructuredDataKey.CUSTOM_MODEL_DATA, (data, tag) -> tag.putInt("CustomModelData", data)); + register(StructuredDataKey.HIDE_ADDITIONAL_TOOLTIP, (data, tag) -> putHideFlag(tag, 0x20)); + register(StructuredDataKey.REPAIR_COST, (data, tag) -> tag.putInt("RepairCost", data)); + + } + + private static void convertEnchantments(final Enchantments data, final CompoundTag tag) { + final ListTag enchantments = new ListTag<>(CompoundTag.class); + for (final Int2IntMap.Entry entry : data.enchantments().int2IntEntrySet()) { + final String identifier = Enchantments1_20_3.idToKey(entry.getIntKey()); + if (identifier == null) { + continue; + } + + final CompoundTag enchantment = new CompoundTag(); + enchantment.putString("id", identifier); + enchantment.putShort("lvl", (short) entry.getIntKey()); + enchantments.add(enchantment); + } + tag.put("Enchantments", enchantments); + + if (!data.showInTooltip()) { + putHideFlag(tag, 0x01); + } + } + + private static void putHideFlag(final CompoundTag tag, final int value) { + tag.putInt("HideFlags", tag.getInt("HideFlags") | value); } public static void rewrite(final StructuredData data, final CompoundTag tag) { @@ -41,7 +116,7 @@ final class StructuredDataConverter { //noinspection unchecked final Rewriter rewriter = (Rewriter) REWRITERS.get(data.key()); if (rewriter != null) { - rewriter.rewrite(data, tag); + rewriter.rewrite(data.value(), tag); } } @@ -52,6 +127,6 @@ final class StructuredDataConverter { @FunctionalInterface interface Rewriter { - void rewrite(StructuredData data, CompoundTag tag); + void rewrite(T data, CompoundTag tag); } }