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
2020-08-25 04:22:08 +02:00
index 0f7e162b342ac589dc357508a1dcd1ab02d1fe3e..cc20b73a993d021789840ed2ee7ba1fc8310dc5a 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
2020-06-25 16:09:55 +02:00
@@ -155,7 +155,7 @@ public abstract class EntityHuman extends EntityLiving {
this.es();
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;
}
2020-06-25 16:09:55 +02:00
@@ -350,6 +350,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
2020-08-26 10:22:13 +02:00
index af3d0ffd6d8b3a7f794e5084551cd6d31df10fc1..a7dc4aabf10c9b9b10897e7540c4da9b0a2b6eb8 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
2020-08-25 04:22:08 +02:00
@@ -412,7 +412,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;
}
2020-08-25 04:22:08 +02:00
@@ -586,7 +586,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();
2020-08-26 10:22:13 +02:00
@@ -1139,7 +1139,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();
2020-08-26 10:22:13 +02:00
@@ -1199,7 +1199,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
2020-08-26 10:22:13 +02:00
@@ -1263,7 +1263,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-08-29 06:17:19 +02:00
index 922dfbf6963b9b4c02f056ff39900825c6ee071e..a96acce8b866b0976cc2077cbbfcd5d4fa17b3c3 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-08-29 06:17:19 +02:00
@@ -2070,7 +2070,7 @@ public class PlayerConnection implements PacketListenerPlayIn {
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
+ CraftEventFactory.handleInventoryCloseEvent(this.player, org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLAYER); // CraftBukkit // Paper
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-08-28 13:06:39 +02:00
index ea7527b16571cef5c285e898f5a9fd0c063948fa..df8a92b082139c84afc32cf02541faa82c914d4f 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-06-27 07:12:11 +02:00
@@ -420,7 +420,7 @@ public abstract class PlayerList {
2018-07-19 01:16:19 +02:00
entityplayer.a(StatisticList.LEAVE_GAME);
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-06-24 02:25:28 +02:00
- 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");
cserver.getPluginManager().callEvent(playerQuitEvent);
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
2020-08-25 04:22:08 +02:00
index f841621a7fe1a2364f821ceb661972d6b03b3b9a..d1699502eac193f25d2fc369526596a0b62574a3 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-08-25 04:22:08 +02:00
@@ -1005,7 +1005,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-08-25 04:22:08 +02:00
@@ -1063,7 +1063,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
2020-08-28 13:06:39 +02:00
index a1c318aa6501d9d0bdd53dc1fb8a99b7a782b4ce..51dce3aa8184b6f7566e29bf8e0285864f9b3a7f 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
2020-07-02 01:41:58 +02:00
@@ -436,8 +436,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
2020-08-25 04:22:08 +02:00
index 23889ce169baf956cfb39f1ec21a8369f80c5555..25175230583bbd6fcfc864dcbae4111dcc0d3b33 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
2020-08-25 04:22:08 +02:00
@@ -790,7 +790,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
2020-08-28 13:06:39 +02:00
index 81fc5bcb24e94500bea1a78a369f6fd58865700e..fe88f7e62e547b9aa4ff273341aaee9209aed82b 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
2020-08-28 13:06:39 +02:00
@@ -1335,12 +1335,22 @@ 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) {
+ 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
// 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 03:57:33 +02:00
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}