Fix lecterns on 1.20.60, add support for virtual books (#4471)

* Fix lecterns on 1.20.60, start on virtual lecterns

* Fix: virtual books & actual books opening twice, resolve other issues, remove debug

* undo some unnecessary diff

* Don't try to send virtual books to pre 1.20.60 clients

* address review by camotoy
This commit is contained in:
chris 2024-03-02 03:21:31 +01:00 committed by GitHub
parent 3c4a1a82c9
commit 5d95bf65a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 218 additions and 34 deletions

View file

@ -32,8 +32,10 @@ import org.cloudburstmc.protocol.bedrock.packet.BlockEntityDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.ContainerClosePacket;
import org.cloudburstmc.protocol.bedrock.packet.ContainerOpenPacket;
import org.cloudburstmc.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.Container;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.LecternContainer;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.type.BlockMapping;
import org.geysermc.geyser.session.GeyserSession;
@ -151,13 +153,27 @@ public class BlockInventoryHolder extends InventoryHolder {
@Override
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (((Container) inventory).isUsingRealBlock()) {
// No need to reset a block since we didn't change any blocks
// But send a container close packet because we aren't destroying the original.
ContainerClosePacket packet = new ContainerClosePacket();
packet.setId((byte) inventory.getBedrockId());
packet.setServerInitiated(true);
session.sendUpstreamPacket(packet);
if (inventory instanceof Container container) {
if (container.isUsingRealBlock() && !(inventory instanceof LecternContainer)) {
// No need to reset a block since we didn't change any blocks
// But send a container close packet because we aren't destroying the original.
ContainerClosePacket packet = new ContainerClosePacket();
packet.setId((byte) inventory.getBedrockId());
packet.setServerInitiated(true);
session.sendUpstreamPacket(packet);
return;
}
} else {
GeyserImpl.getInstance().getLogger().warning("Tried to close a non-container inventory in a block inventory holder! ");
if (GeyserImpl.getInstance().getLogger().isDebug()) {
GeyserImpl.getInstance().getLogger().debug("Current inventory: " + inventory);
GeyserImpl.getInstance().getLogger().debug("Open inventory: " + session.getOpenInventory());
}
// Try to save ourselves? maybe?
// https://github.com/GeyserMC/Geyser/issues/4141
// TODO: improve once this issue is pinned down properly
session.setOpenInventory(null);
session.setInventoryTranslator(InventoryTranslator.PLAYER_INVENTORY_TRANSLATOR);
return;
}

View file

@ -53,6 +53,7 @@ public class StoredItemMappings {
private final ItemMapping upgradeTemplate;
private final ItemMapping wheat;
private final ItemMapping writableBook;
private final ItemMapping writtenBook;
public StoredItemMappings(Map<Item, ItemMapping> itemMappings) {
this.bamboo = load(itemMappings, Items.BAMBOO);
@ -68,6 +69,7 @@ public class StoredItemMappings {
this.upgradeTemplate = load(itemMappings, Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE);
this.wheat = load(itemMappings, Items.WHEAT);
this.writableBook = load(itemMappings, Items.WRITABLE_BOOK);
this.writtenBook = load(itemMappings, Items.WRITTEN_BOOK);
}
@NonNull

View file

@ -89,6 +89,7 @@ import org.cloudburstmc.protocol.bedrock.data.command.CommandEnumData;
import org.cloudburstmc.protocol.bedrock.data.command.CommandPermission;
import org.cloudburstmc.protocol.bedrock.data.command.SoftEnumUpdateType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.cloudburstmc.protocol.bedrock.packet.*;
import org.cloudburstmc.protocol.common.DefinitionRegistry;
import org.cloudburstmc.protocol.common.util.OptionalBoolean;
@ -595,6 +596,12 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
*/
private final Queue<Long> keepAliveCache = new ConcurrentLinkedQueue<>();
/**
* Stores the book that is currently being read. Used in {@link org.geysermc.geyser.translator.protocol.java.inventory.JavaOpenBookTranslator}
*/
@Setter
private @Nullable ItemData currentBook = null;
private final GeyserCameraData cameraData;
private final GeyserEntityData entityData;

View file

@ -36,36 +36,71 @@ import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.erosion.util.LecternUtils;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.Container;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.LecternContainer;
import org.geysermc.geyser.inventory.PlayerInventory;
import org.geysermc.geyser.inventory.updater.InventoryUpdater;
import org.geysermc.geyser.inventory.updater.ContainerInventoryUpdater;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.BlockEntityUtils;
import org.geysermc.geyser.util.InventoryUtils;
import java.util.Collections;
public class LecternInventoryTranslator extends BaseInventoryTranslator {
private final InventoryUpdater updater;
public class LecternInventoryTranslator extends AbstractBlockInventoryTranslator {
/**
* Hack: Java opens a lectern first, and then follows it up with a ClientboundContainerSetContentPacket
* to actually send the book's contents. We delay opening the inventory until the book was sent.
*/
private boolean initialized = false;
public LecternInventoryTranslator() {
super(1);
this.updater = new InventoryUpdater();
super(1, "minecraft:lectern[facing=north,has_book=true,powered=true]", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.LECTERN , ContainerInventoryUpdater.INSTANCE);
}
@Override
public boolean prepareInventory(GeyserSession session, Inventory inventory) {
super.prepareInventory(session, inventory);
if (((Container) inventory).isUsingRealBlock()) {
initialized = false; // We have to wait until we get the book to show to the client
} else {
updateBook(session, inventory, inventory.getItem(0)); // See JavaOpenBookTranslator; placed here manually
initialized = true;
}
return true;
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
// Hacky, but we're dealing with LECTERNS! It cannot not be hacky.
// "initialized" indicates whether we've received the book from the Java server yet.
// dropping lectern book is the fun workaround when we have to enter the gui to drop the book.
// Since we leave it immediately... don't open it!
if (initialized && !session.isDroppingLecternBook()) {
super.openInventory(session, inventory);
}
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
// Of course, sending a simple ContainerClosePacket, or even breaking the block doesn't work to close a lectern.
// Heck, the latter crashes the client xd
// BDS just sends an empty base lectern tag... that kicks out the client. Fine. Let's do that!
LecternContainer lecternContainer = (LecternContainer) inventory;
Vector3i position = lecternContainer.isUsingRealBlock() ? session.getLastInteractionBlockPosition() : inventory.getHolderPosition();
var baseLecternTag = LecternUtils.getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 0);
BlockEntityUtils.updateBlockEntity(session, baseLecternTag.build(), position);
super.closeInventory(session, inventory); // Removes the fake blocks if need be
// Now: Restore the lectern, if it actually exists
if (lecternContainer.isUsingRealBlock()) {
GeyserImpl.getInstance().getWorldManager().sendLecternData(session, position.getX(), position.getY(), position.getZ());
}
}
@Override
@ -82,13 +117,19 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
public void updateInventory(GeyserSession session, Inventory inventory) {
GeyserItemStack itemStack = inventory.getItem(0);
if (!itemStack.isEmpty()) {
boolean isDropping = session.isDroppingLecternBook();
updateBook(session, inventory, itemStack);
if (!initialized && !isDropping) {
initialized = true;
openInventory(session, inventory);
}
}
}
@Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
this.updater.updateSlot(this, session, inventory, slot);
super.updateSlot(session, inventory, slot);
if (slot == 0) {
updateBook(session, inventory, inventory.getItem(0));
}
@ -107,11 +148,14 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
CompoundTag tag = book.getNbt();
// Position has to be the last interacted position... right?
Vector3i position = session.getLastInteractionBlockPosition();
Vector3i position = lecternContainer.isUsingRealBlock() ? session.getLastInteractionBlockPosition() : inventory.getHolderPosition();
// If shouldExpectLecternHandled returns true, this is already handled for us
// shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
boolean shouldRefresh = !session.getGeyser().getWorldManager().shouldExpectLecternHandled(session) && !session.getLecternCache().contains(position);
// TODO: yeet after 1.20.60 is minimum supported version
boolean shouldRefresh = !session.getGeyser().getWorldManager().shouldExpectLecternHandled(session)
&& !session.getLecternCache().contains(position)
&& !GameProtocol.is1_20_60orHigher(session.getUpstream().getProtocolVersion());
NbtMap blockEntityTag;
if (tag != null) {
@ -147,10 +191,11 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
// the block entity tag
lecternContainer.setBlockEntityTag(blockEntityTag);
lecternContainer.setPosition(position);
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
session.getLecternCache().add(position);
if (shouldRefresh) {
// Update the lectern because it's not updated client-side
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
session.getLecternCache().add(position);
// Close the window - we will reopen it once the client has this data synced
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getJavaId());
session.sendDownstreamGamePacket(closeWindowPacket);
@ -161,6 +206,6 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
@Override
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
return new LecternContainer(name, windowId, this.size, containerType, playerInventory);
return new LecternContainer(name, windowId, this.size + playerInventory.getSize(), containerType, playerInventory);
}
}

View file

@ -380,6 +380,8 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
} else if (packet.getItemInHand().getDefinition() == session.getItemMappings().getStoredItems().glassBottle().getBedrockDefinition()) {
// Handled in case 0
break;
} else if (packet.getItemInHand().getDefinition() == session.getItemMappings().getStoredItems().writtenBook().getBedrockDefinition()) {
session.setCurrentBook(packet.getItemInHand());
}
}
@ -387,7 +389,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
session.sendDownstreamGamePacket(useItemPacket);
List<LegacySetItemSlotData> legacySlots = packet.getLegacySlots();
if (packet.getActions().size() == 1 && legacySlots.size() > 0) {
if (packet.getActions().size() == 1 && !legacySlots.isEmpty()) {
InventoryActionData actionData = packet.getActions().get(0);
LegacySetItemSlotData slotData = legacySlots.get(0);
if (slotData.getContainerId() == 6 && !actionData.getFromItem().isNull()) {

View file

@ -31,8 +31,10 @@ import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.Ser
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
import org.cloudburstmc.protocol.bedrock.packet.LecternUpdatePacket;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.LecternContainer;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils;
@ -77,6 +79,15 @@ public class BedrockLecternUpdateTranslator extends PacketTranslator<LecternUpda
int newJavaPage = (packet.getPage() * 2);
int currentJavaPage = (lecternContainer.getCurrentBedrockPage() * 2);
// So, fun fact: We need to separately handle fake lecterns!
// Since those are not actually a real lectern... the Java server won't respond to our requests.
if (!lecternContainer.isUsingRealBlock()) {
LecternInventoryTranslator translator = (LecternInventoryTranslator) session.getInventoryTranslator();
Inventory inventory = session.getOpenInventory();
translator.updateProperty(session, inventory, 0, newJavaPage);
return;
}
// Send as many click button packets as we need to
// Java has the option to specify exact page numbers by adding 100 to the number, but buttonId variable
// is a byte when transmitted over the network and therefore this stops us at 128

View file

@ -43,6 +43,7 @@ import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils;
import java.util.concurrent.TimeUnit;
@ -134,6 +135,10 @@ public class BedrockInteractTranslator extends PacketTranslator<InteractPacket>
containerOpenPacket.setBlockPosition(entity.getPosition().toInt());
session.sendUpstreamPacket(containerOpenPacket);
}
} else {
// Case: Player opens a player inventory, while we think it shouldn't have!
// Close all inventories, reset to player inventory.
InventoryUtils.closeInventory(session, session.getOpenInventory().getJavaId(), false);
}
break;
}

View file

@ -27,6 +27,7 @@ package org.geysermc.geyser.translator.protocol.java.inventory;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory.ClientboundContainerSetContentPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.session.GeyserSession;
@ -48,12 +49,12 @@ public class JavaContainerSetContentTranslator extends PacketTranslator<Clientbo
int inventorySize = inventory.getSize();
for (int i = 0; i < packet.getItems().length; i++) {
if (i >= inventorySize) {
GeyserImpl geyser = session.getGeyser();
geyser.getLogger().warning("ClientboundContainerSetContentPacket sent to " + session.bedrockUsername()
GeyserLogger logger = session.getGeyser().getLogger();
logger.warning("ClientboundContainerSetContentPacket sent to " + session.bedrockUsername()
+ " that exceeds inventory size!");
if (geyser.getConfig().isDebugMode()) {
geyser.getLogger().debug(packet);
geyser.getLogger().debug(inventory);
if (logger.isDebug()) {
logger.debug(packet);
logger.debug(inventory);
}
updateInventory(session, inventory, packet.getContainerId());
// 1.18.1 behavior: the previous items will be correctly set, but the state ID and carried item will not

View file

@ -34,7 +34,7 @@ import org.cloudburstmc.protocol.bedrock.data.inventory.crafting.recipe.ShapedRe
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemDescriptorWithCount;
import org.cloudburstmc.protocol.bedrock.packet.CraftingDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe;
@ -65,8 +65,9 @@ public class JavaContainerSetSlotTranslator extends PacketTranslator<Clientbound
//TODO: support window id -2, should update player inventory
Inventory inventory = InventoryUtils.getInventory(session, packet.getContainerId());
if (inventory == null)
if (inventory == null) {
return;
}
InventoryTranslator translator = session.getInventoryTranslator();
if (translator != null) {
@ -76,12 +77,12 @@ public class JavaContainerSetSlotTranslator extends PacketTranslator<Clientbound
int slot = packet.getSlot();
if (slot >= inventory.getSize()) {
GeyserImpl geyser = session.getGeyser();
geyser.getLogger().warning("ClientboundContainerSetSlotPacket sent to " + session.bedrockUsername()
GeyserLogger logger = session.getGeyser().getLogger();
logger.warning("ClientboundContainerSetSlotPacket sent to " + session.bedrockUsername()
+ " that exceeds inventory size!");
if (geyser.getConfig().isDebugMode()) {
geyser.getLogger().debug(packet);
geyser.getLogger().debug(inventory);
if (logger.isDebug()) {
logger.debug(packet.toString());
logger.debug(inventory.toString());
}
// 1.19.0 behavior: the state ID will not be set due to exception
return;

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.protocol.java.inventory;
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory.ClientboundOpenBookPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils;
import java.util.Objects;
@Translator(packet = ClientboundOpenBookPacket.class)
public class JavaOpenBookTranslator extends PacketTranslator<ClientboundOpenBookPacket> {
/**
* Unlike other fake inventories that rely on placing blocks in the world;
* the virtual lectern workaround for books isn't triggered the same way.
* Specifically, we don't get a window id - hence, we just use our own!
*/
private final static int FAKE_LECTERN_WINDOW_ID = -69;
@Override
public void translate(GeyserSession session, ClientboundOpenBookPacket packet) {
GeyserItemStack stack = session.getPlayerInventory().getItemInHand();
// Don't spawn a fake lectern for books already opened "normally" by the client.
if (stack.getItemData(session).equals(session.getCurrentBook())) {
session.setCurrentBook(null);
return;
}
// Only post 1.20.60 is it possible to tell the client to open a lectern.
if (!GameProtocol.is1_20_60orHigher(session.getUpstream().getProtocolVersion())) {
return;
}
if (stack.asItem().equals(Items.WRITTEN_BOOK)) {
Inventory openInventory = session.getOpenInventory();
if (openInventory != null) {
InventoryUtils.closeInventory(session, openInventory.getJavaId(), true);
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(openInventory.getJavaId());
session.sendDownstreamGamePacket(closeWindowPacket);
}
InventoryTranslator translator = InventoryTranslator.inventoryTranslator(ContainerType.LECTERN);
session.setInventoryTranslator(translator);
// Should never be null
Objects.requireNonNull(translator, "lectern translator must exist");
Inventory inventory = translator.createInventory("", FAKE_LECTERN_WINDOW_ID, ContainerType.LECTERN , session.getPlayerInventory());
inventory.setItem(0, stack, session);
InventoryUtils.openInventory(session, inventory);
}
}
}

View file

@ -45,6 +45,7 @@ import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.Container;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.LecternContainer;
import org.geysermc.geyser.inventory.PlayerInventory;
import org.geysermc.geyser.inventory.click.Click;
import org.geysermc.geyser.inventory.recipe.GeyserRecipe;
@ -123,7 +124,9 @@ public class InventoryUtils {
if (inventory != null) {
InventoryTranslator translator = session.getInventoryTranslator();
translator.closeInventory(session, inventory);
if (confirm && inventory.isDisplayed() && !inventory.isPending() && !(translator instanceof LecternInventoryTranslator)) {
if (confirm && inventory.isDisplayed() && !inventory.isPending()
&& !(translator instanceof LecternInventoryTranslator) // TODO: double-check
) {
session.setClosingInventory(true);
}
}
@ -133,6 +136,10 @@ public class InventoryUtils {
public static @Nullable Inventory getInventory(GeyserSession session, int javaId) {
if (javaId == 0) {
// ugly hack: lecterns aren't their own inventory on Java, and can hence be closed with e.g. an id of 0
if (session.getOpenInventory() instanceof LecternContainer) {
return session.getOpenInventory();
}
return session.getPlayerInventory();
} else {
Inventory openInventory = session.getOpenInventory();