mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2026-01-04 15:41:40 +00:00
Add PlayerInventoryOverflowEvent (#232)
* Add PlayerInventoryOverflowEvent * Add missing toggle * Move to correct package * Use handlerList to check instead of using explicit enable / isEnabled
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
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/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java b/src/main/java/org/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..44c65eb6c503b94ac73d2b2169359be1b4810c98
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/org/dreeam/leaf/event/player/PlayerInventoryOverflowEvent.java
|
||||
@@ -0,0 +1,63 @@
|
||||
+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();
|
||||
+
|
||||
+ 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;
|
||||
+ }
|
||||
+}
|
||||
@@ -0,0 +1,26 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
|
||||
Date: Wed, 19 Feb 2025 00:36:20 -0500
|
||||
Subject: [PATCH] PlayerInventoryOverflowEvent
|
||||
|
||||
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
|
||||
index 8b4f8a475faafe3b8a479160888145c4aa603a27..e97bb84d976229ba0d386efbade71be7347d0a1a 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
|
||||
@@ -340,6 +340,15 @@ public class CraftInventory implements Inventory {
|
||||
}
|
||||
}
|
||||
}
|
||||
+
|
||||
+ // Leaf start - PlayerInventoryOverflowEvent
|
||||
+ if (org.dreeam.leaf.event.player.PlayerInventoryOverflowEvent.getHandlerList().getRegisteredListeners().length > 0 && !leftover.isEmpty() && this.getHolder() instanceof org.bukkit.craftbukkit.entity.CraftPlayer craftPlayer) {
|
||||
+ new org.dreeam.leaf.event.player.PlayerInventoryOverflowEvent(craftPlayer, leftover).callEvent();
|
||||
+
|
||||
+ leftover = new HashMap<>();
|
||||
+ }
|
||||
+ // Leaf end - PlayerInventoryOverflowEvent
|
||||
+
|
||||
return leftover;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user