Geyser/core/src/main/java/org/geysermc/geyser/translator/inventory/LecternInventoryTranslator....

219 lines
11 KiB
Java
Raw Normal View History

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
*/
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;
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;
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.ContainerInventoryUpdater;
import org.geysermc.geyser.network.GameProtocol;
2022-06-08 12:09:14 +00:00
import org.geysermc.geyser.session.GeyserSession;
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;
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() {
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
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;
2020-12-28 05:29:27 +00:00
}
@Override
2021-11-22 19:52:26 +00:00
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);
}
2020-12-28 05:29:27 +00:00
}
@Override
2021-11-22 19:52:26 +00:00
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());
}
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) {
GeyserItemStack itemStack = inventory.getItem(0);
if (!itemStack.isEmpty()) {
boolean isDropping = session.isDroppingLecternBook();
updateBook(session, inventory, itemStack);
if (!initialized && !isDropping) {
initialized = true;
openInventory(session, inventory);
}
}
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-14 09:27:49 +00:00
// If we're not in a real lectern, the Java server thinks we are still in the player inventory.
if (((LecternContainer) inventory).isFakeLectern()) {
InventoryTranslator.PLAYER_INVENTORY_TRANSLATOR.updateSlot(session, session.getPlayerInventory(), slot);
return;
}
super.updateSlot(session, inventory, slot);
2020-12-28 05:29:27 +00:00
if (slot == 0) {
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) {
LecternContainer lecternContainer = (LecternContainer) inventory;
if (session.isDroppingLecternBook()) {
// We have to enter the inventory GUI to eject the book
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), 3);
session.sendDownstreamGamePacket(packet);
session.setDroppingLecternBook(false);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
CompoundTag tag = book.getNbt();
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
// 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) {
int pagesSize = ((ListTag) tag.get("pages")).size();
ItemData itemData = book.getItemData(session);
NbtMapBuilder lecternTag = LecternUtils.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 = LecternUtils.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()
2021-02-27 02:53:24 +00:00
.putString("photoname", "")
.putString("text", "")
.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);
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
if (shouldRefresh) {
// the lectern cache doesn't always exist; only when we must refresh
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);
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) {
return new LecternContainer(name, windowId, this.size + playerInventory.getSize(), containerType, playerInventory);
2020-12-28 05:29:27 +00:00
}
}