Paper/patches/server/0213-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 29044088ea933e2ba2c7f98d7a59c880a8d774fb..c7de5cd2cca4580ef7ff59763eae3c8d6eef209c 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
@@ -1475,7 +1475,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
}
}
}
@@ -2460,7 +2460,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 3014467f1c9159e04afd0bb816e02af6599f1f71..29f91654226ae349e310121c8fef20d9c1f6c183 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-12-05 23:12:48 +01:00
@@ -692,7 +692,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-12-05 23:12:48 +01:00
@@ -885,7 +885,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-12-05 23:12:48 +01:00
@@ -1509,7 +1509,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-12-05 23:12:48 +01:00
@@ -1537,7 +1537,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 8fb792d88852e78fded9f7ae2da26a1d9d09c959..6f6fee0ad12c8a251f86be0fbef32c005e354ee3 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
2023-12-05 23:12:48 +01:00
@@ -219,6 +219,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;
@@ -2538,10 +2539,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
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 782cc6d910efe5bc5498d0083afab42fad6c4fa2..49e3252d6af633d69bdf05ce71dc2a1b3d24f05a 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-12-05 23:12:48 +01:00
@@ -518,7 +518,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
}
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
2023-12-05 23:12:48 +01:00
index 8d40021ce31d5f8fd8a5462a80e92fb919b4b162..0d8261e15a1a66df17727788c387dca8fbe2ae84 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-12-05 23:12:48 +01:00
@@ -265,7 +265,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-09-22 00:01:00 +02:00
@@ -497,6 +497,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 f0194d4431821638e43a0f1b12b39f668ad147d1..21e3070a9aab9b4d8d352655d0145c9921857c0e 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 f929327522b98c12823ebbf94531c1c3681b1efa..701c8c8320f922c6227046d8275595279e5dba9a 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
@@ -1194,7 +1194,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 73963d1e17fa09f17913a2b9fdce0845949e3bec..fc9cf505f32b7cd5f7785314ce16d71c9236b640 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
@@ -1331,7 +1331,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();
@@ -1518,8 +1518,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());
}