Update lectern if updated via the WindowItemsPacket

This commit is contained in:
Camotoy 2021-06-07 14:19:17 -04:00
parent 8510451dde
commit a2b3e1946d
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 65 additions and 55 deletions

View file

@ -78,12 +78,24 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
GeyserItemStack itemStack = inventory.getItem(0);
if (!itemStack.isEmpty()) {
updateBook(session, inventory, itemStack);
}
}
@Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
this.updater.updateSlot(this, session, inventory, slot);
if (slot == 0) {
updateBook(session, inventory, inventory.getItem(0));
}
}
/**
* Translate the data of the book in the lectern into a block entity tag.
*/
private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
LecternContainer lecternContainer = (LecternContainer) inventory;
if (session.isDroppingLecternBook()) {
// We have to enter the inventory GUI to eject the book
@ -92,18 +104,17 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
session.setDroppingLecternBook(false);
InventoryUtils.closeInventory(session, inventory.getId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
// If the method returns true, this is already handled for us
GeyserItemStack geyserItemStack = inventory.getItem(0);
CompoundTag tag = geyserItemStack.getNbt();
CompoundTag tag = book.getNbt();
// Position has to be the last interacted position... right?
Vector3i position = session.getLastInteractionBlockPosition();
// 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.getConnector().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
NbtMap blockEntityTag;
if (tag != null) {
int pagesSize = ((ListTag) tag.get("pages")).size();
ItemData itemData = geyserItemStack.getItemData(session);
ItemData itemData = book.getItemData(session);
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize);
lecternTag.putCompound("book", NbtMap.builder()
.putByte("Count", (byte) itemData.getCount())
@ -145,7 +156,6 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
}
}
}
}
@Override
public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) {