Improved slots
This commit is contained in:
@@ -102,6 +102,13 @@ public interface SlotBuilder {
|
|||||||
*/
|
*/
|
||||||
SlotBuilder onMiddleClick(@NotNull SlotHandler handler);
|
SlotBuilder onMiddleClick(@NotNull SlotHandler handler);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevent all clicks.
|
||||||
|
*
|
||||||
|
* @return The builder.
|
||||||
|
*/
|
||||||
|
SlotBuilder preventAllClicks();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify the ItemStack.
|
* Modify the ItemStack.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -5,14 +5,19 @@ import com.willfp.eco.core.gui.slot.functional.SlotProvider
|
|||||||
|
|
||||||
class EcoCaptiveSlot(
|
class EcoCaptiveSlot(
|
||||||
provider: SlotProvider,
|
provider: SlotProvider,
|
||||||
private val captiveFromEmpty: Boolean
|
private val captiveFromEmpty: Boolean,
|
||||||
|
onLeftClick: SlotHandler,
|
||||||
|
onRightClick: SlotHandler,
|
||||||
|
onShiftLeftClick: SlotHandler,
|
||||||
|
onShiftRightClick: SlotHandler,
|
||||||
|
onMiddleClick: SlotHandler,
|
||||||
) : EcoSlot(
|
) : EcoSlot(
|
||||||
provider,
|
provider,
|
||||||
allowMovingItem,
|
onLeftClick.captiveIfNoop(),
|
||||||
allowMovingItem,
|
onRightClick.captiveIfNoop(),
|
||||||
allowMovingItem,
|
onShiftLeftClick.captiveIfNoop(),
|
||||||
allowMovingItem,
|
onShiftRightClick.captiveIfNoop(),
|
||||||
allowMovingItem,
|
onMiddleClick.captiveIfNoop(),
|
||||||
{ _, _, prev -> prev }
|
{ _, _, prev -> prev }
|
||||||
) {
|
) {
|
||||||
override fun isCaptive(): Boolean {
|
override fun isCaptive(): Boolean {
|
||||||
@@ -24,6 +29,14 @@ class EcoCaptiveSlot(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun SlotHandler.captiveIfNoop(): SlotHandler {
|
||||||
|
return if (this == NoOpSlot) {
|
||||||
|
allowMovingItem
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val allowMovingItem = SlotHandler { event, _, _ ->
|
private val allowMovingItem = SlotHandler { event, _, _ ->
|
||||||
event.isCancelled = false
|
event.isCancelled = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,33 @@
|
|||||||
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.event.inventory.InventoryClickEvent
|
||||||
|
|
||||||
|
internal object NoOpSlot : SlotHandler {
|
||||||
|
override fun handle(event: InventoryClickEvent, slot: Slot, menu: Menu) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
return other is NoOpSlot
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 =
|
|
||||||
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 +54,15 @@ class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun preventAllClicks(): SlotBuilder {
|
||||||
|
onLeftClick = NoOpSlot
|
||||||
|
onRightClick = NoOpSlot
|
||||||
|
onShiftLeftClick = NoOpSlot
|
||||||
|
onShiftRightClick = NoOpSlot
|
||||||
|
onMiddleClick = NoOpSlot
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
override fun setCaptive(fromEmpty: Boolean): SlotBuilder {
|
override fun setCaptive(fromEmpty: Boolean): SlotBuilder {
|
||||||
captive = true
|
captive = true
|
||||||
captiveFromEmpty = fromEmpty
|
captiveFromEmpty = fromEmpty
|
||||||
@@ -60,9 +76,25 @@ 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,
|
||||||
|
onLeftClick,
|
||||||
|
onRightClick,
|
||||||
|
onShiftLeftClick,
|
||||||
|
onShiftRightClick,
|
||||||
|
onMiddleClick
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
EcoSlot(provider, onLeftClick, onRightClick, onShiftLeftClick, onShiftRightClick, onMiddleClick, updater)
|
EcoSlot(
|
||||||
|
provider,
|
||||||
|
onLeftClick,
|
||||||
|
onRightClick,
|
||||||
|
onShiftLeftClick,
|
||||||
|
onShiftRightClick,
|
||||||
|
onMiddleClick,
|
||||||
|
updater
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user