From 14e5fad21aa431f7dbe387408268b7837c4a7870 Mon Sep 17 00:00:00 2001 From: Exanthiax <107284021+Exanthiax@users.noreply.github.com> Date: Sun, 9 Apr 2023 20:24:55 +0100 Subject: [PATCH] Ability to reset-buys for all players --- .../ecoshop/commands/CommandResetBuys.kt | 82 ++++++++++--------- .../core-plugin/src/main/resources/lang.yml | 4 + 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandResetBuys.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandResetBuys.kt index 1621df5..47db9ff 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandResetBuys.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandResetBuys.kt @@ -3,6 +3,7 @@ package com.willfp.ecoshop.commands import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.impl.Subcommand import com.willfp.eco.util.StringUtils +import com.willfp.eco.util.formatEco import com.willfp.eco.util.savedDisplayName import com.willfp.ecoshop.shop.ShopItems import org.bukkit.Bukkit @@ -20,59 +21,64 @@ class CommandResetBuys(plugin: EcoPlugin) : Subcommand( sender.sendMessage(plugin.langYml.getMessage("must-specify-player")) return } - - @Suppress("DEPRECATION") - val player = Bukkit.getOfflinePlayer(args[0]) - - if (!player.hasPlayedBefore() && !player.isOnline) { - sender.sendMessage(plugin.langYml.getMessage("invalid-player")) - return - } - if (args.size == 1) { sender.sendMessage(plugin.langYml.getMessage("must-specify-item")) return } - - val item = ShopItems.getByID(args[1]) - + val itemID = args[1] + val item = ShopItems.getByID(itemID) if (item == null) { sender.sendMessage(plugin.langYml.getMessage("invalid-item")) return } - - item.resetTimesBought(player) - - sender.sendMessage( - plugin.langYml.getMessage("reset-buys", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + val playerName = args[0] + val searchPhrase = plugin.langYml.getString("messages.all-player-search-phrase") + val offlinePlayers = Bukkit.getOfflinePlayers() + val isResettingAllPlayers = playerName.equals(searchPhrase, ignoreCase = true) + if (isResettingAllPlayers) { + offlinePlayers.forEach { player -> + item.resetTimesBought(player) + } + val broadcast = plugin.langYml.getStrings("messages.broadcast-all-players-reset-buys").formatEco() + broadcast.forEach { message -> + Bukkit.broadcastMessage(message.replace("%item%", item.displayName)) + } + val message = plugin.langYml.getMessage("command-all-players-reset-buys") + sender.sendMessage(message) + } else { + val player = offlinePlayers.find { it.name?.equals(playerName, ignoreCase = true) == true } + if (player == null) { + sender.sendMessage(plugin.langYml.getMessage("invalid-player")) + return + } + item.resetTimesBought(player) + val message = plugin.langYml.getMessage("reset-buys", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) .replace("%player%", player.savedDisplayName) .replace("%item%", item.displayName) - ) + sender.sendMessage(message) + } } override fun tabComplete(sender: CommandSender, args: List): List { val completions = mutableListOf() - - if (args.isEmpty()) { - return Bukkit.getOnlinePlayers().map { it.name } + val searchPhrase = plugin.langYml.getString("messages.all-player-search-phrase") + val nonNullPlayerNames = Bukkit.getOnlinePlayers().map { it.name } + when (args.size) { + // When no arguments, return 'online' player names. + 0 -> { + return nonNullPlayerNames + } + // When 1 argument, return partial matches of 'online' player names (and ALL). + 1 -> { + StringUtil.copyPartialMatches(args[0], nonNullPlayerNames, completions) + completions.add(searchPhrase) + } + // When 2 arguments, return partial matches of Shop Item Ids. + 2 -> { + val shopItemIds = ShopItems.values().map { it.id } + StringUtil.copyPartialMatches(args[1], shopItemIds, completions) + } } - - if (args.size == 1) { - StringUtil.copyPartialMatches( - args[0], - Bukkit.getOnlinePlayers().map { it.name }, - completions - ) - } - - if (args.size == 2) { - StringUtil.copyPartialMatches( - args[1], - ShopItems.values().map { it.id }, - completions - ) - } - return completions } } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 19d95e8..7d54386 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -4,6 +4,10 @@ messages: not-player: "&cThis command must be run by a player" invalid-command: "&cUnknown subcommand!" reloaded: "Reloaded! Took %time%ms" + all-player-search-phrase: "@everyone" + + broadcast-all-players-reset-buys: "&3Mobcoins Shop Has been RESET!" + command-all-players-reset-buys: "&3Purchases limits have been reset for all players" must-specify-player: "&cYou must specify a player!" invalid-player: "&cInvalid player!"