From ffd0c211fc872f86fac56dda9f5163e55ab930ad Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+DoctorMacc@users.noreply.github.com> Date: Mon, 30 Nov 2020 10:55:35 -0500 Subject: [PATCH] Fix villagers accepting books (#1605) * Hopefully fix villagers accepting books Fixes a couple of inconsistencies getting books from the creative menu. * Fix fake news --- .../entity/BedrockEntityEventTranslator.java | 4 +-- .../MerchantInventoryTranslator.java | 1 - .../translators/item/ItemRegistry.java | 11 +++++++- .../translators/item/ItemTranslator.java | 26 +++++++++++-------- .../java/window/JavaSetSlotTranslator.java | 4 +-- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/BedrockEntityEventTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/BedrockEntityEventTranslator.java index 18fd6614e..7cf3fec29 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/BedrockEntityEventTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/BedrockEntityEventTranslator.java @@ -42,13 +42,13 @@ public class BedrockEntityEventTranslator extends PacketTranslator JAVA_IDENTIFIER_MAP = new HashMap<>(); + /** + * A list of all identifiers that only exist on Java. Used to prevent creative items from becoming these unintentionally. + */ + private static final List JAVA_ONLY_ITEMS = Arrays.asList("minecraft:spectral_arrow", "minecraft:debug_stick", + "minecraft:knowledge_book"); + public static final ItemData[] CREATIVE_ITEMS; public static final List ITEMS = new ArrayList<>(); @@ -245,7 +251,10 @@ public class ItemRegistry { if (itemEntry.getBedrockId() == data.getId() && (itemEntry.getBedrockData() == data.getDamage() || // Make exceptions for potions and tipped arrows, whose damage values can vary (itemEntry.getJavaIdentifier().endsWith("potion") || itemEntry.getJavaIdentifier().equals("minecraft:arrow")))) { - return itemEntry; + if (!JAVA_ONLY_ITEMS.contains(itemEntry.getJavaIdentifier())) { + // From a Bedrock item data, we aren't getting one of these items + return itemEntry; + } } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java index 0df179cf4..0ea2ce0c8 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java @@ -111,6 +111,10 @@ public abstract class ItemTranslator { translator.translateToJava(itemStack.getNbt(), javaItem); } } + if (itemStack.getNbt().isEmpty()) { + // Otherwise, seems to causes issues with villagers accepting books, and I don't see how this will break anything else. - Camotoy + itemStack = new ItemStack(itemStack.getId(), itemStack.getAmount(), null); + } } return itemStack; } @@ -126,7 +130,7 @@ public abstract class ItemTranslator { // This is a fallback for maps with no nbt if (nbt == null && bedrockItem.getJavaIdentifier().equals("minecraft:filled_map")) { - nbt = new com.github.steveice10.opennbt.tag.builtin.CompoundTag(""); + nbt = new CompoundTag(""); nbt.put(new IntTag("map", 0)); } @@ -218,7 +222,7 @@ public abstract class ItemTranslator { NbtMapBuilder builder = NbtMap.builder(); if (tag.getValue() != null && !tag.getValue().isEmpty()) { for (String str : tag.getValue().keySet()) { - com.github.steveice10.opennbt.tag.builtin.Tag javaTag = tag.get(str); + Tag javaTag = tag.get(str); Object translatedTag = translateToBedrockNBT(javaTag); if (translatedTag == null) continue; @@ -287,21 +291,21 @@ public abstract class ItemTranslator { return new NbtList(type, tagList); } - if (tag instanceof com.github.steveice10.opennbt.tag.builtin.CompoundTag) { - com.github.steveice10.opennbt.tag.builtin.CompoundTag compoundTag = (com.github.steveice10.opennbt.tag.builtin.CompoundTag) tag; + if (tag instanceof CompoundTag) { + CompoundTag compoundTag = (CompoundTag) tag; return translateNbtToBedrock(compoundTag); } return null; } - public com.github.steveice10.opennbt.tag.builtin.CompoundTag translateToJavaNBT(String name, NbtMap tag) { - com.github.steveice10.opennbt.tag.builtin.CompoundTag javaTag = new com.github.steveice10.opennbt.tag.builtin.CompoundTag(name); - Map javaValue = javaTag.getValue(); + public CompoundTag translateToJavaNBT(String name, NbtMap tag) { + CompoundTag javaTag = new CompoundTag(name); + Map javaValue = javaTag.getValue(); if (tag != null && !tag.isEmpty()) { for (String str : tag.keySet()) { Object bedrockTag = tag.get(str); - com.github.steveice10.opennbt.tag.builtin.Tag translatedTag = translateToJavaNBT(str, bedrockTag); + Tag translatedTag = translateToJavaNBT(str, bedrockTag); if (translatedTag == null) continue; @@ -313,7 +317,7 @@ public abstract class ItemTranslator { return javaTag; } - private com.github.steveice10.opennbt.tag.builtin.Tag translateToJavaNBT(String name, Object object) { + private Tag translateToJavaNBT(String name, Object object) { if (object instanceof int[]) { return new IntArrayTag(name, (int[]) object); } @@ -355,10 +359,10 @@ public abstract class ItemTranslator { } if (object instanceof List) { - List tags = new ArrayList<>(); + List tags = new ArrayList<>(); for (Object value : (List) object) { - com.github.steveice10.opennbt.tag.builtin.Tag javaTag = translateToJavaNBT("", value); + Tag javaTag = translateToJavaNBT("", value); if (javaTag != null) tags.add(javaTag); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java index 19d7db217..3a2790028 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java @@ -29,12 +29,10 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSl import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; -import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.network.translators.Translator; +import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.utils.InventoryUtils; -import java.util.Objects; - @Translator(packet = ServerSetSlotPacket.class) public class JavaSetSlotTranslator extends PacketTranslator {