diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index bee7edc69..3b67896ac 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1446,17 +1446,19 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } } - CloseWindowPacket closeWindowPacket; - if (openInventory == null) { - closeWindowPacket = new CloseWindowPacket((byte) 0); - } else { - closeWindowPacket = new CloseWindowPacket(openInventory.getWindowId()); - openInventory.removeViewer(this); // Clear cache - this.openInventory = null; + if (openInventory == getOpenInventory()) { + CloseWindowPacket closeWindowPacket; + if (openInventory == null) { + closeWindowPacket = new CloseWindowPacket((byte) 0); + } else { + closeWindowPacket = new CloseWindowPacket(openInventory.getWindowId()); + openInventory.removeViewer(this); // Clear cache + this.openInventory = null; + } + sendPacket(closeWindowPacket); + inventory.update(); + this.didCloseInventory = true; } - sendPacket(closeWindowPacket); - inventory.update(); - this.didCloseInventory = true; } /** diff --git a/src/test/java/net/minestom/server/inventory/InventoryIntegrationTest.java b/src/test/java/net/minestom/server/inventory/InventoryIntegrationTest.java index d204f3962..8ab1c0cd8 100644 --- a/src/test/java/net/minestom/server/inventory/InventoryIntegrationTest.java +++ b/src/test/java/net/minestom/server/inventory/InventoryIntegrationTest.java @@ -4,7 +4,7 @@ import net.kyori.adventure.text.Component; import net.minestom.server.api.Env; import net.minestom.server.api.EnvTest; import net.minestom.server.coordinate.Pos; -import net.minestom.server.entity.EquipmentSlot; +import net.minestom.server.event.item.ItemDropEvent; import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; import net.minestom.server.network.packet.server.play.EntityEquipmentPacket; @@ -12,9 +12,7 @@ import net.minestom.server.network.packet.server.play.SetSlotPacket; import net.minestom.server.network.packet.server.play.WindowItemsPacket; import org.junit.jupiter.api.Test; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; @EnvTest public class InventoryIntegrationTest { @@ -75,7 +73,7 @@ public class InventoryIntegrationTest { var connection = env.createConnection(); var player = connection.connect(instance, new Pos(0, 42, 0)).join(); assertEquals(instance, player.getInstance()); - + Inventory inventory = new Inventory(InventoryType.CHEST_6_ROW, Component.empty()); player.openInventory(inventory); assertEquals(inventory, player.getOpenInventory()); @@ -112,4 +110,39 @@ public class InventoryIntegrationTest { equipmentTracker.assertEmpty(); } + @Test + public void closeInventoryTest(Env env) { + var instance = env.createFlatInstance(); + var connection = env.createConnection(); + var player = connection.connect(instance, new Pos(0, 42, 0)).join(); + final var inventory = new Inventory(InventoryType.CHEST_1_ROW, "title"); + player.openInventory(inventory); + assertSame(inventory, player.getOpenInventory()); + player.closeInventory(); + assertNull(player.getOpenInventory()); + } + + @Test + public void openInventoryOnItemDropFromInventoryClosingTest(Env env) { + var instance = env.createFlatInstance(); + var connection = env.createConnection(); + var player = connection.connect(instance, new Pos(0, 42, 0)).join(); + var listener = env.listen(ItemDropEvent.class); + final var firstInventory = new Inventory(InventoryType.CHEST_1_ROW, "title"); + player.openInventory(firstInventory); + assertSame(firstInventory, player.getOpenInventory()); + firstInventory.setCursorItem(player, ItemStack.of(Material.STONE)); + + listener.followup(); + player.closeInventory(); + assertNull(player.getOpenInventory()); + + player.openInventory(firstInventory); + firstInventory.setCursorItem(player, ItemStack.of(Material.STONE)); + final var secondInventory = new Inventory(InventoryType.CHEST_1_ROW, "title"); + listener.followup(event -> event.getPlayer().openInventory(secondInventory)); + player.closeInventory(); + assertSame(secondInventory, player.getOpenInventory()); + } + }