From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Tue, 3 Jul 2018 21:56:23 -0400 Subject: [PATCH] InventoryCloseEvent Reason API Allows you to determine why an inventory was closed, enabling plugin developers to "confirm" things based on if it was player triggered close or not. diff --git a/src/main/java/net/minecraft/server/level/EntityPlayer.java b/src/main/java/net/minecraft/server/level/EntityPlayer.java index 8b79d547a3296f056731cbb66508494d84809e94..b3c5a22bf2eb360399ebfbf90fc74843dcd78509 100644 --- a/src/main/java/net/minecraft/server/level/EntityPlayer.java +++ b/src/main/java/net/minecraft/server/level/EntityPlayer.java @@ -546,7 +546,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting { } // Paper end if (!this.world.isClientSide && !this.activeContainer.canUse(this)) { - this.closeInventory(); + this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper this.activeContainer = this.defaultContainer; } @@ -719,7 +719,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting { // SPIGOT-943 - only call if they have an inventory open if (this.activeContainer != this.defaultContainer) { - this.closeInventory(); + this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DEATH); // Paper } net.kyori.adventure.text.Component deathMessage = event.deathMessage() != null ? event.deathMessage() : net.kyori.adventure.text.Component.empty(); // Paper - Adventure @@ -1284,7 +1284,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting { return OptionalInt.empty(); } else { if (this.activeContainer != this.defaultContainer) { - this.closeInventory(); + this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper } this.nextContainerCounter(); @@ -1344,7 +1344,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting { } // CraftBukkit end if (this.activeContainer != this.defaultContainer) { - this.closeInventory(); + this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper } // this.nextContainerCounter(); // CraftBukkit - moved up @@ -1408,7 +1408,12 @@ public class EntityPlayer extends EntityHuman implements ICrafting { @Override public void closeInventory() { - CraftEventFactory.handleInventoryCloseEvent(this); // CraftBukkit + // Paper start + closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN); + } + public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) { + CraftEventFactory.handleInventoryCloseEvent(this, reason); // CraftBukkit + // Paper end this.playerConnection.sendPacket(new PacketPlayOutCloseWindow(this.activeContainer.windowId)); this.o(); } diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java index 3876958f20e675c4c8fd505c5fb333ccb9cdc8c9..eb909ef3a34045480034bd7d7f3e5ac674f664b5 100644 --- a/src/main/java/net/minecraft/server/level/WorldServer.java +++ b/src/main/java/net/minecraft/server/level/WorldServer.java @@ -1123,7 +1123,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { for (TileEntity tileentity : chunk.getTileEntities().values()) { if (tileentity instanceof net.minecraft.world.IInventory) { for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((net.minecraft.world.IInventory) tileentity).getViewers())) { - h.closeInventory(); + h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper } } } @@ -1181,7 +1181,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { // Spigot Start if (entity.getBukkitEntity() instanceof org.bukkit.inventory.InventoryHolder) { for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((org.bukkit.inventory.InventoryHolder) entity.getBukkitEntity()).getInventory().getViewers())) { - h.closeInventory(); + h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper } } // Spigot End diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java index 415f34109019e68638f76fefa52bd8d40c449e12..c4f14fabc83a9cb13f8f82b651119e0ac7d8aa02 100644 --- a/src/main/java/net/minecraft/server/network/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java @@ -190,6 +190,7 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; // Paper import org.bukkit.event.inventory.InventoryCreativeEvent; import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -2308,10 +2309,15 @@ public class PlayerConnection implements PacketListenerPlayIn { @Override public void a(PacketPlayInCloseWindow packetplayinclosewindow) { + // Paper start + handleContainerClose(packetplayinclosewindow, InventoryCloseEvent.Reason.PLAYER); + } + public void handleContainerClose(PacketPlayInCloseWindow packetplayinclosewindow, InventoryCloseEvent.Reason reason) { + // Paper end PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.getWorldServer()); if (this.player.isFrozen()) return; // CraftBukkit - CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit + CraftEventFactory.handleInventoryCloseEvent(this.player, reason); // CraftBukkit // Paper this.player.o(); } diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java index 41b63da42067397122897d23fd7fb0e517d7c552..2939a8186254a39ef20d778421b0ed984bb20a32 100644 --- a/src/main/java/net/minecraft/server/players/PlayerList.java +++ b/src/main/java/net/minecraft/server/players/PlayerList.java @@ -493,7 +493,7 @@ public abstract class PlayerList { // CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it // See SPIGOT-5799, SPIGOT-6145 if (entityplayer.activeContainer != entityplayer.defaultContainer) { - entityplayer.closeInventory(); + entityplayer.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper } PlayerQuitEvent playerQuitEvent = new PlayerQuitEvent(cserver.getPlayer(entityplayer), net.kyori.adventure.text.Component.translatable("multiplayer.player.left", net.kyori.adventure.text.format.NamedTextColor.YELLOW, com.destroystokyo.paper.PaperConfig.useDisplayNameInQuit ? entityplayer.getBukkitEntity().displayName() : net.kyori.adventure.text.Component.text(entityplayer.getName()))); diff --git a/src/main/java/net/minecraft/world/entity/player/EntityHuman.java b/src/main/java/net/minecraft/world/entity/player/EntityHuman.java index e9dd1ee6dbc8baff395c69e2cb1d7b3a4551f90a..5f92ba350658c7f1cac9111f06d95e6ffc5600c3 100644 --- a/src/main/java/net/minecraft/world/entity/player/EntityHuman.java +++ b/src/main/java/net/minecraft/world/entity/player/EntityHuman.java @@ -249,7 +249,7 @@ public abstract class EntityHuman extends EntityLiving { this.et(); super.tick(); if (!this.world.isClientSide && this.activeContainer != null && !this.activeContainer.canUse(this)) { - this.closeInventory(); + this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper this.activeContainer = this.defaultContainer; } @@ -444,6 +444,13 @@ public abstract class EntityHuman extends EntityLiving { return 20; } + // Paper start - unused code, but to keep signatures aligned + public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) { + closeInventory(); + this.activeContainer = this.defaultContainer; + } + // Paper end + public void closeInventory() { this.activeContainer = this.defaultContainer; } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index a3396a19ba60daeb7240f23831e4bc28631098cc..2ca9d061ec951fb0563959620f0e99590e3038af 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -372,7 +372,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { if (((EntityPlayer) getHandle()).playerConnection == null) return; if (getHandle().activeContainer != getHandle().defaultContainer) { // fire INVENTORY_CLOSE if one already open - ((EntityPlayer) getHandle()).playerConnection.a(new PacketPlayInCloseWindow(getHandle().activeContainer.windowId)); + ((EntityPlayer) getHandle()).playerConnection.handleContainerClose(new PacketPlayInCloseWindow(getHandle().activeContainer.windowId), org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper } EntityPlayer player = (EntityPlayer) getHandle(); Container container; @@ -442,8 +442,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { @Override public void closeInventory() { - getHandle().closeInventory(); + // Paper start + getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLUGIN); } + public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) { + getHandle().closeInventory(reason); + } + // Paper end @Override public boolean isBlocking() { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 5a9b23fd4a414c52ce4b1ccb8696a4408b0b7c90..1851f261ce8e454cbe4012c0c4ea6a69e926daa0 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -895,7 +895,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { // Close any foreign inventory if (getHandle().activeContainer != getHandle().defaultContainer) { - getHandle().closeInventory(); + getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper } // Check if the fromWorld and toWorld are the same. diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 8f60cc5bf1583b48c6052f7598e99b4c63203852..681eb48c568029d6e3c5fa140a2c330a5f27df6f 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -1166,7 +1166,7 @@ public class CraftEventFactory { public static Container callInventoryOpenEvent(EntityPlayer player, Container container, boolean cancelled) { if (player.activeContainer != player.defaultContainer) { // fire INVENTORY_CLOSE if one already open - player.playerConnection.a(new PacketPlayInCloseWindow(player.activeContainer.windowId)); + player.playerConnection.handleContainerClose(new PacketPlayInCloseWindow(player.activeContainer.windowId), InventoryCloseEvent.Reason.OPEN_NEW); // Paper } CraftServer server = player.world.getServer(); @@ -1331,8 +1331,18 @@ public class CraftEventFactory { return event; } + // Paper start + /** + * Incase plugins hooked into this or Spigot adds a new inventory close event. Prefer to pass a reason + * @param human + */ + @Deprecated public static void handleInventoryCloseEvent(EntityHuman human) { - InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView()); + handleInventoryCloseEvent(human, org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN); + } + public static void handleInventoryCloseEvent(EntityHuman human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) { + // Paper end + InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason); // Paper human.world.getServer().getPluginManager().callEvent(event); human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity()); }