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

Added ability to activate boosters from console #40

Open
wants to merge 2 commits into
base: master
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 @@ -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

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -75,15 +79,15 @@ class Booster(

val duration = config.getInt("duration")

fun getActivationMessages(player: Player): List<String> {
fun getActivationMessages(player: Player?): List<String> {
val messages = mutableListOf<String>()

for (string in config.getFormattedStrings(
"messages.activation",
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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
)

Expand All @@ -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)
}
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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
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
Expand All @@ -14,11 +21,10 @@ class CommandActivate(plugin: EcoPlugin) :
plugin,
"activate",
"boosters.command.activate",
true
false
) {

override fun onExecute(sender: CommandSender, args: List<String>) {
val player = sender as? Player ?: return

if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("requires-booster"))
Expand All @@ -32,6 +38,13 @@ class CommandActivate(plugin: EcoPlugin) :
return
}

val player = sender as? Player

if (player == null) {
Bukkit.getServer().activateBoosterConsole(booster)
return
}

player.incrementBoosters(booster, 1)
player.activateBooster(booster)
}
Expand Down
41 changes: 21 additions & 20 deletions eco-core/core-plugin/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
@@ -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"