Reduce nesting in item nbt translators

This commit is contained in:
RednedEpic 2020-05-24 14:21:06 -05:00
parent 6dabc22d22
commit 56d84c24d3
5 changed files with 145 additions and 135 deletions

View file

@ -46,49 +46,51 @@ public class BasicItemTranslator extends NbtItemStackTranslator {
@Override @Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("display")) { if (!itemTag.contains("display")) {
CompoundTag displayTag = itemTag.get("display"); return;
if (displayTag.contains("Name")) { }
StringTag nameTag = displayTag.get("Name"); CompoundTag displayTag = itemTag.get("display");
if (displayTag.contains("Name")) {
StringTag nameTag = displayTag.get("Name");
try {
displayTag.put(new StringTag("Name", toBedrockMessage(nameTag)));
} catch (Exception ex) {
}
}
if (displayTag.contains("Lore")) {
ListTag loreTag = displayTag.get("Lore");
List<Tag> lore = new ArrayList<>();
for (Tag tag : loreTag.getValue()) {
if (!(tag instanceof StringTag)) return;
try { try {
displayTag.put(new StringTag("Name", toBedrockMessage(nameTag))); lore.add(new StringTag("", toBedrockMessage((StringTag) tag)));
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
displayTag.put(new ListTag("Lore", lore));
if (displayTag.contains("Lore")) {
ListTag loreTag = displayTag.get("Lore");
List<Tag> lore = new ArrayList<>();
for (Tag tag : loreTag.getValue()) {
if (!(tag instanceof StringTag)) return;
try {
lore.add(new StringTag("", toBedrockMessage((StringTag) tag)));
} catch (Exception ex) {
}
}
displayTag.put(new ListTag("Lore", lore));
}
} }
} }
@Override @Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("display")) { if (!itemTag.contains("display")) {
CompoundTag displayTag = itemTag.get("display"); return;
if (displayTag.contains("Name")) { }
StringTag nameTag = displayTag.get("Name"); CompoundTag displayTag = itemTag.get("display");
displayTag.put(new StringTag("Name", toJavaMessage(nameTag))); if (displayTag.contains("Name")) {
} StringTag nameTag = displayTag.get("Name");
displayTag.put(new StringTag("Name", toJavaMessage(nameTag)));
}
if (displayTag.contains("Lore")) { if (displayTag.contains("Lore")) {
ListTag loreTag = displayTag.get("Lore"); ListTag loreTag = displayTag.get("Lore");
List<Tag> lore = new ArrayList<>(); List<Tag> lore = new ArrayList<>();
for (Tag tag : loreTag.getValue()) { for (Tag tag : loreTag.getValue()) {
if (!(tag instanceof StringTag)) return; if (!(tag instanceof StringTag)) return;
lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag))); lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag)));
}
displayTag.put(new ListTag("Lore", lore));
} }
displayTag.put(new ListTag("Lore", lore));
} }
} }

View file

@ -42,43 +42,45 @@ public class BookPagesTranslator extends NbtItemStackTranslator {
@Override @Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("pages")) { if (!itemTag.contains("pages")) {
List<Tag> pages = new ArrayList<>(); return;
ListTag pagesTag = itemTag.get("pages");
for (Tag tag : pagesTag.getValue()) {
if (!(tag instanceof StringTag))
continue;
StringTag textTag = (StringTag) tag;
CompoundTag pageTag = new CompoundTag("");
pageTag.put(new StringTag("photoname", ""));
pageTag.put(new StringTag("text", MessageUtils.getBedrockMessage(textTag.getValue())));
pages.add(pageTag);
}
itemTag.remove("pages");
itemTag.put(new ListTag("pages", pages));
} }
List<Tag> pages = new ArrayList<>();
ListTag pagesTag = itemTag.get("pages");
for (Tag tag : pagesTag.getValue()) {
if (!(tag instanceof StringTag))
continue;
StringTag textTag = (StringTag) tag;
CompoundTag pageTag = new CompoundTag("");
pageTag.put(new StringTag("photoname", ""));
pageTag.put(new StringTag("text", MessageUtils.getBedrockMessage(textTag.getValue())));
pages.add(pageTag);
}
itemTag.remove("pages");
itemTag.put(new ListTag("pages", pages));
} }
@Override @Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("pages")) { if (!itemTag.contains("pages")) {
List<Tag> pages = new ArrayList<>(); return;
ListTag pagesTag = itemTag.get("pages");
for (Tag tag : pagesTag.getValue()) {
if (!(tag instanceof CompoundTag))
continue;
CompoundTag pageTag = (CompoundTag) tag;
StringTag textTag = pageTag.get("text");
pages.add(new StringTag(MessageUtils.getJavaMessage(textTag.getValue())));
}
itemTag.remove("pages");
itemTag.put(new ListTag("pages", pages));
} }
List<Tag> pages = new ArrayList<>();
ListTag pagesTag = itemTag.get("pages");
for (Tag tag : pagesTag.getValue()) {
if (!(tag instanceof CompoundTag))
continue;
CompoundTag pageTag = (CompoundTag) tag;
StringTag textTag = pageTag.get("text");
pages.add(new StringTag(MessageUtils.getJavaMessage(textTag.getValue())));
}
itemTag.remove("pages");
itemTag.put(new ListTag("pages", pages));
} }
} }

View file

@ -37,25 +37,27 @@ public class EnchantedBookTranslator extends NbtItemStackTranslator {
@Override @Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("StoredEnchantments")) { if (!itemTag.contains("StoredEnchantments")) {
Tag enchTag = itemTag.get("StoredEnchantments"); return;
if (enchTag instanceof ListTag) { }
enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue()); Tag enchTag = itemTag.get("StoredEnchantments");
itemTag.remove("StoredEnchantments"); if (enchTag instanceof ListTag) {
itemTag.put(enchTag); enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
} itemTag.remove("StoredEnchantments");
itemTag.put(enchTag);
} }
} }
@Override @Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("Enchantments")) { if (!itemTag.contains("Enchantments")) {
Tag enchTag = itemTag.get("Enchantments"); return;
if (enchTag instanceof ListTag) { }
enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue()); Tag enchTag = itemTag.get("Enchantments");
itemTag.remove("Enchantments"); if (enchTag instanceof ListTag) {
itemTag.put(enchTag); enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
} itemTag.remove("Enchantments");
itemTag.put(enchTag);
} }
} }

View file

@ -73,48 +73,50 @@ public class EnchantmentTranslator extends NbtItemStackTranslator {
@Override @Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("ench")) { if (!itemTag.contains("ench")) {
ListTag enchantmentTag = itemTag.get("ench"); return;
List<Tag> enchantments = new ArrayList<>();
List<Tag> storedEnchantments = new ArrayList<>();
for (Tag value : enchantmentTag.getValue()) {
if (!(value instanceof CompoundTag))
continue;
CompoundTag tagValue = (CompoundTag) value;
ShortTag bedrockId = tagValue.get("id");
if (bedrockId == null) continue;
ShortTag geyserStoredEnchantmentTag = tagValue.get("GeyserStoredEnchantment");
Enchantment enchantment = Enchantment.getByBedrockId(bedrockId.getValue());
if (enchantment != null) {
CompoundTag javaTag = new CompoundTag("");
Map<String, Tag> javaValue = javaTag.getValue();
javaValue.put("id", new StringTag("id", enchantment.getJavaIdentifier()));
ShortTag levelTag = tagValue.get("lvl");
javaValue.put("lvl", new IntTag("lvl", levelTag != null ? levelTag.getValue() : 1));
javaTag.setValue(javaValue);
if (geyserStoredEnchantmentTag != null) {
tagValue.remove("GeyserStoredEnchantment");
storedEnchantments.add(javaTag);
} else {
enchantments.add(javaTag);
}
} else {
GeyserConnector.getInstance().getLogger().debug("Unknown bedrock enchantment: " + bedrockId);
}
}
if (!enchantments.isEmpty()) {
itemTag.put(new ListTag("Enchantments", enchantments));
}
if (!storedEnchantments.isEmpty()) {
itemTag.put(new ListTag("StoredEnchantments", enchantments));
}
itemTag.remove("ench");
} }
ListTag enchantmentTag = itemTag.get("ench");
List<Tag> enchantments = new ArrayList<>();
List<Tag> storedEnchantments = new ArrayList<>();
for (Tag value : enchantmentTag.getValue()) {
if (!(value instanceof CompoundTag))
continue;
CompoundTag tagValue = (CompoundTag) value;
ShortTag bedrockId = tagValue.get("id");
if (bedrockId == null) continue;
ShortTag geyserStoredEnchantmentTag = tagValue.get("GeyserStoredEnchantment");
Enchantment enchantment = Enchantment.getByBedrockId(bedrockId.getValue());
if (enchantment != null) {
CompoundTag javaTag = new CompoundTag("");
Map<String, Tag> javaValue = javaTag.getValue();
javaValue.put("id", new StringTag("id", enchantment.getJavaIdentifier()));
ShortTag levelTag = tagValue.get("lvl");
javaValue.put("lvl", new IntTag("lvl", levelTag != null ? levelTag.getValue() : 1));
javaTag.setValue(javaValue);
if (geyserStoredEnchantmentTag != null) {
tagValue.remove("GeyserStoredEnchantment");
storedEnchantments.add(javaTag);
} else {
enchantments.add(javaTag);
}
} else {
GeyserConnector.getInstance().getLogger().debug("Unknown bedrock enchantment: " + bedrockId);
}
}
if (!enchantments.isEmpty()) {
itemTag.put(new ListTag("Enchantments", enchantments));
}
if (!storedEnchantments.isEmpty()) {
itemTag.put(new ListTag("StoredEnchantments", enchantments));
}
itemTag.remove("ench");
} }

View file

@ -38,29 +38,31 @@ public class LeatherArmorTranslator extends NbtItemStackTranslator {
@Override @Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("display")) { if (!itemTag.contains("display")) {
CompoundTag displayTag = itemTag.get("display"); return;
if (displayTag.contains("color")) { }
IntTag color = displayTag.get("color"); CompoundTag displayTag = itemTag.get("display");
if (color != null) { if (displayTag.contains("color")) {
itemTag.put(new IntTag("customColor", color.getValue())); IntTag color = displayTag.get("color");
displayTag.remove("color"); if (color != null) {
} itemTag.put(new IntTag("customColor", color.getValue()));
displayTag.remove("color");
} }
} }
} }
@Override @Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
if (itemTag.contains("customColor")) { if (!itemTag.contains("customColor")) {
IntTag color = itemTag.get("customColor"); return;
CompoundTag displayTag = itemTag.get("display");
if (displayTag == null) {
displayTag = new CompoundTag("display");
}
displayTag.put(color);
itemTag.remove("customColor");
} }
IntTag color = itemTag.get("customColor");
CompoundTag displayTag = itemTag.get("display");
if (displayTag == null) {
displayTag = new CompoundTag("display");
}
displayTag.put(color);
itemTag.remove("customColor");
} }
@Override @Override