From 25f2a62033d73ab2f4851280d3d18003743656a6 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 5 Jun 2023 14:27:04 +0100 Subject: [PATCH] Added dynamic items, open effects, and close effects --- build.gradle.kts | 2 +- .../ecomenus/commands/CommandForceOpen.kt | 2 +- .../ecomenus/components/ConfigurableSlot.kt | 17 +++++++++++++- .../com/willfp/ecomenus/menus/EcoMenu.kt | 22 ++++++++++++++++++- .../kotlin/com/willfp/ecomenus/menus/Menu.kt | 5 ++--- .../ecomenus/slots/impl/SlotTypeCommand.kt | 7 ++---- .../ecomenus/slots/impl/SlotTypeEffects.kt | 16 +------------- .../src/main/resources/menus/_example.yml | 7 ++++++ gradle.properties | 4 ++-- 9 files changed, 53 insertions(+), 29 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 157d2f4..1877445 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,7 +37,7 @@ allprojects { } dependencies { - compileOnly("com.willfp:eco:6.63.0") + compileOnly("com.willfp:eco:6.64.0") compileOnly("org.jetbrains:annotations:23.0.0") compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10") } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt index 95dc278..c1b1736 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt @@ -20,7 +20,7 @@ class CommandForceOpen(plugin: EcoPlugin) : Subcommand( menu.forceOpen(player) sender.sendMessage( - plugin.langYml.getMessage("opened", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + plugin.langYml.getMessage("opened") .replace("%player%", player.savedDisplayName) .replace("%menu%", menu.id) ) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/components/ConfigurableSlot.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/components/ConfigurableSlot.kt index e22d217..b142ee3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/components/ConfigurableSlot.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/components/ConfigurableSlot.kt @@ -11,6 +11,8 @@ import com.willfp.eco.core.gui.slot import com.willfp.eco.core.gui.slot.Slot import com.willfp.eco.core.items.Items import com.willfp.eco.core.placeholder.context.placeholderContext +import com.willfp.eco.core.placeholder.findPlaceholders +import com.willfp.eco.core.placeholder.translatePlaceholders import com.willfp.ecomenus.slots.SlotTypes import com.willfp.libreforge.ConfigViolation import com.willfp.libreforge.EmptyProvidedHolder @@ -40,10 +42,23 @@ class ConfigurableSlot( private val showIfNotMet = config.getBool("show-if-not-met") + private val itemLookupString = config.getString("item") + + private val isDynamicBaseItem = itemLookupString.findPlaceholders().isNotEmpty() + private val baseItem = Items.lookup(config.getString("item")).item private val slot = slot({ player, _ -> - baseItem.clone().fast().apply { + val item = if (isDynamicBaseItem) { + Items.lookup( + itemLookupString + .translatePlaceholders(placeholderContext(player = player)) + ).item + } else { + baseItem + } + + item.clone().fast().apply { if (config.has("lore")) { this.lore = config.getFormattedStrings( "lore", placeholderContext( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenu.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenu.kt index e76926e..1bb5fc7 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenu.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenu.kt @@ -8,6 +8,8 @@ import com.willfp.ecomenus.commands.DynamicMenuCommand import com.willfp.libreforge.EmptyProvidedHolder import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.conditions.Conditions +import com.willfp.libreforge.effects.Effects +import com.willfp.libreforge.effects.executors.impl.NormalExecutorFactory import org.bukkit.entity.Player class EcoMenu( @@ -21,11 +23,23 @@ class EcoMenu( private val conditions = Conditions.compile( config.getSubsections("conditions"), - ViolationContext(plugin, "Menu $id conditions") + ViolationContext(plugin, "menu $id conditions") ) private val cannotOpenMessages = config.getFormattedStrings("cannot-open-messages") + private val openEffects = Effects.compileChain( + config.getSubsections("open-effects"), + NormalExecutorFactory.create(), + ViolationContext(plugin, "menu $id open effects") + ) + + private val closeEffects = Effects.compileChain( + config.getSubsections("close-effects"), + NormalExecutorFactory.create(), + ViolationContext(plugin, "menu $id close effects") + ) + init { if (commandName != null) { DynamicMenuCommand(plugin, this, commandName).register() @@ -45,5 +59,11 @@ class EcoMenu( fun forceOpen(player: Player, parent: Menu? = null) { menu.open(player, parent) + openEffects?.trigger(player) + } + + fun handleClose(player: Player) { + closeEffects?.trigger(player) + menu.previousMenus[player].popOrNull()?.open(player) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/Menu.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/Menu.kt index 6269199..59b34af 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/Menu.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/Menu.kt @@ -91,9 +91,8 @@ fun buildMenu(plugin: EcoPlugin, menu: EcoMenu, config: Config): Menu { } } - onClose { event, menu -> - val player = event.player as Player - menu.previousMenus[player].popOrNull()?.open(player) + onClose { event, _ -> + menu.handleClose(event.player as Player) } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeCommand.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeCommand.kt index 76d68a5..ae85a43 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeCommand.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeCommand.kt @@ -4,8 +4,8 @@ import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.gui.menu.Menu import com.willfp.eco.core.gui.slot.Slot -import com.willfp.eco.core.integrations.placeholder.PlaceholderManager import com.willfp.eco.core.placeholder.context.placeholderContext +import com.willfp.eco.core.placeholder.translatePlaceholders import com.willfp.ecomenus.slots.SlotFunction import com.willfp.ecomenus.slots.SlotType import com.willfp.libreforge.ViolationContext @@ -30,10 +30,7 @@ class CommandSlotFunction( for (command in commands) { Bukkit.dispatchCommand( sender ?: player, - PlaceholderManager.translatePlaceholders( - command.replace("%player%", player.name), - placeholderContext(player = player) - ) + command.translatePlaceholders(placeholderContext(player = player)) ) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeEffects.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeEffects.kt index 1b12f65..28d2ebb 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeEffects.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/slots/impl/SlotTypeEffects.kt @@ -32,21 +32,7 @@ object SlotTypeEffects : SlotType("effects") { private val chain: Chain ) : SlotFunction { override fun execute(player: Player, event: InventoryClickEvent, slot: Slot, menu: Menu) { - chain.trigger( - DispatchedTrigger( - player, - TriggerClickSlot, - TriggerData( - player = player - ) - ) - ) + chain.trigger(player) } } - - private object TriggerClickSlot : Trigger("click_slot") { - override val parameters = setOf( - TriggerParameter.PLAYER - ) - } } diff --git a/eco-core/core-plugin/src/main/resources/menus/_example.yml b/eco-core/core-plugin/src/main/resources/menus/_example.yml index 2ee1c51..388578b 100644 --- a/eco-core/core-plugin/src/main/resources/menus/_example.yml +++ b/eco-core/core-plugin/src/main/resources/menus/_example.yml @@ -21,6 +21,13 @@ conditions: [ ] cannot-open-messages: - "&cYou cannot open this menu!" +# Read https://plugins.auxilor.io/effects/configuring-an-effect +# Effects to run when the GUI is opened +open-effects: [ ] + +# Effects to run when the GUI is closed +close-effects: [ ] + # Options for the page arrows # If on the first page, the backwards arrow will not be shown, # and if on the last page, the forwards arrow will not be shown. diff --git a/gradle.properties b/gradle.properties index e3dc98b..fcc8263 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ #libreforge-updater #Sat Jun 03 15:45:10 BST 2023 kotlin.code.style=official -libreforge-version=4.17.2 -version=0.0.1-ALPHA \ No newline at end of file +libreforge-version=4.18.0 +version=0.0.2-ALPHA \ No newline at end of file