2020-12-22 03:44:01 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-12-22 03:44:01 +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-22 03:44:01 +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;
|
2022-10-30 03:02:11 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntSets;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.EnchantOptionData;
|
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.CraftRecipeAction;
|
|
|
|
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;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.PlayerEnchantOptionsPacket;
|
2022-01-30 16:15:07 +00:00
|
|
|
import org.geysermc.geyser.inventory.*;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.item.Enchantment;
|
2022-01-30 16:15:07 +00:00
|
|
|
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
|
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2020-12-23 06:21:00 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2020-12-22 03:44:01 +00:00
|
|
|
|
|
|
|
public class EnchantingInventoryTranslator extends AbstractBlockInventoryTranslator {
|
|
|
|
public EnchantingInventoryTranslator() {
|
2022-10-30 00:23:21 +00:00
|
|
|
super(2, "minecraft:enchanting_table", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.ENCHANTMENT, UIInventoryUpdater.INSTANCE);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
|
2020-12-23 06:21:00 +00:00
|
|
|
int slotToUpdate;
|
|
|
|
EnchantingContainer enchantingInventory = (EnchantingContainer) inventory;
|
|
|
|
boolean shouldUpdate = false;
|
|
|
|
switch (key) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
// Experience required
|
|
|
|
slotToUpdate = key;
|
|
|
|
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setXpCost(value);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
// Enchantment type
|
|
|
|
slotToUpdate = key - 4;
|
2021-04-16 15:42:03 +00:00
|
|
|
// "value" here is the Java enchant ordinal, so that does not need to be changed
|
|
|
|
// The Bedrock index might need changed, so let's look it up and see.
|
|
|
|
int bedrockIndex = value;
|
|
|
|
if (bedrockIndex != -1) {
|
2021-04-26 18:15:24 +00:00
|
|
|
Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + Enchantment.JavaEnchantment.of(bedrockIndex).name().toLowerCase());
|
2020-12-23 06:21:00 +00:00
|
|
|
if (enchantment != null) {
|
|
|
|
// Convert the Java enchantment index to Bedrock's
|
2021-04-16 15:42:03 +00:00
|
|
|
bedrockIndex = enchantment.ordinal();
|
2020-12-23 06:21:00 +00:00
|
|
|
} else {
|
2021-04-16 15:42:03 +00:00
|
|
|
// There is no Bedrock enchantment equivalent
|
|
|
|
bedrockIndex = -1;
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 15:42:03 +00:00
|
|
|
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setEnchantIndex(value, bedrockIndex);
|
2020-12-23 06:21:00 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
// Enchantment level
|
|
|
|
slotToUpdate = key - 7;
|
|
|
|
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setEnchantLevel(value);
|
|
|
|
shouldUpdate = true; // Java sends each property as its own packet, so let's only update after all properties have been sent
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2021-04-16 15:42:03 +00:00
|
|
|
GeyserEnchantOption enchantOption = enchantingInventory.getGeyserEnchantOptions()[slotToUpdate];
|
|
|
|
if (shouldUpdate && enchantOption.hasChanged()) {
|
|
|
|
enchantingInventory.getEnchantOptions()[slotToUpdate] = enchantOption.build(session);
|
2020-12-23 06:21:00 +00:00
|
|
|
PlayerEnchantOptionsPacket packet = new PlayerEnchantOptionsPacket();
|
|
|
|
packet.getOptions().addAll(Arrays.asList(enchantingInventory.getEnchantOptions()));
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Inventory inventory) {
|
|
|
|
return action.getType() == ItemStackRequestActionType.CRAFT_RECIPE;
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2020-12-23 06:21:00 +00:00
|
|
|
// Client has requested an item to be enchanted
|
2022-12-21 00:47:45 +00:00
|
|
|
CraftRecipeAction craftRecipeData = (CraftRecipeAction) request.getActions()[0];
|
2020-12-23 06:21:00 +00:00
|
|
|
EnchantingContainer enchantingInventory = (EnchantingContainer) inventory;
|
|
|
|
int javaSlot = -1;
|
|
|
|
for (int i = 0; i < enchantingInventory.getEnchantOptions().length; i++) {
|
|
|
|
EnchantOptionData enchantData = enchantingInventory.getEnchantOptions()[i];
|
|
|
|
if (enchantData != null) {
|
|
|
|
if (craftRecipeData.getRecipeNetworkId() == enchantData.getEnchantNetId()) {
|
|
|
|
// Enchant net ID is how we differentiate between what item Bedrock wants
|
|
|
|
javaSlot = enchantingInventory.getGeyserEnchantOptions()[i].getJavaIndex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (javaSlot == -1) {
|
|
|
|
// Slot should be determined as 0, 1, or 2
|
2020-12-24 23:29:25 +00:00
|
|
|
return rejectRequest(request);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
2022-08-29 16:26:30 +00:00
|
|
|
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), javaSlot);
|
2020-12-23 06:21:00 +00:00
|
|
|
session.sendDownstreamPacket(packet);
|
2022-01-30 16:15:07 +00:00
|
|
|
return acceptRequest(request, makeContainerEntries(session, inventory, IntSets.emptySet()));
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
public int bedrockSlotToJava(ItemStackRequestSlotData slotInfoData) {
|
2020-12-23 06:21:00 +00:00
|
|
|
if (slotInfoData.getContainer() == ContainerSlotType.ENCHANTING_INPUT) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (slotInfoData.getContainer() == ContainerSlotType.ENCHANTING_LAPIS) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return super.bedrockSlotToJava(slotInfoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.ENCHANTING_INPUT, 14);
|
|
|
|
}
|
|
|
|
if (slot == 1) {
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.ENCHANTING_LAPIS, 15);
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrockContainer(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
if (slot == 1) {
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrock(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
return new EnchantingContainer(name, windowId, this.size, containerType, playerInventory);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
2020-12-22 03:44:01 +00:00
|
|
|
}
|