Reworked slot changes into canCaptivateItem

This commit is contained in:
Auxilor
2022-11-18 18:08:21 +00:00
parent d877b707d6
commit eccb146852
9 changed files with 99 additions and 42 deletions

View File

@@ -32,7 +32,7 @@ public abstract class CustomSlot implements Slot {
}
@Override
public @NotNull ItemStack getItemStack(@NotNull final Player player) {
public final @NotNull ItemStack getItemStack(@NotNull final Player player) {
if (delegate == null) {
throw new IllegalStateException("Custom Slot was not initialized!");
}
@@ -41,18 +41,28 @@ public abstract class CustomSlot implements Slot {
}
@Override
public boolean isCaptive(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack item) {
public final 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, item);
return delegate.isCaptive(player, menu);
}
@Override
public boolean isCaptiveFromEmpty() {
public final boolean canCaptivateItem(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack itemStack) {
if (delegate == null) {
throw new IllegalStateException("Custom Slot was not initialized!");
}
return delegate.canCaptivateItem(player, menu, itemStack);
}
@Override
public final boolean isCaptiveFromEmpty() {
if (delegate == null) {
throw new IllegalStateException("Custom Slot was not initialized!");
}

View File

@@ -35,10 +35,16 @@ public abstract class ReactiveSlot implements Slot {
}
@Override
public boolean isCaptive(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack item) {
return getSlot(player, menu).isCaptive(player, menu, item);
public final boolean isCaptive(@NotNull final Player player,
@NotNull final Menu menu) {
return getSlot(player, menu).isCaptive(player, menu);
}
@Override
public final boolean canCaptivateItem(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack itemStack) {
return getSlot(player, menu).canCaptivateItem(player, menu, itemStack);
}
@Override

View File

@@ -51,14 +51,13 @@ public interface Slot extends GUIComponent {
*
* @param player The player.
* @param menu The menu.
* @param itemStack The item.
* @param itemStack The item; use null if the item is unknown.
* @return If captive.
*/
default boolean isCaptive(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack itemStack) {
// Delegate to no-item version for backwards compatibility.
return this.isCaptive(player, menu);
default boolean canCaptivateItem(@NotNull final Player player,
@NotNull final Menu menu,
@Nullable final ItemStack itemStack) {
return true;
}
/**

View File

@@ -1,17 +1,15 @@
package com.willfp.eco.core.gui.slot;
import com.willfp.eco.core.gui.slot.functional.CaptiveCondition;
import com.willfp.eco.core.gui.slot.functional.SlotHandler;
import com.willfp.eco.core.gui.slot.functional.SlotModifier;
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;
/**
@@ -146,17 +144,15 @@ public interface SlotBuilder {
* @param predicate The predicate. Returns true when the slot should not be captive.
* @return The builder.
*/
default SlotBuilder notCaptiveFor(@NotNull final Predicate<Player> predicate) {
return this.notCaptiveFor((player, itemStack) -> predicate.test(player));
}
SlotBuilder notCaptiveFor(@NotNull final Predicate<Player> predicate);
/**
* Prevent captive for players and items that match a predicate.
* Set a whitelist for allowed captive items.
*
* @param predicate The predicate. Returns true when the slot should not be captive.
* @param condition The condition. Returns true when the slot should be captive.
* @return The builder.
*/
default SlotBuilder notCaptiveFor(@NotNull BiPredicate<Player, @Nullable ItemStack> predicate) {
default SlotBuilder setCaptiveCondition(@NotNull final CaptiveCondition condition) {
return this;
}

View File

@@ -0,0 +1,25 @@
package com.willfp.eco.core.gui.slot.functional;
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;
/**
* Interface to test if a captive slot is captive given a player, menu, and item.
*/
@FunctionalInterface
public interface CaptiveCondition {
/**
* Get if the slot is captive.
*
* @param player The player.
* @param menu The menu.
* @param itemStack The item.
* @return If captive.
*/
boolean isCaptive(@NotNull Player player,
@NotNull Menu menu,
@Nullable ItemStack itemStack);
}

View File

@@ -72,11 +72,11 @@ fun SlotBuilder.onClick(clickType: ClickType, action: (Player, InventoryClickEve
/** @see SlotBuilder.notCaptiveFor */
fun SlotBuilder.notCaptiveFor(test: (Player) -> Boolean): SlotBuilder =
this.notCaptiveFor { t, _ -> test(t) }
this.notCaptiveFor { test(it) }
/** @see SlotBuilder.notCaptiveFor */
fun SlotBuilder.notCaptiveFor(test: (Player, ItemStack?) -> Boolean): SlotBuilder =
this.notCaptiveFor { player, item -> test(player, item) }
/** @see SlotBuilder.setCaptiveCondition */
fun SlotBuilder.setCaptiveCondition(test: (Player, Menu, ItemStack?) -> Boolean): SlotBuilder =
this.setCaptiveCondition { a, b, c -> test(a, b, c) }
/**
* @see SlotBuilder.setModifier