mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-04-17 19:12:14 +02:00
Add debug, re-add force open hack
This commit is contained in:
parent
727fc2738e
commit
218f85ad39
7 changed files with 53 additions and 25 deletions
core/src/main/java/org/geysermc/geyser
|
@ -29,6 +29,7 @@ import net.kyori.adventure.text.Component;
|
|||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.geysermc.geyser.command.GeyserCommandSource;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -119,6 +120,10 @@ public interface GeyserLogger extends GeyserCommandSource {
|
|||
*/
|
||||
void setDebug(boolean debug);
|
||||
|
||||
default void sessionDebugLog(GeyserSession session, String message) {
|
||||
debug("(" + session.bedrockUsername() + "): " + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* If debug is enabled for this logger
|
||||
*/
|
||||
|
|
|
@ -98,6 +98,10 @@ public abstract class Inventory {
|
|||
@Setter
|
||||
private boolean pending = false;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean isCurrentlyDelayed = false;
|
||||
|
||||
@Getter
|
||||
private final InventoryTranslator translator;
|
||||
|
||||
|
|
|
@ -294,9 +294,6 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
|
|||
@Setter
|
||||
private @Nullable Inventory openInventory;
|
||||
|
||||
@Setter
|
||||
private @Nullable Inventory oldInventory;
|
||||
|
||||
@Setter
|
||||
private boolean closingInventory;
|
||||
|
||||
|
|
|
@ -94,7 +94,6 @@ public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
|
|||
|
||||
@Override
|
||||
public boolean prepareInventory(GeyserSession session, Inventory inventory) {
|
||||
GeyserImpl.getInstance().getLogger().info("double chest inv prep");
|
||||
if (canUseRealBlock(session, inventory)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -148,7 +147,6 @@ public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
|
|||
session.sendUpstreamPacket(dataPacket);
|
||||
|
||||
inventory.setHolderPosition(position);
|
||||
GeyserImpl.getInstance().getLogger().info("holder pos: " + position);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,9 @@
|
|||
package org.geysermc.geyser.translator.protocol.bedrock;
|
||||
|
||||
import org.cloudburstmc.protocol.bedrock.packet.ContainerClosePacket;
|
||||
import org.cloudburstmc.protocol.bedrock.packet.NetworkStackLatencyPacket;
|
||||
import org.geysermc.geyser.GeyserImpl;
|
||||
import org.geysermc.geyser.inventory.Container;
|
||||
import org.geysermc.geyser.inventory.Inventory;
|
||||
import org.geysermc.geyser.inventory.MerchantContainer;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
|
@ -34,11 +37,14 @@ import org.geysermc.geyser.translator.protocol.Translator;
|
|||
import org.geysermc.geyser.translator.protocol.java.inventory.JavaMerchantOffersTranslator;
|
||||
import org.geysermc.geyser.util.InventoryUtils;
|
||||
|
||||
import static org.geysermc.geyser.util.InventoryUtils.MAGIC_VIRTUAL_INVENTORY_HACK;
|
||||
|
||||
@Translator(packet = ContainerClosePacket.class)
|
||||
public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerClosePacket> {
|
||||
|
||||
@Override
|
||||
public void translate(GeyserSession session, ContainerClosePacket packet) {
|
||||
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, packet.toString());
|
||||
byte bedrockId = packet.getId();
|
||||
|
||||
//Client wants close confirmation
|
||||
|
@ -51,28 +57,36 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
|
|||
// 1.16.200 - window ID is always -1 sent from Bedrock for merchant containers
|
||||
bedrockId = (byte) openInventory.getBedrockId();
|
||||
|
||||
// // If virtual inventories are opened too quickly, they can be occasionally rejected
|
||||
// if (openInventory instanceof Container container && !(container instanceof MerchantContainer) && !container.isUsingRealBlock()) {
|
||||
// if (session.getContainerOpenAttempts() < 3) {
|
||||
// session.setContainerOpenAttempts(session.getContainerOpenAttempts() + 1);
|
||||
// session.getInventoryTranslator().openInventory(session, session.getOpenInventory());
|
||||
// session.getInventoryTranslator().updateInventory(session, session.getOpenInventory());
|
||||
// session.getOpenInventory().setDisplayed(true);
|
||||
// return;
|
||||
// } else {
|
||||
// GeyserImpl.getInstance().getLogger().debug("Exceeded 3 attempts to open a virtual inventory!");
|
||||
// GeyserImpl.getInstance().getLogger().debug(packet + " " + session.getOpenInventory().getClass().getSimpleName());
|
||||
// }
|
||||
// }
|
||||
// If virtual inventories are opened too quickly, they can be occasionally rejected
|
||||
// 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);
|
||||
container.setCurrentlyDelayed(true);
|
||||
session.setPendingInventoryId(container.getJavaId());
|
||||
|
||||
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
|
||||
latencyPacket.setFromServer(true);
|
||||
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
|
||||
session.sendUpstreamPacket(latencyPacket);
|
||||
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "New latency packet hack attempt: " + latencyPacket.toString());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
session.setContainerOpenAttempts(0);
|
||||
|
||||
if (openInventory != null) {
|
||||
if (bedrockId == openInventory.getBedrockId()) {
|
||||
GeyserImpl.getInstance().getLogger().debug("bedrock id matches, closing inventory java-side");
|
||||
InventoryUtils.sendJavaContainerClose(session, openInventory);
|
||||
InventoryUtils.closeInventory(session, openInventory.getJavaId(), false);
|
||||
} else if (openInventory.isPending()) {
|
||||
GeyserImpl.getInstance().getLogger().info("opening pending inventory!");
|
||||
InventoryUtils.displayInventory(session, openInventory);
|
||||
openInventory.setPending(false);
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
|
|||
|
||||
@Override
|
||||
public void translate(GeyserSession session, ClientboundOpenScreenPacket packet) {
|
||||
GeyserImpl.getInstance().getLogger().info(packet.toString());
|
||||
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, packet.toString());
|
||||
if (packet.getContainerId() == 0) {
|
||||
return;
|
||||
}
|
||||
|
@ -77,8 +77,8 @@ public class JavaOpenScreenTranslator extends PacketTranslator<ClientboundOpenSc
|
|||
|
||||
Inventory newInventory = newTranslator.createInventory(name, packet.getContainerId(), packet.getType(), session.getPlayerInventory());
|
||||
if (openInventory != null) {
|
||||
// TODO properly test. This would allow us to open repeating virtual inventories quite a bit faster
|
||||
// Attempt to re-use existing open inventories, if possible
|
||||
// // TODO this would allow us to open repeating virtual inventories quite a bit faster.
|
||||
// // Attempt to re-use existing open inventories, if possible
|
||||
// if (newTranslator.canReuseInventory(session, newInventory, openInventory)) {
|
||||
// // We need to handle pending virtual block inventories *slightly* different
|
||||
// if (session.getPendingInventoryId() == openInventory.getJavaId()
|
||||
|
|
|
@ -87,12 +87,13 @@ public class InventoryUtils {
|
|||
public static final long MAGIC_VIRTUAL_INVENTORY_HACK = -9876543210L;
|
||||
|
||||
public static void openInventory(GeyserSession session, Inventory inventory) {
|
||||
session.setOldInventory(session.getOpenInventory());
|
||||
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()));
|
||||
inventory.setPending(true);
|
||||
return;
|
||||
}
|
||||
|
@ -103,15 +104,20 @@ public class InventoryUtils {
|
|||
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!");
|
||||
session.setPendingInventoryId(-1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Current inventory isn't null! Let's see if we need to open it.
|
||||
if (currentInventory.getJavaId() == session.getPendingInventoryId()) {
|
||||
if (currentInventory.isCurrentlyDelayed() && currentInventory.getJavaId() == session.getPendingInventoryId()) {
|
||||
GeyserImpl.getInstance().getLogger().sessionDebugLog(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());
|
||||
|
||||
session.setPendingInventoryId(-1);
|
||||
openInventory(session, currentInventory);
|
||||
}
|
||||
|
@ -119,8 +125,11 @@ public class InventoryUtils {
|
|||
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());
|
||||
|
||||
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
|
||||
|
@ -128,6 +137,7 @@ public class InventoryUtils {
|
|||
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
|
||||
session.sendUpstreamPacket(latencyPacket);
|
||||
} else {
|
||||
GeyserImpl.getInstance().getLogger().sessionDebugLog(session, "Immediately opening inventory!");
|
||||
openAndUpdateInventory(session, inventory);
|
||||
}
|
||||
} else {
|
||||
|
@ -142,6 +152,7 @@ public class InventoryUtils {
|
|||
inventory.getTranslator().updateInventory(session, inventory);
|
||||
inventory.setDisplayed(true);
|
||||
inventory.setPending(false);
|
||||
inventory.setCurrentlyDelayed(false);
|
||||
session.setPendingInventoryId(-1);
|
||||
}
|
||||
|
||||
|
@ -172,8 +183,7 @@ public class InventoryUtils {
|
|||
session.getBundleCache().onInventoryClose(inventory);
|
||||
}
|
||||
|
||||
session.setPendingInventoryId(-1);
|
||||
session.setOldInventory(null);
|
||||
GeyserImpl.getInstance().getLogger().info("Closed inventory: " + inventory.getJavaId() + " is waiting on confirm?" + session.isClosingInventory());
|
||||
session.setOpenInventory(null);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue