Translate bedrock enchantments to java

This commit is contained in:
AJ Ferguson 2019-12-02 15:53:46 -09:00
parent 7a58840821
commit 1b37a19280
2 changed files with 43 additions and 2 deletions

View file

@ -82,4 +82,11 @@ enum Enchantment {
} }
return null; return null;
} }
public static Enchantment getByBedrockId(int bedrockId) {
if (bedrockId >= 0 && bedrockId < Enchantment.values().length) {
return Enchantment.values()[bedrockId];
}
return null;
}
} }

View file

@ -58,6 +58,17 @@ public class ItemTranslator {
if (data.getTag() == null) { if (data.getTag() == null) {
return new ItemStack(javaItem.getJavaId(), data.getCount()); return new ItemStack(javaItem.getJavaId(), data.getCount());
} else if (javaItem.getJavaIdentifier().equals("minecraft:enchanted_book")) {
CompoundTag javaTag = translateToJavaNBT(data.getTag());
Map<String, Tag> javaValue = javaTag.getValue();
Tag enchTag = javaValue.get("Enchantments");
if (enchTag instanceof ListTag) {
enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
javaValue.remove("Enchantments");
javaValue.put("StoredEnchantments", enchTag);
javaTag.setValue(javaValue);
}
return new ItemStack(javaItem.getJavaId(), data.getCount(), javaTag);
} }
return new ItemStack(javaItem.getJavaId(), data.getCount(), translateToJavaNBT(data.getTag())); return new ItemStack(javaItem.getJavaId(), data.getCount(), translateToJavaNBT(data.getTag()));
} }
@ -109,7 +120,7 @@ public class ItemTranslator {
if (translatedTag == null) if (translatedTag == null)
continue; continue;
javaValue.put(str, translatedTag); javaValue.put(translatedTag.getName(), translatedTag);
} }
} }
@ -172,6 +183,29 @@ public class ItemTranslator {
com.nukkitx.nbt.tag.ListTag listTag = (com.nukkitx.nbt.tag.ListTag) tag; com.nukkitx.nbt.tag.ListTag listTag = (com.nukkitx.nbt.tag.ListTag) tag;
List<Tag> tags = new ArrayList<>(); List<Tag> tags = new ArrayList<>();
if (tag.getName().equals("ench")) {
for (Object value : listTag.getValue()) {
if (!(value instanceof com.nukkitx.nbt.tag.CompoundTag))
continue;
com.nukkitx.nbt.tag.CompoundTag tagValue = (com.nukkitx.nbt.tag.CompoundTag) value;
int bedrockId = tagValue.getAsShort("id", (short) -1);
Enchantment enchantment = Enchantment.getByBedrockId(bedrockId);
if (enchantment != null) {
CompoundTag javaTag = new CompoundTag("");
Map<String, Tag> javaValue = javaTag.getValue();
javaValue.put("id", new StringTag("id", enchantment.getJavaIdentifier()));
javaValue.put("lvl", new IntTag("lvl", tagValue.getAsShort("lvl")));
javaTag.setValue(javaValue);
tags.add(javaTag);
} else {
Geyser.getLogger().debug("Unknown bedrock enchantment: " + bedrockId);
}
}
return new ListTag("Enchantments", tags);
}
for (Object value : listTag.getValue()) { for (Object value : listTag.getValue()) {
if (!(value instanceof com.nukkitx.nbt.tag.Tag)) if (!(value instanceof com.nukkitx.nbt.tag.Tag))
continue; continue;
@ -200,7 +234,7 @@ public class ItemTranslator {
if (translatedTag == null) if (translatedTag == null)
continue; continue;
javaValue.put(str, translatedTag); javaValue.put(translatedTag.getName(), translatedTag);
} }
} }