2020-12-28 05:29:27 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-12-28 05:29:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.inventory;
|
2020-12-28 05:29:27 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket;
|
2020-12-28 05:29:27 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.math.vector.Vector3i;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.nbt.NbtMap;
|
|
|
|
import org.cloudburstmc.nbt.NbtMapBuilder;
|
|
|
|
import org.cloudburstmc.nbt.NbtType;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
|
2023-03-30 19:44:55 +00:00
|
|
|
import org.geysermc.erosion.util.LecternUtils;
|
2024-03-02 02:21:31 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
|
|
|
import org.geysermc.geyser.inventory.Container;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
|
|
|
import org.geysermc.geyser.inventory.LecternContainer;
|
|
|
|
import org.geysermc.geyser.inventory.PlayerInventory;
|
2024-03-02 02:21:31 +00:00
|
|
|
import org.geysermc.geyser.inventory.updater.ContainerInventoryUpdater;
|
|
|
|
import org.geysermc.geyser.network.GameProtocol;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.util.BlockEntityUtils;
|
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2020-12-28 05:29:27 +00:00
|
|
|
|
2021-02-27 02:53:24 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
|
2024-03-02 02:21:31 +00:00
|
|
|
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;
|
2020-12-28 05:29:27 +00:00
|
|
|
|
|
|
|
public LecternInventoryTranslator() {
|
2024-03-02 02:21:31 +00:00
|
|
|
super(1, "minecraft:lectern[facing=north,has_book=true,powered=true]", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.LECTERN , ContainerInventoryUpdater.INSTANCE);
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-11-14 20:12:46 +00:00
|
|
|
public boolean prepareInventory(GeyserSession session, Inventory inventory) {
|
2024-03-02 02:21:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-11-14 20:12:46 +00:00
|
|
|
return true;
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void openInventory(GeyserSession session, Inventory inventory) {
|
2024-03-02 02:21:31 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void closeInventory(GeyserSession session, Inventory inventory) {
|
2024-03-02 02:21:31 +00:00
|
|
|
// 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());
|
|
|
|
}
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
|
2020-12-28 05:29:27 +00:00
|
|
|
if (key == 0) { // Lectern page update
|
|
|
|
LecternContainer lecternContainer = (LecternContainer) inventory;
|
|
|
|
lecternContainer.setCurrentBedrockPage(value / 2);
|
|
|
|
lecternContainer.setBlockEntityTag(lecternContainer.getBlockEntityTag().toBuilder().putInt("page", lecternContainer.getCurrentBedrockPage()).build());
|
|
|
|
BlockEntityUtils.updateBlockEntity(session, lecternContainer.getBlockEntityTag(), lecternContainer.getPosition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateInventory(GeyserSession session, Inventory inventory) {
|
2021-06-07 18:19:17 +00:00
|
|
|
GeyserItemStack itemStack = inventory.getItem(0);
|
|
|
|
if (!itemStack.isEmpty()) {
|
2024-03-02 02:21:31 +00:00
|
|
|
boolean isDropping = session.isDroppingLecternBook();
|
2021-06-07 18:19:17 +00:00
|
|
|
updateBook(session, inventory, itemStack);
|
2024-03-02 02:21:31 +00:00
|
|
|
|
|
|
|
if (!initialized && !isDropping) {
|
|
|
|
initialized = true;
|
|
|
|
openInventory(session, inventory);
|
|
|
|
}
|
2021-06-07 18:19:17 +00:00
|
|
|
}
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
|
2024-03-02 02:21:31 +00:00
|
|
|
super.updateSlot(session, inventory, slot);
|
2020-12-28 05:29:27 +00:00
|
|
|
if (slot == 0) {
|
2021-06-07 18:19:17 +00:00
|
|
|
updateBook(session, inventory, inventory.getItem(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate the data of the book in the lectern into a block entity tag.
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
|
2021-06-07 18:19:17 +00:00
|
|
|
LecternContainer lecternContainer = (LecternContainer) inventory;
|
|
|
|
if (session.isDroppingLecternBook()) {
|
|
|
|
// We have to enter the inventory GUI to eject the book
|
2022-08-29 16:26:30 +00:00
|
|
|
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), 3);
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(packet);
|
2021-06-07 18:19:17 +00:00
|
|
|
session.setDroppingLecternBook(false);
|
2022-08-29 16:26:30 +00:00
|
|
|
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
|
2021-06-07 18:19:17 +00:00
|
|
|
} else if (lecternContainer.getBlockEntityTag() == null) {
|
|
|
|
CompoundTag tag = book.getNbt();
|
2024-03-02 02:21:31 +00:00
|
|
|
Vector3i position = lecternContainer.isUsingRealBlock() ? session.getLastInteractionBlockPosition() : inventory.getHolderPosition();
|
|
|
|
|
2021-06-07 18:19:17 +00:00
|
|
|
// 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
|
2024-03-02 02:21:31 +00:00
|
|
|
// 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());
|
2021-06-07 18:19:17 +00:00
|
|
|
|
|
|
|
NbtMap blockEntityTag;
|
|
|
|
if (tag != null) {
|
|
|
|
int pagesSize = ((ListTag) tag.get("pages")).size();
|
|
|
|
ItemData itemData = book.getItemData(session);
|
2023-03-30 19:44:55 +00:00
|
|
|
NbtMapBuilder lecternTag = LecternUtils.getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize);
|
2021-06-07 18:19:17 +00:00
|
|
|
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.
|
2023-03-30 19:44:55 +00:00
|
|
|
NbtMapBuilder lecternTag = LecternUtils.getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1);
|
2021-06-07 18:19:17 +00:00
|
|
|
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()
|
2021-02-27 02:53:24 +00:00
|
|
|
.putString("photoname", "")
|
|
|
|
.putString("text", "")
|
2021-06-07 18:19:17 +00:00
|
|
|
.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
|
|
|
|
// the block entity tag
|
|
|
|
lecternContainer.setBlockEntityTag(blockEntityTag);
|
|
|
|
lecternContainer.setPosition(position);
|
2024-03-02 02:21:31 +00:00
|
|
|
|
|
|
|
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
|
|
|
|
session.getLecternCache().add(position);
|
|
|
|
|
2021-06-07 18:19:17 +00:00
|
|
|
if (shouldRefresh) {
|
|
|
|
// Close the window - we will reopen it once the client has this data synced
|
2022-08-29 16:26:30 +00:00
|
|
|
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getJavaId());
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(closeWindowPacket);
|
2022-08-29 16:26:30 +00:00
|
|
|
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
2024-03-02 02:21:31 +00:00
|
|
|
return new LecternContainer(name, windowId, this.size + playerInventory.getSize(), containerType, playerInventory);
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
}
|