Paper/patches/server/0216-InventoryCloseEvent-Reason-API.patch

221 lines
13 KiB
Diff
Raw Normal View History

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
index 68257f257dd3b167e237482c8d149590103896b2..33ce550ea68d4862e0966ed827200cf426909d85 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
@@ -1449,7 +1449,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
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();
+ h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
@@ -2417,7 +2417,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-06-12 21:30:37 +02:00
// Spigot Start
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();
+ h.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
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
index dda3b911fcaa294ec7b8e9d696880fcb8325f69c..8e2340ca473c8cacfe9d888091834986bad101ce 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
2023-06-08 07:21:04 +02:00
@@ -682,7 +682,7 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
}
// Paper end
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();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper
this.containerMenu = this.inventoryMenu;
}
2023-06-08 07:21:04 +02:00
@@ -875,7 +875,7 @@ public class ServerPlayer extends 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();
+ 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
2023-06-08 07:21:04 +02:00
@@ -1494,7 +1494,7 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02: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
2023-06-08 07:21:04 +02:00
@@ -1522,7 +1522,13 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
@Override
public void closeContainer() {
- CraftEventFactory.handleInventoryCloseEvent(this); // CraftBukkit
+ // Paper start
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
+ // 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
index 5cac531de81a38fa79826e65477c5e31f9af35cd..925ef9f0f8b6d65a206c1b301f39a2571d3d4c82 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
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#9188) * Updated Upstream (Bukkit/CraftBukkit/Spigot) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 2fcba9b2 SPIGOT-7347: Add missing documentation and details to ShapedRecipe c278419d PR-854: Move getHighestBlockYAt methods from World to RegionAccessor 201399fb PR-853: Add API for directly setting Display transformation matrices ecfa559a PR-849: Add InventoryView#setTitle 653d7edb SPIGOT-519: Add TNTPrimeEvent 22fccc09 PR-846: Add method to get chunk load level a070a52c PR-844: Add methods to convert Vector to and from JOML vectors cc7111fe PR-276: Add accessors to Wither's invulnerability ticks 777d24e9 SPIGOT-7209: Accessors and events for player's exp cooldown ccb2d01b SPIGOT-6308: Deprecate the location name property of map items cd04a31b PR-780: Add PlayerSpawnChangeEvent 7d1f5b64 SPIGOT-6780: Improve documentation for World#spawnFallingBlock 5696668a SPIGOT-6885: Add test and easier to debug code for reference in yaml configuration comments 2e13cff7 PR-589: Expand the FishHook API 2c7d3da5 PR-279: Minor edits to various Javadocs CraftBukkit Changes: 01b2e1af4 SPIGOT-7346: Disallow players from executing commands after disconnecting 7fe5ee022 PR-1186: Move getHighestBlockYAt methods from World to RegionAccessor bcc85ef67 PR-1185: Add API for directly setting Display transformation matrices a7cfc778f PR-1176: Add InventoryView#setTitle 563d42226 SPIGOT-519: Add TNTPrimeEvent ccbc6abca Add test for Chunk.LoadLevel mirroring 2926e0513 PR-1171: Add method to get chunk load level 63cad7f84 PR-375: Add accessors to Wither's invulnerability ticks bfd8b1ac8 SPIGOT-7209: Accessors and events for player's exp cooldown f92a41c39 PR-1181: Consolidate Location conversion code 10f866759 SPIGOT-6308: Deprecate the location name property of map items 82f7b658a PR-1095: Add PlayerSpawnChangeEvent b421af7e4 PR-808: Expand the FishHook API 598ad7b3f Increase outdated build delay Spigot Changes: d1bd3bd2 Rebuild patches e4265cc8 SPIGOT-7297: Entity Tracking Range option for Display entities * Work around javac bug * Call PlayerSpawnChangeEvent * Updated Upstream (Bukkit/CraftBukkit/Spigot) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 2fcba9b2 SPIGOT-7347: Add missing documentation and details to ShapedRecipe c278419d PR-854: Move getHighestBlockYAt methods from World to RegionAccessor 201399fb PR-853: Add API for directly setting Display transformation matrices CraftBukkit Changes: 01b2e1af4 SPIGOT-7346: Disallow players from executing commands after disconnecting 7fe5ee022 PR-1186: Move getHighestBlockYAt methods from World to RegionAccessor bcc85ef67 PR-1185: Add API for directly setting Display transformation matrices Spigot Changes: 7da74dae Rebuild patches
2023-05-12 13:10:08 +02:00
@@ -220,6 +220,7 @@ import org.bukkit.event.inventory.ClickType;
2021-06-11 14:02:28 +02: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;
@@ -2682,10 +2683,15 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
2021-06-11 14:02:28 +02:00
@Override
public void handleContainerClose(ServerboundContainerClosePacket packet) {
+ // Paper start
2023-06-07 21:54:11 +02:00
+ this.handleContainerClose(packet, InventoryCloseEvent.Reason.PLAYER);
2021-06-11 14:02:28 +02:00
+ }
2022-06-11 11:02:09 +02:00
+ public void handleContainerClose(ServerboundContainerClosePacket packet, InventoryCloseEvent.Reason reason) {
2021-06-11 14:02:28 +02:00
+ // Paper end
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
index 875b08503fbbac256fad1b3f3d9c742bf1cdced2..e00c1916202a4e0550cc19c9cb279448a9fcdd1e 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
2023-06-07 21:54:11 +02:00
@@ -533,7 +533,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();
+ entityplayer.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper
}
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() : 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
index 734d00e84ec0bcef91a12546873de64e33e14d0c..c7bcbc3f0542a74c30e29bded3bfc0e94daeab25 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
2023-06-07 21:54:11 +02:00
@@ -269,7 +269,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();
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.CANT_USE); // Paper
this.containerMenu = this.inventoryMenu;
}
2023-06-07 21:54:11 +02:00
@@ -492,6 +492,13 @@ public abstract class Player extends LivingEntity {
2021-06-12 21:30:37 +02:00
2021-06-11 14:02:28 +02:00
}
+ // Paper start - 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;
+ }
+ // 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
index f59733b6918dcbbb2190f76d8b8841a2d8818cdf..ba6105a970381d642cd7955754cc47135207027a 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
@@ -376,7 +376,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));
+ ((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId), org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
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;
@@ -446,8 +446,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();
2021-06-11 14:02:28 +02:00
+ // Paper start
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);
+ }
+ // 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 2072778d96fc5616ec6e6b85b29585fbb43775bb..0561e8cf242d9da126779d3beba46cbc7d59a7a0 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
@@ -1182,7 +1182,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();
+ this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper
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
index 8e8401dec4d3157a05216b790db5481ec8e0495a..6cf79ea58448853a6e8f26609b1b8987d30588a6 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
@@ -1273,7 +1273,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));
+ player.connection.handleContainerClose(new ServerboundContainerClosePacket(player.containerMenu.containerId), InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2023-06-07 21:54:11 +02:00
CraftServer server = player.level().getCraftServer();
@@ -1447,8 +1447,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());
}