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

View File

@@ -20,6 +20,17 @@ public final class MenuUtils {
return new Pair<>(row + 1, column + 1); 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() { private MenuUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); 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 = fun SlotBuilder.onMiddleClick(action: (InventoryClickEvent, Slot, Menu) -> Unit): SlotBuilder =
this.onMiddleClick { a, b, c -> action(a, b, c) } 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 * @see SlotBuilder.setModifier
* @deprecated Use SlotUpdater instead. * @deprecated Use SlotUpdater instead.

View File

@@ -43,6 +43,10 @@ class MenuRenderedInventory(
val renderedItem = slot.getItemStack(player) val renderedItem = slot.getItemStack(player)
val itemStack = inventory.getItem(i) ?: continue val itemStack = inventory.getItem(i) ?: continue
if (slot.isNotCaptiveFor(player)) {
continue
}
if (!slot.isCaptiveFromEmpty) { if (!slot.isCaptiveFromEmpty) {
if (itemStack == renderedItem) { if (itemStack == renderedItem) {
continue 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.SlotHandler
import com.willfp.eco.core.gui.slot.functional.SlotProvider import com.willfp.eco.core.gui.slot.functional.SlotProvider
import org.bukkit.entity.Player
class EcoCaptiveSlot( class EcoCaptiveSlot(
provider: SlotProvider, provider: SlotProvider,
private val captiveFromEmpty: Boolean private val captiveFromEmpty: Boolean,
private val notCaptiveFor: (Player) -> Boolean
) : EcoSlot( ) : EcoSlot(
provider, provider,
allowMovingItem, captiveWithTest(notCaptiveFor),
allowMovingItem, captiveWithTest(notCaptiveFor),
allowMovingItem, captiveWithTest(notCaptiveFor),
allowMovingItem, captiveWithTest(notCaptiveFor),
allowMovingItem, captiveWithTest(notCaptiveFor),
{ _, _, prev -> prev } { _, _, prev -> prev }
) { ) {
override fun isCaptive(): Boolean { override fun isCaptive(): Boolean {
@@ -22,8 +24,14 @@ class EcoCaptiveSlot(
override fun isCaptiveFromEmpty(): Boolean { override fun isCaptiveFromEmpty(): Boolean {
return captiveFromEmpty return captiveFromEmpty
} }
override fun isNotCaptiveFor(player: Player): Boolean {
return notCaptiveFor(player)
}
} }
private val allowMovingItem = SlotHandler { event, _, _ -> private fun captiveWithTest(test: (Player) -> Boolean): SlotHandler {
event.isCancelled = false return SlotHandler { event, _, _ ->
} event.isCancelled = test(event.whoClicked as Player)
}
}

View File

@@ -1,26 +1,39 @@
package com.willfp.eco.internal.gui.slot 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.Slot
import com.willfp.eco.core.gui.slot.SlotBuilder 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.SlotHandler
import com.willfp.eco.core.gui.slot.functional.SlotProvider import com.willfp.eco.core.gui.slot.functional.SlotProvider
import com.willfp.eco.core.gui.slot.functional.SlotUpdater 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 { class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
private var captive = false private var captive = false
private var captiveFromEmpty = false private var captiveFromEmpty = false
private var updater: SlotUpdater = SlotUpdater { player, menu, _ -> provider.provide(player, menu) } private var updater: SlotUpdater = SlotUpdater { player, menu, _ -> provider.provide(player, menu) }
private var onLeftClick = private var onLeftClick: SlotHandler = NoOpSlot
SlotHandler { _, _, _ -> run { } } private var onRightClick: SlotHandler = NoOpSlot
private var onRightClick = private var onShiftLeftClick: SlotHandler = NoOpSlot
SlotHandler { _, _, _ -> run { } } private var onShiftRightClick: SlotHandler = NoOpSlot
private var onShiftLeftClick = private var onMiddleClick: SlotHandler = NoOpSlot
SlotHandler { _, _, _ -> run { } }
private var onShiftRightClick = private var notCaptiveFor: (Player) -> Boolean = { false }
SlotHandler { _, _, _ -> run { } }
private var onMiddleClick =
SlotHandler { _, _, _ -> run { } }
override fun onLeftClick(action: SlotHandler): SlotBuilder { override fun onLeftClick(action: SlotHandler): SlotBuilder {
onLeftClick = action onLeftClick = action
@@ -47,6 +60,11 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
return this return this
} }
override fun notCaptiveFor(predicate: Predicate<Player>): SlotBuilder {
notCaptiveFor = { predicate.test(it) }
return this
}
override fun setCaptive(fromEmpty: Boolean): SlotBuilder { override fun setCaptive(fromEmpty: Boolean): SlotBuilder {
captive = true captive = true
captiveFromEmpty = fromEmpty captiveFromEmpty = fromEmpty
@@ -60,9 +78,21 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
override fun build(): Slot { override fun build(): Slot {
return if (captive) { return if (captive) {
EcoCaptiveSlot(provider, captiveFromEmpty) EcoCaptiveSlot(
provider,
captiveFromEmpty,
notCaptiveFor
)
} else { } 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 plugin-name = eco
kotlin.code.style = official kotlin.code.style = official