9
0
mirror of https://github.com/Auxilor/EcoBits.git synced 2025-12-19 15:09:19 +00:00

Added ability to specify currency-specific commands

This commit is contained in:
_OfTeN_
2023-03-29 14:50:33 +03:00
parent cce707dc2c
commit 688322cd37
3 changed files with 124 additions and 0 deletions

View File

@@ -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<String>) {
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<String>) {
super.onExecute(sender, args)
}
override fun onExecute(sender: Player, args: MutableList<String>) {
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<String>): MutableList<String> {
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%"
}
}
}

View File

@@ -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()
}
}

View File

@@ -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