Didn't make sense not to live for fun
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.willfp.eco.core.gui.menu;
|
||||
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Interface to run on menu close.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface CloseHandler {
|
||||
/**
|
||||
* Performs this operation on the given arguments.
|
||||
*
|
||||
* @param event The close event.
|
||||
* @param menu The menu.
|
||||
*/
|
||||
void handle(@NotNull InventoryCloseEvent event,
|
||||
@NotNull Menu menu);
|
||||
}
|
||||
@@ -31,6 +31,14 @@ public interface MenuBuilder {
|
||||
int column,
|
||||
@NotNull Slot slot);
|
||||
|
||||
/**
|
||||
* Run function to modify the builder.
|
||||
*
|
||||
* @param modifier The modifier.
|
||||
* @return The builder.
|
||||
*/
|
||||
MenuBuilder modfiy(@NotNull Consumer<MenuBuilder> modifier);
|
||||
|
||||
/**
|
||||
* Set the menu mask.
|
||||
*
|
||||
@@ -45,7 +53,18 @@ public interface MenuBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
MenuBuilder onClose(@NotNull Consumer<InventoryCloseEvent> action);
|
||||
default MenuBuilder onClose(@NotNull Consumer<InventoryCloseEvent> action) {
|
||||
onClose((event, menu) -> action.accept(event));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu close handler.
|
||||
*
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
MenuBuilder onClose(@NotNull CloseHandler action);
|
||||
|
||||
/**
|
||||
* Build the menu.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.eco.core.gui.slot;
|
||||
|
||||
import com.willfp.eco.core.Eco;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -26,6 +27,15 @@ public interface Slot {
|
||||
*/
|
||||
boolean isCaptive();
|
||||
|
||||
/**
|
||||
* Create a builder for an ItemStack.
|
||||
*
|
||||
* @return The builder.
|
||||
*/
|
||||
static SlotBuilder builder() {
|
||||
return Eco.getHandler().getGUIFactory().createSlotBuilder((player, menu) -> new ItemStack(Material.AIR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for an ItemStack.
|
||||
*
|
||||
@@ -42,7 +52,6 @@ public interface Slot {
|
||||
* @param provider The provider.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
static SlotBuilder builder(@NotNull final Function<Player, ItemStack> provider) {
|
||||
return Eco.getHandler().getGUIFactory().createSlotBuilder((player, menu) -> provider.apply(player));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ public interface SlotBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
default SlotBuilder onLeftClick(@NotNull BiConsumer<InventoryClickEvent, Slot> action) {
|
||||
return onLeftClick((event, slot, menu) -> action.accept(event, slot));
|
||||
}
|
||||
@@ -34,7 +33,6 @@ public interface SlotBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
default SlotBuilder onRightClick(@NotNull BiConsumer<InventoryClickEvent, Slot> action) {
|
||||
return onRightClick((event, slot, menu) -> action.accept(event, slot));
|
||||
}
|
||||
@@ -53,7 +51,6 @@ public interface SlotBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
default SlotBuilder onShiftLeftClick(@NotNull BiConsumer<InventoryClickEvent, Slot> action) {
|
||||
return onShiftLeftClick((event, slot, menu) -> action.accept(event, slot));
|
||||
}
|
||||
@@ -72,7 +69,6 @@ public interface SlotBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
default SlotBuilder onShiftRightClick(@NotNull BiConsumer<InventoryClickEvent, Slot> action) {
|
||||
return onShiftRightClick((event, slot, menu) -> action.accept(event, slot));
|
||||
}
|
||||
@@ -91,7 +87,6 @@ public interface SlotBuilder {
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
@Deprecated
|
||||
default SlotBuilder onMiddleClick(@NotNull BiConsumer<InventoryClickEvent, Slot> action) {
|
||||
return onMiddleClick((event, slot, menu) -> action.accept(event, slot));
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@ public interface SlotHandler {
|
||||
* @param menu The menu.
|
||||
*/
|
||||
void handle(@NotNull InventoryClickEvent event,
|
||||
@NotNull Slot slot,
|
||||
@NotNull Menu menu);
|
||||
@NotNull Slot slot,
|
||||
@NotNull Menu menu);
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ open class EcoDropQueue(player: Player) : InternalDropQueue {
|
||||
var xp: Int
|
||||
val player: Player
|
||||
var loc: Location
|
||||
var hasTelekinesis = false
|
||||
|
||||
private var hasTelekinesis = false
|
||||
override fun addItem(item: ItemStack): InternalDropQueue {
|
||||
items.add(item)
|
||||
return this
|
||||
|
||||
@@ -10,11 +10,14 @@ class EcoFastCollatedDropQueue(player: Player) : EcoDropQueue(player) {
|
||||
val fetched = COLLATED_MAP[player]
|
||||
|
||||
if (fetched == null) {
|
||||
COLLATED_MAP[player] = CollatedDrops(items, loc, xp)
|
||||
COLLATED_MAP[player] = CollatedDrops(items, loc, xp, hasTelekinesis)
|
||||
} else {
|
||||
fetched.addDrops(items)
|
||||
fetched.location = loc
|
||||
fetched.addXp(xp)
|
||||
if (this.hasTelekinesis) {
|
||||
fetched.forceTelekinesis()
|
||||
}
|
||||
|
||||
COLLATED_MAP[player] = fetched
|
||||
}
|
||||
@@ -23,8 +26,10 @@ class EcoFastCollatedDropQueue(player: Player) : EcoDropQueue(player) {
|
||||
class CollatedDrops(
|
||||
val drops: MutableList<ItemStack>,
|
||||
var location: Location,
|
||||
var xp: Int
|
||||
var xp: Int,
|
||||
var telekinetic: Boolean
|
||||
) {
|
||||
|
||||
fun addDrops(toAdd: List<ItemStack>): CollatedDrops {
|
||||
drops.addAll(toAdd)
|
||||
return this
|
||||
@@ -34,6 +39,10 @@ class EcoFastCollatedDropQueue(player: Player) : EcoDropQueue(player) {
|
||||
this.xp += xp
|
||||
return this
|
||||
}
|
||||
|
||||
fun forceTelekinesis() {
|
||||
telekinetic = true
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.internal.gui.menu
|
||||
|
||||
import com.willfp.eco.core.gui.menu.CloseHandler
|
||||
import com.willfp.eco.core.gui.menu.Menu
|
||||
import com.willfp.eco.core.gui.slot.Slot
|
||||
import com.willfp.eco.internal.gui.slot.EcoSlot
|
||||
@@ -9,13 +10,12 @@ import org.bukkit.entity.Player
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||
import org.bukkit.inventory.Inventory
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import java.util.function.Consumer
|
||||
|
||||
class EcoMenu(
|
||||
private val rows: Int,
|
||||
private val slots: List<MutableList<EcoSlot>>,
|
||||
private val title: String,
|
||||
private val onClose: Consumer<InventoryCloseEvent>
|
||||
private val onClose: CloseHandler
|
||||
): Menu {
|
||||
override fun getSlot(row: Int, column: Int): Slot {
|
||||
if (row < 1 || row > this.rows) {
|
||||
@@ -59,7 +59,7 @@ class EcoMenu(
|
||||
}
|
||||
|
||||
fun handleClose(event: InventoryCloseEvent) {
|
||||
onClose.accept(event)
|
||||
onClose.handle(event, this)
|
||||
}
|
||||
|
||||
override fun getRows(): Int {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.internal.gui.menu
|
||||
|
||||
import com.willfp.eco.core.gui.menu.CloseHandler
|
||||
import com.willfp.eco.core.gui.menu.Menu
|
||||
import com.willfp.eco.core.gui.menu.MenuBuilder
|
||||
import com.willfp.eco.core.gui.slot.FillerMask
|
||||
@@ -10,7 +11,6 @@ import com.willfp.eco.internal.gui.slot.EcoSlot
|
||||
import com.willfp.eco.util.ListUtils
|
||||
import com.willfp.eco.util.StringUtils
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import java.util.function.Consumer
|
||||
|
||||
@@ -18,7 +18,7 @@ class EcoMenuBuilder(private val rows: Int) : MenuBuilder {
|
||||
private var title = "Menu"
|
||||
private var maskSlots: List<MutableList<Slot?>>
|
||||
private val slots: List<MutableList<Slot?>> = ListUtils.create2DList(rows, 9)
|
||||
private var onClose = Consumer { _: InventoryCloseEvent -> }
|
||||
private var onClose = CloseHandler { _, _ -> }
|
||||
|
||||
override fun setTitle(title: String): MenuBuilder {
|
||||
this.title = StringUtils.format(title)
|
||||
@@ -36,12 +36,17 @@ class EcoMenuBuilder(private val rows: Int) : MenuBuilder {
|
||||
return this
|
||||
}
|
||||
|
||||
override fun modfiy(modifier: Consumer<MenuBuilder>): MenuBuilder {
|
||||
modifier.accept(this)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun setMask(mask: FillerMask): MenuBuilder {
|
||||
maskSlots = mask.mask
|
||||
return this
|
||||
}
|
||||
|
||||
override fun onClose(action: Consumer<InventoryCloseEvent>): MenuBuilder {
|
||||
override fun onClose(action: CloseHandler): MenuBuilder {
|
||||
onClose = action
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -8,11 +8,17 @@ class CollatedRunnable(plugin: EcoPlugin) {
|
||||
init {
|
||||
plugin.scheduler.runTimer({
|
||||
for ((key, value) in EcoFastCollatedDropQueue.COLLATED_MAP) {
|
||||
EcoDropQueue(key)
|
||||
val queue = EcoDropQueue(key)
|
||||
.setLocation(value.location)
|
||||
.addItems(value.drops)
|
||||
.addXP(value.xp)
|
||||
.push()
|
||||
|
||||
if (value.telekinetic) {
|
||||
queue.forceTelekinesis()
|
||||
}
|
||||
|
||||
queue.push()
|
||||
|
||||
EcoFastCollatedDropQueue.COLLATED_MAP.remove(key)
|
||||
}
|
||||
EcoFastCollatedDropQueue.COLLATED_MAP.clear()
|
||||
|
||||
@@ -63,6 +63,10 @@ class GUIListener(plugin: EcoPlugin) : PluginDependent<EcoPlugin>(plugin), Liste
|
||||
|
||||
val inv = player.openInventory.topInventory
|
||||
|
||||
if (inv == event.clickedInventory) {
|
||||
return
|
||||
}
|
||||
|
||||
val menu = MenuHandler.getMenu(inv) ?: return
|
||||
|
||||
val rowColumn = MenuUtils.convertSlotToRowColumn(inv.firstEmpty())
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.PluginDependent
|
||||
import com.willfp.eco.core.gui.menu.Menu
|
||||
import com.willfp.eco.core.gui.slot.FillerMask
|
||||
import com.willfp.eco.core.gui.slot.FillerSlot
|
||||
import com.willfp.eco.core.gui.slot.Slot
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.event.EventHandler
|
||||
@@ -13,17 +14,26 @@ import org.bukkit.inventory.ItemStack
|
||||
|
||||
class GUITester(plugin: EcoPlugin) : PluginDependent<EcoPlugin>(plugin), Listener {
|
||||
private val menu: Menu = Menu.builder(3)
|
||||
.setMask(FillerMask(
|
||||
Material.BLACK_STAINED_GLASS_PANE,
|
||||
"111111111",
|
||||
"100000001",
|
||||
"111111111"
|
||||
)).setSlot(
|
||||
.setMask(
|
||||
FillerMask(
|
||||
Material.BLACK_STAINED_GLASS_PANE,
|
||||
"111111111",
|
||||
"100000001",
|
||||
"111111111"
|
||||
)
|
||||
).setSlot(
|
||||
2, 2,
|
||||
Slot.builder(ItemStack(Material.RED_STAINED_GLASS_PANE))
|
||||
Slot.builder()
|
||||
.setCaptive()
|
||||
.build()
|
||||
).build()
|
||||
).modfiy { builder ->
|
||||
run {
|
||||
val slot = FillerSlot(ItemStack(Material.RED_STAINED_GLASS_PANE))
|
||||
for (i in 3..8) {
|
||||
builder.setSlot(2, i, slot)
|
||||
}
|
||||
}
|
||||
}.build()
|
||||
|
||||
@EventHandler
|
||||
fun test(event: AsyncPlayerChatEvent) {
|
||||
@@ -31,6 +41,6 @@ class GUITester(plugin: EcoPlugin) : PluginDependent<EcoPlugin>(plugin), Listene
|
||||
return
|
||||
}
|
||||
|
||||
plugin.scheduler.run{menu.open(event.player)}
|
||||
plugin.scheduler.run { menu.open(event.player) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user