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

Merge pull request #4 from Opalinium/main

Add `all` arg for `CommandReset`
This commit is contained in:
Opal
2023-06-19 01:44:27 -07:00
committed by GitHub
2 changed files with 42 additions and 23 deletions

View File

@@ -7,9 +7,9 @@ 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
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
@@ -30,6 +30,11 @@ class CommandReset(
return
}
if (args[0].equals("all", true)) {
resetAllPlayersCurrency(sender, args)
return
}
@Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0])
@@ -38,27 +43,42 @@ class CommandReset(
return
}
if (this.currency == null) {
if (args.size < 2) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
val currency = determineCurrency(sender, args) ?: return
resetPlayerCurrency(sender, player, currency)
}
val currency = this.currency ?: Currencies.getByID(args[1].lowercase())
private fun determineCurrency(sender: CommandSender, args: List<String>): Currency? {
val currency = this.currency ?: if (args.size > argOffset + 1) Currencies.getByID(args[argOffset + 1].lowercase()) else null
if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return
}
return currency
}
private fun resetPlayerCurrency(sender: CommandSender, player: OfflinePlayer, currency: Currency) {
player.setBalance(currency, currency.default)
sender.sendMessage(
player.name?.let {
plugin.langYml.getMessage("reset-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.savedDisplayName)
.replace("%amount%", currency.default.toNiceString())
.replace("%currency%", currency.name)
}
)
}
private fun resetAllPlayersCurrency(sender: CommandSender, args: List<String>) {
val currency = determineCurrency(sender, args) ?: return
Bukkit.getOfflinePlayers().forEach { player ->
player.setBalance(currency, currency.default)
}
sender.sendMessage(
plugin.langYml.getMessage("reset-all-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%amount%", currency.default.toNiceString())
.replace("%currency%", currency.name)
)
}
@@ -66,26 +86,24 @@ class CommandReset(
val completions = mutableListOf<String>()
if (args.isEmpty()) {
return Bukkit.getOnlinePlayers().map { it.name }
return Bukkit.getOfflinePlayers().mapNotNull { it.name }
}
if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Bukkit.getOnlinePlayers().map { it.name },
Bukkit.getOfflinePlayers().map { it.name } + listOf("all"),
completions
)
}
if (this.currency == null) {
if (args.size == 2) {
if (this.currency == null && args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
}
return completions
}

View File

@@ -13,6 +13,7 @@ messages:
invalid-amount: "&cInvalid amount!"
set-currency: "&fSet %player%&r to have &a%amount%&r &f%currency%&f!"
reset-currency: "&fReset %player%&r to have &a%amount%&r &f%currency%&f!"
reset-all-currency: "&fReset All Players&r to have &a%amount%&r &f%currency%&f!"
gave-currency: "&fGave %player%&r &a%amount%&r &f%currency%&f!"
other-balance: "&f%player%&f has &a%amount%&r &f%currency%&f!"
balance: "You have &a%amount%&r &f%currency%&f!"