9
0
mirror of https://github.com/Auxilor/EcoBits.git synced 2026-01-06 15:52:04 +00:00

Reworked dynamic commands

This commit is contained in:
Auxilor
2023-05-02 15:41:30 +01:00
parent 688322cd37
commit 5b29f4dd76
10 changed files with 195 additions and 193 deletions

View File

@@ -5,13 +5,15 @@ import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.getBalance
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.util.StringUtil
class CommandBalance(
plugin: EcoPlugin
plugin: EcoPlugin,
currency: Currency? = null
) : Subcommand(
plugin,
"balance",

View File

@@ -6,13 +6,15 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.getBalance
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandGet(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"get",
@@ -33,12 +35,14 @@ class CommandGet(
return
}
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
@@ -68,12 +72,14 @@ class CommandGet(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
return completions

View File

@@ -6,19 +6,23 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandGive(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"give",
"ecobits.command.give",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-player"))
@@ -33,24 +37,26 @@ class CommandGive(
return
}
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return
}
if (args.size < 3) {
if (args.size < 3 + argOffset) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-amount"))
return
}
val amount = args[2].toDoubleOrNull()
val amount = args[2 + argOffset].toDoubleOrNull()
if (amount == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-amount"))
@@ -82,17 +88,19 @@ class CommandGive(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -6,19 +6,23 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandGivesilent(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"givesilent",
"ecobits.command.givesilent",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
return
@@ -31,17 +35,19 @@ class CommandGivesilent(
return
}
if (args.size < 2) {
if (this.currency == null) {
if (args.size < 2) {
return
}
}
val currency = this.currency ?: Currencies.getByID(args[1].lowercase()) ?: return
if (args.size < 3 + argOffset) {
return
}
val currency = Currencies.getByID(args[1].lowercase()) ?: return
if (args.size < 3) {
return
}
val amount = args[2].toDoubleOrNull() ?: return
val amount = args[2 + argOffset].toDoubleOrNull() ?: return
player.adjustBalance(currency, amount)
}
@@ -61,17 +67,19 @@ class CommandGivesilent(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -6,6 +6,7 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import com.willfp.ecobits.currencies.getBalance
import org.bukkit.Bukkit
@@ -16,13 +17,16 @@ import kotlin.math.ceil
import kotlin.math.floor
class CommandPay(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"pay",
"ecobits.command.pay",
true
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(player: Player, args: List<String>) {
if (args.isEmpty()) {
player.sendMessage(plugin.langYml.getMessage("must-specify-player"))
@@ -37,24 +41,26 @@ class CommandPay(
return
}
if (args.size < 2) {
player.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
player.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null || !currency.isPayable) {
player.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return
}
if (args.size < 3) {
if (args.size < 3 + argOffset) {
player.sendMessage(plugin.langYml.getMessage("must-specify-amount"))
return
}
val amount = args[2].toDoubleOrNull()
val amount = args[2 + argOffset].toDoubleOrNull()
if (amount == null || amount <= 0) {
player.sendMessage(plugin.langYml.getMessage("invalid-amount"))
@@ -102,17 +108,19 @@ class CommandPay(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().filter { it.isPayable }.map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().filter { it.isPayable }.map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -6,6 +6,7 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.getBalance
import com.willfp.ecobits.currencies.setBalance
import org.bukkit.Bukkit
@@ -13,13 +14,16 @@ import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandReset(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"reset",
"ecobits.command.reset",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-player"))
@@ -34,12 +38,14 @@ class CommandReset(
return
}
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
@@ -71,12 +77,14 @@ class CommandReset(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
return completions

View File

@@ -6,6 +6,7 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import com.willfp.ecobits.currencies.setBalance
import org.bukkit.Bukkit
@@ -13,13 +14,16 @@ import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandSet(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"set",
"ecobits.command.set",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-player"))
@@ -34,24 +38,26 @@ class CommandSet(
return
}
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return
}
if (args.size < 3) {
if (args.size < 3 + argOffset) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-amount"))
return
}
val amount = args[2].toDoubleOrNull()
val amount = args[2 + argOffset].toDoubleOrNull()
if (amount == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-amount"))
@@ -83,17 +89,19 @@ class CommandSet(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -7,19 +7,23 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandTake(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"take",
"ecobits.command.take",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-player"))
@@ -34,24 +38,26 @@ class CommandTake(
return
}
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
}
val currency = Currencies.getByID(args[1].lowercase())
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return
}
if (args.size < 3) {
if (args.size < 3 + argOffset) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-amount"))
return
}
val amount = args[2].toDoubleOrNull()
val amount = args[2 + argOffset].toDoubleOrNull()
if (amount == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-amount"))
@@ -83,17 +89,19 @@ class CommandTake(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -6,19 +6,23 @@ import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandTakesilent(
plugin: EcoPlugin
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"takesilent",
"ecobits.command.takesilent",
false
) {
private val argOffset = if (currency == null) 0 else -1
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
return
@@ -31,17 +35,19 @@ class CommandTakesilent(
return
}
if (args.size < 2) {
if (this.currency == null) {
if (args.size < 2) {
return
}
}
val currency = this.currency ?: Currencies.getByID(args[1].lowercase()) ?: return
if (args.size < 3 + argOffset) {
return
}
val currency = Currencies.getByID(args[1].lowercase()) ?: return
if (args.size < 3) {
return
}
val amount = args[2].toDoubleOrNull() ?: return
val amount = args[2 + argOffset].toDoubleOrNull() ?: return
player.adjustBalance(currency, -amount)
}
@@ -61,17 +67,19 @@ class CommandTakesilent(
)
}
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
if (args.size == 3) {
if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2],
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)

View File

@@ -15,89 +15,27 @@ class DynamicCurrencyCommand(
plugin: EcoPlugin,
label: String,
val currency: Currency
): PluginCommand(
) : PluginCommand(
plugin,
label,
"ecobits.command.${currency.id}",
false
) {
init {
DynamicCurrencySubcommand.allCommands.forEach {
this.addSubcommand(
DynamicCurrencySubcommand(plugin, it, currency)
)
}
this.addSubcommand(CommandGive(plugin, currency))
.addSubcommand(CommandGivesilent(plugin, currency))
.addSubcommand(CommandGet(plugin, currency))
.addSubcommand(CommandSet(plugin, currency))
.addSubcommand(CommandReset(plugin, currency))
.addSubcommand(CommandPay(plugin, currency))
.addSubcommand(CommandBalance(plugin, currency))
.addSubcommand(CommandTake(plugin, currency))
.addSubcommand(CommandTakesilent(plugin, currency))
}
override fun onExecute(sender: CommandSender, args: MutableList<String>) {
sender.sendMessage(this.plugin.langYml.getMessage("invalid-command"))
sender.sendMessage(
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%"
}
}
}
}