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

198 lines
10 KiB
Diff
Raw Normal View History

From f49837ffa30f83b202f8586e3ba022e4d7f9b29b 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
2019-05-14 04:20:58 +02:00
index f5dac5082..d1a7a873b 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
2019-04-30 03:20:24 +02:00
@@ -149,7 +149,7 @@ public abstract class EntityHuman extends EntityLiving {
this.dA();
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;
}
2019-04-30 03:20:24 +02:00
@@ -336,6 +336,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
2019-05-14 04:20:58 +02:00
index 29ebee2d9..d46ddba34 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
2019-05-06 04:58:04 +02:00
@@ -360,7 +360,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;
}
2019-05-06 04:58:04 +02:00
@@ -528,7 +528,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 03:20:24 +02:00
// 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
}
2019-04-30 03:20:24 +02:00
String deathMessage = event.getDeathMessage();
2019-05-06 04:58:04 +02:00
@@ -1053,7 +1053,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 03:20:24 +02:00
return OptionalInt.empty();
} else {
if (this.activeContainer != this.defaultContainer) {
- this.closeInventory();
+ this.closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper
}
2019-04-30 03:20:24 +02:00
this.nextContainerCounter();
@@ -1106,7 +1106,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
}
2019-04-30 03:20:24 +02:00
// this.nextContainerCounter(); // CraftBukkit - moved up
@@ -1170,7 +1170,12 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2019-04-30 03:20:24 +02:00
@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.m();
}
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
index 8be09b1d0..0385eced6 100644
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
@@ -2050,7 +2050,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.m();
}
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
2019-05-14 04:20:58 +02:00
index 5ac4a7c28..006f7b8b9 100644
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
2019-05-14 04:20:58 +02:00
@@ -402,7 +402,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
- org.bukkit.craftbukkit.event.CraftEventFactory.handleInventoryCloseEvent(entityplayer);
+ org.bukkit.craftbukkit.event.CraftEventFactory.handleInventoryCloseEvent(entityplayer, 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);
2019-04-30 03:20:24 +02:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2019-05-14 04:20:58 +02:00
index 8bc0a8cca..067379252 100644
2019-04-30 03:20:24 +02:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2019-05-14 04:20:58 +02:00
@@ -981,7 +981,7 @@ public class WorldServer extends World {
2019-04-30 03:20:24 +02:00
{
if ( h instanceof org.bukkit.craftbukkit.entity.CraftHumanEntity )
{
- ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory();
+ ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
2019-05-14 04:20:58 +02:00
@@ -1004,7 +1004,7 @@ public class WorldServer extends World {
2019-04-30 03:20:24 +02:00
{
if ( h instanceof org.bukkit.craftbukkit.entity.CraftHumanEntity )
{
- ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory();
+ ( (org.bukkit.craftbukkit.entity.CraftHumanEntity) h).getHandle().closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNLOADED); // Paper
}
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index d0a019f50..79fc638a0 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2019-05-06 04:58:04 +02:00
@@ -607,8 +607,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2019-05-06 04:58:04 +02:00
@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
2019-05-06 04:58:04 +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
2019-05-14 04:20:58 +02:00
index be7c5d174..ba60ece45 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2019-05-06 04:58:04 +02:00
@@ -771,7 +771,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 ef62edec4..dc83a90a4 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1241,8 +1241,19 @@ 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) {
- InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView());
+ handleInventoryCloseEvent(human, org.bukkit.event.inventory.InventoryCloseEvent.Reason.UNKNOWN);
+ }
+ public static void handleInventoryCloseEvent(EntityHuman human, org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
+ InventoryCloseEvent event = new InventoryCloseEvent(human.activeContainer.getBukkitView(), reason);
+ // Paper end
human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
}
--
2.21.0