2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-07-04 03:57:33 +02:00
From: Aikar <aikar@aikar.co>
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
2021-01-29 01:32:08 +01:00
index 111ec5443116cbeecc7c9ecee3a585895d8376ec..4eea9bf6efb875cd7ba77958c9d3814237d8c74d 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
2021-01-29 01:32:08 +01:00
@@ -153,7 +153,7 @@ public abstract class EntityHuman extends EntityLiving {
2020-11-03 03:22:15 +01:00
this.et();
2018-07-19 01:16:19 +02:00
super.tick();
2018-07-04 03:57:33 +02:00
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;
}
2021-01-29 01:32:08 +01:00
@@ -348,6 +348,13 @@ public abstract class EntityHuman extends EntityLiving {
2019-04-30 03:20:24 +02:00
return 20;
2018-07-04 03:57:33 +02:00
}
+ // 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
2021-01-29 01:32:08 +01:00
index 2fafae87a59b4d5dfab251078d467f8a19b0f620..cadcf029f1b675f9e847d07f2d3eacce8b19ec96 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
2021-01-29 01:32:08 +01:00
@@ -416,7 +416,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 03:57:33 +02:00
}
// 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;
}
2021-01-29 01:32:08 +01:00
@@ -590,7 +590,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 03:20:24 +02:00
// 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
2018-07-04 03:57:33 +02:00
}
2019-04-30 03:20:24 +02:00
String deathMessage = event.getDeathMessage();
2021-01-29 01:32:08 +01:00
@@ -1160,7 +1160,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 03:20:24 +02:00
return OptionalInt.empty();
2018-07-04 03:57:33 +02:00
} else {
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2019-04-30 03:20:24 +02:00
this.nextContainerCounter();
2021-01-29 01:32:08 +01:00
@@ -1220,7 +1220,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 03:57:33 +02:00
}
// CraftBukkit end
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2019-04-30 03:20:24 +02:00
// this.nextContainerCounter(); // CraftBukkit - moved up
2021-01-29 01:32:08 +01:00
@@ -1284,7 +1284,12 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 03:57:33 +02:00
2019-04-30 03:20:24 +02:00
@Override
2018-07-04 03:57:33 +02:00
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));
2020-06-25 16:09:55 +02:00
this.o();
2018-07-04 03:57:33 +02:00
}
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-12-08 08:51:55 +01:00
index f92a1d69119af0c6baee83cd96c344695f23dc3c..7276807e0a22c640edc2c3e94c263f175f47d642 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-11-03 03:22:15 +01:00
@@ -43,6 +43,7 @@ import org.bukkit.event.inventory.ClickType;
2020-08-31 13:12:14 +02:00
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;
2020-12-08 08:51:55 +01:00
@@ -2161,10 +2162,15 @@ public class PlayerConnection implements PacketListenerPlayIn {
2020-08-31 13:12:14 +02:00
@Override
public void a(PacketPlayInCloseWindow packetplayinclosewindow) {
+ // Paper start
+ handleContainerClose(packetplayinclosewindow, InventoryCloseEvent.Reason.PLAYER);
+ }
+ public void handleContainerClose(PacketPlayInCloseWindow packetplayinclosewindow, InventoryCloseEvent.Reason reason) {
+ // Paper end
2018-07-19 01:16:19 +02:00
PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.getWorldServer());
2018-07-04 03:57:33 +02:00
if (this.player.isFrozen()) return; // CraftBukkit
- CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
2020-08-31 13:12:14 +02:00
+ CraftEventFactory.handleInventoryCloseEvent(this.player, reason); // CraftBukkit // Paper
2018-07-04 03:57:33 +02:00
2020-06-25 16:09:55 +02:00
this.player.o();
2018-07-04 03:57:33 +02:00
}
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
2020-11-18 04:45:18 +01:00
index 02479d398189f0956a6e2a8882f398e33853873e..020cd10518328b81495e44b816446c96bfe1bdcc 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
2020-10-01 15:16:32 +02:00
@@ -422,7 +422,7 @@ public abstract class PlayerList {
2018-07-04 03:57:33 +02:00
// CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
2020-10-01 15:16:32 +02:00
// See SPIGOT-5799, SPIGOT-6145
if (entityplayer.activeContainer != entityplayer.defaultContainer) {
- entityplayer.closeInventory();
+ entityplayer.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper
}
2018-07-04 03:57:33 +02:00
PlayerQuitEvent playerQuitEvent = new PlayerQuitEvent(cserver.getPlayer(entityplayer), "\u00A7e" + entityplayer.getName() + " left the game");
2019-04-30 03:20:24 +02:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2021-01-29 01:32:08 +01:00
index a1491094b58cf814d7b05d6c0ce4067bf34dae1a..a0d1e9f7535db896c0589dc02cb03bda23086167 100644
2019-04-30 03:20:24 +02:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2020-11-18 04:45:18 +01:00
@@ -1014,7 +1014,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2020-05-12 07:27:03 +02:00
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
2019-04-30 03:20:24 +02:00
}
}
2020-05-12 07:27:03 +02:00
}
2020-11-18 04:45:18 +01:00
@@ -1072,7 +1072,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2020-05-12 07:27:03 +02:00
// 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
2018-07-04 03:57:33 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2021-02-05 23:52:27 +01:00
index 99bd0b3f17dea43e85806fcad317ef8227d95715..08955d127859785bad49139c7f211f0ce7ea62ed 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2021-02-05 23:52:27 +01:00
@@ -369,7 +369,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2020-08-31 13:12:14 +02:00
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;
2021-02-05 23:52:27 +01:00
@@ -435,8 +435,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2018-07-04 03:57:33 +02:00
2019-05-06 04:58:04 +02:00
@Override
2018-07-04 03:57:33 +02:00
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
2019-05-06 04:58:04 +02:00
@Override
2018-07-04 03:57:33 +02:00
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
2021-02-05 23:52:27 +01:00
index 0fc25497e7605a04088f888aab99482bd7187ae5..290bd5f50618be0a9d1e10afa3642d47501d192d 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2021-02-05 23:52:27 +01:00
@@ -812,7 +812,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2018-07-04 03:57:33 +02:00
// 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
2021-02-05 23:52:27 +01:00
index 70a678dc8637897378f9924bae159233b181f817..44fd948d9ce23e541bf47c89ff3d36ac8da9e41a 100644
2018-07-04 03:57:33 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2021-02-05 23:52:27 +01:00
@@ -1165,7 +1165,7 @@ public class CraftEventFactory {
2020-08-31 13:12:14 +02:00
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();
2021-02-05 23:52:27 +01:00
@@ -1330,8 +1330,18 @@ public class CraftEventFactory {
2018-07-04 03:57:33 +02:00
return event;
}
+ // Paper start
2018-07-04 18:33:38 +02:00
+ /**
+ * 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) {
2020-10-01 15:16:32 +02:00
- InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView());
2018-07-04 18:33:38 +02:00
+ handleInventoryCloseEvent(human, org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
+ }
2018-07-04 03:57:33 +02:00
+ public static void handleInventoryCloseEvent(EntityHuman human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ // Paper end
2020-06-24 02:25:28 +02:00
+ InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason); // Paper
2018-07-04 03:57:33 +02:00
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}