1
0
Fork 0
mirror of https://github.com/GeyserMC/Geyser.git synced 2025-04-17 19:12:14 +02:00

Minor cleanup

This commit is contained in:
onebeastchris 2025-03-30 17:26:11 +02:00
parent 55a57a5707
commit b32f35a784
12 changed files with 75 additions and 32 deletions

View file

@ -120,8 +120,13 @@ public interface GeyserLogger extends GeyserCommandSource {
*/
void setDebug(boolean debug);
default void sessionDebugLog(GeyserSession session, String message) {
debug("(" + session.bedrockUsername() + "): " + message);
/**
* A method to debug information specific to a session.
*/
default void debug(GeyserSession session, String message, Object... arguments) {
if (isDebug()) {
debug("( %s ) " + message, session.bedrockUsername(), arguments);
}
}
/**

View file

@ -31,7 +31,6 @@ import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.util.InventoryUtils;
import org.incendo.cloud.context.CommandContext;
import java.util.Objects;
@ -52,6 +51,6 @@ public class AdvancedTooltipsCommand extends GeyserCommand {
+ MinecraftLocale.getLocaleString("debug.prefix", session.locale())
+ " " + ChatColor.RESET
+ MinecraftLocale.getLocaleString("debug.advanced_tooltips." + onOrOff, session.locale()));
InventoryUtils.getInventoryTranslator(session).updateInventory(session, session.getPlayerInventory());
session.getPlayerInventory().updateInventory(session);
}
}

View file

@ -49,6 +49,7 @@ public class Container extends Inventory {
/**
* Used to minimize delay when switching between "same" containers.
* Currently unused; see {@link org.geysermc.geyser.translator.protocol.java.inventory.JavaOpenScreenTranslator} for info.
*/
@Setter
private boolean isReusingBlock = false;

View file

@ -92,11 +92,11 @@ public abstract class Inventory {
@Getter
@Setter
private boolean displayed = false;
private boolean pending = false;
@Getter
@Setter
private boolean pending = false;
private boolean displayed = false;
@Getter
@Setter
@ -192,4 +192,20 @@ public abstract class Inventory {
public boolean shouldConfirmContainerClose() {
return true;
}
/*
* Helper methods to avoid using the wrong translator to update specific inventories.
*/
public void updateInventory(GeyserSession session) {
this.translator.updateInventory(session, this);
}
public void updateProperty(GeyserSession session, int rawProperty, int value) {
this.translator.updateProperty(session, this, rawProperty, value);
}
public void updateSlot(GeyserSession session, int slot) {
this.translator.updateSlot(session, this, slot);
}
}

View file

@ -184,7 +184,7 @@ public class BlockInventoryHolder extends InventoryHolder {
containerOpenPacket.setUniqueEntityId(container.getHolderId());
session.sendUpstreamPacket(containerOpenPacket);
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, containerOpenPacket.toString());
GeyserImpl.getInstance().getLogger().debug(session, containerOpenPacket.toString());
}
@Override

View file

@ -160,7 +160,7 @@ public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.sendUpstreamPacket(containerOpenPacket);
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, containerOpenPacket.toString());
GeyserImpl.getInstance().getLogger().debug(session, containerOpenPacket.toString());
}
@Override

View file

@ -32,7 +32,6 @@ import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.InventoryUtils;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentTypes;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
@ -133,7 +132,7 @@ public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket>
// Update local copy
session.getPlayerInventory().setItem(36 + session.getPlayerInventory().getHeldItemSlot(), GeyserItemStack.from(bookItem), session);
InventoryUtils.getInventoryTranslator(session).updateInventory(session, session.getPlayerInventory());
session.getPlayerInventory().updateInventory(session);
String title;
if (packet.getAction() == BookEditPacket.Action.SIGN_BOOK) {

View file

@ -46,7 +46,7 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
@Override
public void translate(GeyserSession session, ContainerClosePacket packet) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, packet.toString());
GeyserImpl.getInstance().getLogger().debug(session, packet.toString());
byte bedrockId = packet.getId();
//Client wants close confirmation
@ -60,7 +60,7 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
bedrockId = (byte) openInventory.getBedrockId();
// If virtual inventories are opened too quickly, they can be occasionally rejected
// We just try and queue a new one
// We just try and queue a new one.
if (openInventory instanceof Container container && !(container instanceof MerchantContainer) && !container.isUsingRealBlock()) {
if (session.getContainerOpenAttempts() < 3) {
container.setPending(true);
@ -72,12 +72,12 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
latencyPacket.setFromServer(true);
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
session.sendUpstreamPacket(latencyPacket);
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "New latency packet hack attempt: " + latencyPacket);
GeyserImpl.getInstance().getLogger().debug(session, "Unable to open a virtual inventory, sending another latency packet!");
}, 200, TimeUnit.MILLISECONDS);
return;
} else {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Exceeded 3 attempts to open a virtual inventory!");
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, packet + " " + session.getOpenInventory().getClass().getSimpleName());
GeyserImpl.getInstance().getLogger().debug(session, "Exceeded 3 attempts to open a virtual inventory!");
GeyserImpl.getInstance().getLogger().debug(session, packet + " " + session.getOpenInventory().getClass().getSimpleName());
}
}
}
@ -86,11 +86,9 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
if (openInventory != null) {
if (bedrockId == openInventory.getBedrockId()) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "bedrock id matches, closing inventory java-side");
InventoryUtils.sendJavaContainerClose(session, openInventory);
InventoryUtils.closeInventory(session, openInventory.getJavaId(), false);
} else if (openInventory.isPending()) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "opening pending inventory!");
InventoryUtils.displayInventory(session, openInventory);
if (openInventory instanceof MerchantContainer merchantContainer && merchantContainer.getPendingOffersPacket() != null) {

View file

@ -31,7 +31,6 @@ import org.geysermc.geyser.inventory.CartographyContainer;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils;
/**
* Used to send strings to the server and filter out unwanted words.
@ -49,7 +48,7 @@ public class BedrockFilterTextTranslator extends PacketTranslator<FilterTextPack
packet.setFromServer(true);
if (session.getOpenInventory() instanceof AnvilContainer anvilContainer) {
packet.setText(anvilContainer.checkForRename(session, packet.getText()));
InventoryUtils.getInventoryTranslator(session).updateSlot(session, anvilContainer, 1);
anvilContainer.updateSlot(session, 1);
}
session.sendUpstreamPacket(packet);
}

View file

@ -27,7 +27,6 @@ package org.geysermc.geyser.translator.protocol.java.inventory;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.InventoryUtils;
@ -42,7 +41,6 @@ public class JavaContainerSetDataTranslator extends PacketTranslator<Clientbound
if (inventory == null)
return;
InventoryTranslator translator = InventoryUtils.getInventoryTranslator(session);
translator.updateProperty(session, inventory, packet.getRawProperty(), packet.getValue());
inventory.updateProperty(session, packet.getRawProperty(), packet.getValue());
}
}

View file

@ -46,7 +46,7 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
@Override
public void translate(GeyserSession session, ClientboundOpenScreenPacket packet) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, packet.toString());
GeyserImpl.getInstance().getLogger().debug(session, packet.toString());
if (packet.getContainerId() == 0) {
return;
}

View file

@ -84,49 +84,67 @@ public class InventoryUtils {
public static final ItemStack REFRESH_ITEM = new ItemStack(1, 127, new DataComponents(new HashMap<>()));
/**
* An arbitrary, negative long used to delay the opening of virtual inventories until the client is
* likely ready for it. The {@link org.geysermc.geyser.translator.protocol.bedrock.BedrockNetworkStackLatencyTranslator}
* will then call {@link #openPendingInventory(GeyserSession)}, which would finish opening the inventory.
*/
public static final long MAGIC_VIRTUAL_INVENTORY_HACK = -9876543210L;
/**
* The main entrypoint to open an inventory. It will mark inventories as pending when the client isn't ready to
* open the new inventory yet.
*
* @param session the geyser session
* @param inventory the new inventory to open
*/
public static void openInventory(GeyserSession session, Inventory inventory) {
session.setOpenInventory(inventory);
if (session.isClosingInventory() || !session.getUpstream().isInitialized() || session.getPendingInventoryId() != -1) {
// Wait for close confirmation from client before opening the new inventory.
// Handled in BedrockContainerCloseTranslator
// or - client hasn't yet loaded in; wait until inventory is shown
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Inv (%s) set pending: closing inv? %s, pending inv id? %s"
.formatted(inventory.getJavaId(), session.isClosingInventory(), session.getPendingInventoryId()));
GeyserImpl.getInstance().getLogger().debug(session, "Inv (%s) set pending: closing inv? %s, pending inv id? %s", inventory.getJavaId(), session.isClosingInventory(), session.getPendingInventoryId());
inventory.setPending(true);
return;
}
displayInventory(session, inventory);
}
/**
* Called when the Bedrock client is ready to open a pending inventory.
* Due to the nature of possible changes in the delayed time, this method also re-checks for changes that might have
* occurred in the time. For example, a queued virtual inventory might be "outdated", so we wouldn't open it.
*/
public static void openPendingInventory(GeyserSession session) {
Inventory currentInventory = session.getOpenInventory();
if (currentInventory == null || !currentInventory.isPending()) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "No pending inventory, not opening an inventory!");
GeyserImpl.getInstance().getLogger().debug(session, "No pending inventory, not opening an inventory! Current inventory: %s", currentInventory);
session.setPendingInventoryId(-1);
return;
}
// Current inventory isn't null! Let's see if we need to open it.
if (currentInventory.isCurrentlyDelayed() && currentInventory.getJavaId() == session.getPendingInventoryId()) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Attempting to open currently delayed inventory with matching java id! " + currentInventory.getJavaId());
GeyserImpl.getInstance().getLogger().debug(session, "Attempting to open currently delayed inventory with matching java id! " + currentInventory.getJavaId());
openAndUpdateInventory(session, currentInventory);
return;
}
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Opening any pending inventory! " + currentInventory.getJavaId());
GeyserImpl.getInstance().getLogger().debug(session, "Opening any pending inventory! " + currentInventory.getJavaId());
session.setPendingInventoryId(-1);
openInventory(session, currentInventory);
}
/**
* Prepares and displays the current inventory. If necessary, it will queue the opening of virtual inventories.
* @param inventory the inventory to display
*/
public static void displayInventory(GeyserSession session, Inventory inventory) {
InventoryTranslator translator = inventory.getTranslator();
if (translator.prepareInventory(session, inventory)) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "prepared inventory!");
if (translator.shouldDelayInventoryOpen(session, inventory)) {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "starting delayed inventory open!");
inventory.setPending(true);
inventory.setCurrentlyDelayed(true);
session.setPendingInventoryId(inventory.getJavaId());
@ -135,8 +153,9 @@ public class InventoryUtils {
latencyPacket.setFromServer(true);
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
session.sendUpstreamPacket(latencyPacket);
GeyserImpl.getInstance().getLogger().debug(session, "Queuing virtual inventory with id %s", inventory.getJavaId());
} else {
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Immediately opening inventory!");
openAndUpdateInventory(session, inventory);
}
} else {
@ -146,6 +165,9 @@ public class InventoryUtils {
}
}
/**
* Opens and updates an inventory, and resets no longer used inventory variables.
*/
public static void openAndUpdateInventory(GeyserSession session, Inventory inventory) {
inventory.getTranslator().openInventory(session, inventory);
inventory.getTranslator().updateInventory(session, inventory);
@ -166,6 +188,12 @@ public class InventoryUtils {
return inventory.getTranslator();
}
/**
* Closes the inventory that matches the java id.
* @param session the session to close the inventory for
* @param javaId the id of the inventory to close
* @param confirm whether to wait for the session to process the close before opening a new inventory.
*/
public static void closeInventory(GeyserSession session, int javaId, boolean confirm) {
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
updateCursor(session);
@ -182,7 +210,7 @@ public class InventoryUtils {
session.getBundleCache().onInventoryClose(inventory);
}
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Closed inventory: " + (inventory != null ? inventory.getJavaId() : "null") + " is waiting on confirm?" + session.isClosingInventory());
GeyserImpl.getInstance().getLogger().debug(session, "Closed inventory: " + (inventory != null ? inventory.getJavaId() : "null") + " Waiting on confirm? %s", session.isClosingInventory());
session.setOpenInventory(null);
}