9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-api/paper-patches/features/0011-PlayerInventoryOverflowEvent.patch
Dreeam f1df5351ca Update PlayerInventoryOverflowEvent (#304)
* Add configurable option in config for PlayerInventoryOverflowEvent and able to define the class name of listener
* Update checking method for overflow items handling logic, only fire event when actual listener is listening to it.
2025-05-02 16:13:19 -04:00

106 lines
3.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Wed, 19 Feb 2025 00:34:16 -0500
Subject: [PATCH] PlayerInventoryOverflowEvent
diff --git a/src/main/java/org/bukkit/event/HandlerList.java b/src/main/java/org/bukkit/event/HandlerList.java
index 64d8916a8ca1cc5678a34c17a8bbbff45323beb0..0a1f989a35e0f2e878176e273e9f3b65b96bc67b 100644
--- a/src/main/java/org/bukkit/event/HandlerList.java
+++ b/src/main/java/org/bukkit/event/HandlerList.java
@@ -66,6 +66,7 @@ public class HandlerList {
h.handlers = null;
}
}
+ org.dreeam.leaf.event.player.PlayerInventoryOverflowEvent.isListeningInvOverflowCached = -1; // Leaf - PlayerInventoryOverflowEvent
}
}
@@ -79,6 +80,7 @@ public class HandlerList {
for (HandlerList h : allLists) {
h.unregister(plugin);
}
+ org.dreeam.leaf.event.player.PlayerInventoryOverflowEvent.isListeningInvOverflowCached = -1; // Leaf - PlayerInventoryOverflowEvent
}
}
@@ -92,6 +94,7 @@ public class HandlerList {
for (HandlerList h : allLists) {
h.unregister(listener);
}
+ org.dreeam.leaf.event.player.PlayerInventoryOverflowEvent.isListeningInvOverflowCached = -1; // Leaf - PlayerInventoryOverflowEvent
}
}
diff --git a/src/main/java/org/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java b/src/main/java/org/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..eae76671190ef84529c0dd503263e43a15a74e8a
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java
@@ -0,0 +1,65 @@
+package org.dreeam.leaf.event.player;
+
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.ItemStack;
+import org.jspecify.annotations.NullMarked;
+
+import java.util.Map;
+
+/**
+ * Called when a plugin uses {@link Inventory#addItem(ItemStack...)} to add items
+ * into player's inventory and the inventory becomes full.
+ * <p>
+ * Notice that, using this event is the worst option. It's better to handle
+ * those overflow / leftover items via {@link Inventory#addItem(ItemStack...)}
+ * logic directly.
+ * Also, See <a href="https://github.com/PaperMC/Paper/issues/11886">Paper#11886</a>
+ * for more.
+ */
+@NullMarked
+public class PlayerInventoryOverflowEvent extends PlayerEvent {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ public static short isListeningInvOverflowCached = -1;
+
+ private final Inventory inventory;
+ private final Map<Integer, ItemStack> overflowItemStacks;
+
+ public PlayerInventoryOverflowEvent(final Player player, final Map<Integer, ItemStack> overflowItemStacks) {
+ super(player);
+ this.inventory = player.getInventory();
+ this.overflowItemStacks = overflowItemStacks;
+ }
+
+ /**
+ * Gets player's inventory, which is full
+ *
+ * @return The player inventory.
+ */
+ public Inventory getPlayerInventory() {
+ return this.inventory;
+ }
+
+ /**
+ * Gets overflow items which cannot be added into
+ * the full inventory
+ *
+ * @return The overflow items.
+ */
+ public Map<Integer, ItemStack> getOverflowItemStacks() {
+ return this.overflowItemStacks;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+}