Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to reset-buys for all players #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>): List<String> {
val completions = mutableListOf<String>()

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
}
}
4 changes: 4 additions & 0 deletions eco-core/core-plugin/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down