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

Add Option to Control Command Target for Offline Players #11

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ allprojects {
compileOnly("org.jetbrains:annotations:23.0.0")
compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.9.20")

compileOnly("me.clip:placeholderapi:2.11.2")
compileOnly("me.clip:placeholderapi:2.11.6")
compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.0")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import org.bukkit.event.Listener
class EcoBitsPlugin : EcoPlugin() {
val serverID = configYml.getString("server-id")

//
val allowOfflinePlayersString = configYml.getString("allow-offline-players-in-commands")
val allowOfflinePlayers = allowOfflinePlayersString.toBoolean()
//

init {
instance = this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,45 @@ import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.EcoBitsPlugin
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.getBalance
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil

class CommandGet(
plugin: EcoPlugin,
private val currency: Currency? = null
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"get",
"ecobits.command.get",
false
plugin,
"get",
"ecobits.command.get",
false
) {
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-player"))
return
}

@Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0])
val allowOfflinePlayers = EcoBitsPlugin.instance.allowOfflinePlayers
val targetPlayer: OfflinePlayer? = if (allowOfflinePlayers) {
Bukkit.getOfflinePlayer(args[0])
} else {
Bukkit.getPlayer(args[0])
}

if (!player.hasPlayedBefore() && !player.isOnline) {
// Don't allow the usage on offline players
if (targetPlayer == null || (!allowOfflinePlayers && !targetPlayer.isOnline)) {
sender.sendMessage(plugin.langYml.getMessage("player-not-online"))
return
}

// Don't allow the usage on player that don't exist
if (!targetPlayer.hasPlayedBefore() && !targetPlayer.isOnline) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return
}
Expand All @@ -50,10 +63,10 @@ class CommandGet(
}

sender.sendMessage(
plugin.langYml.getMessage("other-balance", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.savedDisplayName)
.replace("%amount%", player.getBalance(currency).toNiceString())
.replace("%currency%", currency.name)
plugin.langYml.getMessage("other-balance", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", targetPlayer.savedDisplayName)
.replace("%amount%", targetPlayer.getBalance(currency).toNiceString())
.replace("%currency%", currency.name)
)
}

Expand All @@ -66,20 +79,18 @@ class CommandGet(

if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
)
}

if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
if (this.currency == null && args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
)
}

return completions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.EcoBitsPlugin
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil

class CommandGive(
plugin: EcoPlugin,
private val currency: Currency? = null
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"give",
"ecobits.command.give",
false
plugin,
"give",
"ecobits.command.give",
false
) {
private val argOffset = if (currency == null) 0 else -1

Expand All @@ -29,10 +31,21 @@ class CommandGive(
return
}

@Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0])
val allowOfflinePlayers = EcoBitsPlugin.instance.allowOfflinePlayers
val targetPlayer: OfflinePlayer? = if (allowOfflinePlayers) {
Bukkit.getOfflinePlayer(args[0])
} else {
Bukkit.getPlayer(args[0])
}

if (!player.hasPlayedBefore() && !player.isOnline) {
// Don't allow the usage on offline players
if (targetPlayer == null || (!allowOfflinePlayers && !targetPlayer.isOnline)) {
sender.sendMessage(plugin.langYml.getMessage("player-not-online"))
return
}

// Don't allow the usage on player that don't exist
if (!targetPlayer.hasPlayedBefore() && !targetPlayer.isOnline) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return
}
Expand Down Expand Up @@ -63,13 +76,13 @@ class CommandGive(
return
}

player.adjustBalance(currency, amount.toBigDecimal())
targetPlayer.adjustBalance(currency, amount.toBigDecimal())

sender.sendMessage(
plugin.langYml.getMessage("gave-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.savedDisplayName)
.replace("%amount%", amount.toNiceString())
.replace("%currency%", currency.name)
plugin.langYml.getMessage("gave-currency", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", targetPlayer.savedDisplayName)
.replace("%amount%", amount.toNiceString())
.replace("%currency%", currency.name)
)
}

Expand All @@ -82,27 +95,25 @@ class CommandGive(

if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
)
}

if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
if (this.currency == null && args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
)
}

if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecobits.EcoBitsPlugin
import com.willfp.ecobits.currencies.Currencies
import com.willfp.ecobits.currencies.Currency
import com.willfp.ecobits.currencies.adjustBalance
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil

class CommandGivesilent(
plugin: EcoPlugin,
private val currency: Currency? = null
plugin: EcoPlugin,
private val currency: Currency? = null
) : Subcommand(
plugin,
"givesilent",
"ecobits.command.givesilent",
false
plugin,
"givesilent",
"ecobits.command.givesilent",
false
) {
private val argOffset = if (currency == null) 0 else -1

Expand All @@ -28,10 +30,22 @@ class CommandGivesilent(
return
}

@Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0])
val allowOfflinePlayers = EcoBitsPlugin.instance.allowOfflinePlayers
val targetPlayer: OfflinePlayer? = if (allowOfflinePlayers) {
Bukkit.getOfflinePlayer(args[0])
} else {
Bukkit.getPlayer(args[0])
}

if (!player.hasPlayedBefore() && !player.isOnline) {
// Don't allow the usage on offline players
if (targetPlayer == null || (!allowOfflinePlayers && !targetPlayer.isOnline)) {
sender.sendMessage(plugin.langYml.getMessage("player-not-online"))
return
}

// Don't allow the usage on player that don't exist
if (!targetPlayer.hasPlayedBefore() && !targetPlayer.isOnline) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return
}

Expand All @@ -49,7 +63,7 @@ class CommandGivesilent(

val amount = args[2 + argOffset].toDoubleOrNull() ?: return

player.adjustBalance(currency, amount.toBigDecimal())
targetPlayer.adjustBalance(currency, amount.toBigDecimal())
}

override fun tabComplete(sender: CommandSender, args: List<String>): List<String> {
Expand All @@ -61,27 +75,25 @@ class CommandGivesilent(

if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
args[0],
Bukkit.getOnlinePlayers().map { it.name },
completions
)
}

if (this.currency == null) {
if (args.size == 2) {
StringUtil.copyPartialMatches(
if (this.currency == null && args.size == 2) {
StringUtil.copyPartialMatches(
args[1],
Currencies.values().map { it.id },
completions
)
}
)
}

if (args.size == 3 + argOffset) {
StringUtil.copyPartialMatches(
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
args[2 + argOffset],
arrayOf(1, 2, 3, 4, 5).map { it.toString() },
completions
)
}

Expand Down
Loading