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/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java index cbffebfe323ff4ddec792c49ebc3391fc9ac35ac..73a33eff97d26e5b3542d787344013e18e52db68 100644 --- a/src/main/java/net/minecraft/server/EntityHuman.java +++ b/src/main/java/net/minecraft/server/EntityHuman.java @@ -155,7 +155,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; } @@ -350,6 +350,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/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java index 36ac4fde147fb5c4d26f1bdbcf027fb0bf3edca7..dd3ae707776626d54894d19d5b0968256104c4dd 100644 --- a/src/main/java/net/minecraft/server/EntityPlayer.java +++ b/src/main/java/net/minecraft/server/EntityPlayer.java @@ -415,7 +415,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; } @@ -589,7 +589,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 } String deathMessage = event.getDeathMessage(); @@ -1142,7 +1142,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(); @@ -1202,7 +1202,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 @@ -1266,7 +1266,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/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java index e72a519645e9383c989fc3ad8059c1c88c1b6382..bad3923c0ec5b99d4e102d61ad88321aa854350e 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java @@ -43,6 +43,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; @@ -2161,10 +2162,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/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java index 3fa1ea2958bec57d40b702bdbdc511d67b165d7e..129f4095cb98e1e4295b9a12105d06e239d96f6b 100644 --- a/src/main/java/net/minecraft/server/PlayerList.java +++ b/src/main/java/net/minecraft/server/PlayerList.java @@ -422,7 +422,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), "\u00A7e" + entityplayer.getName() + " left the game"); diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java index fde791f4b1d80d3dab7d5a91d644295a80297f34..79d0f4eff913560d718132b50f63a79ff365938d 100644 --- a/src/main/java/net/minecraft/server/WorldServer.java +++ b/src/main/java/net/minecraft/server/WorldServer.java @@ -1012,7 +1012,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { for (TileEntity tileentity : chunk.getTileEntities().values()) { if (tileentity instanceof IInventory) { for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((IInventory) tileentity).getViewers())) { - h.closeInventory(); + h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper } } } @@ -1070,7 +1070,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/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index a1c318aa6501d9d0bdd53dc1fb8a99b7a782b4ce..ebcd2d043b3e6cf90b62eda1ea7a2d8c01f1c7a8 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -370,7 +370,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; @@ -436,8 +436,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 df1896d447295123dbf7f8c8dea163be7849132f..18f0d23e38abb9c3e326a727cc4b4e040adb432d 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -813,7 +813,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 f4242870654ea6faa6829348717cd2f3c3407679..52762d75edd35dd3bc9668e3f7a3c79be279b783 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -1173,7 +1173,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(); @@ -1338,8 +1338,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()); }