9
0
mirror of https://github.com/Auxilor/EcoBits.git synced 2025-12-24 01:19:22 +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.eco.util.toNiceString
import com.willfp.ecobits.currencies.Currencies import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.getBalance
import com.willfp.ecobits.currencies.setBalance import com.willfp.ecobits.currencies.setBalance
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil import org.bukkit.util.StringUtil
@@ -30,6 +30,11 @@ class CommandReset(
return return
} }
if (args[0].equals("all", true)) {
resetAllPlayersCurrency(sender, args)
return
}
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0]) val player = Bukkit.getOfflinePlayer(args[0])
@@ -38,27 +43,42 @@ class CommandReset(
return return
} }
if (this.currency == null) { val currency = determineCurrency(sender, args) ?: return
if (args.size < 2) { resetPlayerCurrency(sender, player, currency)
sender.sendMessage(plugin.langYml.getMessage("must-specify-currency"))
return
}
} }
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) { if (currency == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-currency")) sender.sendMessage(plugin.langYml.getMessage("invalid-currency"))
return }
return currency
} }
private fun resetPlayerCurrency(sender: CommandSender, player: OfflinePlayer, currency: Currency) {
player.setBalance(currency, currency.default) player.setBalance(currency, currency.default)
sender.sendMessage( sender.sendMessage(
player.name?.let {
plugin.langYml.getMessage("reset-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) plugin.langYml.getMessage("reset-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.savedDisplayName) .replace("%player%", player.savedDisplayName)
.replace("%amount%", currency.default.toNiceString()) .replace("%amount%", currency.default.toNiceString())
.replace("%currency%", currency.name) .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>() val completions = mutableListOf<String>()
if (args.isEmpty()) { if (args.isEmpty()) {
return Bukkit.getOnlinePlayers().map { it.name } return Bukkit.getOfflinePlayers().mapNotNull { it.name }
} }
if (args.size == 1) { if (args.size == 1) {
StringUtil.copyPartialMatches( StringUtil.copyPartialMatches(
args[0], args[0],
Bukkit.getOnlinePlayers().map { it.name }, Bukkit.getOfflinePlayers().map { it.name } + listOf("all"),
completions completions
) )
} }
if (this.currency == null) { if (this.currency == null && args.size == 2) {
if (args.size == 2) {
StringUtil.copyPartialMatches( StringUtil.copyPartialMatches(
args[1], args[1],
Currencies.values().map { it.id }, Currencies.values().map { it.id },
completions completions
) )
} }
}
return completions return completions
} }

View File

@@ -13,6 +13,7 @@ messages:
invalid-amount: "&cInvalid amount!" invalid-amount: "&cInvalid amount!"
set-currency: "&fSet %player%&r to have &a%amount%&r &f%currency%&f!" 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-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!" gave-currency: "&fGave %player%&r &a%amount%&r &f%currency%&f!"
other-balance: "&f%player%&f has &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!" balance: "You have &a%amount%&r &f%currency%&f!"