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.
This commit is contained in:
@@ -387,7 +387,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
|
||||
if (((ServerPlayer) this.getHandle()).connection == null) return;
|
||||
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
|
||||
// fire INVENTORY_CLOSE if one already open
|
||||
((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 - Inventory close reason
|
||||
}
|
||||
ServerPlayer player = (ServerPlayer) this.getHandle();
|
||||
AbstractContainerMenu container;
|
||||
@@ -457,8 +457,14 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
|
||||
|
||||
@Override
|
||||
public void closeInventory() {
|
||||
this.getHandle().closeContainer();
|
||||
// Paper start - Inventory close reason
|
||||
this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.PLUGIN);
|
||||
}
|
||||
@Override
|
||||
public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
|
||||
getHandle().closeContainer(reason);
|
||||
}
|
||||
// Paper end - Inventory close reason
|
||||
|
||||
@Override
|
||||
public boolean isBlocking() {
|
||||
|
||||
@@ -1278,7 +1278,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
|
||||
// Close any foreign inventory
|
||||
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
|
||||
this.getHandle().closeContainer();
|
||||
this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper - Inventory close reason
|
||||
}
|
||||
|
||||
// Check if the fromWorld and toWorld are the same.
|
||||
|
||||
@@ -1281,7 +1281,7 @@ public class CraftEventFactory {
|
||||
|
||||
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 - Inventory close reason
|
||||
}
|
||||
|
||||
CraftServer server = player.level().getCraftServer();
|
||||
@@ -1477,8 +1477,18 @@ 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(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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user