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

Commit

Permalink
Fix broken test and behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldenStack committed Feb 4, 2024
1 parent b75049f commit d20593e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
27 changes: 10 additions & 17 deletions src/main/java/net/minestom/server/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import net.minestom.server.instance.Instance;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.instance.block.Block;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
Expand Down Expand Up @@ -1707,22 +1706,17 @@ public void setBelowNameTag(BelowNameTag belowNameTag) {
return openInventory;
}

private boolean tryCloseInventory() {
private void tryCloseInventory(boolean fromClient) {
var closedInventory = getOpenInventory();
if (closedInventory != null) {
if (closedInventory.removeViewer(this)) {
if (closedInventory == getOpenInventory()) {
this.openInventory = null;
}
return true;
if (closedInventory == null) return;

didCloseInventory = fromClient;

if (closedInventory.removeViewer(this)) {
if (closedInventory == getOpenInventory()) {
this.openInventory = null;
}
} else {
// Don't remove it as a viewer, but pretend that it was
inventory.handleClose(this);
return true;
}

return false;
}

/**
Expand All @@ -1735,7 +1729,7 @@ public boolean openInventory(@NotNull Inventory inventory) {
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this);

EventDispatcher.callCancellable(inventoryOpenEvent, () -> {
tryCloseInventory();
tryCloseInventory(false);

Inventory newInventory = inventoryOpenEvent.getEventInventory();
if (newInventory.addViewer(this)) {
Expand All @@ -1755,9 +1749,8 @@ public void closeInventory() {

@ApiStatus.Internal
public void closeInventory(boolean fromClient) {
boolean result = tryCloseInventory();
tryCloseInventory(fromClient);
inventory.update();
this.didCloseInventory = result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public void handleClose(@NotNull Player player) {

setCursorItem(player, ItemStack.AIR);
getClickPreprocessor().clearCache(player);
player.sendPacket(new CloseWindowPacket(getWindowId()));
if (player.didCloseInventory()) {
player.UNSAFE_changeDidCloseInventory(false);
} else {
player.sendPacket(new CloseWindowPacket(getWindowId()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void doNotReceiveClosePacketFromServerWhenSendingClientCloseWindowPacket(
assertEquals(instance, player.getInstance());

var packetTracker = connection.trackIncoming(CloseWindowPacket.class);
var inventory = new Inventory(InventoryType.CHEST_2_ROW, Component.text("Test"));
var inventory = new ContainerInventory(InventoryType.CHEST_2_ROW, Component.text("Test"));
player.openInventory(inventory);
player.closeInventory(); // Closes the inventory server-side, should send a CloseWindowPacket
player.openInventory(inventory);
Expand Down

0 comments on commit d20593e

Please sign in to comment.