2021-06-11 14:02:28 +02: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
2024-06-13 21:04:27 +02:00
index 5bf18ab5c397b32d880ea1827f62945d8d0e80ce..ce6be7aed7b392c3e0c851f3f6e1e216bccceaf5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
2024-06-13 21:04:27 +02:00
@@ -1239,7 +1239,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-07-11 09:01:29 +02:00
for (net.minecraft.world.level.block.entity.BlockEntity tileentity : chunk.getBlockEntities().values()) {
2021-06-11 14:02:28 +02:00
if (tileentity instanceof net.minecraft.world.Container) {
for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((net.minecraft.world.Container) tileentity).getViewers())) {
- h.closeInventory();
2024-01-21 19:37:09 +01:00
+ h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
}
}
2024-06-13 21:04:27 +02:00
@@ -2196,7 +2196,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-06-12 21:30:37 +02:00
// Spigot Start
2022-01-09 06:12:05 +01: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 21:30:37 +02:00
for (org.bukkit.entity.HumanEntity h : Lists.newArrayList(((org.bukkit.inventory.InventoryHolder) entity.getBukkitEntity()).getInventory().getViewers())) {
- h.closeInventory();
2024-01-21 19:37:09 +01:00
+ h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper - Inventory close reason
2021-06-12 21:30:37 +02:00
}
2021-06-11 14:02:28 +02:00
}
2021-06-12 21:30:37 +02:00
// Spigot End
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-06-14 19:15:52 +02:00
index ebedd613fa243c2caa4c3783b2bd3a7ad9c6644f..16f7278ce8d5b8c45026baeb017fdce1ed5945c8 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-06-13 21:04:27 +02:00
@@ -707,7 +707,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-06-11 14:02:28 +02:00
}
2024-01-24 11:45:17 +01:00
// Paper end - Configurable container update tick rate
2023-06-07 21:54:11 +02:00
if (!this.level().isClientSide && !this.containerMenu.stillValid(this)) {
2021-06-11 14:02:28 +02:00
- this.closeContainer();
2024-01-21 19:37:09 +01:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
this.containerMenu = this.inventoryMenu;
}
2024-06-13 21:04:27 +02:00
@@ -927,7 +927,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-06-11 14:02:28 +02:00
// SPIGOT-943 - only call if they have an inventory open
if (this.containerMenu != this.inventoryMenu) {
- this.closeContainer();
2024-01-21 19:37:09 +01:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DEATH); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
net.kyori.adventure.text.Component deathMessage = event.deathMessage() != null ? event.deathMessage() : net.kyori.adventure.text.Component.empty(); // Paper - Adventure
2024-06-13 21:04:27 +02:00
@@ -1594,7 +1594,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-06-11 14:02:28 +02:00
}
// CraftBukkit end
if (this.containerMenu != this.inventoryMenu) {
- this.closeContainer();
2024-01-21 19:37:09 +01:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
// this.nextContainerCounter(); // CraftBukkit - moved up
2024-06-13 21:04:27 +02:00
@@ -1624,7 +1624,13 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-06-11 14:02:28 +02:00
@Override
public void closeContainer() {
- CraftEventFactory.handleInventoryCloseEvent(this); // CraftBukkit
2024-01-21 19:37:09 +01:00
+ // Paper start - Inventory close reason
2021-06-12 21:30:37 +02:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
2021-06-11 14:02:28 +02:00
+ }
2021-06-12 21:30:37 +02:00
+ @Override
+ public void closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
2021-06-11 14:02:28 +02:00
+ CraftEventFactory.handleInventoryCloseEvent(this, reason); // CraftBukkit
2024-01-21 19:37:09 +01:00
+ // Paper end - Inventory close reason
2021-06-11 14:02:28 +02:00
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
2024-06-13 21:04:27 +02:00
index ecc2ce39629f6edbd09e96c85e14ab2196ac3173..75736ebe99e96a20c2c3e7ac5350ab55402fac81 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-06-13 21:04:27 +02:00
@@ -2619,10 +2619,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-11 14:02:28 +02:00
@Override
public void handleContainerClose(ServerboundContainerClosePacket packet) {
2024-01-21 19:37:09 +01:00
+ // Paper start - Inventory close reason
2024-02-01 10:15:57 +01:00
+ this.handleContainerClose(packet, org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLAYER);
2021-06-11 14:02:28 +02:00
+ }
2024-02-01 10:15:57 +01:00
+ public void handleContainerClose(ServerboundContainerClosePacket packet, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
2024-01-21 19:37:09 +01:00
+ // Paper end - Inventory close reason
2023-06-07 21:54:11 +02:00
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
2021-06-11 14:02:28 +02: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
2024-06-14 01:32:45 +02:00
index e959c7e45bffdcf46908a6919c07a0cdf9aa83bb..3cb56ccb3a42871488b76706d7a6edecd122c418 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2024-06-13 21:04:27 +02:00
@@ -510,7 +510,7 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02: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();
2024-01-21 19:37:09 +01:00
+ entityplayer.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
2023-09-22 00:01:00 +02: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() : io.papermc.paper.adventure.PaperAdventure.asAdventure(entityplayer.getDisplayName()))); // Paper - Adventure
2021-06-11 14:02:28 +02: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
2024-06-13 21:04:27 +02:00
index d4077e88910347fb332996ce88262a1a0577818f..28cbe9ac2b8d5a21dba11b4162d187a9333e1ddb 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
2024-06-13 21:04:27 +02:00
@@ -278,7 +278,7 @@ public abstract class Player extends LivingEntity {
2021-06-11 14:02:28 +02:00
this.updateIsUnderwater();
super.tick();
2023-06-07 21:54:11 +02:00
if (!this.level().isClientSide && this.containerMenu != null && !this.containerMenu.stillValid(this)) {
2021-06-11 14:02:28 +02:00
- this.closeContainer();
2024-01-21 19:37:09 +01:00
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
this.containerMenu = this.inventoryMenu;
}
2024-04-24 04:21:40 +02:00
@@ -496,6 +496,13 @@ public abstract class Player extends LivingEntity {
2021-06-12 21:30:37 +02:00
2021-06-11 14:02:28 +02:00
}
2024-01-21 19:37:09 +01:00
+ // Paper start - Inventory close reason; unused code, but to keep signatures aligned
2021-06-12 21:30:37 +02:00
+ public void closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
2021-06-11 14:02:28 +02:00
+ closeContainer();
+ this.containerMenu = this.inventoryMenu;
+ }
2024-01-21 19:37:09 +01:00
+ // Paper end - Inventory close reason
2021-06-11 14:02:28 +02:00
+
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
2024-04-24 04:21:40 +02:00
index dd3377a4f69e5ac10905e52d0eecc2427e72d856..c79607a2f45b7a487a95cf98b9b0eb6b36501410 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2024-04-24 04:21:40 +02:00
@@ -377,7 +377,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2021-06-12 21:30:37 +02:00
if (((ServerPlayer) this.getHandle()).connection == null) return;
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
2021-06-11 14:02:28 +02:00
// fire INVENTORY_CLOSE if one already open
2021-06-12 21:30:37 +02:00
- ((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId));
2024-01-21 19:37:09 +01:00
+ ((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId), org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
2021-06-12 21:30:37 +02:00
ServerPlayer player = (ServerPlayer) this.getHandle();
2021-06-11 14:02:28 +02:00
AbstractContainerMenu container;
2024-04-24 04:21:40 +02:00
@@ -447,8 +447,14 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2021-06-11 14:02:28 +02:00
@Override
public void closeInventory() {
2021-06-12 21:30:37 +02:00
- this.getHandle().closeContainer();
2024-01-21 19:37:09 +01:00
+ // Paper start - Inventory close reason
2021-07-30 11:37:11 +02:00
+ this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLUGIN);
2021-06-11 14:02:28 +02:00
}
2021-06-12 21:30:37 +02:00
+ @Override
2021-06-11 14:02:28 +02:00
+ public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ getHandle().closeContainer(reason);
+ }
2024-01-21 19:37:09 +01:00
+ // Paper end - Inventory close reason
2021-06-11 14:02:28 +02:00
@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
2024-06-14 16:01:00 +02:00
index 27298cd0558b1ff2ab2ca341d62da2922507414b..38bc1747d2010b9233c64de81eb1c042bd1357b5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-06-13 21:04:27 +02:00
@@ -1273,7 +1273,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 14:02:28 +02:00
// Close any foreign inventory
2021-06-12 21:30:37 +02:00
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
- this.getHandle().closeContainer();
2024-01-21 19:37:09 +01:00
+ this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper - Inventory close reason
2021-06-11 14:02:28 +02: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
2024-06-14 19:15:52 +02:00
index 86bddb9c39d8487f050d126dbb479315cb8a2818..8d86649579cb7eb90e3866ed81f2ba4f342f8b1c 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-06-14 19:15:52 +02:00
@@ -1264,7 +1264,7 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02: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));
2024-01-21 19:37:09 +01:00
+ player.connection.handleContainerClose(new ServerboundContainerClosePacket(player.containerMenu.containerId), InventoryCloseEvent.Reason.OPEN_NEW); // Paper - Inventory close reason
2021-06-11 14:02:28 +02:00
}
2023-06-07 21:54:11 +02:00
CraftServer server = player.level().getCraftServer();
2024-06-14 19:15:52 +02:00
@@ -1451,8 +1451,18 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02: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
2023-06-07 21:54:11 +02:00
human.level().getCraftServer().getPluginManager().callEvent(event);
2021-06-11 14:02:28 +02:00
human.containerMenu.transferTo(human.inventoryMenu, human.getBukkitEntity());
}