Compare commits

..

3 Commits

Author SHA1 Message Date
Auxilor
0f91aec3b7 Slots can now be not captive for some players 2022-05-29 11:34:32 +01:00
Auxilor
d2bf38c5c9 Updated to 6.36.3 2022-05-29 11:11:33 +01:00
Auxilor
2c96b79aba Improved slots 2022-05-29 11:11:16 +01:00
8 changed files with 100 additions and 23 deletions

View File

@@ -28,6 +28,16 @@ public interface Slot {
*/
boolean isCaptive();
/**
* If the slot is not captive for a player.
*
* @param player The player.
* @return If not captive for the player.
*/
default boolean isNotCaptiveFor(@NotNull Player player) {
return false;
}
/**
* If the slot is captive from empty.
* If true, a captive item will be returned even if the item is the same as the rendered item.

View File

@@ -3,10 +3,12 @@ package com.willfp.eco.core.gui.slot;
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.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
/**
* Builder to create slots.
@@ -102,6 +104,14 @@ public interface SlotBuilder {
*/
SlotBuilder onMiddleClick(@NotNull SlotHandler handler);
/**
* 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 notCaptiveFor(@NotNull Predicate<Player> predicate);
/**
* Modify the ItemStack.
*

View File

@@ -20,6 +20,17 @@ public final class MenuUtils {
return new Pair<>(row + 1, column + 1);
}
/**
* Convert row and column to 0-53 slot.
*
* @param row The row.
* @param column The column.
* @return The slot.
*/
public static int rowColumnToSlot(final int row, final int column) {
return (column - 1) + ((row - 1) * 9);
}
private MenuUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -32,6 +32,10 @@ fun SlotBuilder.onShiftRightClick(action: (InventoryClickEvent, Slot, Menu) -> U
fun SlotBuilder.onMiddleClick(action: (InventoryClickEvent, Slot, Menu) -> Unit): SlotBuilder =
this.onMiddleClick { a, b, c -> action(a, b, c) }
/** @see SlotBuilder.notCaptiveFor */
fun SlotBuilder.notCaptiveFor(test: (Player) -> Boolean): SlotBuilder =
this.notCaptiveFor { test(it) }
/**
* @see SlotBuilder.setModifier
* @deprecated Use SlotUpdater instead.

View File

@@ -43,6 +43,10 @@ class MenuRenderedInventory(
val renderedItem = slot.getItemStack(player)
val itemStack = inventory.getItem(i) ?: continue
if (slot.isNotCaptiveFor(player)) {
continue
}
if (!slot.isCaptiveFromEmpty) {
if (itemStack == renderedItem) {
continue

View File

@@ -2,17 +2,19 @@ package com.willfp.eco.internal.gui.slot
import com.willfp.eco.core.gui.slot.functional.SlotHandler
import com.willfp.eco.core.gui.slot.functional.SlotProvider
import org.bukkit.entity.Player
class EcoCaptiveSlot(
provider: SlotProvider,
private val captiveFromEmpty: Boolean
private val captiveFromEmpty: Boolean,
private val notCaptiveFor: (Player) -> Boolean
) : EcoSlot(
provider,
allowMovingItem,
allowMovingItem,
allowMovingItem,
allowMovingItem,
allowMovingItem,
captiveWithTest(notCaptiveFor),
captiveWithTest(notCaptiveFor),
captiveWithTest(notCaptiveFor),
captiveWithTest(notCaptiveFor),
captiveWithTest(notCaptiveFor),
{ _, _, prev -> prev }
) {
override fun isCaptive(): Boolean {
@@ -22,8 +24,14 @@ class EcoCaptiveSlot(
override fun isCaptiveFromEmpty(): Boolean {
return captiveFromEmpty
}
override fun isNotCaptiveFor(player: Player): Boolean {
return notCaptiveFor(player)
}
}
private val allowMovingItem = SlotHandler { event, _, _ ->
event.isCancelled = false
}
private fun captiveWithTest(test: (Player) -> Boolean): SlotHandler {
return SlotHandler { event, _, _ ->
event.isCancelled = test(event.whoClicked as Player)
}
}

View File

@@ -1,26 +1,39 @@
package com.willfp.eco.internal.gui.slot
import com.willfp.eco.core.gui.menu.Menu
import com.willfp.eco.core.gui.slot.Slot
import com.willfp.eco.core.gui.slot.SlotBuilder
import com.willfp.eco.core.gui.slot.functional.SlotHandler
import com.willfp.eco.core.gui.slot.functional.SlotProvider
import com.willfp.eco.core.gui.slot.functional.SlotUpdater
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
import java.util.function.Predicate
internal object NoOpSlot : SlotHandler {
override fun handle(event: InventoryClickEvent, slot: Slot, menu: Menu) {
}
override fun equals(other: Any?): Boolean {
return other is NoOpSlot
}
}
internal class NoOpForPlayer
class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
private var captive = false
private var captiveFromEmpty = false
private var updater: SlotUpdater = SlotUpdater { player, menu, _ -> provider.provide(player, menu) }
private var onLeftClick =
SlotHandler { _, _, _ -> run { } }
private var onRightClick =
SlotHandler { _, _, _ -> run { } }
private var onShiftLeftClick =
SlotHandler { _, _, _ -> run { } }
private var onShiftRightClick =
SlotHandler { _, _, _ -> run { } }
private var onMiddleClick =
SlotHandler { _, _, _ -> run { } }
private var onLeftClick: SlotHandler = NoOpSlot
private var onRightClick: SlotHandler = NoOpSlot
private var onShiftLeftClick: SlotHandler = NoOpSlot
private var onShiftRightClick: SlotHandler = NoOpSlot
private var onMiddleClick: SlotHandler = NoOpSlot
private var notCaptiveFor: (Player) -> Boolean = { false }
override fun onLeftClick(action: SlotHandler): SlotBuilder {
onLeftClick = action
@@ -47,6 +60,11 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
return this
}
override fun notCaptiveFor(predicate: Predicate<Player>): SlotBuilder {
notCaptiveFor = { predicate.test(it) }
return this
}
override fun setCaptive(fromEmpty: Boolean): SlotBuilder {
captive = true
captiveFromEmpty = fromEmpty
@@ -60,9 +78,21 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
override fun build(): Slot {
return if (captive) {
EcoCaptiveSlot(provider, captiveFromEmpty)
EcoCaptiveSlot(
provider,
captiveFromEmpty,
notCaptiveFor
)
} else {
EcoSlot(provider, onLeftClick, onRightClick, onShiftLeftClick, onShiftRightClick, onMiddleClick, updater)
EcoSlot(
provider,
onLeftClick,
onRightClick,
onShiftLeftClick,
onShiftRightClick,
onMiddleClick,
updater
)
}
}
}
}

View File

@@ -1,3 +1,3 @@
version = 6.36.2
version = 6.36.3
plugin-name = eco
kotlin.code.style = official