Add support for SetContentPacket containerId 0

This commit is contained in:
Camotoy 2022-12-04 00:56:43 -05:00
parent 02208a5aed
commit f9a52ffc96
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F

View file

@ -31,6 +31,7 @@ import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory; import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.InventoryTranslator; import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.inventory.PlayerInventoryTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils; import org.geysermc.geyser.util.InventoryUtils;
@ -46,7 +47,7 @@ public class JavaContainerSetContentTranslator extends PacketTranslator<Clientbo
int inventorySize = inventory.getSize(); int inventorySize = inventory.getSize();
for (int i = 0; i < packet.getItems().length; i++) { for (int i = 0; i < packet.getItems().length; i++) {
if (i > inventorySize) { if (i >= inventorySize) {
GeyserImpl geyser = session.getGeyser(); GeyserImpl geyser = session.getGeyser();
geyser.getLogger().warning("ClientboundContainerSetContentPacket sent to " + session.bedrockUsername() geyser.getLogger().warning("ClientboundContainerSetContentPacket sent to " + session.bedrockUsername()
+ " that exceeds inventory size!"); + " that exceeds inventory size!");
@ -54,10 +55,7 @@ public class JavaContainerSetContentTranslator extends PacketTranslator<Clientbo
geyser.getLogger().debug(packet); geyser.getLogger().debug(packet);
geyser.getLogger().debug(inventory); geyser.getLogger().debug(inventory);
} }
InventoryTranslator translator = session.getInventoryTranslator(); updateInventory(session, inventory, packet.getContainerId());
if (translator != null) {
translator.updateInventory(session, inventory);
}
// 1.18.1 behavior: the previous items will be correctly set, but the state ID and carried item will not // 1.18.1 behavior: the previous items will be correctly set, but the state ID and carried item will not
// as this produces a stack trace on the client. // as this produces a stack trace on the client.
// If Java processes this correctly in the future, we can revert this behavior // If Java processes this correctly in the future, we can revert this behavior
@ -68,10 +66,7 @@ public class JavaContainerSetContentTranslator extends PacketTranslator<Clientbo
inventory.setItem(i, newItem, session); inventory.setItem(i, newItem, session);
} }
InventoryTranslator translator = session.getInventoryTranslator(); updateInventory(session, inventory, packet.getContainerId());
if (translator != null) {
translator.updateInventory(session, inventory);
}
int stateId = packet.getStateId(); int stateId = packet.getStateId();
session.setEmulatePost1_16Logic(stateId > 0 || stateId != inventory.getStateId()); session.setEmulatePost1_16Logic(stateId > 0 || stateId != inventory.getStateId());
@ -80,4 +75,14 @@ public class JavaContainerSetContentTranslator extends PacketTranslator<Clientbo
session.getPlayerInventory().setCursor(GeyserItemStack.from(packet.getCarriedItem()), session); session.getPlayerInventory().setCursor(GeyserItemStack.from(packet.getCarriedItem()), session);
InventoryUtils.updateCursor(session); InventoryUtils.updateCursor(session);
} }
private void updateInventory(GeyserSession session, Inventory inventory, int containerId) {
InventoryTranslator translator = session.getInventoryTranslator();
if (containerId == 0 && !(translator instanceof PlayerInventoryTranslator)) {
// In rare cases, the window ID can still be 0 but Java treats it as valid
InventoryTranslator.PLAYER_INVENTORY_TRANSLATOR.updateInventory(session, inventory);
} else if (translator != null) {
translator.updateInventory(session, inventory);
}
}
} }