Fix villagers accepting books (#1605)

* Hopefully fix villagers accepting books

Fixes a couple of inconsistencies getting books from the creative menu.

* Fix fake news
This commit is contained in:
Camotoy 2020-11-30 10:55:35 -05:00 committed by GitHub
parent eb687e6638
commit ffd0c211fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 18 deletions

View file

@ -42,13 +42,13 @@ public class BedrockEntityEventTranslator extends PacketTranslator<EntityEventPa
@Override @Override
public void translate(EntityEventPacket packet, GeyserSession session) { public void translate(EntityEventPacket packet, GeyserSession session) {
switch (packet.getType()) { switch (packet.getType()) {
// Resend the packet so we get the eating sounds
case EATING_ITEM: case EATING_ITEM:
// Resend the packet so we get the eating sounds
session.sendUpstreamPacket(packet); session.sendUpstreamPacket(packet);
return; return;
case COMPLETE_TRADE: case COMPLETE_TRADE:
ClientSelectTradePacket selectTradePacket = new ClientSelectTradePacket(packet.getData()); ClientSelectTradePacket selectTradePacket = new ClientSelectTradePacket(packet.getData());
session.getDownstream().getSession().send(selectTradePacket); session.sendDownstreamPacket(selectTradePacket);
Entity villager = session.getPlayerEntity(); Entity villager = session.getPlayerEntity();
Inventory openInventory = session.getInventoryCache().getOpenInventory(); Inventory openInventory = session.getInventoryCache().getOpenInventory();

View file

@ -27,7 +27,6 @@ package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId; import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
import com.nukkitx.protocol.bedrock.data.inventory.InventoryActionData; import com.nukkitx.protocol.bedrock.data.inventory.InventoryActionData;
import com.nukkitx.protocol.bedrock.data.inventory.InventorySource;
import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.updater.CursorInventoryUpdater; import org.geysermc.connector.network.translators.inventory.updater.CursorInventoryUpdater;

View file

@ -52,6 +52,12 @@ public class ItemRegistry {
private static final Map<String, ItemEntry> JAVA_IDENTIFIER_MAP = new HashMap<>(); private static final Map<String, ItemEntry> 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<String> JAVA_ONLY_ITEMS = Arrays.asList("minecraft:spectral_arrow", "minecraft:debug_stick",
"minecraft:knowledge_book");
public static final ItemData[] CREATIVE_ITEMS; public static final ItemData[] CREATIVE_ITEMS;
public static final List<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>(); public static final List<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
@ -245,7 +251,10 @@ public class ItemRegistry {
if (itemEntry.getBedrockId() == data.getId() && (itemEntry.getBedrockData() == data.getDamage() || if (itemEntry.getBedrockId() == data.getId() && (itemEntry.getBedrockData() == data.getDamage() ||
// Make exceptions for potions and tipped arrows, whose damage values can vary // Make exceptions for potions and tipped arrows, whose damage values can vary
(itemEntry.getJavaIdentifier().endsWith("potion") || itemEntry.getJavaIdentifier().equals("minecraft:arrow")))) { (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;
}
} }
} }

View file

@ -111,6 +111,10 @@ public abstract class ItemTranslator {
translator.translateToJava(itemStack.getNbt(), javaItem); 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; return itemStack;
} }
@ -126,7 +130,7 @@ public abstract class ItemTranslator {
// This is a fallback for maps with no nbt // This is a fallback for maps with no nbt
if (nbt == null && bedrockItem.getJavaIdentifier().equals("minecraft:filled_map")) { 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)); nbt.put(new IntTag("map", 0));
} }
@ -218,7 +222,7 @@ public abstract class ItemTranslator {
NbtMapBuilder builder = NbtMap.builder(); NbtMapBuilder builder = NbtMap.builder();
if (tag.getValue() != null && !tag.getValue().isEmpty()) { if (tag.getValue() != null && !tag.getValue().isEmpty()) {
for (String str : tag.getValue().keySet()) { 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); Object translatedTag = translateToBedrockNBT(javaTag);
if (translatedTag == null) if (translatedTag == null)
continue; continue;
@ -287,21 +291,21 @@ public abstract class ItemTranslator {
return new NbtList(type, tagList); return new NbtList(type, tagList);
} }
if (tag instanceof com.github.steveice10.opennbt.tag.builtin.CompoundTag) { if (tag instanceof CompoundTag) {
com.github.steveice10.opennbt.tag.builtin.CompoundTag compoundTag = (com.github.steveice10.opennbt.tag.builtin.CompoundTag) tag; CompoundTag compoundTag = (CompoundTag) tag;
return translateNbtToBedrock(compoundTag); return translateNbtToBedrock(compoundTag);
} }
return null; return null;
} }
public com.github.steveice10.opennbt.tag.builtin.CompoundTag translateToJavaNBT(String name, NbtMap tag) { public CompoundTag translateToJavaNBT(String name, NbtMap tag) {
com.github.steveice10.opennbt.tag.builtin.CompoundTag javaTag = new com.github.steveice10.opennbt.tag.builtin.CompoundTag(name); CompoundTag javaTag = new CompoundTag(name);
Map<String, com.github.steveice10.opennbt.tag.builtin.Tag> javaValue = javaTag.getValue(); Map<String, Tag> javaValue = javaTag.getValue();
if (tag != null && !tag.isEmpty()) { if (tag != null && !tag.isEmpty()) {
for (String str : tag.keySet()) { for (String str : tag.keySet()) {
Object bedrockTag = tag.get(str); Object bedrockTag = tag.get(str);
com.github.steveice10.opennbt.tag.builtin.Tag translatedTag = translateToJavaNBT(str, bedrockTag); Tag translatedTag = translateToJavaNBT(str, bedrockTag);
if (translatedTag == null) if (translatedTag == null)
continue; continue;
@ -313,7 +317,7 @@ public abstract class ItemTranslator {
return javaTag; 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[]) { if (object instanceof int[]) {
return new IntArrayTag(name, (int[]) object); return new IntArrayTag(name, (int[]) object);
} }
@ -355,10 +359,10 @@ public abstract class ItemTranslator {
} }
if (object instanceof List) { if (object instanceof List) {
List<com.github.steveice10.opennbt.tag.builtin.Tag> tags = new ArrayList<>(); List<Tag> tags = new ArrayList<>();
for (Object value : (List<?>) object) { for (Object value : (List<?>) object) {
com.github.steveice10.opennbt.tag.builtin.Tag javaTag = translateToJavaNBT("", value); Tag javaTag = translateToJavaNBT("", value);
if (javaTag != null) if (javaTag != null)
tags.add(javaTag); tags.add(javaTag);
} }

View file

@ -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.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; 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.Translator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils; import org.geysermc.connector.utils.InventoryUtils;
import java.util.Objects;
@Translator(packet = ServerSetSlotPacket.class) @Translator(packet = ServerSetSlotPacket.class)
public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket> { public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket> {