2020-12-28 05:29:27 +00:00
|
|
|
/*
|
2021-01-01 19:22:26 +00:00
|
|
|
* Copyright (c) 2019-2021 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.connector.network.translators.inventory.translators;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientClickWindowButtonPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
|
|
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
|
|
|
import org.geysermc.connector.inventory.LecternContainer;
|
|
|
|
import org.geysermc.connector.inventory.PlayerInventory;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.inventory.updater.InventoryUpdater;
|
|
|
|
import org.geysermc.connector.utils.BlockEntityUtils;
|
|
|
|
import org.geysermc.connector.utils.InventoryUtils;
|
|
|
|
|
|
|
|
public class LecternInventoryTranslator extends BaseInventoryTranslator {
|
|
|
|
private final InventoryUpdater updater;
|
|
|
|
|
|
|
|
public LecternInventoryTranslator() {
|
|
|
|
super(1);
|
|
|
|
this.updater = new LecternInventoryUpdater();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
|
|
|
|
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
|
|
|
|
public void updateInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
|
|
|
|
this.updater.updateSlot(this, session, inventory, slot);
|
|
|
|
if (slot == 0) {
|
|
|
|
LecternContainer lecternContainer = (LecternContainer) inventory;
|
|
|
|
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);
|
2021-02-25 05:14:01 +00:00
|
|
|
InventoryUtils.closeInventory(session, inventory.getId(), false);
|
2020-12-28 05:29:27 +00:00
|
|
|
} 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();
|
|
|
|
if (tag != null) {
|
|
|
|
// Position has to be the last interacted position... right?
|
2021-02-12 19:39:41 +00:00
|
|
|
Vector3i position = session.getLastInteractionBlockPosition();
|
2020-12-28 18:16:17 +00:00
|
|
|
// shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
|
2020-12-28 05:29:27 +00:00
|
|
|
boolean shouldRefresh = !session.getConnector().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
|
|
|
|
int pagesSize = ((ListTag) tag.get("pages")).size();
|
|
|
|
ItemData itemData = geyserItemStack.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());
|
|
|
|
NbtMap blockEntityTag = lecternTag.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);
|
|
|
|
if (shouldRefresh) {
|
|
|
|
// Update the lectern because it's not updated client-side
|
|
|
|
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
|
|
|
|
session.getLecternCache().add(position);
|
|
|
|
// Close the window - we will reopen it once the client has this data synced
|
|
|
|
ClientCloseWindowPacket closeWindowPacket = new ClientCloseWindowPacket(lecternContainer.getId());
|
|
|
|
session.sendDownstreamPacket(closeWindowPacket);
|
2021-02-25 05:14:01 +00:00
|
|
|
InventoryUtils.closeInventory(session, inventory.getId(), false);
|
2020-12-28 05:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) {
|
|
|
|
return new LecternContainer(name, windowId, this.size, playerInventory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NbtMapBuilder getBaseLecternTag(int x, int y, int z, int totalPages) {
|
|
|
|
NbtMapBuilder builder = NbtMap.builder()
|
|
|
|
.putInt("x", x)
|
|
|
|
.putInt("y", y)
|
|
|
|
.putInt("z", z)
|
|
|
|
.putString("id", "Lectern");
|
|
|
|
if (totalPages != 0) {
|
|
|
|
builder.putByte("hasBook", (byte) 1);
|
|
|
|
builder.putInt("totalPages", totalPages);
|
|
|
|
} else {
|
2020-12-28 18:16:17 +00:00
|
|
|
// Not usually needed, but helps with kicking out Bedrock players from reading the UI
|
2020-12-28 05:29:27 +00:00
|
|
|
builder.putByte("hasBook", (byte) 0);
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class LecternInventoryUpdater extends InventoryUpdater {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|