3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-12-26 00:00:41 +01:00

Ensure bedrock inventory id is at most 100 (#3260)

Dieser Commit ist enthalten in:
AJ Ferguson 2022-08-29 12:26:30 -04:00 committet von GitHub
Ursprung 670308edc2
Commit 8e47a9f5e9
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
24 geänderte Dateien mit 56 neuen und 49 gelöschten Zeilen

Datei anzeigen

@ -45,7 +45,7 @@ import java.util.Arrays;
@ToString
public abstract class Inventory {
@Getter
protected final int id;
protected final int javaId;
/**
* The Java inventory state ID from the server. As of Java Edition 1.18.1 this value has one instance per player.
@ -94,15 +94,22 @@ public abstract class Inventory {
this("Inventory", id, size, containerType);
}
protected Inventory(String title, int id, int size, ContainerType containerType) {
protected Inventory(String title, int javaId, int size, ContainerType containerType) {
this.title = title;
this.id = id;
this.javaId = javaId;
this.size = size;
this.containerType = containerType;
this.items = new GeyserItemStack[size];
Arrays.fill(items, GeyserItemStack.EMPTY);
}
// This is to prevent conflicts with special bedrock inventory IDs.
// The vanilla java server only sends an ID between 1 and 100 when opening an inventory,
// so this is rarely needed. (certain plugins)
public int getBedrockId() {
return javaId <= 100 ? javaId : (javaId % 100) + 1;
}
public GeyserItemStack getItem(int slot) {
if (slot > this.size) {
GeyserImpl.getInstance().getLogger().debug("Tried to get an item out of bounds! " + this);

Datei anzeigen

@ -144,7 +144,7 @@ public final class ClickPlan {
}
ServerboundContainerClickPacket clickPacket = new ServerboundContainerClickPacket(
inventory.getId(),
inventory.getJavaId(),
stateId,
action.slot,
action.click.actionType,

Datei anzeigen

@ -133,7 +133,7 @@ public class BlockInventoryHolder extends InventoryHolder {
@Override
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setId((byte) inventory.getId());
containerOpenPacket.setId((byte) inventory.getBedrockId());
containerOpenPacket.setType(containerType);
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
@ -146,7 +146,7 @@ public class BlockInventoryHolder extends InventoryHolder {
// No need to reset a block since we didn't change any blocks
// But send a container close packet because we aren't destroying the original.
ContainerClosePacket packet = new ContainerClosePacket();
packet.setId((byte) inventory.getId());
packet.setId((byte) inventory.getBedrockId());
packet.setUnknownBool0(true); //TODO needs to be changed in Protocol to "server-side" or something
session.sendUpstreamPacket(packet);
return;

Datei anzeigen

@ -59,7 +59,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContainerId(inventory.getBedrockId());
contentPacket.setContents(bedrockItems);
session.sendUpstreamPacket(contentPacket);
}
@ -70,7 +70,7 @@ public class ChestInventoryUpdater extends InventoryUpdater {
return true;
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId());
slotPacket.setContainerId(inventory.getBedrockId());
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(inventory.getItem(javaSlot).getItemData(session));
session.sendUpstreamPacket(slotPacket);

Datei anzeigen

@ -47,7 +47,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContainerId(inventory.getBedrockId());
contentPacket.setContents(Arrays.asList(bedrockItems));
session.sendUpstreamPacket(contentPacket);
}
@ -58,7 +58,7 @@ public class ContainerInventoryUpdater extends InventoryUpdater {
return true;
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId());
slotPacket.setContainerId(inventory.getBedrockId());
slotPacket.setSlot(translator.javaSlotToBedrock(javaSlot));
slotPacket.setItem(inventory.getItem(javaSlot).getItemData(session));
session.sendUpstreamPacket(slotPacket);

Datei anzeigen

@ -47,7 +47,7 @@ public class HorseInventoryUpdater extends InventoryUpdater {
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContainerId(inventory.getBedrockId());
contentPacket.setContents(Arrays.asList(bedrockItems));
session.sendUpstreamPacket(contentPacket);
}

Datei anzeigen

@ -62,7 +62,7 @@ public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator
@Override
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (!((BeaconContainer) inventory).isUsingRealBlock()) {
InventoryUtils.closeInventory(session, inventory.getId(), false);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
return;
}
super.openInventory(translator, session, inventory);

Datei anzeigen

@ -43,7 +43,7 @@ public class BrewingInventoryTranslator extends AbstractBlockInventoryTranslator
public void openInventory(GeyserSession session, Inventory inventory) {
super.openInventory(session, inventory);
ContainerSetDataPacket dataPacket = new ContainerSetDataPacket();
dataPacket.setWindowId((byte) inventory.getId());
dataPacket.setWindowId((byte) inventory.getBedrockId());
dataPacket.setProperty(ContainerSetDataPacket.BREWING_STAND_FUEL_TOTAL);
dataPacket.setValue(20);
session.sendUpstreamPacket(dataPacket);
@ -52,7 +52,7 @@ public class BrewingInventoryTranslator extends AbstractBlockInventoryTranslator
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
ContainerSetDataPacket dataPacket = new ContainerSetDataPacket();
dataPacket.setWindowId((byte) inventory.getId());
dataPacket.setWindowId((byte) inventory.getBedrockId());
switch (key) {
case 0:
dataPacket.setProperty(ContainerSetDataPacket.BREWING_STAND_BREW_TIME);

Datei anzeigen

@ -127,7 +127,7 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
// Slot should be determined as 0, 1, or 2
return rejectRequest(request);
}
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), javaSlot);
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), javaSlot);
session.sendDownstreamPacket(packet);
return acceptRequest(request, makeContainerEntries(session, inventory, IntSets.emptySet()));
}

Datei anzeigen

@ -52,7 +52,7 @@ public class Generic3X3InventoryTranslator extends AbstractBlockInventoryTransla
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setId((byte) inventory.getId());
containerOpenPacket.setId((byte) inventory.getBedrockId());
// Required for opening the real block - otherwise, if the container type is incorrect, it refuses to open
containerOpenPacket.setType(((Generic3X3Container) inventory).isDropper() ? com.nukkitx.protocol.bedrock.data.inventory.ContainerType.DROPPER : com.nukkitx.protocol.bedrock.data.inventory.ContainerType.DISPENSER);
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());

Datei anzeigen

@ -804,7 +804,7 @@ public abstract class InventoryTranslator {
*/
//TODO: compatibility for simulated inventory (ClickPlan)
private static int findTempSlot(Inventory inventory, GeyserItemStack item, boolean emptyOnly, int... slotBlacklist) {
int offset = inventory.getId() == 0 ? 1 : 0; //offhand is not a viable temp slot
int offset = inventory.getJavaId() == 0 ? 1 : 0; //offhand is not a viable temp slot
HashSet<GeyserItemStack> itemBlacklist = new HashSet<>(slotBlacklist.length + 1);
itemBlacklist.add(item);

Datei anzeigen

@ -99,10 +99,10 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
LecternContainer lecternContainer = (LecternContainer) inventory;
if (session.isDroppingLecternBook()) {
// We have to enter the inventory GUI to eject the book
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), 3);
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), 3);
session.sendDownstreamPacket(packet);
session.setDroppingLecternBook(false);
InventoryUtils.closeInventory(session, inventory.getId(), false);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
CompoundTag tag = book.getNbt();
// Position has to be the last interacted position... right?
@ -150,9 +150,9 @@ public class LecternInventoryTranslator extends BaseInventoryTranslator {
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
session.getLecternCache().add(position);
// Close the window - we will reopen it once the client has this data synced
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getJavaId());
session.sendDownstreamPacket(closeWindowPacket);
InventoryUtils.closeInventory(session, inventory.getId(), false);
InventoryUtils.closeInventory(session, inventory.getJavaId(), false);
}
}
}

Datei anzeigen

@ -147,7 +147,7 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
// 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. :)
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), index);
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), index);
session.sendDownstreamPacket(packet);
GeyserItemStack inputCopy = inventory.getItem(0).copy(1);

Datei anzeigen

@ -68,7 +68,7 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
ItemStack javaOutput = craftingData.output();
// Getting the index of the item in the Java stonecutter list
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), button);
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getJavaId(), button);
session.sendDownstreamPacket(packet);
container.setStonecutterButton(button);
if (inventory.getItem(1).getJavaId() != javaOutput.getId()) {

Datei anzeigen

@ -130,7 +130,7 @@ public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setId((byte) inventory.getId());
containerOpenPacket.setId((byte) inventory.getBedrockId());
containerOpenPacket.setType(ContainerType.CONTAINER);
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
@ -143,7 +143,7 @@ public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
// No need to reset a block since we didn't change any blocks
// But send a container close packet because we aren't destroying the original.
ContainerClosePacket packet = new ContainerClosePacket();
packet.setId((byte) inventory.getId());
packet.setId((byte) inventory.getBedrockId());
packet.setUnknownBool0(true); //TODO needs to be changed in Protocol to "server-side" or something
session.sendUpstreamPacket(packet);
return;

Datei anzeigen

@ -43,7 +43,7 @@ public abstract class AbstractFurnaceInventoryTranslator extends AbstractBlockIn
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
ContainerSetDataPacket dataPacket = new ContainerSetDataPacket();
dataPacket.setWindowId((byte) inventory.getId());
dataPacket.setWindowId((byte) inventory.getBedrockId());
switch (key) {
case 0:
dataPacket.setProperty(ContainerSetDataPacket.FURNACE_LIT_TIME);

Datei anzeigen

@ -105,7 +105,7 @@ public abstract class ChestedHorseInventoryTranslator extends AbstractHorseInven
}
InventoryContentPacket horseContentsPacket = new InventoryContentPacket();
horseContentsPacket.setContainerId(inventory.getId());
horseContentsPacket.setContainerId(inventory.getBedrockId());
horseContentsPacket.setContents(Arrays.asList(horseItems));
session.sendUpstreamPacket(horseContentsPacket);
}

Datei anzeigen

@ -40,23 +40,23 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
@Override
public void translate(GeyserSession session, ContainerClosePacket packet) {
byte windowId = packet.getId();
byte bedrockId = packet.getId();
//Client wants close confirmation
session.sendUpstreamPacket(packet);
session.setClosingInventory(false);
if (windowId == -1 && session.getOpenInventory() instanceof MerchantContainer) {
if (bedrockId == -1 && session.getOpenInventory() instanceof MerchantContainer) {
// 1.16.200 - window ID is always -1 sent from Bedrock
windowId = (byte) session.getOpenInventory().getId();
bedrockId = (byte) session.getOpenInventory().getBedrockId();
}
Inventory openInventory = session.getOpenInventory();
if (openInventory != null) {
if (windowId == openInventory.getId()) {
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(windowId);
if (bedrockId == openInventory.getBedrockId()) {
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(openInventory.getJavaId());
session.sendDownstreamPacket(closeWindowPacket);
InventoryUtils.closeInventory(session, windowId, false);
InventoryUtils.closeInventory(session, openInventory.getJavaId(), false);
} else if (openInventory.isPending()) {
InventoryUtils.displayInventory(session, openInventory);
openInventory.setPending(false);

Datei anzeigen

@ -114,7 +114,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
changedItem = Int2ObjectMaps.singleton(hotbarSlot, itemStack.getItemStack());
}
ServerboundContainerClickPacket dropPacket = new ServerboundContainerClickPacket(
inventory.getId(), inventory.getStateId(), hotbarSlot, clickType.actionType, clickType.action,
inventory.getJavaId(), inventory.getStateId(), hotbarSlot, clickType.actionType, clickType.action,
inventory.getCursor().getItemStack(), changedItem);
session.sendDownstreamPacket(dropPacket);
return;
@ -371,7 +371,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
changedSlots.put(bedrockHotbarSlot, armorSlotItem.getItemStack());
ServerboundContainerClickPacket clickPacket = new ServerboundContainerClickPacket(
playerInventory.getId(), playerInventory.getStateId(), armorSlot,
playerInventory.getJavaId(), playerInventory.getStateId(), armorSlot,
click.actionType, click.action, null, changedSlots);
session.sendDownstreamPacket(clickPacket);
}

Datei anzeigen

@ -68,9 +68,9 @@ public class BedrockLecternUpdateTranslator extends PacketTranslator<LecternUpda
if (lecternContainer.getCurrentBedrockPage() == packet.getPage()) {
// The same page means Bedrock is closing the window
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getJavaId());
session.sendDownstreamPacket(closeWindowPacket);
InventoryUtils.closeInventory(session, lecternContainer.getId(), false);
InventoryUtils.closeInventory(session, lecternContainer.getJavaId(), false);
} else {
// Each "page" Bedrock gives to us actually represents two pages (think opening a book and seeing two pages)
// Each "page" on Java is just one page (think a spiral notebook folded back to only show one page)
@ -82,12 +82,12 @@ public class BedrockLecternUpdateTranslator extends PacketTranslator<LecternUpda
// is a byte when transmitted over the network and therefore this stops us at 128
if (newJavaPage > currentJavaPage) {
for (int i = currentJavaPage; i < newJavaPage; i++) {
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getId(), 2);
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getJavaId(), 2);
session.sendDownstreamPacket(clickButtonPacket);
}
} else {
for (int i = currentJavaPage; i > newJavaPage; i--) {
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getId(), 1);
ServerboundContainerButtonClickPacket clickButtonPacket = new ServerboundContainerButtonClickPacket(session.getOpenInventory().getJavaId(), 1);
session.sendDownstreamPacket(clickButtonPacket);
}
}

Datei anzeigen

@ -38,6 +38,6 @@ public class JavaContainerCloseTranslator extends PacketTranslator<ClientboundCo
public void translate(GeyserSession session, ClientboundContainerClosePacket packet) {
// Sometimes the server can request a window close of ID 0... when the window isn't even open
// Don't confirm in this instance
InventoryUtils.closeInventory(session, packet.getContainerId(), (session.getOpenInventory() != null && session.getOpenInventory().getId() == packet.getContainerId()));
InventoryUtils.closeInventory(session, packet.getContainerId(), (session.getOpenInventory() != null && session.getOpenInventory().getJavaId() == packet.getContainerId()));
}
}

Datei anzeigen

@ -54,7 +54,7 @@ public class JavaMerchantOffersTranslator extends PacketTranslator<ClientboundMe
@Override
public void translate(GeyserSession session, ClientboundMerchantOffersPacket packet) {
Inventory openInventory = session.getOpenInventory();
if (!(openInventory instanceof MerchantContainer merchantInventory && openInventory.getId() == packet.getContainerId())) {
if (!(openInventory instanceof MerchantContainer merchantInventory && openInventory.getJavaId() == packet.getContainerId())) {
return;
}

Datei anzeigen

@ -49,7 +49,7 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
// No translator exists for this window type. Close all windows and return.
if (newTranslator == null) {
if (openInventory != null) {
InventoryUtils.closeInventory(session, openInventory.getId(), true);
InventoryUtils.closeInventory(session, openInventory.getJavaId(), true);
}
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(packet.getContainerId());
session.sendDownstreamPacket(closeWindowPacket);
@ -64,7 +64,7 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
// In rare cases, inventories can do funny things where it keeps the same window type up but change the contents.
if (openInventory.getContainerType() != packet.getType()) {
// Sometimes the server can double-open an inventory with the same ID - don't confirm in that instance.
InventoryUtils.closeInventory(session, openInventory.getId(), openInventory.getId() != packet.getContainerId());
InventoryUtils.closeInventory(session, openInventory.getJavaId(), openInventory.getJavaId() != packet.getContainerId());
}
}

Datei anzeigen

@ -90,7 +90,7 @@ public class InventoryUtils {
if (translator instanceof DoubleChestInventoryTranslator && !((Container) inventory).isUsingRealBlock()) {
session.scheduleInEventLoop(() -> {
Inventory openInv = session.getOpenInventory();
if (openInv != null && openInv.getId() == inventory.getId()) {
if (openInv != null && openInv.getJavaId() == inventory.getJavaId()) {
translator.openInventory(session, inventory);
translator.updateInventory(session, inventory);
} else if (openInv != null && openInv.isPending()) {
@ -108,11 +108,11 @@ public class InventoryUtils {
}
}
public static void closeInventory(GeyserSession session, int windowId, boolean confirm) {
public static void closeInventory(GeyserSession session, int javaId, boolean confirm) {
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
updateCursor(session);
Inventory inventory = getInventory(session, windowId);
Inventory inventory = getInventory(session, javaId);
if (inventory != null) {
InventoryTranslator translator = session.getInventoryTranslator();
translator.closeInventory(session, inventory);
@ -124,12 +124,12 @@ public class InventoryUtils {
session.setOpenInventory(null);
}
public static Inventory getInventory(GeyserSession session, int windowId) {
if (windowId == 0) {
public static Inventory getInventory(GeyserSession session, int javaId) {
if (javaId == 0) {
return session.getPlayerInventory();
} else {
Inventory openInventory = session.getOpenInventory();
if (openInventory != null && windowId == openInventory.getId()) {
if (openInventory != null && javaId == openInventory.getJavaId()) {
return openInventory;
}
return null;