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.protocol.bedrock;
|
2020-12-28 05:29:27 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
|
2020-12-28 05:29:27 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket;
|
2021-11-18 03:02:38 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
|
2020-12-28 05:29:27 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.LecternUpdatePacket;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.LecternContainer;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2020-12-28 05:29:27 +00:00
|
|
|
|
2021-02-12 19:39:41 +00:00
|
|
|
/**
|
|
|
|
* Used to translate moving pages, or closing the inventory
|
|
|
|
*/
|
2020-12-28 05:29:27 +00:00
|
|
|
@Translator(packet = LecternUpdatePacket.class)
|
|
|
|
public class BedrockLecternUpdateTranslator extends PacketTranslator<LecternUpdatePacket> {
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void translate(GeyserSession session, LecternUpdatePacket packet) {
|
2020-12-28 05:29:27 +00:00
|
|
|
if (packet.isDroppingBook()) {
|
|
|
|
// Bedrock drops the book outside of the GUI. Java drops it in the GUI
|
|
|
|
// So, we enter the GUI and then drop it! :)
|
|
|
|
session.setDroppingLecternBook(true);
|
|
|
|
|
|
|
|
// Emulate an interact packet
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundUseItemOnPacket blockPacket = new ServerboundUseItemOnPacket(
|
2022-05-25 23:17:49 +00:00
|
|
|
packet.getBlockPosition(),
|
2021-11-18 03:02:38 +00:00
|
|
|
Direction.DOWN,
|
2020-12-28 05:29:27 +00:00
|
|
|
Hand.MAIN_HAND,
|
2021-03-12 16:51:37 +00:00
|
|
|
0, 0, 0, // Java doesn't care about these when dealing with a lectern
|
2022-05-25 19:55:15 +00:00
|
|
|
false,
|
2022-07-10 03:02:19 +00:00
|
|
|
session.getWorldCache().nextPredictionSequence());
|
2020-12-28 05:29:27 +00:00
|
|
|
session.sendDownstreamPacket(blockPacket);
|
|
|
|
} else {
|
|
|
|
// Bedrock wants to either move a page or exit
|
2021-09-10 18:10:56 +00:00
|
|
|
if (!(session.getOpenInventory() instanceof LecternContainer lecternContainer)) {
|
2021-11-20 21:34:30 +00:00
|
|
|
session.getGeyser().getLogger().debug("Expected lectern but it wasn't open!");
|
2021-01-22 18:36:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-13 23:07:43 +00:00
|
|
|
|
2020-12-28 05:29:27 +00:00
|
|
|
if (lecternContainer.getCurrentBedrockPage() == packet.getPage()) {
|
|
|
|
// The same page means Bedrock is closing the window
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
|
2020-12-28 05:29:27 +00:00
|
|
|
session.sendDownstreamPacket(closeWindowPacket);
|
2021-02-25 05:14:01 +00:00
|
|
|
InventoryUtils.closeInventory(session, lecternContainer.getId(), false);
|
2020-12-28 05:29:27 +00:00
|
|
|
} else {
|
|
|
|
// Each "page" Bedrock gives to us actually represents two pages (think opening a book and seeing two pages)
|
|
|
|
// Each "page" on Java is just one page (think a spiral notebook folded back to only show one page)
|
|
|
|
int newJavaPage = (packet.getPage() * 2);
|
|
|
|
int currentJavaPage = (lecternContainer.getCurrentBedrockPage() * 2);
|
2021-03-13 23:07:43 +00:00
|
|
|
|
2020-12-28 05:29:27 +00:00
|
|
|
// Send as many click button packets as we need to
|
|
|
|
// Java has the option to specify exact page numbers by adding 100 to the number, but buttonId variable
|
2021-03-12 16:51:37 +00:00
|
|
|
// is a byte when transmitted over the network and therefore this stops us at 128
|
2020-12-28 05:29:27 +00:00
|
|
|
if (newJavaPage > currentJavaPage) {
|
|
|
|
for (int i = currentJavaPage; i < newJavaPage; i++) {
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getId(), 2);
|
2020-12-28 05:29:27 +00:00
|
|
|
session.sendDownstreamPacket(clickButtonPacket);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = currentJavaPage; i > newJavaPage; i--) {
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getId(), 1);
|
2020-12-28 05:29:27 +00:00
|
|
|
session.sendDownstreamPacket(clickButtonPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|