mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Do not send CloseWindowPacket if inventory was reopened during closing (#1218)
This commit is contained in:
parent
4feac211c7
commit
de5a396c15
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user