Villager trade fixes (#1350)

This commit mainly focuses on fixing the crashing of villagers that occurred pre-1.14.

Co-authored-by: AJ Ferguson <AJ-Ferguson@users.noreply.github.com>
This commit is contained in:
Camotoy 2020-09-29 13:19:37 -04:00 committed by GitHub
parent 650c02ef66
commit a5b00e09a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

View file

@ -35,9 +35,8 @@ import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
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.world.block.BlockTranslator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.LocaleUtils; import org.geysermc.connector.network.translators.world.block.BlockTranslator;
@AllArgsConstructor @AllArgsConstructor
public class BlockInventoryHolder extends InventoryHolder { public class BlockInventoryHolder extends InventoryHolder {
@ -60,7 +59,7 @@ public class BlockInventoryHolder extends InventoryHolder {
.putInt("x", position.getX()) .putInt("x", position.getX())
.putInt("y", position.getY()) .putInt("y", position.getY())
.putInt("z", position.getZ()) .putInt("z", position.getZ())
.putString("CustomName", LocaleUtils.getLocaleString(inventory.getTitle(), session.getClientData().getLanguageCode())).build(); .putString("CustomName", inventory.getTitle()).build();
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket(); BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag); dataPacket.setData(tag);
dataPacket.setBlockPosition(position); dataPacket.setBlockPosition(position);

View file

@ -29,7 +29,6 @@ import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientClose
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
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;
@ -37,6 +36,7 @@ import org.geysermc.connector.network.translators.PacketTranslator;
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.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils; import org.geysermc.connector.utils.InventoryUtils;
import org.geysermc.connector.utils.LocaleUtils;
@Translator(packet = ServerOpenWindowPacket.class) @Translator(packet = ServerOpenWindowPacket.class)
public class JavaOpenWindowTranslator extends PacketTranslator<ServerOpenWindowPacket> { public class JavaOpenWindowTranslator extends PacketTranslator<ServerOpenWindowPacket> {
@ -71,6 +71,8 @@ public class JavaOpenWindowTranslator extends PacketTranslator<ServerOpenWindowP
GeyserConnector.getInstance().getLogger().debug("JavaOpenWindowTranslator: " + e.toString()); GeyserConnector.getInstance().getLogger().debug("JavaOpenWindowTranslator: " + e.toString());
} }
name = LocaleUtils.getLocaleString(name, session.getClientData().getLanguageCode());
Inventory newInventory = new Inventory(name, packet.getWindowId(), packet.getType(), newTranslator.size + 36); Inventory newInventory = new Inventory(name, packet.getWindowId(), packet.getType(), newTranslator.size + 36);
session.getInventoryCache().cacheInventory(newInventory); session.getInventoryCache().cacheInventory(newInventory);
if (openInventory != null) { if (openInventory != null) {

View file

@ -36,6 +36,8 @@ import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData; import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.packet.UpdateTradePacket; import com.nukkitx.protocol.bedrock.packet.UpdateTradePacket;
import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.type.EntityType;
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.Translator; import org.geysermc.connector.network.translators.Translator;
@ -60,14 +62,20 @@ public class JavaTradeListTranslator extends PacketTranslator<ServerTradeListPac
UpdateTradePacket updateTradePacket = new UpdateTradePacket(); UpdateTradePacket updateTradePacket = new UpdateTradePacket();
updateTradePacket.setTradeTier(packet.getVillagerLevel() - 1); updateTradePacket.setTradeTier(packet.getVillagerLevel() - 1);
updateTradePacket.setContainerId((short) packet.getWindowId()); //TODO: CHECK THIS AND THE ONE BELOW updateTradePacket.setContainerId((short) packet.getWindowId());
updateTradePacket.setContainerType(ContainerType.TRADE); updateTradePacket.setContainerType(ContainerType.TRADE);
Inventory openInv = session.getInventoryCache().getOpenInventory();
String displayName; String displayName;
if (openInv != null && openInv.getId() == packet.getWindowId()) {
displayName = openInv.getTitle();
} else {
Entity realVillager = session.getEntityCache().getEntityByGeyserId(session.getLastInteractedVillagerEid()); Entity realVillager = session.getEntityCache().getEntityByGeyserId(session.getLastInteractedVillagerEid());
if (realVillager != null && realVillager.getMetadata().containsKey(EntityData.NAMETAG) && realVillager.getMetadata().getString(EntityData.NAMETAG) != null) { if (realVillager != null && realVillager.getMetadata().containsKey(EntityData.NAMETAG) && realVillager.getMetadata().getString(EntityData.NAMETAG) != null) {
displayName = realVillager.getMetadata().getString(EntityData.NAMETAG); displayName = realVillager.getMetadata().getString(EntityData.NAMETAG);
} else { } else {
displayName = packet.isRegularVillager() ? "Villager" : "Wandering Trader"; displayName = realVillager != null &&
realVillager.getEntityType() == EntityType.WANDERING_TRADER ? "Wandering Trader" : "Villager";
}
} }
updateTradePacket.setDisplayName(displayName); updateTradePacket.setDisplayName(displayName);
updateTradePacket.setSize(0); updateTradePacket.setSize(0);
@ -87,7 +95,7 @@ public class JavaTradeListTranslator extends PacketTranslator<ServerTradeListPac
recipe.putInt("buyCountB", trade.getSecondInput() != null ? trade.getSecondInput().getAmount() : 0); recipe.putInt("buyCountB", trade.getSecondInput() != null ? trade.getSecondInput().getAmount() : 0);
recipe.putInt("buyCountA", trade.getFirstInput().getAmount()); recipe.putInt("buyCountA", trade.getFirstInput().getAmount());
recipe.putInt("demand", trade.getDemand()); recipe.putInt("demand", trade.getDemand());
recipe.putInt("tier", packet.getVillagerLevel() - 1); recipe.putInt("tier", packet.getVillagerLevel() > 0 ? packet.getVillagerLevel() - 1 : 0); // -1 crashes client
recipe.put("buyA", getItemTag(session, trade.getFirstInput(), trade.getSpecialPrice())); recipe.put("buyA", getItemTag(session, trade.getFirstInput(), trade.getSpecialPrice()));
if (trade.getSecondInput() != null) { if (trade.getSecondInput() != null) {
recipe.put("buyB", getItemTag(session, trade.getSecondInput(), 0)); recipe.put("buyB", getItemTag(session, trade.getSecondInput(), 0));