Skip to content

Commit 4b789b8

Browse files
committed
Fixes related to hacky plugins that spam open screen packets
1 parent a925cd0 commit 4b789b8

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

core/src/main/java/org/geysermc/geyser/session/GeyserSession.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
299299

300300
/**
301301
* Stores the bedrock inventory id of the pending inventory, or -1 if no inventory is pending.
302+
* This id is only set when the block that should be opened exists.
302303
*/
303304
@Setter
304-
private int pendingInventoryId = -1;
305+
private int pendingOrCurrentBedrockInventoryId = -1;
305306

306307
/**
307308
* Use {@link #getNextItemNetId()} instead for consistency

core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockContainerCloseTranslator.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.geysermc.geyser.inventory.Inventory;
3232
import org.geysermc.geyser.inventory.MerchantContainer;
3333
import org.geysermc.geyser.session.GeyserSession;
34+
import org.geysermc.geyser.translator.inventory.MerchantInventoryTranslator;
3435
import org.geysermc.geyser.translator.protocol.PacketTranslator;
3536
import org.geysermc.geyser.translator.protocol.Translator;
3637
import org.geysermc.geyser.translator.protocol.java.inventory.JavaMerchantOffersTranslator;
@@ -56,28 +57,26 @@ public void translate(GeyserSession session, ContainerClosePacket packet) {
5657
Inventory openInventory = session.getOpenInventory();
5758
if (bedrockId == -1 && openInventory != null) {
5859
// 1.16.200 - window ID is always -1 sent from Bedrock for merchant containers
59-
bedrockId = (byte) openInventory.getBedrockId();
60+
if (openInventory.getTranslator() instanceof MerchantInventoryTranslator) {
61+
bedrockId = (byte) openInventory.getBedrockId();
62+
}
6063

6164
// If virtual inventories are opened too quickly, they can be occasionally rejected
6265
// We just try and queue a new one.
63-
if (openInventory.getTranslator().requiresOpeningDelay(session, openInventory)) {
66+
if (openInventory.getBedrockId() == session.getPendingOrCurrentBedrockInventoryId()) {
67+
// Before making another attempt to re-open, let's make sure we actually need this inventory open.
6468
if (session.getContainerOpenAttempts() < 3) {
6569
openInventory.setPending(true);
6670
openInventory.setDelayed(true);
67-
session.setPendingInventoryId(openInventory.getBedrockId());
71+
session.setPendingOrCurrentBedrockInventoryId(openInventory.getBedrockId());
6872

69-
byte finalBedrockId = bedrockId;
7073
session.scheduleInEventLoop(() -> {
71-
if (InventoryUtils.shouldQueueRejectedInventory(session)) {
72-
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
73-
latencyPacket.setFromServer(true);
74-
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
75-
session.sendUpstreamPacket(latencyPacket);
76-
GeyserImpl.getInstance().getLogger().debug(session, "Unable to open a virtual inventory, sending another latency packet!");
77-
} else {
78-
closeCurrentOrOpenPending(session, finalBedrockId, session.getOpenInventory());
79-
}
80-
}, 200, TimeUnit.MILLISECONDS);
74+
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
75+
latencyPacket.setFromServer(true);
76+
latencyPacket.setTimestamp(MAGIC_VIRTUAL_INVENTORY_HACK);
77+
session.sendUpstreamPacket(latencyPacket);
78+
GeyserImpl.getInstance().getLogger().debug(session, "Unable to open a virtual inventory, sent another latency packet!");
79+
}, 100, TimeUnit.MILLISECONDS);
8180
return;
8281
} else {
8382
GeyserImpl.getInstance().getLogger().debug(session, "Exceeded 3 attempts to open a virtual inventory!");
@@ -86,6 +85,7 @@ public void translate(GeyserSession session, ContainerClosePacket packet) {
8685
}
8786
}
8887

88+
session.setPendingOrCurrentBedrockInventoryId(-1);
8989
session.setContainerOpenAttempts(0);
9090
closeCurrentOrOpenPending(session, bedrockId, openInventory);
9191
}

core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockNetworkStackLatencyTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void translate(GeyserSession session, NetworkStackLatencyPacket packet) {
6565
return;
6666
}
6767

68-
if (session.getPendingInventoryId() != -1) {
68+
if (session.getPendingOrCurrentBedrockInventoryId() != -1) {
6969
InventoryUtils.openPendingInventory(session);
7070
} else {
7171
session.scheduleInEventLoop(() -> {

core/src/main/java/org/geysermc/geyser/util/InventoryUtils.java

+8-19
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public class InventoryUtils {
100100
*/
101101
public static void openInventory(GeyserSession session, Inventory inventory) {
102102
session.setOpenInventory(inventory);
103-
if (session.isClosingInventory() || !session.getUpstream().isInitialized() || session.getPendingInventoryId() != -1) {
103+
if (session.isClosingInventory() || !session.getUpstream().isInitialized() || session.getPendingOrCurrentBedrockInventoryId() != -1) {
104104
// Wait for close confirmation from client before opening the new inventory.
105105
// Handled in BedrockContainerCloseTranslator
106106
// or - client hasn't yet loaded in; wait until inventory is shown
107-
GeyserImpl.getInstance().getLogger().debug(session, "Inventory (%s) set pending: closing inv? %s, pending inv id? %s", debugInventory(inventory), session.isClosingInventory(), session.getPendingInventoryId());
108107
inventory.setPending(true);
108+
GeyserImpl.getInstance().getLogger().debug(session, "Inventory (%s) set pending: closing inv? %s, pending inv id? %s", debugInventory(inventory), session.isClosingInventory(), session.getPendingOrCurrentBedrockInventoryId());
109109
return;
110110
}
111111
displayInventory(session, inventory);
@@ -119,32 +119,21 @@ public static void openInventory(GeyserSession session, Inventory inventory) {
119119
public static void openPendingInventory(GeyserSession session) {
120120
Inventory currentInventory = session.getOpenInventory();
121121
if (currentInventory == null || !currentInventory.isPending()) {
122+
session.setPendingOrCurrentBedrockInventoryId(-1);
122123
GeyserImpl.getInstance().getLogger().debug(session, "No pending inventory, not opening an inventory! Current inventory: %s", debugInventory(currentInventory));
123-
session.setPendingInventoryId(-1);
124124
return;
125125
}
126126

127127
// Current inventory isn't null! Let's see if we need to open it.
128-
if (currentInventory.isDelayed() && currentInventory.getBedrockId() == session.getPendingInventoryId()) {
128+
if (currentInventory.isDelayed() && currentInventory.getBedrockId() == session.getPendingOrCurrentBedrockInventoryId()) {
129129
GeyserImpl.getInstance().getLogger().debug(session, "Attempting to open currently delayed inventory with matching bedrock id! " + currentInventory.getBedrockId());
130130
openAndUpdateInventory(session, currentInventory);
131131
return;
132132
}
133133

134134
GeyserImpl.getInstance().getLogger().debug(session, "Opening any pending inventory! " + debugInventory(currentInventory));
135-
136-
session.setPendingInventoryId(-1);
137-
openInventory(session, currentInventory);
138-
}
139-
140-
public static boolean shouldQueueRejectedInventory(GeyserSession session) {
141-
Inventory currentInventory = session.getOpenInventory();
142-
if (currentInventory == null || !currentInventory.isDelayed() || currentInventory.getBedrockId() != session.getPendingInventoryId()) {
143-
GeyserImpl.getInstance().getLogger().debug(session, "Aborting NetworkStackLatency hack as the inventory has changed!");
144-
return false;
145-
}
146-
147-
return true;
135+
session.setPendingOrCurrentBedrockInventoryId(-1);
136+
displayInventory(session, currentInventory);
148137
}
149138

150139
/**
@@ -157,7 +146,7 @@ public static void displayInventory(GeyserSession session, Inventory inventory)
157146
if (translator.requiresOpeningDelay(session, inventory)) {
158147
inventory.setPending(true);
159148
inventory.setDelayed(true);
160-
session.setPendingInventoryId(inventory.getBedrockId());
149+
session.setPendingOrCurrentBedrockInventoryId(inventory.getBedrockId());
161150

162151
NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket();
163152
latencyPacket.setFromServer(true);
@@ -184,7 +173,6 @@ public static void openAndUpdateInventory(GeyserSession session, Inventory inven
184173
inventory.setDisplayed(true);
185174
inventory.setPending(false);
186175
inventory.setDelayed(false);
187-
session.setPendingInventoryId(-1);
188176
}
189177

190178
/**
@@ -221,6 +209,7 @@ public static void closeInventory(GeyserSession session, int javaId, boolean con
221209
GeyserImpl.getInstance().getLogger().debug(session, "Closed inventory: (java id: %s/bedrock id: %s), waiting on confirm? %s", inventory.getJavaId(), inventory.getBedrockId(), session.isClosingInventory());
222210
}
223211

212+
session.setPendingOrCurrentBedrockInventoryId(-1);
224213
session.setOpenInventory(null);
225214
}
226215

0 commit comments

Comments
 (0)