Added ability to filter items for captive slots

This commit is contained in:
_OfTeN_
2022-11-13 02:50:23 +03:00
parent eb4dc168fc
commit 6f55787c84
9 changed files with 79 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ import com.willfp.eco.core.gui.menu.Menu;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Base class for custom slot implementations.
@@ -49,6 +50,17 @@ public abstract class CustomSlot implements Slot {
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);
}
@Override
public boolean isCaptiveFromEmpty() {
if (delegate == null) {

View File

@@ -5,6 +5,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Base class for custom slot implementations.
@@ -39,6 +40,13 @@ public abstract class ReactiveSlot implements Slot {
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);
}
@Override
public final @NotNull Slot getActionableSlot(@NotNull final Player player,
@NotNull final Menu menu) {

View File

@@ -9,6 +9,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
@@ -45,6 +46,20 @@ public interface Slot extends GUIComponent {
return false;
}
/**
* If the slot is captive for this item. (Can this item be placed in it).
*
* @param player The player.
* @param menu The menu.
* @param itemStack The item.
* @return If captive.
*/
default boolean isCaptiveForItem(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack itemStack) {
return false;
}
/**
* Get the actionable slot to be shown.
* <p>

View File

@@ -6,9 +6,12 @@ import com.willfp.eco.core.gui.slot.functional.SlotUpdater;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
/**
@@ -143,7 +146,15 @@ public interface SlotBuilder {
* @param predicate The predicate. Returns true when the slot should not be captive.
* @return The builder.
*/
SlotBuilder notCaptiveFor(@NotNull Predicate<Player> predicate);
SlotBuilder notCaptiveFor(Predicate<Player> predicate);
/**
* Prevent captive for players that match a predicate.
*
* @param predicate The predicate. Returns true when the slot should not be captive.
* @return The builder.
*/
SlotBuilder notCaptiveForItem(BiPredicate<Player, @Nullable ItemStack> predicate);
/**
* Set the ItemStack updater.

View File

@@ -74,6 +74,10 @@ fun SlotBuilder.onClick(clickType: ClickType, action: (Player, InventoryClickEve
fun SlotBuilder.notCaptiveFor(test: (Player) -> Boolean): SlotBuilder =
this.notCaptiveFor { test(it) }
/** @see SlotBuilder.notCaptiveFor */
fun SlotBuilder.notCaptiveForItem(test: (Player, ItemStack?) -> Boolean): SlotBuilder =
this.notCaptiveForItem { player, item -> test(player, item) }
/**
* @see SlotBuilder.setModifier
* @deprecated Use SlotUpdater instead.