From a51121031190b5fce2b996e70310b076114fb737 Mon Sep 17 00:00:00 2001 From: refractored Date: Fri, 6 Sep 2024 23:35:26 -0500 Subject: [PATCH 1/2] feat: Starting boosters from console --- .../boosters/boosters/ActivatedBooster.kt | 6 +-- .../com/willfp/boosters/boosters/Booster.kt | 26 ++++++++--- .../boosters/commands/CommandActivate.kt | 44 ++++++++++++++++++- .../core-plugin/src/main/resources/lang.yml | 41 ++++++++--------- 4 files changed, 86 insertions(+), 31 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/ActivatedBooster.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/ActivatedBooster.kt index f26cf9d..ea98f39 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/ActivatedBooster.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/ActivatedBooster.kt @@ -49,10 +49,10 @@ fun Server.scanForBoosters() { data class ActivatedBooster( val booster: Booster, - val uuid: UUID + val uuid: UUID? ) { - val player: OfflinePlayer - get() = Bukkit.getOfflinePlayer(uuid) + val player: OfflinePlayer? + get() = uuid?.let { Bukkit.getOfflinePlayer(it) } override fun equals(other: Any?): Boolean { if (other !is ActivatedBooster) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt index 830af63..5340f58 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt @@ -55,7 +55,11 @@ class Booster( return null } - val uuid = UUID.fromString(activeKey) + val uuid = try { + UUID.fromString(activeKey) + } catch (e: IllegalArgumentException) { + null + } return ActivatedBooster(this, uuid) } @@ -75,7 +79,7 @@ class Booster( val duration = config.getInt("duration") - fun getActivationMessages(player: Player): List { + fun getActivationMessages(player: Player?): List { val messages = mutableListOf() for (string in config.getFormattedStrings( @@ -83,7 +87,7 @@ class Booster( StringUtils.FormatOption.WITHOUT_PLACEHOLDERS )) { @Suppress("DEPRECATION") - messages.add(string.replace("%player%", player.displayName)) + messages.add(string.replace("%player%", player?.displayName ?: plugin.langYml.getString("console-displayname"))) } return messages @@ -132,7 +136,7 @@ class Booster( if (active != null) { plugin.langYml.getString("active-placeholder") - .replace("%player%", active.player.savedDisplayName) + .replace("%player%", active.player?.savedDisplayName ?: plugin.langYml.getString("console-displayname")) .replace("%booster%", active.booster.name) .formatEco(formatPlaceholders = false) } else { @@ -147,7 +151,13 @@ class Booster( plugin, "${id}_player", ) { - active?.player?.savedDisplayName ?: "" + val active = this.active + + if (active == null) { + "" + } else { + active.player?.savedDisplayName ?: plugin.langYml.getString("console-displayname").formatEco(formatPlaceholders = false) + } } ) @@ -158,7 +168,11 @@ class Booster( ) { val active = this.active - active?.booster?.name ?: "" + if (active == null) { + "" + } else { + active.player?.name ?: plugin.langYml.getString("console-displayname").formatEco(formatPlaceholders = false) + } } ) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt index f4022bd..ff804e0 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt @@ -1,10 +1,16 @@ package com.willfp.boosters.commands import com.willfp.boosters.activateBooster +import com.willfp.boosters.boosters.ActivatedBooster +import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Boosters +import com.willfp.boosters.boosters.activateBooster import com.willfp.boosters.incrementBoosters import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.util.formatEco +import org.bukkit.Bukkit +import org.bukkit.Sound import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.util.StringUtil @@ -14,11 +20,10 @@ class CommandActivate(plugin: EcoPlugin) : plugin, "activate", "boosters.command.activate", - true + false ) { override fun onExecute(sender: CommandSender, args: List) { - val player = sender as? Player ?: return if (args.isEmpty()) { sender.sendMessage(plugin.langYml.getMessage("requires-booster")) @@ -32,10 +37,45 @@ class CommandActivate(plugin: EcoPlugin) : return } + val player = sender as? Player + + if (player == null) { + activateBoosterConsole(booster) + return + } + player.incrementBoosters(booster, 1) player.activateBooster(booster) } + private fun activateBoosterConsole(booster: Booster) { + + for (activationCommand in booster.activationCommands) { + Bukkit.dispatchCommand( + Bukkit.getConsoleSender(), + activationCommand.replace("%player%", plugin.langYml.getMessage("console-displayname").formatEco(formatPlaceholders = false)) + ) + } + + for (activationMessage in booster.getActivationMessages(null)) { + @Suppress("DEPRECATION") + Bukkit.broadcastMessage(activationMessage) + } + + Bukkit.getServer().activateBooster( + ActivatedBooster(booster, null) + ) + + for (player in Bukkit.getOnlinePlayers()) { + player.playSound( + player.location, + Sound.UI_TOAST_CHALLENGE_COMPLETE, + 2f, + 0.9f + ) + } + } + override fun tabComplete(sender: CommandSender, args: List): List { val completions = mutableListOf() diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 8470f42..72ebebb 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -1,20 +1,21 @@ -messages: - prefix: "&a&lBoosters&r &8» &r" - no-permission: "&cYou don't have permission to do this!" - not-player: "&cThis command must be run by a player" - invalid-command: "&cUnknown subcommand!" - reloaded: "Reloaded!" - - requires-player: "&cYou must specify a player!" - invalid-player: "&cInvalid player!" - requires-booster: "&cYou must specify a booster!" - invalid-booster: "&cInvalid booster!" - gave-booster: "Gave %player% %booster% &fx%amount%!" - already-active: "&cThis booster is already active!" - dont-have: "&cYou don't have any of these boosters! Get some at &astore.ecomc.net" - cancelled: "Cancelled the active boosters" - -no-currently-active: "&cNot Active!" -no-currently-active-list: "&cThere isn't any booster active!" -no-currently-active-ids-list: "None" -active-placeholder: "&fThanks %player% &ffor activating %booster%&f!" +messages: + prefix: "&a&lBoosters&r &8» &r" + no-permission: "&cYou don't have permission to do this!" + not-player: "&cThis command must be run by a player" + invalid-command: "&cUnknown subcommand!" + reloaded: "Reloaded!" + + requires-player: "&cYou must specify a player!" + invalid-player: "&cInvalid player!" + requires-booster: "&cYou must specify a booster!" + invalid-booster: "&cInvalid booster!" + gave-booster: "Gave %player% %booster% &fx%amount%!" + already-active: "&cThis booster is already active!" + dont-have: "&cYou don't have any of these boosters! Get some at &astore.ecomc.net" + cancelled: "Cancelled the active boosters" + +no-currently-active: "&cNot Active!" +no-currently-active-list: "&cThere isn't any booster active!" +no-currently-active-ids-list: "None" +active-placeholder: "&fThanks %player% &ffor activating %booster%&f!" +console-displayname: "&cConsole" \ No newline at end of file From 3849a4f6b4779ffbd7fd803fe595283140f81216 Mon Sep 17 00:00:00 2001 From: refractored Date: Fri, 6 Sep 2024 23:51:49 -0500 Subject: [PATCH 2/2] refactor(CommandActivate) --- .../com/willfp/boosters/BoosterUtils.kt | 29 +++++++++++++++++ .../boosters/commands/CommandActivate.kt | 31 ++----------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt index c3c9666..9316caa 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt @@ -7,8 +7,10 @@ import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Boosters import com.willfp.boosters.boosters.activateBooster import com.willfp.eco.core.data.profile +import com.willfp.eco.util.formatEco import org.bukkit.Bukkit import org.bukkit.OfflinePlayer +import org.bukkit.Server import org.bukkit.Sound import org.bukkit.entity.Player @@ -38,6 +40,33 @@ fun OfflinePlayer.incrementBoosters(booster: Booster, amount: Int) { this.setAmountOfBooster(booster, this.getAmountOfBooster(booster) + amount) } +fun Server.activateBoosterConsole(booster: Booster) { + for (activationCommand in booster.activationCommands) { + Bukkit.dispatchCommand( + Bukkit.getConsoleSender(), + activationCommand.replace("%player%", BoostersPlugin.instance.langYml.getMessage("console-displayname").formatEco(formatPlaceholders = false)) + ) + } + + for (activationMessage in booster.getActivationMessages(null)) { + @Suppress("DEPRECATION") + Bukkit.broadcastMessage(activationMessage) + } + + Bukkit.getServer().activateBooster( + ActivatedBooster(booster, null) + ) + + for (player in Bukkit.getOnlinePlayers()) { + player.playSound( + player.location, + Sound.UI_TOAST_CHALLENGE_COMPLETE, + 2f, + 0.9f + ) + } +} + fun Player.activateBooster(booster: Booster): Boolean { val amount = this.getAmountOfBooster(booster) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt index ff804e0..3548bcb 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandActivate.kt @@ -1,6 +1,7 @@ package com.willfp.boosters.commands import com.willfp.boosters.activateBooster +import com.willfp.boosters.activateBoosterConsole import com.willfp.boosters.boosters.ActivatedBooster import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Boosters @@ -40,7 +41,7 @@ class CommandActivate(plugin: EcoPlugin) : val player = sender as? Player if (player == null) { - activateBoosterConsole(booster) + Bukkit.getServer().activateBoosterConsole(booster) return } @@ -48,34 +49,6 @@ class CommandActivate(plugin: EcoPlugin) : player.activateBooster(booster) } - private fun activateBoosterConsole(booster: Booster) { - - for (activationCommand in booster.activationCommands) { - Bukkit.dispatchCommand( - Bukkit.getConsoleSender(), - activationCommand.replace("%player%", plugin.langYml.getMessage("console-displayname").formatEco(formatPlaceholders = false)) - ) - } - - for (activationMessage in booster.getActivationMessages(null)) { - @Suppress("DEPRECATION") - Bukkit.broadcastMessage(activationMessage) - } - - Bukkit.getServer().activateBooster( - ActivatedBooster(booster, null) - ) - - for (player in Bukkit.getOnlinePlayers()) { - player.playSound( - player.location, - Sound.UI_TOAST_CHALLENGE_COMPLETE, - 2f, - 0.9f - ) - } - } - override fun tabComplete(sender: CommandSender, args: List): List { val completions = mutableListOf()