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-06-27 05:12:11 +00:00
index 08141147f9795546e9397abed95834ed5e69a126..d9e5d71a87140c90b79902887bd2f481f02956dc 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
2020-06-25 14:09:55 +00:00
@@ -155,7 +155,7 @@ public abstract class EntityHuman extends EntityLiving {
this.es();
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;
}
2020-06-25 14:09:55 +00:00
@@ -350,6 +350,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-07-22 08:01:35 +00:00
index ef0f8a66eefefce5f78ff35833bfe45437885e0b..5053b1a09bd992b884a5871cb8f355ffbcb26260 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-06-30 05:20:29 +00:00
@@ -408,7 +408,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-06-30 05:20:29 +00:00
@@ -582,7 +582,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-06-30 05:20:29 +00:00
@@ -1163,7 +1163,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-06-30 05:20:29 +00:00
@@ -1223,7 +1223,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-06-30 05:20:29 +00:00
@@ -1287,7 +1287,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));
2020-06-25 14:09:55 +00:00
this.o();
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-06-30 05:20:29 +00:00
index 411e6ff17ac50a410da038ad538ad56ba3aef8a8..201b2b0dcbaf6765390c18052f3b3450e63066e4 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-06-28 01:54:05 +00:00
@@ -2080,7 +2080,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
2020-06-25 14:09:55 +00:00
this.player.o();
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-07-22 08:01:35 +00:00
index 628a5f109d6c44ba97876c42aefb5570deef6eb3..c4b0394d07b2af2ca14a68a300668197143e8fcc 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-06-27 05:12:11 +00:00
@@ -420,7 +420,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
2020-06-24 00:25:28 +00:00
- entityplayer.closeInventory();
+ entityplayer.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper
2018-07-04 01:57:33 +00:00
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-07-22 08:01:35 +00:00
index 5ccd9457f81dab2b2cb821960c0557c925623b50..f8139831254e676c2142a39858e8c0629943badc 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-07-14 10:58:55 +00:00
@@ -966,7 +966,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2020-05-12 05:27:03 +00: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 01:20:24 +00:00
}
}
2020-05-12 05:27:03 +00:00
}
2020-07-14 10:58:55 +00:00
@@ -1024,7 +1024,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2020-05-12 05:27:03 +00: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 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-07-01 23:41:58 +00:00
index fe8a21202340c5892f5166ec294212c6e44ed3a5..00d67c9911c52ddcdf48fda7998bcd2a8a35f0eb 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-07-01 23:41:58 +00:00
@@ -436,8 +436,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-07-22 08:01:35 +00:00
index 13d03e8fa783ac38ea2dd60529f395a3fd3ed96d..62e62226bc11df7de80cd1029c7d549596cd760d 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-07-22 08:01:35 +00:00
@@ -797,7 +797,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-07-03 06:08:00 +00:00
index 5bcf38d3fcfa569c50d66ed67ad22db7e7d36207..72f495327fe56e7d7ba41e39e99a69622a6861b2 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-07-03 06:08:00 +00:00
@@ -1334,12 +1334,22 @@ 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) {
+ 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) {
+ // Paper end
2020-06-24 00:25:28 +00:00
// SPIGOT-5799 - no need to fire for when no inventory open
if (human.activeContainer == human.defaultContainer) {
return;
}
- InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView());
+ InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason); // Paper
2018-07-04 01:57:33 +00:00
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}