forked from GeyserMC/Geyser
Reduce nesting in item nbt translators
This commit is contained in:
parent
6dabc22d22
commit
56d84c24d3
5 changed files with 145 additions and 135 deletions
|
@ -46,49 +46,51 @@ public class BasicItemTranslator extends NbtItemStackTranslator {
|
|||
|
||||
@Override
|
||||
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("display")) {
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag.contains("Name")) {
|
||||
StringTag nameTag = displayTag.get("Name");
|
||||
if (!itemTag.contains("display")) {
|
||||
return;
|
||||
}
|
||||
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 {
|
||||
displayTag.put(new StringTag("Name", toBedrockMessage(nameTag)));
|
||||
lore.add(new StringTag("", toBedrockMessage((StringTag) tag)));
|
||||
} 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 {
|
||||
lore.add(new StringTag("", toBedrockMessage((StringTag) tag)));
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
displayTag.put(new ListTag("Lore", lore));
|
||||
}
|
||||
displayTag.put(new ListTag("Lore", lore));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("display")) {
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag.contains("Name")) {
|
||||
StringTag nameTag = displayTag.get("Name");
|
||||
displayTag.put(new StringTag("Name", toJavaMessage(nameTag)));
|
||||
}
|
||||
if (!itemTag.contains("display")) {
|
||||
return;
|
||||
}
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag.contains("Name")) {
|
||||
StringTag nameTag = displayTag.get("Name");
|
||||
displayTag.put(new StringTag("Name", toJavaMessage(nameTag)));
|
||||
}
|
||||
|
||||
if (displayTag.contains("Lore")) {
|
||||
ListTag loreTag = displayTag.get("Lore");
|
||||
List<Tag> lore = new ArrayList<>();
|
||||
for (Tag tag : loreTag.getValue()) {
|
||||
if (!(tag instanceof StringTag)) return;
|
||||
lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag)));
|
||||
}
|
||||
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;
|
||||
lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag)));
|
||||
}
|
||||
displayTag.put(new ListTag("Lore", lore));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,43 +42,45 @@ public class BookPagesTranslator extends NbtItemStackTranslator {
|
|||
|
||||
@Override
|
||||
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("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));
|
||||
if (!itemTag.contains("pages")) {
|
||||
return;
|
||||
}
|
||||
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
|
||||
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("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));
|
||||
if (!itemTag.contains("pages")) {
|
||||
return;
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,25 +37,27 @@ public class EnchantedBookTranslator extends NbtItemStackTranslator {
|
|||
|
||||
@Override
|
||||
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("StoredEnchantments")) {
|
||||
Tag enchTag = itemTag.get("StoredEnchantments");
|
||||
if (enchTag instanceof ListTag) {
|
||||
enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
|
||||
itemTag.remove("StoredEnchantments");
|
||||
itemTag.put(enchTag);
|
||||
}
|
||||
if (!itemTag.contains("StoredEnchantments")) {
|
||||
return;
|
||||
}
|
||||
Tag enchTag = itemTag.get("StoredEnchantments");
|
||||
if (enchTag instanceof ListTag) {
|
||||
enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
|
||||
itemTag.remove("StoredEnchantments");
|
||||
itemTag.put(enchTag);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("Enchantments")) {
|
||||
Tag enchTag = itemTag.get("Enchantments");
|
||||
if (enchTag instanceof ListTag) {
|
||||
enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
|
||||
itemTag.remove("Enchantments");
|
||||
itemTag.put(enchTag);
|
||||
}
|
||||
if (!itemTag.contains("Enchantments")) {
|
||||
return;
|
||||
}
|
||||
Tag enchTag = itemTag.get("Enchantments");
|
||||
if (enchTag instanceof ListTag) {
|
||||
enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
|
||||
itemTag.remove("Enchantments");
|
||||
itemTag.put(enchTag);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -73,48 +73,50 @@ public class EnchantmentTranslator extends NbtItemStackTranslator {
|
|||
|
||||
@Override
|
||||
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("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");
|
||||
if (!itemTag.contains("ench")) {
|
||||
return;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -38,29 +38,31 @@ public class LeatherArmorTranslator extends NbtItemStackTranslator {
|
|||
|
||||
@Override
|
||||
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("display")) {
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag.contains("color")) {
|
||||
IntTag color = displayTag.get("color");
|
||||
if (color != null) {
|
||||
itemTag.put(new IntTag("customColor", color.getValue()));
|
||||
displayTag.remove("color");
|
||||
}
|
||||
if (!itemTag.contains("display")) {
|
||||
return;
|
||||
}
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag.contains("color")) {
|
||||
IntTag color = displayTag.get("color");
|
||||
if (color != null) {
|
||||
itemTag.put(new IntTag("customColor", color.getValue()));
|
||||
displayTag.remove("color");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
|
||||
if (itemTag.contains("customColor")) {
|
||||
IntTag color = itemTag.get("customColor");
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag == null) {
|
||||
displayTag = new CompoundTag("display");
|
||||
}
|
||||
displayTag.put(color);
|
||||
itemTag.remove("customColor");
|
||||
if (!itemTag.contains("customColor")) {
|
||||
return;
|
||||
}
|
||||
IntTag color = itemTag.get("customColor");
|
||||
CompoundTag displayTag = itemTag.get("display");
|
||||
if (displayTag == null) {
|
||||
displayTag = new CompoundTag("display");
|
||||
}
|
||||
displayTag.put(color);
|
||||
itemTag.remove("customColor");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue