From bcb7401c74883a08bde92e44dced2bc55096d283 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 17 Nov 2022 15:10:05 +0000 Subject: [PATCH] Fixed captive slot changes PR --- .../willfp/eco/core/gui/slot/CustomSlot.java | 16 +++----------- .../eco/core/gui/slot/ReactiveSlot.java | 8 +------ .../com/willfp/eco/core/gui/slot/Slot.java | 11 +++++----- .../willfp/eco/core/gui/slot/SlotBuilder.java | 10 ++++++--- .../com/willfp/eco/core/gui/GUIHelpers.kt | 6 ++--- .../eco/internal/gui/slot/EcoCaptiveSlot.kt | 20 +++++------------ .../eco/internal/gui/slot/EcoSlotBuilder.kt | 22 ++++--------------- .../eco/internal/spigot/gui/GUIListener.kt | 2 +- 8 files changed, 31 insertions(+), 64 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/CustomSlot.java b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/CustomSlot.java index 8ace01c0..b646759f 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/CustomSlot.java +++ b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/CustomSlot.java @@ -42,23 +42,13 @@ public abstract class CustomSlot implements Slot { @Override public boolean isCaptive(@NotNull final Player player, - @NotNull final Menu menu) { - if (delegate == null) { - throw new IllegalStateException("Custom Slot was not initialized!"); - } - - return delegate.isCaptive(player, menu); - } - - @Override - public boolean isCaptiveForItem(@NotNull final Player player, @NotNull final Menu menu, @Nullable final ItemStack item) { if (delegate == null) { throw new IllegalStateException("Custom Slot was not initialized!"); } - return delegate.isCaptiveForItem(player, menu, item); + return delegate.isCaptive(player, menu, item); } @Override @@ -71,8 +61,8 @@ public abstract class CustomSlot implements Slot { } @Override - public final Slot getActionableSlot(@NotNull final Player player, - @NotNull final Menu menu) { + public final @NotNull Slot getActionableSlot(@NotNull final Player player, + @NotNull final Menu menu) { return delegate; } diff --git a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/ReactiveSlot.java b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/ReactiveSlot.java index cc698f6b..8bbb54cf 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/ReactiveSlot.java +++ b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/ReactiveSlot.java @@ -36,15 +36,9 @@ public abstract class ReactiveSlot implements Slot { @Override public boolean isCaptive(@NotNull final Player player, - @NotNull final Menu menu) { - return getSlot(player, menu).isCaptive(player, menu); - } - - @Override - public boolean isCaptiveForItem(@NotNull final Player player, @NotNull final Menu menu, @Nullable final ItemStack item) { - return getSlot(player, menu).isCaptiveForItem(player, menu, item); + return getSlot(player, menu).isCaptive(player, menu, item); } @Override diff --git a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/Slot.java b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/Slot.java index 1e5d8a17..a68398c8 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/Slot.java +++ b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/Slot.java @@ -47,17 +47,18 @@ public interface Slot extends GUIComponent { } /** - * If the slot is captive for this item. (Can this item be placed in it). + * If the slot is captive for a given item (can this item be placed in it). * - * @param player The player. - * @param menu The menu. + * @param player The player. + * @param menu The menu. * @param itemStack The item. * @return If captive. */ - default boolean isCaptiveForItem(@NotNull final Player player, + default boolean isCaptive(@NotNull final Player player, @NotNull final Menu menu, @Nullable final ItemStack itemStack) { - return false; + // Delegate to no-item version for backwards compatibility. + return this.isCaptive(player, menu); } /** diff --git a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/SlotBuilder.java b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/SlotBuilder.java index ca5feba8..f726e760 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/gui/slot/SlotBuilder.java +++ b/eco-api/src/main/java/com/willfp/eco/core/gui/slot/SlotBuilder.java @@ -146,15 +146,19 @@ public interface SlotBuilder { * @param predicate The predicate. Returns true when the slot should not be captive. * @return The builder. */ - SlotBuilder notCaptiveFor(Predicate predicate); + default SlotBuilder notCaptiveFor(@NotNull final Predicate predicate) { + return this.notCaptiveFor((player, itemStack) -> predicate.test(player)); + } /** - * Prevent captive for players that match a predicate. + * Prevent captive for players and items that match a predicate. * * @param predicate The predicate. Returns true when the slot should not be captive. * @return The builder. */ - SlotBuilder notCaptiveForItem(BiPredicate predicate); + default SlotBuilder notCaptiveFor(@NotNull BiPredicate predicate) { + return this; + } /** * Set the ItemStack updater. diff --git a/eco-api/src/main/kotlin/com/willfp/eco/core/gui/GUIHelpers.kt b/eco-api/src/main/kotlin/com/willfp/eco/core/gui/GUIHelpers.kt index 2ce74786..a82429f0 100644 --- a/eco-api/src/main/kotlin/com/willfp/eco/core/gui/GUIHelpers.kt +++ b/eco-api/src/main/kotlin/com/willfp/eco/core/gui/GUIHelpers.kt @@ -72,11 +72,11 @@ fun SlotBuilder.onClick(clickType: ClickType, action: (Player, InventoryClickEve /** @see SlotBuilder.notCaptiveFor */ fun SlotBuilder.notCaptiveFor(test: (Player) -> Boolean): SlotBuilder = - this.notCaptiveFor { test(it) } + this.notCaptiveFor { t, _ -> test(t) } /** @see SlotBuilder.notCaptiveFor */ -fun SlotBuilder.notCaptiveForItem(test: (Player, ItemStack?) -> Boolean): SlotBuilder = - this.notCaptiveForItem { player, item -> test(player, item) } +fun SlotBuilder.notCaptiveFor(test: (Player, ItemStack?) -> Boolean): SlotBuilder = + this.notCaptiveFor { player, item -> test(player, item) } /** * @see SlotBuilder.setModifier diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoCaptiveSlot.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoCaptiveSlot.kt index 75aa6483..a84468a2 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoCaptiveSlot.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoCaptiveSlot.kt @@ -11,21 +11,16 @@ import org.bukkit.inventory.ItemStack class EcoCaptiveSlot( provider: SlotProvider, private val captiveFromEmpty: Boolean, - private val notCaptiveFor: (Player) -> Boolean, - private val notCaptiveForItem: (Player, ItemStack?) -> Boolean + private val notCaptiveFor: (Player, ItemStack?) -> Boolean ) : EcoSlot( provider, ClickType.values().associateWith { - captiveWithTest(notCaptiveFor, notCaptiveForItem).toSingletonList() + captiveWithTest(notCaptiveFor).toSingletonList() }, { _, _, prev -> prev } ) { - override fun isCaptive(player: Player, menu: Menu): Boolean { - return !notCaptiveFor(player) - } - - override fun isCaptiveForItem(player: Player, menu: Menu, itemStack: ItemStack?): Boolean { - return !notCaptiveForItem(player, itemStack) + override fun isCaptive(player: Player, menu: Menu, itemStack: ItemStack?): Boolean { + return !notCaptiveFor(player, itemStack) } override fun isCaptiveFromEmpty(): Boolean { @@ -33,9 +28,6 @@ class EcoCaptiveSlot( } } -private fun captiveWithTest(test: (Player) -> Boolean, - itemTest: (Player, ItemStack?) -> Boolean): SlotHandler { - return SlotHandler { event, _, _ -> - event.isCancelled = itemTest(event.whoClicked as Player, event.cursor) || test(event.whoClicked as Player) - } +private fun captiveWithTest(itemTest: (Player, ItemStack?) -> Boolean): SlotHandler = SlotHandler { event, _, _ -> + event.isCancelled = itemTest(event.whoClicked as Player, event.cursor) } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt index 7d9e7d7b..41e463e6 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt @@ -9,7 +9,6 @@ import org.bukkit.entity.Player import org.bukkit.event.inventory.ClickType import org.bukkit.inventory.ItemStack import java.util.function.BiPredicate -import java.util.function.Predicate class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder { private var captive = false @@ -18,27 +17,15 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder { private val handlers = mutableMapOf>() - private var notCaptiveFor: (Player) -> Boolean = { false } - private var notCaptiveForItem: (Player, ItemStack?) -> Boolean = { _, _ -> false} + private var notCaptiveFor: (Player, ItemStack?) -> Boolean = { _, _ -> false} override fun onClick(type: ClickType, action: SlotHandler): SlotBuilder { handlers.computeIfAbsent(type) { mutableListOf() } += action return this } - override fun notCaptiveFor(predicate: Predicate): SlotBuilder { - notCaptiveFor = { predicate.test(it) } - return this - } - - /** - * Prevent captive for players that match a predicate. - * - * @param predicate The predicate. Returns true when the slot should not be captive. - * @return The builder. - */ - override fun notCaptiveForItem(predicate: BiPredicate): SlotBuilder { - notCaptiveForItem = { player, item -> predicate.test(player, item) } + override fun notCaptiveFor(predicate: BiPredicate): SlotBuilder { + notCaptiveFor = { player, item -> predicate.test(player, item) } return this } @@ -58,8 +45,7 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder { EcoCaptiveSlot( provider, captiveFromEmpty, - notCaptiveFor, - notCaptiveForItem + notCaptiveFor ) } else { EcoSlot( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/gui/GUIListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/gui/GUIListener.kt index c425fd15..1e7b32ef 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/gui/GUIListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/gui/GUIListener.kt @@ -95,7 +95,7 @@ class GUIListener(private val plugin: EcoPlugin) : Listener { val slot = menu.getSlot(row, column, player) - if (!slot.isCaptive(player, menu) || !slot.isCaptiveForItem(player, menu, event.currentItem)) { + if (!slot.isCaptive(player, menu, event.currentItem)) { event.isCancelled = true } }