2021-06-11 12:02:28 +00:00
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/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
2022-08-14 08:03:13 +00:00
index 0b1676f71e8da09af3cc0db1c6c17059a6bcf60c..45038c25904d919e0b69deaecdc09272df05bab4 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
2022-07-27 19:49:24 +00:00
@@ -1159,7 +1159,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-07-11 07:01:29 +00:00
for (net.minecraft.world.level.block.entity.BlockEntity tileentity : chunk.getBlockEntities().values()) {
2021-06-11 12:02:28 +00:00
if (tileentity instanceof net.minecraft.world.Container) {
for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((net.minecraft.world.Container) tileentity).getViewers())) {
- h.closeInventory();
+ h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
2022-07-27 19:49:24 +00:00
@@ -2163,7 +2163,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-06-12 19:30:37 +00:00
// Spigot Start
2022-01-09 05:12:05 +00:00
if (entity.getBukkitEntity() instanceof org.bukkit.inventory.InventoryHolder && (!(entity instanceof ServerPlayer) || entity.getRemovalReason() != Entity.RemovalReason.KILLED)) { // SPIGOT-6876: closeInventory clears death message
2021-06-12 19:30:37 +00:00
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
}
2021-06-11 12:02:28 +00:00
}
2021-06-12 19:30:37 +00:00
// Spigot End
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2022-08-05 22:58:34 +00:00
index 4eb69b62b4c3f568cad83fdc4b19cbde2c588be7..35a6dd9cfed631f66e31c71911433ec880711905 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2022-07-27 19:49:24 +00:00
@@ -610,7 +610,7 @@ public class ServerPlayer extends Player {
2021-06-11 12:02:28 +00:00
}
// Paper end
if (!this.level.isClientSide && !this.containerMenu.stillValid(this)) {
- this.closeContainer();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper
this.containerMenu = this.inventoryMenu;
}
2022-07-27 19:49:24 +00:00
@@ -803,7 +803,7 @@ public class ServerPlayer extends Player {
2021-06-11 12:02:28 +00:00
// SPIGOT-943 - only call if they have an inventory open
if (this.containerMenu != this.inventoryMenu) {
- this.closeContainer();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DEATH); // Paper
}
net.kyori.adventure.text.Component deathMessage = event.deathMessage() != null ? event.deathMessage() : net.kyori.adventure.text.Component.empty(); // Paper - Adventure
2022-07-27 19:49:24 +00:00
@@ -1441,7 +1441,7 @@ public class ServerPlayer extends Player {
2021-06-11 12:02:28 +00:00
}
// CraftBukkit end
if (this.containerMenu != this.inventoryMenu) {
- this.closeContainer();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
// this.nextContainerCounter(); // CraftBukkit - moved up
2022-07-27 19:49:24 +00:00
@@ -1469,7 +1469,13 @@ public class ServerPlayer extends Player {
2021-06-11 12:02:28 +00:00
@Override
public void closeContainer() {
- CraftEventFactory.handleInventoryCloseEvent(this); // CraftBukkit
+ // Paper start
2021-06-12 19:30:37 +00:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
2021-06-11 12:02:28 +00:00
+ }
2021-06-12 19:30:37 +00:00
+ @Override
+ public void closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
2021-06-11 12:02:28 +00:00
+ CraftEventFactory.handleInventoryCloseEvent(this, reason); // CraftBukkit
+ // Paper end
this.connection.send(new ClientboundContainerClosePacket(this.containerMenu.containerId));
this.doCloseContainer();
}
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2022-08-14 08:03:13 +00:00
index 727350fb397e9be2917fa383dc6b8abeb15670e7..f7552186d8a5144c23d495bdc6a450e55d8f40d1 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2022-08-14 08:03:13 +00:00
@@ -219,6 +219,7 @@ import org.bukkit.event.inventory.ClickType;
2021-06-11 12:02:28 +00:00
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.event.inventory.InventoryCloseEvent; // Paper
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.inventory.SmithItemEvent;
2022-08-14 08:03:13 +00:00
@@ -2773,10 +2774,15 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
2021-06-11 12:02:28 +00:00
@Override
public void handleContainerClose(ServerboundContainerClosePacket packet) {
+ // Paper start
+ handleContainerClose(packet, InventoryCloseEvent.Reason.PLAYER);
+ }
2022-06-11 09:02:09 +00:00
+ public void handleContainerClose(ServerboundContainerClosePacket packet, InventoryCloseEvent.Reason reason) {
2021-06-11 12:02:28 +00:00
+ // Paper end
2022-06-11 09:02:09 +00:00
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.getLevel());
2021-06-11 12:02:28 +00:00
if (this.player.isImmobile()) return; // CraftBukkit
- CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
+ CraftEventFactory.handleInventoryCloseEvent(this.player, reason); // CraftBukkit // Paper
this.player.doCloseContainer();
}
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
2022-08-05 22:58:34 +00:00
index 8d46d3a77fa9717075dcfef44a42948c2f274182..04a41c3ab90fcdfaedb1646137fb5dde12a9331b 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2022-07-27 22:10:27 +00:00
@@ -507,7 +507,7 @@ public abstract class PlayerList {
2021-06-11 12:02:28 +00:00
// CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
// See SPIGOT-5799, SPIGOT-6145
if (entityplayer.containerMenu != entityplayer.inventoryMenu) {
- entityplayer.closeContainer();
+ entityplayer.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper
}
2022-06-09 08:51:45 +00:00
PlayerQuitEvent playerQuitEvent = new PlayerQuitEvent(entityplayer.getBukkitEntity(), net.kyori.adventure.text.Component.translatable("multiplayer.player.left", net.kyori.adventure.text.format.NamedTextColor.YELLOW, io.papermc.paper.configuration.GlobalConfiguration.get().messages.useDisplayNameInQuitMessage ? entityplayer.getBukkitEntity().displayName() : net.kyori.adventure.text.Component.text(entityplayer.getScoreboardName())));
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
2022-07-27 19:49:24 +00:00
index 702a5e524127f9655279a24b54b8d4248dec460e..2520ba136cf17392120f6187a73015f438c302f1 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
2022-07-27 19:49:24 +00:00
@@ -271,7 +271,7 @@ public abstract class Player extends LivingEntity {
2021-06-11 12:02:28 +00:00
this.updateIsUnderwater();
super.tick();
if (!this.level.isClientSide && this.containerMenu != null && !this.containerMenu.stillValid(this)) {
- this.closeContainer();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper
this.containerMenu = this.inventoryMenu;
}
2022-07-27 19:49:24 +00:00
@@ -495,6 +495,13 @@ public abstract class Player extends LivingEntity {
2021-06-12 19:30:37 +00:00
2021-06-11 12:02:28 +00:00
}
+ // Paper start - unused code, but to keep signatures aligned
2021-06-12 19:30:37 +00:00
+ public void closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
2021-06-11 12:02:28 +00:00
+ closeContainer();
+ this.containerMenu = this.inventoryMenu;
+ }
+ // Paper end
+
public void closeContainer() {
this.containerMenu = this.inventoryMenu;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2022-06-11 09:02:09 +00:00
index 4ff81744b7c9113f57cf1fa89bb943902711b2dc..404ed5e8f54d70a50de4232c6ea0f6163b34c2ab 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2022-06-10 01:20:47 +00:00
@@ -374,7 +374,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2021-06-12 19:30:37 +00:00
if (((ServerPlayer) this.getHandle()).connection == null) return;
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
2021-06-11 12:02:28 +00:00
// fire INVENTORY_CLOSE if one already open
2021-06-12 19:30:37 +00:00
- ((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId));
+ ((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId), org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
2021-06-11 12:02:28 +00:00
}
2021-06-12 19:30:37 +00:00
ServerPlayer player = (ServerPlayer) this.getHandle();
2021-06-11 12:02:28 +00:00
AbstractContainerMenu container;
2022-06-10 01:20:47 +00:00
@@ -444,8 +444,14 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2021-06-11 12:02:28 +00:00
@Override
public void closeInventory() {
2021-06-12 19:30:37 +00:00
- this.getHandle().closeContainer();
2021-06-11 12:02:28 +00:00
+ // Paper start
2021-07-30 09:37:11 +00:00
+ this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLUGIN);
2021-06-11 12:02:28 +00:00
}
2021-06-12 19:30:37 +00:00
+ @Override
2021-06-11 12:02:28 +00:00
+ public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ getHandle().closeContainer(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
2022-08-14 08:03:13 +00:00
index 7234e96d6d956d84fbcbcb321c1fb05906da6adb..69c3602abfddc9c065736cf08c32f88029df0e5c 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2022-07-28 02:07:26 +00:00
@@ -1079,7 +1079,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 12:02:28 +00:00
// Close any foreign inventory
2021-06-12 19:30:37 +00:00
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
- this.getHandle().closeContainer();
+ this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper
2021-06-11 12:02:28 +00:00
}
// 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
2022-06-20 17:12:05 +00:00
index e00f87e4a9384c60e5fe4c33a9f27541366ae81b..479835a1fb087e18a1098438f6a3d71cd5589001 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2022-06-20 17:12:05 +00:00
@@ -1225,7 +1225,7 @@ public class CraftEventFactory {
2021-06-11 12:02:28 +00:00
public static AbstractContainerMenu callInventoryOpenEvent(ServerPlayer player, AbstractContainerMenu container, boolean cancelled) {
if (player.containerMenu != player.inventoryMenu) { // fire INVENTORY_CLOSE if one already open
- player.connection.handleContainerClose(new ServerboundContainerClosePacket(player.containerMenu.containerId));
+ player.connection.handleContainerClose(new ServerboundContainerClosePacket(player.containerMenu.containerId), InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
CraftServer server = player.level.getCraftServer();
2022-06-20 17:12:05 +00:00
@@ -1391,8 +1391,18 @@ public class CraftEventFactory {
2021-06-11 12:02:28 +00:00
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(net.minecraft.world.entity.player.Player human) {
- InventoryCloseEvent event = new InventoryCloseEvent(human.containerMenu.getBukkitView());
+ handleInventoryCloseEvent(human, org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
+ }
+ public static void handleInventoryCloseEvent(net.minecraft.world.entity.player.Player human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ // Paper end
+ InventoryCloseEvent event = new InventoryCloseEvent(human.containerMenu.getBukkitView(), reason); // Paper
human.level.getCraftServer().getPluginManager().callEvent(event);
human.containerMenu.transferTo(human.inventoryMenu, human.getBukkitEntity());
}