Translate armour trims on items (#3837)

This commit is contained in:
Konicai 2023-06-08 23:32:04 -04:00 committed by GitHub
parent d6e095424c
commit ab577f66ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -144,7 +144,7 @@ public class Item {
}
/**
* Takes NBT from Java Edition and converts any value that Bedrock parses differently. <br>
* Takes NBT from Bedrock Edition and converts any value that Java parses differently. <br>
* Do note that this method is, these days, only called in three places (as of 2023/~1.19):
* <ul>
* <li>Extra recipe loading</li>