From aa70210cab244894e4ab606dd8881ada9bd35f18 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 3 Jun 2023 19:26:54 +0100 Subject: [PATCH] Added /ecomenus open and /ecomenus forceopen --- .../ecomenus/commands/CommandEcoMenus.kt | 2 + .../ecomenus/commands/CommandForceOpen.kt | 50 +++++++++++++++++++ .../willfp/ecomenus/commands/CommandOpen.kt | 50 +++++++++++++++++++ .../com/willfp/ecomenus/menus/EcoMenu.kt | 4 ++ .../com/willfp/ecomenus/menus/EcoMenus.kt | 4 +- .../core-plugin/src/main/resources/lang.yml | 4 ++ .../src/main/resources/paper-plugin.yml | 14 ++++-- .../core-plugin/src/main/resources/plugin.yml | 14 ++++-- 8 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandOpen.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandEcoMenus.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandEcoMenus.kt index b986087..73fce1e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandEcoMenus.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandEcoMenus.kt @@ -12,6 +12,8 @@ class CommandEcoMenus(plugin: EcoPlugin) : PluginCommand( ) { init { this.addSubcommand(CommandReload(plugin)) + .addSubcommand(CommandOpen(plugin)) + .addSubcommand(CommandForceOpen(plugin)) } override fun onExecute(sender: CommandSender, args: List) { 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 new file mode 100644 index 0000000..95dc278 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandForceOpen.kt @@ -0,0 +1,50 @@ +package com.willfp.ecomenus.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.util.StringUtils +import com.willfp.eco.util.savedDisplayName +import com.willfp.ecomenus.menus.EcoMenus +import org.bukkit.command.CommandSender +import org.bukkit.util.StringUtil + +class CommandForceOpen(plugin: EcoPlugin) : Subcommand( + plugin, + "forceopen", + "ecomenus.command.forceopen", + false +) { + override fun onExecute(sender: CommandSender, args: List) { + val menu = notifyNull(EcoMenus[args.getOrNull(0)], "invalid-menu") + val player = notifyPlayerRequired(args.getOrNull(1), "invalid-player") + + menu.forceOpen(player) + sender.sendMessage( + plugin.langYml.getMessage("opened", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%player%", player.savedDisplayName) + .replace("%menu%", menu.id) + ) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + EcoMenus.values().map { it.id }, + completions + ) + } + + if (args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + plugin.server.onlinePlayers.map { it.name }, + completions + ) + } + + return completions + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandOpen.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandOpen.kt new file mode 100644 index 0000000..2ac4a05 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/commands/CommandOpen.kt @@ -0,0 +1,50 @@ +package com.willfp.ecomenus.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.util.StringUtils +import com.willfp.eco.util.savedDisplayName +import com.willfp.ecomenus.menus.EcoMenus +import org.bukkit.command.CommandSender +import org.bukkit.util.StringUtil + +class CommandOpen(plugin: EcoPlugin) : Subcommand( + plugin, + "open", + "ecomenus.command.open", + false +) { + override fun onExecute(sender: CommandSender, args: List) { + val menu = notifyNull(EcoMenus[args.getOrNull(0)], "invalid-menu") + val player = notifyPlayerRequired(args.getOrNull(1), "invalid-player") + + menu.open(player) + sender.sendMessage( + plugin.langYml.getMessage("opened", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%player%", player.savedDisplayName) + .replace("%menu%", menu.id) + ) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + EcoMenus.values().map { it.id }, + completions + ) + } + + if (args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + plugin.server.onlinePlayers.map { it.name }, + completions + ) + } + + return completions + } +} 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 9716bab..e76926e 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 @@ -40,6 +40,10 @@ class EcoMenu( return } + forceOpen(player, parent) + } + + fun forceOpen(player: Player, parent: Menu? = null) { menu.open(player, parent) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenus.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenus.kt index ffc6837..84fb145 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenus.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomenus/menus/EcoMenus.kt @@ -20,7 +20,7 @@ object EcoMenus : ConfigCategory("menu", "menus") { return registry.values() } - operator fun get(id: String): EcoMenu? { - return registry.get(id) + operator fun get(id: String?): EcoMenu? { + return registry.get(id ?: return null) } } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index b953091..147e452 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -4,3 +4,7 @@ messages: not-player: "&cThis command must be run by a player" invalid-command: "&cUnknown subcommand!" reloaded: "Reloaded!" + + invalid-player: "&cInvalid player!" + invalid-menu: "&cInvalid menu!" + opened: "Opened &a%menu%&r for &a%player%&r!" diff --git a/eco-core/core-plugin/src/main/resources/paper-plugin.yml b/eco-core/core-plugin/src/main/resources/paper-plugin.yml index ecaffe8..cbd4daa 100644 --- a/eco-core/core-plugin/src/main/resources/paper-plugin.yml +++ b/eco-core/core-plugin/src/main/resources/paper-plugin.yml @@ -28,10 +28,18 @@ permissions: children: ecomenus.command.reload: true ecomenus.command.ecomenus: true + ecomenus.command.open: true + ecomenus.command.forceopen: true - ecomenus.command.reload: - description: Allows reloading the config - default: op ecomenus.command.ecomenus: description: Allows the use of /ecomenus. default: true + ecomenus.command.reload: + description: Allows reloading the config + default: op + ecomenus.command.open: + description: Allows the use of /ecomenus open. + default: op + ecomenus.command.forceopen: + description: Allows the use of /ecomenus forceopen. + default: op diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index da21c6f..33ed422 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -27,10 +27,18 @@ permissions: children: ecomenus.command.reload: true ecomenus.command.ecomenus: true + ecomenus.command.open: true + ecomenus.command.forceopen: true - ecomenus.command.reload: - description: Allows reloading the config - default: op ecomenus.command.ecomenus: description: Allows the use of /ecomenus. default: true + ecomenus.command.reload: + description: Allows reloading the config + default: op + ecomenus.command.open: + description: Allows the use of /ecomenus open. + default: op + ecomenus.command.forceopen: + description: Allows the use of /ecomenus forceopen. + default: op