diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/CommandReset.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/CommandReset.kt index 3109a24..4f03a53 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/CommandReset.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/commands/CommandReset.kt @@ -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,25 +43,40 @@ class CommandReset( return } - if (this.currency == null) { - if (args.size < 2) { - sender.sendMessage(plugin.langYml.getMessage("must-specify-currency")) - return - } - } - - val currency = this.currency ?: Currencies.getByID(args[1].lowercase()) + val currency = determineCurrency(sender, args) ?: return + resetPlayerCurrency(sender, player, currency) + } + private fun determineCurrency(sender: CommandSender, args: List): 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( - plugin.langYml.getMessage("reset-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) - .replace("%player%", player.savedDisplayName) + 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) { + 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,27 +86,25 @@ class CommandReset( val completions = mutableListOf() 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) { - StringUtil.copyPartialMatches( - args[1], - Currencies.values().map { it.id }, - completions - ) - } + if (this.currency == null && args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + Currencies.values().map { it.id }, + completions + ) } return completions } -} +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index df1aeb1..3646a01 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -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!"