Paper/Spigot-Server-Patches/0238-InventoryCloseEvent-Reason-API.patch

198 lines
11 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
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
index d94874b02ddcb640c9d94c99d83e77b2e55dce73..1249a7ae8fa8d6ea33cbc276717f1e4553f73b85 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -155,7 +155,7 @@ public abstract class EntityHuman extends EntityLiving {
this.es();
super.tick();
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;
}
@@ -350,6 +350,13 @@ public abstract class EntityHuman extends EntityLiving {
return 20;
}
+ // 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
index 253dac9aff57f4311f8c3134caad760992bd340b..e050f21fb74ff28e1f18894dc5c9452e2c280c45 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -411,7 +411,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
}
// 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;
}
@@ -585,7 +585,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
// 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
}
String deathMessage = event.getDeathMessage();
@@ -1165,7 +1165,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
return OptionalInt.empty();
} else {
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
this.nextContainerCounter();
@@ -1225,7 +1225,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
}
// CraftBukkit end
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
// this.nextContainerCounter(); // CraftBukkit - moved up
@@ -1289,7 +1289,12 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
@Override
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));
this.o();
}
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
index e78306cb187b5f08e32888ebd55c4c486fc8dad2..2cbb0e0e1046efeff7f417d0c976754bd0e7959b 100644
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
@@ -2079,7 +2079,7 @@ public class PlayerConnection implements PacketListenerPlayIn {
PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.getWorldServer());
if (this.player.isFrozen()) return; // CraftBukkit
- CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
+ CraftEventFactory.handleInventoryCloseEvent(this.player, org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLAYER); // CraftBukkit // Paper
this.player.o();
}
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
index 70339603aa3e256470be241bf033f3471e2090ea..ef1ea58c252d063ce282d244236a510655538a11 100644
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
@@ -419,7 +419,7 @@ public abstract class PlayerList {
entityplayer.a(StatisticList.LEAVE_GAME);
// CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
- entityplayer.closeInventory();
+ entityplayer.closeInventory(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);
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 0c677b8e1b1a103c7d727dba19fff1f74b7ed382..8a42f9ed3e1579b8b034de9321e5545505298d6b 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -967,7 +967,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
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
}
}
}
@@ -1025,7 +1025,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
// 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
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index 27b2ee740a705238995dd3bcea62b4bf74ec82dc..f6209e9c0b9e0ab99693025d23847bc62a06225d 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -435,8 +435,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
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
@Override
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
index ac621e2a6e7a2eaba5acc1dabca5d0bef4c8b2b3..0ba5b2a444660f280b673dbd0db9ded38fa6a839 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -789,7 +789,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
// 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
index 78475dd2c0f6cca94586536f12323d8080e05623..ec079f7d04f17cc82bd65aec5abfe269bbfb6922 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1319,12 +1319,22 @@ public class CraftEventFactory {
return event;
}
+ // Paper start
+ /**
+ * 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);
+ }
+ public static void handleInventoryCloseEvent(EntityHuman human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ // Paper end
// 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
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}