From ab577f66ac3214ff5a5279aac0828d47da378caa Mon Sep 17 00:00:00 2001 From: Konicai <71294714+Konicai@users.noreply.github.com> Date: Thu, 8 Jun 2023 23:32:04 -0400 Subject: [PATCH] Translate armour trims on items (#3837) --- .../geysermc/geyser/item/type/ArmorItem.java | 39 +++++++++++++++++++ .../org/geysermc/geyser/item/type/Item.java | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/item/type/ArmorItem.java b/core/src/main/java/org/geysermc/geyser/item/type/ArmorItem.java index fc48c9f34..38144f318 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/ArmorItem.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/ArmorItem.java @@ -25,7 +25,12 @@ package org.geysermc.geyser.item.type; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.geyser.item.ArmorMaterial; +import org.geysermc.geyser.registry.type.ItemMapping; +import org.geysermc.geyser.session.GeyserSession; public class ArmorItem extends Item { private final ArmorMaterial material; @@ -35,8 +40,42 @@ public class ArmorItem extends Item { this.material = material; } + @Override + public void translateNbtToBedrock(@NonNull GeyserSession session, @NonNull CompoundTag tag) { + super.translateNbtToBedrock(session, tag); + + if (tag.get("Trim") instanceof CompoundTag trim) { + StringTag material = trim.remove("material"); + StringTag pattern = trim.remove("pattern"); + // bedrock has an uppercase first letter key, and the value is not namespaced + trim.put(new StringTag("Material", stripNamespace(material.getValue()))); + trim.put(new StringTag("Pattern", stripNamespace(pattern.getValue()))); + } + } + + @Override + public void translateNbtToJava(@NonNull CompoundTag tag, @NonNull ItemMapping mapping) { + super.translateNbtToJava(tag, mapping); + + if (tag.get("Trim") instanceof CompoundTag trim) { + StringTag material = trim.remove("Material"); + StringTag pattern = trim.remove("Pattern"); + // java has a lowercase key, and namespaced value + trim.put(new StringTag("material", "minecraft:" + material.getValue())); + trim.put(new StringTag("pattern", "minecraft:" + pattern.getValue())); + } + } + @Override public boolean isValidRepairItem(Item other) { return material.getRepairIngredient() == other; } + + private static String stripNamespace(String identifier) { + int i = identifier.indexOf(':'); + if (i >= 0) { + return identifier.substring(i + 1); + } + return identifier; + } } diff --git a/core/src/main/java/org/geysermc/geyser/item/type/Item.java b/core/src/main/java/org/geysermc/geyser/item/type/Item.java index af45959bf..eef02ff0e 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/Item.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/Item.java @@ -144,7 +144,7 @@ public class Item { } /** - * Takes NBT from Java Edition and converts any value that Bedrock parses differently.
+ * Takes NBT from Bedrock Edition and converts any value that Java parses differently.
* Do note that this method is, these days, only called in three places (as of 2023/~1.19): *