Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
fix: fix to closing inventories serverside while sending updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Jan 27, 2024
1 parent aec3694 commit f5727c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 deletions.
41 changes: 7 additions & 34 deletions src/main/java/net/minestom/server/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ public void onChunkBatchReceived(float newTargetChunksPerTick) {

/**
* Queues the given chunk to be sent to the player.
*
* @param chunk The chunk to send
*/
public void sendChunk(@NotNull Chunk chunk) {
Expand Down Expand Up @@ -1725,6 +1726,11 @@ public boolean openInventory(@NotNull Inventory inventory) {
* It closes the player inventory (when opened) if {@link #getOpenInventory()} returns null.
*/
public void closeInventory() {
closeInventory(false);
}

@ApiStatus.Internal
public void closeInventory(boolean fromClient) {
Inventory openInventory = getOpenInventory();

// Drop cursor item when closing inventory
Expand Down Expand Up @@ -1752,45 +1758,12 @@ public void closeInventory() {
openInventory.removeViewer(this); // Clear cache
this.openInventory = null;
}
sendPacket(closeWindowPacket);
if (!fromClient) sendPacket(closeWindowPacket);
inventory.update();
this.didCloseInventory = true;
}
}

/**
* Updates the player's inventory state, given a window id to compare against.
* Note: This should only be used when receiving a {@link net.minestom.server.network.packet.client.play.ClientCloseWindowPacket}.
* Use {@link #closeInventory()} instead for more general use.
* @param windowId The window id to compare against.
*/
public void updateCloseInventoryState(byte windowId) {
if (windowId == 0) {
// Put cursor item back in inventory if possible
ItemStack stack = getInventory().getCursorItem();
boolean success = getInventory().addItemStack(stack);
// Drop if inventory is full
if (!success) {
dropItem(stack);
}
getInventory().setCursorItem(ItemStack.AIR);
} else {
if (getOpenInventory() != null && windowId == getOpenInventory().getWindowId()) {
ItemStack stack = getOpenInventory().getCursorItem(this);
// Try to put item into our inventory
boolean success = getInventory().addItemStack(stack);
// Drop if inventory is full
if (!success) {
dropItem(stack);
}
getOpenInventory().setCursorItem(this, ItemStack.AIR);
} else {
// Something has gone wrong
logger.warn("Tried to update inventory state with invalid window id!");
}
}
}

/**
* Used internally to prevent an inventory click to be processed
* when the inventory listeners closed the inventory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.common.ClientPongPacket;
import net.minestom.server.network.packet.client.play.ClientClickWindowPacket;
import net.minestom.server.network.packet.client.play.ClientCloseWindowPacket;
import net.minestom.server.network.packet.client.common.ClientPongPacket;
import net.minestom.server.network.packet.server.common.PingPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket;

Expand Down Expand Up @@ -88,7 +88,7 @@ public static void closeWindowListener(ClientCloseWindowPacket packet, Player pl
InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player.getOpenInventory(), player);
EventDispatcher.call(inventoryCloseEvent);

player.updateCloseInventoryState(packet.windowId());
player.closeInventory(true);

Inventory newInventory = inventoryCloseEvent.getNewInventory();
if (newInventory != null)
Expand Down

0 comments on commit f5727c2

Please sign in to comment.