2019-10-20 11:03:39 +02:00
From b9340661c573f178bfcce53125e5a3346d06a830 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
2019-10-20 11:03:39 +02:00
index e1bd10579e..89915b3599 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
2019-07-20 06:01:24 +02:00
@@ -164,7 +164,7 @@ public abstract class EntityHuman extends EntityLiving {
this.dA();
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;
}
2019-07-20 06:01:24 +02:00
@@ -351,6 +351,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
2019-10-20 11:03:39 +02:00
index 88bc8b5312..1bb580e0e7 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
2019-06-25 03:47:58 +02:00
@@ -362,7 +362,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;
}
2019-07-23 21:17:32 +02:00
@@ -536,7 +536,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();
2019-07-23 21:17:32 +02:00
@@ -1058,7 +1058,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();
2019-10-13 07:28:46 +02:00
@@ -1118,7 +1118,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
2019-10-13 07:28:46 +02:00
@@ -1182,7 +1182,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));
2018-07-19 01:16:19 +02:00
this.m();
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
2019-10-20 11:03:39 +02:00
index 89beac63aa..b9793d4cc6 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
2019-07-20 06:01:24 +02:00
@@ -2025,7 +2025,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
2018-07-19 01:16:19 +02:00
this.player.m();
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
2019-10-20 11:03:39 +02:00
index a167843bfb..9e5cd22d7d 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
2019-05-14 04:20:58 +02:00
@@ -402,7 +402,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
- 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 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
2019-10-20 11:03:39 +02:00
index b9015cb8a7..6ca7c88583 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
2019-10-20 11:03:39 +02:00
@@ -1028,7 +1028,7 @@ public class WorldServer extends World {
2019-04-30 03:20:24 +02: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
}
}
}
2019-10-20 11:03:39 +02:00
@@ -1051,7 +1051,7 @@ public class WorldServer extends World {
2019-04-30 03:20:24 +02: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 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
2019-10-20 11:03:39 +02:00
index 77efc43a5d..81cf5f47a7 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
2019-07-23 21:17:32 +02:00
@@ -619,8 +619,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
2019-10-20 11:03:39 +02:00
index e610c8aaed..ce35e6bbc2 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
2019-06-25 03:47:58 +02:00
@@ -781,7 +781,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
2019-10-20 11:03:39 +02:00
index 8bd1dedcca..ade1d42f7e 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
2019-10-08 20:37:02 +02:00
@@ -1288,8 +1288,19 @@ 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) {
- InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView());
+ 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) {
+ InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason);
+ // Paper end
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}
--
2019-10-20 11:03:39 +02:00
2.22.1
2018-07-04 03:57:33 +02:00