2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-07-04 01:57:33 +00: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
2020-05-06 09:48:49 +00:00
index 6e9dd4d3717567f54ac706715d75bf53d48c5f7d..684978be7ccc401b71b0594828a7783b209a5210 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
2019-07-20 04:01:24 +00:00
@@ -164,7 +164,7 @@ public abstract class EntityHuman extends EntityLiving {
2019-12-11 23:43:22 +00:00
this.dW();
2018-07-18 23:16:19 +00:00
super.tick();
2018-07-04 01:57:33 +00: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;
}
2019-12-11 23:43:22 +00:00
@@ -363,6 +363,13 @@ public abstract class EntityHuman extends EntityLiving {
2019-04-30 01:20:24 +00:00
return 20;
2018-07-04 01:57:33 +00: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
2020-05-06 09:48:49 +00:00
index 88692d9eaea57a4d172d537a6cf2a3bffe058d54..f35d23340665ab323732915efc0c0ad7fe4d964d 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
2020-02-29 23:39:00 +00:00
@@ -368,7 +368,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 01:57:33 +00: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;
}
2020-02-29 23:39:00 +00:00
@@ -542,7 +542,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 01:20:24 +00: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 01:57:33 +00:00
}
2019-04-30 01:20:24 +00:00
String deathMessage = event.getDeathMessage();
2020-04-27 07:34:45 +00:00
@@ -1057,7 +1057,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 01:20:24 +00:00
return OptionalInt.empty();
2018-07-04 01:57:33 +00:00
} else {
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2019-04-30 01:20:24 +00:00
this.nextContainerCounter();
2020-04-27 07:34:45 +00:00
@@ -1117,7 +1117,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 01:57:33 +00:00
}
// CraftBukkit end
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2019-04-30 01:20:24 +00:00
// this.nextContainerCounter(); // CraftBukkit - moved up
2020-04-27 07:34:45 +00:00
@@ -1181,7 +1181,12 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-07-04 01:57:33 +00:00
2019-04-30 01:20:24 +00:00
@Override
2018-07-04 01:57:33 +00: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));
2018-07-18 23:16:19 +00:00
this.m();
2018-07-04 01:57:33 +00:00
}
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-05-06 09:48:49 +00:00
index 4a0a8479448b5872f356379d6f68d9142d85e175..8eb12e89ea84051281149d96ca82905bc4366ba4 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-02-09 00:32:48 +00:00
@@ -2040,7 +2040,7 @@ public class PlayerConnection implements PacketListenerPlayIn {
2018-07-18 23:16:19 +00:00
PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.getWorldServer());
2018-07-04 01:57:33 +00:00
if (this.player.isFrozen()) return; // CraftBukkit
- CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
+ CraftEventFactory.handleInventoryCloseEvent(this.player, org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLAYER); // CraftBukkit // Paper
2018-07-18 23:16:19 +00:00
this.player.m();
2018-07-04 01:57:33 +00:00
}
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
2020-05-06 09:48:49 +00:00
index a61815c794885d7192eec62d05a54e765fb8c838..5ae0927c14a1915b85c13c13c8cf3e6b3dddfe9d 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
2020-04-19 07:11:02 +00:00
@@ -390,7 +390,7 @@ public abstract class PlayerList {
2018-07-18 23:16:19 +00:00
entityplayer.a(StatisticList.LEAVE_GAME);
2018-07-04 01:57:33 +00:00
// CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
- org.bukkit.craftbukkit.event.CraftEventFactory.handleInventoryCloseEvent(entityplayer);
+ org.bukkit.craftbukkit.event.CraftEventFactory.handleInventoryCloseEvent(entityplayer, org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper
PlayerQuitEvent playerQuitEvent = new PlayerQuitEvent(cserver.getPlayer(entityplayer), "\u00A7e" + entityplayer.getName() + " left the game");
cserver.getPluginManager().callEvent(playerQuitEvent);
2019-04-30 01:20:24 +00:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2020-05-06 09:48:49 +00:00
index 60f936f2e50aaf3a894f3fd92c7619263cf27878..7c1a748c9b6982e09626cba60651a6de6a5796d4 100644
2019-04-30 01:20:24 +00:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2020-05-06 07:44:47 +00:00
@@ -1050,7 +1050,7 @@ public class WorldServer extends World {
2019-04-30 01:20:24 +00:00
{
if ( h instanceof org.bukkit.craftbukkit.entity.CraftHumanEntity )
{
- ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory();
+ ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
2020-05-06 07:44:47 +00:00
@@ -1073,7 +1073,7 @@ public class WorldServer extends World {
2019-04-30 01:20:24 +00:00
{
if ( h instanceof org.bukkit.craftbukkit.entity.CraftHumanEntity )
{
- ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory();
+ ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
2018-07-04 01:57:33 +00:00
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2020-05-06 09:48:49 +00:00
index 20f0783025516048851aedef0a70759c2953ae47..a6d75c0e07a25fdb59dde2e3eb2a0213c7112515 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2020-04-08 07:49:15 +00:00
@@ -467,8 +467,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2018-07-04 01:57:33 +00:00
2019-05-06 02:58:04 +00:00
@Override
2018-07-04 01:57:33 +00: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 02:58:04 +00:00
@Override
2018-07-04 01:57:33 +00: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
2020-05-06 09:48:49 +00:00
index c69b3b33d82bac830ea55d30961017339af6aa7e..ffbf2b983bb91c873f403b411a05c563feffeebf 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2020-04-08 07:49:15 +00:00
@@ -785,7 +785,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2018-07-04 01:57:33 +00: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
2020-05-06 09:48:49 +00:00
index 7593159fd6536468febae2ea92d31f7d76af09ba..fc3d9efd84fbe696852216f09500dc4082f49493 100644
2018-07-04 01:57:33 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2020-05-01 22:03:47 +00:00
@@ -1300,8 +1300,19 @@ public class CraftEventFactory {
2018-07-04 01:57:33 +00:00
return event;
}
+ // Paper start
2018-07-04 16:33:38 +00: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) {
- InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView());
+ handleInventoryCloseEvent(human, org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
+ }
2018-07-04 01:57:33 +00:00
+ public static void handleInventoryCloseEvent(EntityHuman human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason);
+ // Paper end
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}