-
-
Notifications
You must be signed in to change notification settings - Fork 823
Workaround for client-sided removal of all items from the crafting grid on inventory close #6008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||
|
|
@@ -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; | ||||||
|
|
||||||
|
|
@@ -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) { | ||||||
|
|
@@ -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; | ||||||
| }); | ||||||
|
|
||||||
| 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, | ||||||
|
||||||
| // 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, |
| 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
|
||||||||||
| private @Nullable ScheduledFuture<?> future = null; | |
| private @Nullable Runnable runnable = null; | |
| private volatile @Nullable ScheduledFuture<?> future = null; | |
| private volatile @Nullable Runnable runnable = null; |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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
AI
Nov 25, 2025
There was a problem hiding this comment.
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).
| this.future = session.scheduleInEventLoop(this.runnable = runnable, delay, unit); | |
| this.runnable = runnable; | |
| this.future = session.scheduleInEventLoop(runnable, delay, unit); |
Uh oh!
There was an error while loading. Please reload this page.