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,71 +78,81 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
@Override @Override
public void updateInventory(GeyserSession session, Inventory inventory) { public void updateInventory(GeyserSession session, Inventory inventory) {
GeyserItemStack itemStack = inventory.getItem(0);
if (!itemStack.isEmpty()) {
updateBook(session, inventory, itemStack);
}
} }
@Override @Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) { public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
this.updater.updateSlot(this, session, inventory, slot); this.updater.updateSlot(this, session, inventory, slot);
if (slot == 0) { if (slot == 0) {
LecternContainer lecternContainer = (LecternContainer) inventory; updateBook(session, inventory, inventory.getItem(0));
if (session.isDroppingLecternBook()) { }
// We have to enter the inventory GUI to eject the book }
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), 3);
session.sendDownstreamPacket(packet);
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();
// Position has to be the last interacted position... right?
Vector3i position = session.getLastInteractionBlockPosition();
// 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) { * Translate the data of the book in the lectern into a block entity tag.
int pagesSize = ((ListTag) tag.get("pages")).size(); */
ItemData itemData = geyserItemStack.getItemData(session); private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize); LecternContainer lecternContainer = (LecternContainer) inventory;
lecternTag.putCompound("book", NbtMap.builder() if (session.isDroppingLecternBook()) {
.putByte("Count", (byte) itemData.getCount()) // We have to enter the inventory GUI to eject the book
.putShort("Damage", (short) 0) ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), 3);
.putString("Name", "minecraft:written_book") session.sendDownstreamPacket(packet);
.putCompound("tag", itemData.getTag()) session.setDroppingLecternBook(false);
.build()); InventoryUtils.closeInventory(session, inventory.getId(), false);
lecternTag.putInt("page", lecternContainer.getCurrentBedrockPage()); } else if (lecternContainer.getBlockEntityTag() == null) {
blockEntityTag = lecternTag.build(); CompoundTag tag = book.getNbt();
} else { // Position has to be the last interacted position... right?
// There is *a* book here, but... no NBT. Vector3i position = session.getLastInteractionBlockPosition();
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1); // If shouldExpectLecternHandled returns true, this is already handled for us
NbtMapBuilder bookTag = NbtMap.builder() // shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
.putByte("Count", (byte) 1) boolean shouldRefresh = !session.getConnector().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
.putShort("Damage", (short) 0)
.putString("Name", "minecraft:writable_book") NbtMap blockEntityTag;
.putCompound("tag", NbtMap.builder().putList("pages", NbtType.COMPOUND, Collections.singletonList( if (tag != null) {
NbtMap.builder() int pagesSize = ((ListTag) tag.get("pages")).size();
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())
.putShort("Damage", (short) 0)
.putString("Name", "minecraft:written_book")
.putCompound("tag", itemData.getTag())
.build());
lecternTag.putInt("page", lecternContainer.getCurrentBedrockPage());
blockEntityTag = lecternTag.build();
} else {
// There is *a* book here, but... no NBT.
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1);
NbtMapBuilder bookTag = NbtMap.builder()
.putByte("Count", (byte) 1)
.putShort("Damage", (short) 0)
.putString("Name", "minecraft:writable_book")
.putCompound("tag", NbtMap.builder().putList("pages", NbtType.COMPOUND, Collections.singletonList(
NbtMap.builder()
.putString("photoname", "") .putString("photoname", "")
.putString("text", "") .putString("text", "")
.build() .build()
)).build()); )).build());
blockEntityTag = lecternTag.putCompound("book", bookTag.build()).build(); blockEntityTag = lecternTag.putCompound("book", bookTag.build()).build();
} }
// Even with serverside access to lecterns, we don't easily know which lectern this is, so we need to rebuild // Even with serverside access to lecterns, we don't easily know which lectern this is, so we need to rebuild
// the block entity tag // the block entity tag
lecternContainer.setBlockEntityTag(blockEntityTag); lecternContainer.setBlockEntityTag(blockEntityTag);
lecternContainer.setPosition(position); lecternContainer.setPosition(position);
if (shouldRefresh) { if (shouldRefresh) {
// Update the lectern because it's not updated client-side // Update the lectern because it's not updated client-side
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position); BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
session.getLecternCache().add(position); session.getLecternCache().add(position);
// Close the window - we will reopen it once the client has this data synced // Close the window - we will reopen it once the client has this data synced
ClientCloseWindowPacket closeWindowPacket = new ClientCloseWindowPacket(lecternContainer.getId()); ClientCloseWindowPacket closeWindowPacket = new ClientCloseWindowPacket(lecternContainer.getId());
session.sendDownstreamPacket(closeWindowPacket); session.sendDownstreamPacket(closeWindowPacket);
InventoryUtils.closeInventory(session, inventory.getId(), false); InventoryUtils.closeInventory(session, inventory.getId(), false);
}
} }
} }
} }