2020-12-24 16:23:47 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-12-24 16:23:47 +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-24 16:23:47 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
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;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemStackRequest;
|
2022-10-30 03:02:11 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemStackResponse;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.StackRequestSlotInfoData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.stackrequestactions.CraftRecipeStackRequestActionData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionType;
|
2022-10-30 03:02:11 +00:00
|
|
|
import org.geysermc.geyser.inventory.BedrockContainerSlot;
|
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
|
|
|
import org.geysermc.geyser.inventory.PlayerInventory;
|
|
|
|
import org.geysermc.geyser.inventory.SlotType;
|
|
|
|
import org.geysermc.geyser.inventory.StonecutterContainer;
|
2022-04-21 01:22:02 +00:00
|
|
|
import org.geysermc.geyser.inventory.recipe.GeyserStonecutterData;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
|
2022-04-21 01:22:02 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2020-12-24 16:23:47 +00:00
|
|
|
|
|
|
|
public class StonecutterInventoryTranslator extends AbstractBlockInventoryTranslator {
|
|
|
|
public StonecutterInventoryTranslator() {
|
2022-10-30 00:23:21 +00:00
|
|
|
super(2, "minecraft:stonecutter[facing=north]", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.STONECUTTER, UIInventoryUpdater.INSTANCE);
|
2020-12-24 16:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-01-16 19:42:17 +00:00
|
|
|
protected boolean shouldHandleRequestFirst(StackRequestActionData action, Inventory inventory) {
|
2022-04-21 01:22:02 +00:00
|
|
|
return action.getType() == StackRequestActionType.CRAFT_RECIPE;
|
2020-12-24 16:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
protected ItemStackResponse translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-04-21 01:22:02 +00:00
|
|
|
// Guarded by shouldHandleRequestFirst
|
|
|
|
CraftRecipeStackRequestActionData data = (CraftRecipeStackRequestActionData) request.getActions()[0];
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-12-24 16:23:47 +00:00
|
|
|
// Look up all possible options of cutting from this ID
|
2022-04-21 01:22:02 +00:00
|
|
|
GeyserStonecutterData craftingData = session.getStonecutterRecipes().get(data.getRecipeNetworkId());
|
|
|
|
if (craftingData == null) {
|
2020-12-24 16:23:47 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2022-04-21 01:22:02 +00:00
|
|
|
StonecutterContainer container = (StonecutterContainer) inventory;
|
|
|
|
int button = craftingData.buttonId();
|
2020-12-30 01:31:48 +00:00
|
|
|
// If we've already pressed the button with this item, no need to press it again!
|
|
|
|
if (container.getStonecutterButton() != button) {
|
2022-04-21 01:22:02 +00:00
|
|
|
ItemStack javaOutput = craftingData.output();
|
|
|
|
|
2020-12-30 01:31:48 +00:00
|
|
|
// Getting the index of the item in the Java stonecutter list
|
2022-08-29 16:26:30 +00:00
|
|
|
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), button);
|
2020-12-30 01:31:48 +00:00
|
|
|
session.sendDownstreamPacket(packet);
|
|
|
|
container.setStonecutterButton(button);
|
|
|
|
if (inventory.getItem(1).getJavaId() != javaOutput.getId()) {
|
|
|
|
// We don't know there is an output here, so we tell ourselves that there is
|
2021-01-04 23:19:43 +00:00
|
|
|
inventory.setItem(1, GeyserItemStack.from(javaOutput), session);
|
2020-12-30 01:31:48 +00:00
|
|
|
}
|
2020-12-24 23:29:25 +00:00
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-12-24 16:23:47 +00:00
|
|
|
return translateRequest(session, inventory, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slotInfoData.getContainer()) {
|
|
|
|
case STONECUTTER_INPUT -> 0;
|
|
|
|
case STONECUTTER_RESULT, CREATIVE_OUTPUT -> 1;
|
|
|
|
default -> super.bedrockSlotToJava(slotInfoData);
|
|
|
|
};
|
2020-12-24 16:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.STONECUTTER_INPUT, 3);
|
|
|
|
}
|
|
|
|
if (slot == 1) {
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.STONECUTTER_RESULT, 50);
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrockContainer(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (slot == 1) {
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrock(slot);
|
|
|
|
}
|
2020-12-24 23:29:25 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SlotType getSlotType(int javaSlot) {
|
|
|
|
if (javaSlot == 1) {
|
|
|
|
return SlotType.OUTPUT;
|
|
|
|
}
|
|
|
|
return super.getSlotType(javaSlot);
|
|
|
|
}
|
2020-12-30 01:31:48 +00:00
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
return new StonecutterContainer(name, windowId, this.size, containerType, playerInventory);
|
2020-12-30 01:31:48 +00:00
|
|
|
}
|
2020-12-24 16:23:47 +00:00
|
|
|
}
|