Crafter translation

This commit is contained in:
Konicai 2023-12-03 10:03:59 -05:00
parent e2062dd182
commit 22009054ab
11 changed files with 436 additions and 3 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.geyser.inventory;
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.geyser.GeyserImpl;
@Getter
public class CrafterContainer extends Container {
@Setter
private boolean triggered = false;
/**
* Bedrock Edition bitmask of the *disabled* slots.
* Disabled slots are 1, enabled slots are 0 - same as Java Edition
*/
private short disabledSlotsMask = 0;
public CrafterContainer(String title, int id, int size, ContainerType containerType, PlayerInventory playerInventory) {
super(title, id, size, containerType, playerInventory);
}
public void setSlot(int slot, boolean enabled) {
if (slot < 0 || slot > 8) {
GeyserImpl.getInstance().getLogger().warning("Crafter slot out of bounds: " + slot);
return;
}
if (enabled) {
disabledSlotsMask = (short) (disabledSlotsMask & ~(1 << slot));
} else {
disabledSlotsMask = (short) (disabledSlotsMask | (1 << slot));
}
}
}

View File

@ -0,0 +1,94 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.geyser.inventory.updater;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerId;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.cloudburstmc.protocol.bedrock.packet.InventoryContentPacket;
import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.CrafterInventoryTranslator;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import java.util.Arrays;
/**
* Read {@link CrafterInventoryTranslator} for context on the complete custom implementation here
*/
public class CrafterInventoryUpdater extends InventoryUpdater {
public static final CrafterInventoryUpdater INSTANCE = new CrafterInventoryUpdater();
@Override
public void updateInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
ItemData[] bedrockItems;
InventoryContentPacket contentPacket;
// crafter grid - but excluding the result slot
bedrockItems = new ItemData[CrafterInventoryTranslator.GRID_SIZE];
for (int i = 0; i < bedrockItems.length; i++) {
bedrockItems[translator.javaSlotToBedrock(i)] = inventory.getItem(i).getItemData(session);
}
contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getBedrockId());
contentPacket.setContents(Arrays.asList(bedrockItems));
session.sendUpstreamPacket(contentPacket);
// inventory and hotbar
bedrockItems = new ItemData[36];
for (int i = 0; i < 36; i++) {
final int offset = i < 9 ? 27 : -9;
bedrockItems[i] = inventory.getItem(CrafterInventoryTranslator.GRID_SIZE + i + offset).getItemData(session);
}
contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(ContainerId.INVENTORY);
contentPacket.setContents(Arrays.asList(bedrockItems));
session.sendUpstreamPacket(contentPacket);
// Crafter result - it doesn't come after the grid, as explained elsewhere.
updateSlot(translator, session, inventory, CrafterInventoryTranslator.JAVA_RESULT_SLOT);
}
@Override
public boolean updateSlot(InventoryTranslator translator, GeyserSession session, Inventory inventory, int javaSlot) {
int containerId;
if (javaSlot < CrafterInventoryTranslator.GRID_SIZE || javaSlot == CrafterInventoryTranslator.JAVA_RESULT_SLOT) {
// Parts of the Crafter UI
// It doesn't seem like BDS sends the result slot, but sending it as slot 50 does actually work (it doesn't seem to show otherwise)
containerId = inventory.getBedrockId();
} else {
containerId = ContainerId.INVENTORY;
}
InventorySlotPacket packet = new InventorySlotPacket();
packet.setContainerId(containerId);
packet.setSlot(translator.javaSlotToBedrock(javaSlot));
packet.setItem(inventory.getItem(javaSlot).getItemData(session));
session.sendUpstreamPacket(packet);
return true;
}
}

View File

@ -32,6 +32,7 @@ import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
import org.cloudburstmc.protocol.bedrock.codec.v622.Bedrock_v622;
import org.cloudburstmc.protocol.bedrock.codec.v630.Bedrock_v630;
import org.cloudburstmc.protocol.bedrock.netty.codec.packet.BedrockPacketCodec;
import org.geysermc.geyser.session.GeyserSession;
import java.util.ArrayList;
import java.util.List;
@ -81,6 +82,10 @@ public final class GameProtocol {
/* Bedrock convenience methods to gatekeep features and easily remove the check on version removal */
public static boolean isPre1_20_50(GeyserSession session) {
return session.getUpstream().getProtocolVersion() < Bedrock_v630.CODEC.getProtocolVersion();
}
/**
* Gets the {@link PacketCodec} for Minecraft: Java Edition.
*

View File

@ -869,4 +869,11 @@ public class LoggingPacketHandler implements BedrockPacketHandler {
public PacketSignal handle(RequestNetworkSettingsPacket packet) {
return defaultHandler(packet);
}
// todo: fix arrangement
@Override
public PacketSignal handle(ToggleCrafterSlotRequestPacket packet) {
return defaultHandler(packet);
}
}

View File

@ -0,0 +1,168 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.geyser.translator.inventory;
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.ItemStackRequestSlotData;
import org.geysermc.geyser.inventory.BedrockContainerSlot;
import org.geysermc.geyser.inventory.CrafterContainer;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.PlayerInventory;
import org.geysermc.geyser.inventory.SlotType;
import org.geysermc.geyser.inventory.updater.CrafterInventoryUpdater;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.BlockEntityUtils;
/**
* Translates the Crafter. Most important thing to know about this class is that
* the result slot comes after the 3x3 grid AND the inventory. This means that the total size of the Crafter (10)
* cannot be used to calculate the inventory slot indices. The Translator and the Updater must then
* override any methods that use the size for such calculations
*/
public class CrafterInventoryTranslator extends AbstractBlockInventoryTranslator {
public static final int JAVA_RESULT_SLOT = 45;
public static final int BEDROCK_RESULT_SLOT = 50;
public static final int GRID_SIZE = 9;
// Properties
private static final int SLOT_ENABLED = 0; // enabled slot value
private static final int TRIGGERED_KEY = 9; // key of triggered state
private static final int TRIGGERED = 1; // triggered value
public CrafterInventoryTranslator() {
super(10, "minecraft:crafter", org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType.CRAFTER, CrafterInventoryUpdater.INSTANCE);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
// the slot bits and triggered state are sent here rather than in a BlockEntityDataPacket. Yippee.
CrafterContainer container = (CrafterContainer) inventory;
if (key == TRIGGERED_KEY) {
container.setTriggered(value == TRIGGERED);
} else {
// enabling and disabling slots of the 3x3 grid
container.setSlot(key, value == SLOT_ENABLED);
}
// Unfortunately this will be called 10 times when a Crafter is opened
// Kind of unavoidable because it must be invoked anytime an individual property is updated
updateBlockEntity(session, container);
}
@Override
public int bedrockSlotToJava(ItemStackRequestSlotData slotInfoData) {
int slot = slotInfoData.getSlot();
switch (slotInfoData.getContainer()) {
case HOTBAR_AND_INVENTORY, HOTBAR, INVENTORY -> {
//hotbar
if (slot >= 9) {
return slot + GRID_SIZE - 9;
} else {
return slot + GRID_SIZE + 27;
}
}
}
return slot;
}
@Override
public int javaSlotToBedrock(int slot) {
if (slot == JAVA_RESULT_SLOT) {
return BEDROCK_RESULT_SLOT;
}
// grid slots 0-8
if (slot < GRID_SIZE) {
return slot;
}
// inventory and hotbar
final int tmp = slot - GRID_SIZE;
if (tmp < 27) {
return tmp + 9;
} else {
return tmp - 27;
}
}
@Override
public BedrockContainerSlot javaSlotToBedrockContainer(int javaSlot) {
if (javaSlot == JAVA_RESULT_SLOT) {
return new BedrockContainerSlot(ContainerSlotType.CRAFTER_BLOCK_CONTAINER, BEDROCK_RESULT_SLOT);
}
// grid slots 0-8
if (javaSlot < GRID_SIZE) {
return new BedrockContainerSlot(ContainerSlotType.LEVEL_ENTITY, javaSlot);
}
// inventory and hotbar
final int tmp = javaSlot - GRID_SIZE;
if (tmp < 27) {
return new BedrockContainerSlot(ContainerSlotType.INVENTORY, tmp + 9);
} else {
return new BedrockContainerSlot(ContainerSlotType.HOTBAR, tmp - 27);
}
}
@Override
public SlotType getSlotType(int javaSlot) {
if (javaSlot == JAVA_RESULT_SLOT) {
return SlotType.OUTPUT;
}
return SlotType.NORMAL;
}
@Override
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
// Java sends the triggered and slot bits incrementally through properties, which we store here
return new CrafterContainer(name, windowId, this.size, containerType, playerInventory);
}
private static void updateBlockEntity(GeyserSession session, CrafterContainer container) {
/*
Here is an example of the tag sent by BDS 1.20.50.24
It doesn't include the position or the block entity ID in the tag, for whatever reason.
CLIENT BOUND BlockEntityDataPacket(blockPosition=(8, 110, 45), data={
"crafting_ticks_remaining": 0i,
"disabled_slots": 511s
})
*/
NbtMapBuilder tag = NbtMap.builder();
// just send some large amount since we don't know, and it'll be resent as 0 when java updates as not triggered
tag.putInt("crafting_ticks_remaining", container.isTriggered() ? 10_000 : 0);
tag.putShort("disabled_slots", container.getDisabledSlotsMask());
BlockEntityUtils.updateBlockEntity(session, tag.build(), container.getHolderPosition());
}
}

View File

@ -86,6 +86,7 @@ public abstract class InventoryTranslator {
put(ContainerType.BEACON, new BeaconInventoryTranslator());
put(ContainerType.BREWING_STAND, new BrewingInventoryTranslator());
put(ContainerType.CARTOGRAPHY, new CartographyInventoryTranslator());
put(ContainerType.CRAFTER_3x3, new CrafterInventoryTranslator());
put(ContainerType.CRAFTING, new CraftingInventoryTranslator());
put(ContainerType.ENCHANTMENT, new EnchantingInventoryTranslator());
put(ContainerType.HOPPER, new HopperInventoryTranslator());

View File

@ -44,7 +44,6 @@ public class DecoratedPotBlockEntityTranslator extends BlockEntityTranslator {
if (tag == null) {
return;
}
// todo 1.20.3 maybe
// exact same format
if (tag.get("sherds") instanceof ListTag sherds) {

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.geyser.translator.level.block.entity;
import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.cloudburstmc.nbt.NbtMapBuilder;
@BlockEntity(type = BlockEntityType.TRIAL_SPAWNER)
public class TrialSpawnerBlockEntityTranslator extends SpawnerBlockEntityTranslator {
@Override
public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState) {
if (tag != null) {
// todo 1.20.3 doesn't seem to work
super.translateTag(builder, tag, blockState);
}
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.geyser.translator.protocol.bedrock;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerSlotStateChangedPacket;
import org.cloudburstmc.protocol.bedrock.packet.ToggleCrafterSlotRequestPacket;
import org.geysermc.geyser.inventory.CrafterContainer;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
@Translator(packet = ToggleCrafterSlotRequestPacket.class)
public class BedrockToggleCrafterSlotRequestTranslator extends PacketTranslator<ToggleCrafterSlotRequestPacket> {
@Override
public void translate(GeyserSession session, ToggleCrafterSlotRequestPacket packet) {
if (!(session.getOpenInventory() instanceof CrafterContainer container)) {
return;
}
// Note that the Bedrock packet uses disabled, whereas the java packet used enabled.
session.sendDownstreamGamePacket(
new ServerboundContainerSlotStateChangedPacket(packet.getSlot(), container.getJavaId(), !packet.isDisabled()));
}
}

View File

@ -30,7 +30,9 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory.Cli
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket;
import net.kyori.adventure.text.Component;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.inventory.OldSmithingTableTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
@ -49,12 +51,18 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
return;
}
InventoryTranslator newTranslator = InventoryTranslator.inventoryTranslator(packet.getType());
InventoryTranslator newTranslator;
Inventory openInventory = session.getOpenInventory();
// Hack: ViaVersion translates the old (pre 1.20) smithing table to a furnace (does not work for Bedrock). We can detect this and translate it back to a smithing table.
if (session.isOldSmithingTable() && packet.getType() == ContainerType.FURNACE && packet.getTitle().equals(SMITHING_TABLE_COMPONENT)) {
newTranslator = OldSmithingTableTranslator.INSTANCE;
} else if (packet.getType() == ContainerType.CRAFTER_3x3 && GameProtocol.isPre1_20_50(session)) {
// Hack 2: Crafters are only supported by 1.20.50 and above. If 1.20.40 tries to open one, they'll get locked out of all inventories.
newTranslator = null; // close immediately below
session.sendMessage(ChatColor.RED + "Update your Bedrock Edition client to 1.20.50 or above to gain access to the Crafter.");
} else {
newTranslator = InventoryTranslator.inventoryTranslator(packet.getType());
}
// No translator exists for this window type. Close all windows and return.

View File

@ -14,7 +14,7 @@ protocol-connection = "3.0.0.Beta1-20231107.190703-112"
raknet = "1.0.0.CR1-20230703.195238-9"
blockstateupdater="1.20.50-20231106.161340-1"
mcauthlib = "d9d773e"
mcprotocollib = "c6ba2fa"
mcprotocollib = "11105ca"
adventure = "4.14.0"
adventure-platform = "4.3.0"
junit = "5.9.2"