diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/DynamicCurrencyCommand.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/DynamicCurrencyCommand.kt new file mode 100644 index 0000000..bdc9f0b --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/DynamicCurrencyCommand.kt @@ -0,0 +1,103 @@ +package com.willfp.ecobits.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.util.containsIgnoreCase +import com.willfp.ecobits.currencies.Currencies +import com.willfp.ecobits.currencies.Currency +import org.bukkit.Bukkit +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.util.StringUtil + +class DynamicCurrencyCommand( + plugin: EcoPlugin, + label: String, + val currency: Currency +): PluginCommand( + plugin, + label, + "ecobits.command.${currency.id}", + false +) { + init { + DynamicCurrencySubcommand.allCommands.forEach { + this.addSubcommand( + DynamicCurrencySubcommand(plugin, it, currency) + ) + } + } + + override fun onExecute(sender: CommandSender, args: MutableList) { + sender.sendMessage(this.plugin.langYml.getMessage("invalid-command")) + } + + + class DynamicCurrencySubcommand( + plugin: EcoPlugin, + val command: String, + val currency: Currency + ): Subcommand( + plugin, + command, + "ecobits.command.${currency.id}.$command", + false + ) { + override fun onExecute(sender: CommandSender, args: MutableList) { + super.onExecute(sender, args) + } + + override fun onExecute(sender: Player, args: MutableList) { + val format = when { + currencyCommands.containsIgnoreCase(command) -> currencyFormat + playerCurrencyCommands.containsIgnoreCase(command) -> playerCurrencyFormat + playerCurrencyAmountCommands.containsIgnoreCase(command) -> playerCurrencyAmountFormat + else -> "" + } + + Bukkit.dispatchCommand(sender, + format.replace( + "%command%", command + ).replace( + "%player%", args.getOrElse(0) { "" } + ).replace( + "%amount%", args.getOrElse(1) { "" } + ).replace( + "%currency%", currency.id + ) + ) + } + + override fun tabComplete(sender: CommandSender, args: MutableList): MutableList { + return when { + args.size == 1 -> { + if (currencyCommands.containsIgnoreCase(command)) { + StringUtil.copyPartialMatches(args.first(), + Currencies.values().map { it.id }, mutableListOf()) + } else { + StringUtil.copyPartialMatches(args.first(), + Bukkit.getOnlinePlayers().map { it.name }, mutableListOf()) + } + } + + args.size == 2 && !currencyCommands.containsIgnoreCase(command) -> { + StringUtil.copyPartialMatches(args.first(), + Currencies.values().map { it.id }, mutableListOf()) + } + + else -> mutableListOf() + } + } + + companion object { + val currencyCommands = listOf("balance") + val playerCurrencyCommands = listOf("get", "reset") + val playerCurrencyAmountCommands = listOf("give", "givesilent", "pay", "set", "take", "takesilent") + val allCommands = currencyCommands + playerCurrencyCommands + playerCurrencyAmountCommands + val currencyFormat = "ecobits %command% %currency%" + val playerCurrencyFormat = "ecobits %command% %player% %currency%" + val playerCurrencyAmountFormat = "ecobits %command% %player% %currency% %amount%" + } + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt index 3d260a0..e995d6a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt @@ -15,6 +15,7 @@ import com.willfp.eco.core.price.Prices import com.willfp.eco.util.savedDisplayName import com.willfp.eco.util.toNiceString import com.willfp.ecobits.EcoBitsPlugin +import com.willfp.ecobits.commands.DynamicCurrencyCommand import com.willfp.ecobits.integrations.IntegrationVault import net.milkbowl.vault.economy.Economy import org.bukkit.Bukkit @@ -40,6 +41,7 @@ class Currency( val default = config.getDouble("default") val name = config.getFormattedString("name") + val max = config.getDouble("max").let { if (it < 0) Double.MAX_VALUE else it } val isPayable = config.getBool("payable") @@ -50,6 +52,8 @@ class Currency( val isLocal = config.getBool("local") + val commands = config.getStrings("commands").map { DynamicCurrencyCommand(plugin, it, this) } + val key = PersistentDataKey( plugin.createNamespacedKey(if (isLocal) "${plugin.serverID}_${id}" else id), PersistentDataKeyType.DOUBLE, @@ -67,6 +71,17 @@ class Currency( }.orElse(null) } + fun registerCommands() { + this.commands.forEach { + println("Registered ${it.name}") + it.register() + } + } + + fun unregisterCommands() { + this.commands.forEach { it.unregister() } + } + init { PlaceholderManager.registerPlaceholder( DynamicPlaceholder( @@ -142,6 +157,9 @@ class Currency( ServicePriority.Highest ) } + + this.unregisterCommands() + this.registerCommands() } } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 772f2f1..22924e5 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -27,3 +27,6 @@ currencies: decimal: true # If decimal amounts are allowed rather than just integer amounts vault: false # If this currency should be registered with vault local: false # If this currency should not sync between servers + commands: # A list of commands dedicated to this currency (for easier paying, checking balance, etc) + - crystals + - ecocrystals