2020-12-23 06:21:00 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-12-23 06:21:00 +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-23 06:21:00 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.ItemStackRequest;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.ItemStackRequestSlotData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.CraftRecipeOptionalAction;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.ItemStackRequestAction;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.ItemStackRequestActionType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponse;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.AnvilContainer;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.inventory.BedrockContainerSlot;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
|
|
|
import org.geysermc.geyser.inventory.PlayerInventory;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.updater.AnvilInventoryUpdater;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2020-12-23 06:21:00 +00:00
|
|
|
|
2022-04-21 01:39:35 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
public class AnvilInventoryTranslator extends AbstractBlockInventoryTranslator {
|
|
|
|
public AnvilInventoryTranslator() {
|
2022-10-30 00:23:21 +00:00
|
|
|
super(3, "minecraft:anvil[facing=north]", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.ANVIL, AnvilInventoryUpdater.INSTANCE,
|
2021-02-12 19:39:41 +00:00
|
|
|
"minecraft:chipped_anvil", "minecraft:damaged_anvil");
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 01:39:35 +00:00
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Inventory inventory) {
|
|
|
|
return action.getType() == ItemStackRequestActionType.CRAFT_RECIPE_OPTIONAL;
|
2022-04-21 01:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
protected ItemStackResponse translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-04-21 01:39:35 +00:00
|
|
|
// Guarded by shouldHandleRequestFirst check
|
2022-12-21 00:47:45 +00:00
|
|
|
CraftRecipeOptionalAction data = (CraftRecipeOptionalAction) request.getActions()[0];
|
2022-04-21 01:39:35 +00:00
|
|
|
AnvilContainer container = (AnvilContainer) inventory;
|
|
|
|
|
2022-11-30 21:05:35 +00:00
|
|
|
if (request.getFilterStrings().length != 0) {
|
|
|
|
// Required as of 1.18.30 - FilterTextPackets no longer appear to be sent
|
|
|
|
String name = request.getFilterStrings()[data.getFilteredStringIndex()];
|
|
|
|
if (!Objects.equals(name, container.getNewName())) { // TODO is this still necessary after pre-1.19.50 support is dropped?
|
|
|
|
container.checkForRename(session, name);
|
|
|
|
}
|
2022-04-21 01:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.translateRequest(session, inventory, request);
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
public int bedrockSlotToJava(ItemStackRequestSlotData slotInfoData) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slotInfoData.getContainer()) {
|
|
|
|
case ANVIL_INPUT -> 0;
|
|
|
|
case ANVIL_MATERIAL -> 1;
|
2022-12-23 17:06:41 +00:00
|
|
|
case ANVIL_RESULT, CREATED_OUTPUT -> 2;
|
2021-09-10 18:10:56 +00:00
|
|
|
default -> super.bedrockSlotToJava(slotInfoData);
|
|
|
|
};
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> new BedrockContainerSlot(ContainerSlotType.ANVIL_INPUT, 1);
|
|
|
|
case 1 -> new BedrockContainerSlot(ContainerSlotType.ANVIL_MATERIAL, 2);
|
|
|
|
case 2 -> new BedrockContainerSlot(ContainerSlotType.ANVIL_RESULT, 50);
|
|
|
|
default -> super.javaSlotToBedrockContainer(slot);
|
|
|
|
};
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> 1;
|
|
|
|
case 1 -> 2;
|
|
|
|
case 2 -> 50;
|
|
|
|
default -> super.javaSlotToBedrock(slot);
|
|
|
|
};
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
return new AnvilContainer(name, windowId, this.size, containerType, playerInventory);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
2021-11-15 01:15:25 +00:00
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
|
2021-11-15 01:15:25 +00:00
|
|
|
// The only property sent by Java is key 0 which is the level cost
|
|
|
|
if (key != 0) return;
|
|
|
|
AnvilContainer anvilContainer = (AnvilContainer) inventory;
|
|
|
|
anvilContainer.setJavaLevelCost(value);
|
|
|
|
anvilContainer.setUseJavaLevelCost(true);
|
|
|
|
updateSlot(session, anvilContainer, 1);
|
|
|
|
}
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|