2020-12-24 00:48:21 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-12-24 00:48:21 +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 00:48:21 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket;
|
2020-12-24 23:29:25 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
2022-10-30 03:02:11 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.nbt.NbtMap;
|
|
|
|
import org.cloudburstmc.nbt.NbtType;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType;
|
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.CraftLoomAction;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.CraftResultsDeprecatedAction;
|
|
|
|
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-03-23 17:57:25 +00:00
|
|
|
import org.geysermc.geyser.inventory.BedrockContainerSlot;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.SlotType;
|
|
|
|
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
|
2022-03-23 17:57:25 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
|
|
|
import org.geysermc.geyser.translator.inventory.item.nbt.BannerTranslator;
|
2020-12-24 00:48:21 +00:00
|
|
|
|
2020-12-24 23:29:25 +00:00
|
|
|
import java.util.Collections;
|
2020-12-24 03:53:54 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-12-24 00:48:21 +00:00
|
|
|
public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
2020-12-24 03:53:54 +00:00
|
|
|
/**
|
|
|
|
* A map of Bedrock patterns to Java index. Used to request for a specific banner pattern.
|
|
|
|
*/
|
|
|
|
private static final Object2IntMap<String> PATTERN_TO_INDEX = new Object2IntOpenHashMap<>();
|
|
|
|
|
|
|
|
static {
|
|
|
|
// Added from left-to-right then up-to-down in the order Java presents it
|
2022-06-26 23:12:43 +00:00
|
|
|
int index = 0;
|
2020-12-24 03:53:54 +00:00
|
|
|
PATTERN_TO_INDEX.put("bl", index++);
|
|
|
|
PATTERN_TO_INDEX.put("br", index++);
|
|
|
|
PATTERN_TO_INDEX.put("tl", index++);
|
|
|
|
PATTERN_TO_INDEX.put("tr", index++);
|
|
|
|
PATTERN_TO_INDEX.put("bs", index++);
|
|
|
|
PATTERN_TO_INDEX.put("ts", index++);
|
|
|
|
PATTERN_TO_INDEX.put("ls", index++);
|
|
|
|
PATTERN_TO_INDEX.put("rs", index++);
|
|
|
|
PATTERN_TO_INDEX.put("cs", index++);
|
|
|
|
PATTERN_TO_INDEX.put("ms", index++);
|
|
|
|
PATTERN_TO_INDEX.put("drs", index++);
|
|
|
|
PATTERN_TO_INDEX.put("dls", index++);
|
|
|
|
PATTERN_TO_INDEX.put("ss", index++);
|
|
|
|
PATTERN_TO_INDEX.put("cr", index++);
|
|
|
|
PATTERN_TO_INDEX.put("sc", index++);
|
|
|
|
PATTERN_TO_INDEX.put("bt", index++);
|
|
|
|
PATTERN_TO_INDEX.put("tt", index++);
|
|
|
|
PATTERN_TO_INDEX.put("bts", index++);
|
|
|
|
PATTERN_TO_INDEX.put("tts", index++);
|
|
|
|
PATTERN_TO_INDEX.put("ld", index++);
|
|
|
|
PATTERN_TO_INDEX.put("rd", index++);
|
|
|
|
PATTERN_TO_INDEX.put("lud", index++);
|
|
|
|
PATTERN_TO_INDEX.put("rud", index++);
|
|
|
|
PATTERN_TO_INDEX.put("mc", index++);
|
|
|
|
PATTERN_TO_INDEX.put("mr", index++);
|
|
|
|
PATTERN_TO_INDEX.put("vh", index++);
|
|
|
|
PATTERN_TO_INDEX.put("hh", index++);
|
|
|
|
PATTERN_TO_INDEX.put("vhr", index++);
|
|
|
|
PATTERN_TO_INDEX.put("hhb", index++);
|
|
|
|
PATTERN_TO_INDEX.put("bo", index++);
|
|
|
|
index++; // Bordure indented, does not appear to exist in Bedrock?
|
|
|
|
PATTERN_TO_INDEX.put("gra", index++);
|
|
|
|
PATTERN_TO_INDEX.put("gru", index);
|
|
|
|
// Bricks do not appear to be a pattern on Bedrock, either
|
|
|
|
}
|
|
|
|
|
2020-12-24 00:48:21 +00:00
|
|
|
public LoomInventoryTranslator() {
|
|
|
|
super(4, "minecraft:loom[facing=north]", ContainerType.LOOM, UIInventoryUpdater.INSTANCE);
|
|
|
|
}
|
|
|
|
|
2020-12-24 23:29:25 +00:00
|
|
|
@Override
|
2022-01-11 21:56:11 +00:00
|
|
|
protected boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer,
|
2021-03-08 19:17:31 +00:00
|
|
|
int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) {
|
2020-12-24 23:29:25 +00:00
|
|
|
if (javaDestinationSlot != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-25 01:43:24 +00:00
|
|
|
GeyserItemStack itemStack = javaSourceSlot == -1 ? session.getPlayerInventory().getCursor() : inventory.getItem(javaSourceSlot);
|
2020-12-24 23:29:25 +00:00
|
|
|
if (itemStack.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject the item if Bedrock is attempting to put in a dye that is not a dye in Java Edition
|
2021-07-13 01:19:40 +00:00
|
|
|
return !itemStack.getMapping(session).getJavaIdentifier().endsWith("_dye");
|
2020-12-24 23:29:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 03:53:54 +00:00
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Inventory inventory) {
|
2020-12-24 03:53:54 +00:00
|
|
|
// If the LOOM_MATERIAL slot is not empty, we are crafting a pattern that does not come from an item
|
2022-12-21 00:47:45 +00:00
|
|
|
return action.getType() == ItemStackRequestActionType.CRAFT_LOOM && inventory.getItem(2).isEmpty();
|
2020-12-24 03:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-12-21 00:47:45 +00:00
|
|
|
ItemStackRequestAction headerData = request.getActions()[0];
|
|
|
|
ItemStackRequestAction data = request.getActions()[1];
|
|
|
|
if (!(headerData instanceof CraftLoomAction)) {
|
2022-06-26 23:12:43 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
2022-12-21 00:47:45 +00:00
|
|
|
if (!(data instanceof CraftResultsDeprecatedAction craftData)) {
|
2020-12-24 03:53:54 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-12-24 03:53:54 +00:00
|
|
|
// Get the patterns compound tag
|
2020-12-25 01:43:24 +00:00
|
|
|
List<NbtMap> newBlockEntityTag = craftData.getResultItems()[0].getTag().getList("Patterns", NbtType.COMPOUND);
|
2020-12-24 03:53:54 +00:00
|
|
|
// Get the pattern that the Bedrock client requests - the last pattern in the Patterns list
|
2020-12-25 01:43:24 +00:00
|
|
|
NbtMap pattern = newBlockEntityTag.get(newBlockEntityTag.size() - 1);
|
2022-12-21 00:47:45 +00:00
|
|
|
String bedrockPattern = ((CraftLoomAction) headerData).getPatternId();
|
2021-10-19 17:16:46 +00:00
|
|
|
|
2020-12-24 03:53:54 +00:00
|
|
|
// Get the Java index of this pattern
|
2021-10-19 17:16:46 +00:00
|
|
|
int index = PATTERN_TO_INDEX.getOrDefault(bedrockPattern, -1);
|
2020-12-24 03:53:54 +00:00
|
|
|
if (index == -1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
// Java's formula: 4 * row + col
|
|
|
|
// And the Java loom window has a fixed row/width of four
|
|
|
|
// So... Number / 4 = row (so we don't have to bother there), and number % 4 is our column, which leads us back to our index. :)
|
2022-08-29 16:26:30 +00:00
|
|
|
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), index);
|
2020-12-24 03:53:54 +00:00
|
|
|
session.sendDownstreamPacket(packet);
|
2020-12-26 17:20:59 +00:00
|
|
|
|
2021-03-12 02:43:08 +00:00
|
|
|
GeyserItemStack inputCopy = inventory.getItem(0).copy(1);
|
2020-12-26 21:41:50 +00:00
|
|
|
inputCopy.setNetId(session.getNextItemNetId());
|
2020-12-24 23:29:25 +00:00
|
|
|
// Add the pattern manually, for better item synchronization
|
|
|
|
if (inputCopy.getNbt() == null) {
|
|
|
|
inputCopy.setNbt(new CompoundTag(""));
|
|
|
|
}
|
|
|
|
CompoundTag blockEntityTag = inputCopy.getNbt().get("BlockEntityTag");
|
|
|
|
CompoundTag javaBannerPattern = BannerTranslator.getJavaBannerPattern(pattern);
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-12-24 23:29:25 +00:00
|
|
|
if (blockEntityTag != null) {
|
|
|
|
ListTag patternsList = blockEntityTag.get("Patterns");
|
|
|
|
if (patternsList != null) {
|
|
|
|
patternsList.add(javaBannerPattern);
|
|
|
|
} else {
|
|
|
|
patternsList = new ListTag("Patterns", Collections.singletonList(javaBannerPattern));
|
|
|
|
blockEntityTag.put(patternsList);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
blockEntityTag = new CompoundTag("BlockEntityTag");
|
|
|
|
ListTag patternsList = new ListTag("Patterns", Collections.singletonList(javaBannerPattern));
|
|
|
|
blockEntityTag.put(patternsList);
|
|
|
|
inputCopy.getNbt().put(blockEntityTag);
|
|
|
|
}
|
2021-03-08 21:57:31 +00:00
|
|
|
|
2020-12-24 23:29:25 +00:00
|
|
|
// Set the new item as the output
|
2020-12-30 00:59:22 +00:00
|
|
|
inventory.setItem(3, inputCopy, session);
|
2020-12-24 03:53:54 +00:00
|
|
|
|
|
|
|
return translateRequest(session, inventory, request);
|
|
|
|
}
|
|
|
|
|
2020-12-24 00:48:21 +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 LOOM_INPUT -> 0;
|
|
|
|
case LOOM_DYE -> 1;
|
|
|
|
case LOOM_MATERIAL -> 2;
|
2022-12-23 17:06:41 +00:00
|
|
|
case LOOM_RESULT, CREATED_OUTPUT -> 3;
|
2021-09-10 18:10:56 +00:00
|
|
|
default -> super.bedrockSlotToJava(slotInfoData);
|
|
|
|
};
|
2020-12-24 00:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> new BedrockContainerSlot(ContainerSlotType.LOOM_INPUT, 9);
|
|
|
|
case 1 -> new BedrockContainerSlot(ContainerSlotType.LOOM_DYE, 10);
|
|
|
|
case 2 -> new BedrockContainerSlot(ContainerSlotType.LOOM_MATERIAL, 11);
|
|
|
|
case 3 -> new BedrockContainerSlot(ContainerSlotType.LOOM_RESULT, 50);
|
|
|
|
default -> super.javaSlotToBedrockContainer(slot);
|
|
|
|
};
|
2020-12-24 00:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> 9;
|
|
|
|
case 1 -> 10;
|
|
|
|
case 2 -> 11;
|
|
|
|
case 3 -> 50;
|
|
|
|
default -> super.javaSlotToBedrock(slot);
|
|
|
|
};
|
2020-12-24 00:48:21 +00:00
|
|
|
}
|
2021-03-12 02:43:08 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SlotType getSlotType(int javaSlot) {
|
|
|
|
if (javaSlot == 3) {
|
|
|
|
return SlotType.OUTPUT;
|
|
|
|
}
|
|
|
|
return super.getSlotType(javaSlot);
|
|
|
|
}
|
2020-12-24 00:48:21 +00:00
|
|
|
}
|