Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.ChunkUtils;
import org.geysermc.geyser.util.EntityUtils;
import org.geysermc.geyser.util.InterruptibleFuture;
import org.geysermc.geyser.util.InventoryUtils;
import org.geysermc.geyser.util.LoginEncryptionUtils;
import org.geysermc.geyser.util.MathUtils;
Expand Down Expand Up @@ -372,6 +373,12 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
@Setter
private ScheduledFuture<?> containerOutputFuture;

/**
* Used to delay specific inventory transactions until we know that the
* client isn't actually about to close their inventory
*/
private final InterruptibleFuture inventoryTransactionFuture;

/**
* Stores session collision
*/
Expand Down Expand Up @@ -794,6 +801,8 @@ public GeyserSession(GeyserImpl geyser, BedrockServerSession bedrockServerSessio
this.collisionManager = new CollisionManager(this);
this.blockBreakHandler = new BlockBreakHandler(this);

this.inventoryTransactionFuture = new InterruptibleFuture(this);

this.playerEntity = new SessionPlayerEntity(this);
collisionManager.updatePlayerBoundingBox(this.playerEntity.getPosition());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.CraftResultsDeprecatedAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.DropAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.ItemStackRequestAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.PlaceAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.SwapAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.TransferItemStackRequestAction;
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponse;
Expand Down Expand Up @@ -88,6 +89,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static org.geysermc.geyser.translator.inventory.BundleInventoryTranslator.isBundle;

Expand Down Expand Up @@ -237,7 +239,7 @@ protected boolean shouldRejectItemPlace(GeyserSession session, Type inventory, C
}

/**
* Should be overrided if this request matches a certain criteria and shouldn't be treated normally.
* Should be overridden if this request matches a certain criteria and shouldn't be treated normally.
* E.G. anvil renaming or enchanting
*/
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Type inventory) {
Expand All @@ -252,7 +254,55 @@ protected ItemStackResponse translateSpecialRequest(GeyserSession session, Type
}

public final void translateRequests(GeyserSession session, Type inventory, List<ItemStackRequest> requests) {
this.translateRequests(session, inventory, requests, true);
}

private void translateRequests(GeyserSession session, Type inventory, List<ItemStackRequest> requests, boolean firstTime) {
boolean refresh = false;

// If we get another request, we're not closing the inventory and should run the queued request asap
// to "not fall behind" on transactions
session.getInventoryTransactionFuture().runCurrentIfPresent();

// Fixes https://github.com/GeyserMC/Geyser/issues/5258
// Bedrock edition has the quirk where it'll remove all items from the crafting inventory
// before closing it, which breaks some Java plugins...
boolean mustDelay = firstTime && requests.stream().allMatch(request -> {
if (request.getActions().length > 0) {
if (request.getActions()[0] instanceof PlaceAction action) {
return action.getSource().getContainerName().getContainer() == ContainerSlotType.CRAFTING_INPUT &&
action.getDestination().getContainerName().getContainer() == ContainerSlotType.HOTBAR_AND_INVENTORY;
}
}
return false;
});
Comment thread
onebeastchris marked this conversation as resolved.

if (mustDelay) {
// We cannot clearly differentiate shift-clicking a single stack...
// So we try to guess and delay the processing (at least 10 ticks) until we either get a new inventory transaction,
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly states "at least 10 ticks" when the actual delay is 750 milliseconds (approximately 37.5 ticks at 20 ticks/second). Either update the comment to match the actual delay (e.g., "at least 750ms" or "approximately 15 ticks"), or adjust the delay value if 10 ticks was the intended duration (500ms).

Suggested change
// So we try to guess and delay the processing (at least 10 ticks) until we either get a new inventory transaction,
// So we try to guess and delay the processing (at least 750ms, approximately 15 ticks) until we either get a new inventory transaction,

Copilot uses AI. Check for mistakes.
// or until we know the inventory wasn't closed
session.getInventoryTransactionFuture().schedule(() -> {
if (session.getPendingOrCurrentBedrockInventoryId() == inventory.getBedrockId()) {
translateRequests(session, inventory, requests, false);
} else {
ItemStackResponsePacket responsePacket = new ItemStackResponsePacket();
requests.forEach(request -> responsePacket.getEntries().add(rejectRequest(request, false)));
session.sendUpstreamPacket(responsePacket);

// Now update current inventory
if (session.getInventoryHolder() != null) {
session.getInventoryHolder().updateInventory();
} else {
session.getPlayerInventoryHolder().updateInventory();
}
}
},
750,
TimeUnit.MILLISECONDS
);
return;
}

ItemStackResponsePacket responsePacket = new ItemStackResponsePacket();
for (ItemStackRequest request : requests) {
ItemStackResponse response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void translate(GeyserSession session, ContainerClosePacket packet) {
}

session.setPendingOrCurrentBedrockInventoryId(-1);
session.getInventoryTransactionFuture().runCurrentIfPresent();

if (holder != null) {
// Send close confirmation to Java edition if container closing is client-initiated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2025 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.util;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.session.GeyserSession;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class InterruptibleFuture {

private final GeyserSession session;

private @Nullable ScheduledFuture<?> future = null;
private @Nullable Runnable runnable = null;
Comment on lines +38 to +39
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields future and runnable are not synchronized, which can lead to visibility issues in a multi-threaded environment. Since this class is used with scheduled tasks that may execute on different threads, these fields should be either volatile or accessed within synchronized blocks to ensure proper memory visibility.

Suggested change
private @Nullable ScheduledFuture<?> future = null;
private @Nullable Runnable runnable = null;
private volatile @Nullable ScheduledFuture<?> future = null;
private volatile @Nullable Runnable runnable = null;

Copilot uses AI. Check for mistakes.

public InterruptibleFuture(GeyserSession session) {
this.session = session;
}

public void runCurrentIfPresent() {
if (future != null && future.cancel(false) && runnable != null) {
runnable.run();
}
Comment on lines +45 to +48
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runCurrentIfPresent() method has a potential race condition. If the scheduled task starts executing between the cancel(false) call and the runnable.run() call, the runnable could be executed twice - once by the scheduler and once by this method. Consider using cancel(true) to interrupt the task or add synchronization to prevent concurrent execution.

Copilot uses AI. Check for mistakes.

future = null;
runnable = null;
}

public void schedule(Runnable runnable, long delay, TimeUnit unit) {
runCurrentIfPresent();
this.future = session.scheduleInEventLoop(this.runnable = runnable, delay, unit);
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment order in this chained expression could lead to a race condition. The expression session.scheduleInEventLoop(this.runnable = runnable, delay, unit) assigns to this.runnable after passing the runnable to the scheduler, but without proper synchronization, the scheduled task could potentially execute before the field assignment is visible. Consider separating the assignment: first assign this.runnable = runnable, then call this.future = session.scheduleInEventLoop(runnable, delay, unit).

Suggested change
this.future = session.scheduleInEventLoop(this.runnable = runnable, delay, unit);
this.runnable = runnable;
this.future = session.scheduleInEventLoop(runnable, delay, unit);

Copilot uses AI. Check for mistakes.
}
}
Loading