Skip to content

Commit

Permalink
Fix: Virtual lecterns not displaying book contents (#5169)
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris authored Nov 29, 2024
1 parent c145c3f commit c240c1c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,24 @@
import org.geysermc.geyser.translator.protocol.java.inventory.JavaOpenBookTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.inventory.ContainerType;

@Getter
public class LecternContainer extends Container {
@Getter @Setter
@Setter
private int currentBedrockPage = 0;
@Getter @Setter
@Setter
private NbtMap blockEntityTag;
@Getter @Setter
@Setter
private Vector3i position;

// Sigh. When the lectern container is created, we don't know (yet) if it's fake or not.
// So... time for a manual check :/
@Getter
private boolean isFakeLectern = false;

public LecternContainer(String title, int id, int size, ContainerType containerType, PlayerInventory playerInventory) {
super(title, id, size, containerType, playerInventory);
}

/**
* When we are using a fake lectern, the Java server expects us to still be in a player inventory.
* We can't use {@link #isUsingRealBlock()} as that may not be determined yet.
* When the Java server asks the client to open a book in their hotbar, we create a fake lectern to show it to the client.
* We can't use the {@link #isUsingRealBlock()} check as we may also be dealing with a real virtual lectern (with its own inventory).
*/
@Override
public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void openInventory(InventoryTranslator translator, GeyserSession session,
@Override
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (inventory instanceof Container container) {
if (container.isUsingRealBlock() && !(inventory instanceof LecternContainer)) {
if (container.isUsingRealBlock() && !(container 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.cloudburstmc.nbt.NbtMapBuilder;
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;
Expand All @@ -55,7 +53,7 @@ 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;
private boolean receivedBook = false;

public LecternInventoryTranslator() {
super(1, Blocks.LECTERN, org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.LECTERN , ContainerInventoryUpdater.INSTANCE);
Expand All @@ -64,11 +62,12 @@ public LecternInventoryTranslator() {
@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
if (((LecternContainer) inventory).isFakeLectern()) {
// See JavaOpenBookTranslator; this isn't a lectern but a book in the player inventory
updateBook(session, inventory, inventory.getItem(0));
receivedBook = true;
} else {
updateBook(session, inventory, inventory.getItem(0)); // See JavaOpenBookTranslator; placed here manually
initialized = true;
receivedBook = false; // We have to wait until we get the book
}
return true;
}
Expand All @@ -79,7 +78,7 @@ public void openInventory(GeyserSession session, Inventory inventory) {
// "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()) {
if (receivedBook && !session.isDroppingLecternBook()) {
super.openInventory(session, inventory);
}
}
Expand Down Expand Up @@ -122,8 +121,8 @@ public void updateInventory(GeyserSession session, Inventory inventory) {
boolean isDropping = session.isDroppingLecternBook();
updateBook(session, inventory, itemStack);

if (!initialized && !isDropping) {
initialized = true;
if (!receivedBook && !isDropping) {
receivedBook = true;
openInventory(session, inventory);
}
}
Expand Down

0 comments on commit c240c1c

Please sign in to comment.