9
0
mirror of https://github.com/Auxilor/EcoSkills.git synced 2026-01-01 21:36:34 +00:00

Improved commands, readded /skills toggleactionbar

This commit is contained in:
Auxilor
2023-05-14 16:50:16 +01:00
parent 781098e66d
commit 6615cfe445
13 changed files with 179 additions and 97 deletions

View File

@@ -0,0 +1,16 @@
package com.willfp.ecoskills
import com.willfp.eco.core.registry.Registrable
import com.willfp.eco.core.registry.Registry
import com.willfp.libreforge.loader.configs.ConfigCategory
abstract class CategoryWithRegistry<T : Registrable>(
id: String,
directory: String
) : ConfigCategory(id, directory) {
protected val registry = Registry<T>()
fun getByID(id: String?): T? = id?.let { registry[id] }
fun values(): Set<T> = registry.values()
}

View File

@@ -1,8 +1,12 @@
package com.willfp.ecoskills.actionbar
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.data.keys.PersistentDataKey
import com.willfp.eco.core.data.keys.PersistentDataKeyType
import com.willfp.eco.core.data.profile
import com.willfp.eco.core.placeholder.context.placeholderContext
import com.willfp.eco.util.containsIgnoreCase
import com.willfp.eco.util.namespacedKeyOf
import org.bukkit.Bukkit
import org.bukkit.GameMode
import org.bukkit.entity.Player
@@ -18,6 +22,19 @@ private val blacklist = mutableMapOf<UUID, Long>()
private val whitelist = mutableMapOf<UUID, Long>()
private val actionBarEnabledKey = PersistentDataKey(
namespacedKeyOf("ecoskills", "actionbar_enabled"),
PersistentDataKeyType.BOOLEAN,
true
)
fun Player.togglePersistentActionBar() {
this.profile.write(actionBarEnabledKey, !this.profile.read(actionBarEnabledKey))
}
val Player.isPersistentActionBarEnabled: Boolean
get() = this.profile.read(actionBarEnabledKey)
fun Player.sendCompatibleActionBarMessage(message: String) {
// Have to use the shit method for compatibility.
@Suppress("DEPRECATION")
@@ -68,7 +85,7 @@ class ActionBarHandler(
.getStrings("persistent-action-bar.disabled-in-worlds")
private fun trySendMessage(player: Player) {
if (player.isPersistentActionBarPaused) {
if (player.isPersistentActionBarPaused || !player.isPersistentActionBarEnabled) {
return
}

View File

@@ -17,6 +17,7 @@ import com.willfp.ecoskills.stats.statModifiers
import com.willfp.ecoskills.stats.stats
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import sun.jvm.hotspot.oops.CellTypeState.value
import java.util.UUID
/*
@@ -81,6 +82,10 @@ fun OfflinePlayer.getBaseStatLevel(stat: Stat): Int =
fun OfflinePlayer.setBaseStatLevel(stat: Stat, value: Int) =
this.stats.set(stat, value)
fun OfflinePlayer.giveBaseStatLevel(stat: Stat, amount: Int) {
this.stats[stat] += amount
}
fun OfflinePlayer.getStatLevel(stat: Stat): Int =
if (this is Player) this.statModifiers.getModifiedValue(stat) else this.getBaseStatLevel(stat)

View File

@@ -4,8 +4,12 @@ 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.ecoskills.api.giveBaseStatLevel
import com.willfp.ecoskills.api.giveSkillXP
import com.willfp.ecoskills.skills.Skill
import com.willfp.ecoskills.skills.Skills
import com.willfp.ecoskills.stats.Stat
import com.willfp.ecoskills.stats.Stats
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
@@ -20,49 +24,34 @@ class CommandGive(plugin: EcoPlugin) :
) {
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("requires-player"))
return
val player = notifyPlayerRequired(args.getOrNull(0), "invalid-player")
val obj = notifyNull(
Skills.getByID(args.getOrNull(1)) ?: Stats.getByID(args.getOrNull(1)),
"invalid-skill-stat"
)
val amount = notifyNull(args.getOrNull(2)?.toIntOrNull(), "invalid-amount")
val key = when (obj) {
is Skill -> {
player.giveSkillXP(obj, amount.toDouble())
"gave-skill-xp"
}
is Stat -> {
player.giveBaseStatLevel(obj, amount)
"gave-stat"
}
else -> ""
}
if (args.size == 1) {
sender.sendMessage(plugin.langYml.getMessage("requires-skill"))
return
}
if (args.size == 2) {
sender.sendMessage(plugin.langYml.getMessage("requires-amount"))
return
}
val player = Bukkit.getPlayer(args[0])
if (player == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return
}
val obj = Skills.getByID(args[1].lowercase())
if (obj == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-skill"))
return
}
val amount = args[2].toIntOrNull()
if (amount == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-amount"))
return
}
player.giveSkillXP(obj, amount.toDouble())
sender.sendMessage(
this.plugin.langYml.getMessage("gave-skill-xp", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
this.plugin.langYml.getMessage(key, StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.name)
.replace("%amount%", amount.toString())
.replace("%skill%", obj.name)
.replace("%obj%", obj.name)
.formatEco()
)
}

View File

@@ -5,6 +5,7 @@ package com.willfp.ecoskills.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.ecoskills.api.resetSkills
import com.willfp.ecoskills.util.offlinePlayers
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
@@ -18,25 +19,22 @@ class CommandReset(plugin: EcoPlugin) :
) {
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("requires-player"))
return
val players = offlinePlayers(args, 0, "invalid-player")
if (players.size > 1) {
sender.sendMessage(plugin.langYml.getMessage("resetting-all-players"))
}
if (args[0].equals("all", ignoreCase = true)) {
sender.sendMessage(plugin.langYml.getMessage("resetting-all-players"))
Bukkit.getOfflinePlayers().forEach { it.resetSkills() }
sender.sendMessage(plugin.langYml.getMessage("reset-all-players"))
} else {
val player = Bukkit.getOfflinePlayer(args[0])
if (!player.hasPlayedBefore()) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return
}
sender.sendMessage(plugin.langYml.getMessage("reset-player"))
for (player in players) {
player.resetSkills()
}
if (players.size > 1) {
sender.sendMessage(plugin.langYml.getMessage("reset-all-players"))
return
} else {
sender.sendMessage(plugin.langYml.getMessage("reset-player"))
}
}
override fun tabComplete(sender: CommandSender, args: List<String>): List<String> {
@@ -45,7 +43,7 @@ class CommandReset(plugin: EcoPlugin) :
if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Bukkit.getOnlinePlayers().map { player -> player.name },
listOf("all") union Bukkit.getOnlinePlayers().map { player -> player.name },
completions
)
return completions
@@ -53,4 +51,4 @@ class CommandReset(plugin: EcoPlugin) :
return emptyList()
}
}
}

View File

@@ -3,7 +3,10 @@ package com.willfp.ecoskills.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.ecoskills.gui.menus.SkillsGUI
import com.willfp.ecoskills.skills.Skills
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.util.StringUtil
class CommandSkills(plugin: EcoPlugin) : PluginCommand(
plugin,
@@ -11,8 +14,33 @@ class CommandSkills(plugin: EcoPlugin) : PluginCommand(
"ecoskills.command.skills",
true
) {
init {
this.addSubcommand(CommandToggleActionBar(plugin))
}
override fun onExecute(player: Player, args: List<String>) {
SkillsGUI.open(player)
if (args.isEmpty()) {
SkillsGUI.open(player)
return
}
val skill = notifyNull(Skills.getByID(args.getOrNull(0)), "invalid-skill")
skill.levelGUI.open(player)
}
override fun tabComplete(sender: CommandSender, args: List<String>): List<String> {
val completions = mutableListOf<String>()
if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Skills.values().map { it.id },
completions
)
return completions
}
return emptyList()
}
override fun getAliases(): List<String> {

View File

@@ -0,0 +1,29 @@
@file:Suppress("DEPRECATION")
package com.willfp.ecoskills.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.ecoskills.actionbar.isPersistentActionBarEnabled
import com.willfp.ecoskills.actionbar.sendCompatibleActionBarMessage
import com.willfp.ecoskills.actionbar.togglePersistentActionBar
import org.bukkit.entity.Player
class CommandToggleActionBar(plugin: EcoPlugin) : Subcommand(
plugin, "toggleactionbar", "ecoskills.command.toggleactionbar", true
) {
override fun onExecute(player: Player, args: List<String>) {
when (player.isPersistentActionBarEnabled) {
true -> {
player.sendCompatibleActionBarMessage("")
player.sendMessage(plugin.langYml.getMessage("disabled-actionbar"))
}
false -> player.sendMessage(plugin.langYml.getMessage("enabled-actionbar"))
}
player.togglePersistentActionBar()
}
}

View File

@@ -1,14 +1,11 @@
package com.willfp.ecoskills.effects
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.registry.Registry
import com.willfp.ecoskills.CategoryWithRegistry
import com.willfp.ecoskills.EcoSkillsPlugin
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.ConfigCategory
object Effects : ConfigCategory("effect", "effects") {
private val registry = Registry<Effect>()
object Effects : CategoryWithRegistry<Effect>("effect", "effects") {
override val supportsSharing = false
override fun clear(plugin: LibreforgePlugin) {
@@ -18,8 +15,4 @@ object Effects : ConfigCategory("effect", "effects") {
override fun acceptConfig(plugin: LibreforgePlugin, id: String, config: Config) {
registry.register(Effect(id, config, plugin as EcoSkillsPlugin))
}
fun getByID(id: String): Effect? = registry[id]
fun values(): Set<Effect> = registry.values()
}

View File

@@ -1,14 +1,11 @@
package com.willfp.ecoskills.mana
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.registry.Registry
import com.willfp.ecoskills.CategoryWithRegistry
import com.willfp.ecoskills.EcoSkillsPlugin
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.ConfigCategory
object MagicTypes : ConfigCategory("magic_type", "magic_types") {
private val registry = Registry<MagicType>()
object MagicTypes : CategoryWithRegistry<MagicType>("magic_type", "magic_types") {
override val supportsSharing = false
override fun clear(plugin: LibreforgePlugin) {
@@ -18,8 +15,4 @@ object MagicTypes : ConfigCategory("magic_type", "magic_types") {
override fun acceptConfig(plugin: LibreforgePlugin, id: String, config: Config) {
registry.register(MagicType(id, config, plugin as EcoSkillsPlugin))
}
fun getByID(id: String): MagicType? = registry[id]
fun values(): Set<MagicType> = registry.values()
}

View File

@@ -1,16 +1,13 @@
package com.willfp.ecoskills.skills
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.registry.Registry
import com.willfp.ecoskills.CategoryWithRegistry
import com.willfp.ecoskills.EcoSkillsPlugin
import com.willfp.ecoskills.gui.menus.SkillsGUI
import com.willfp.ecoskills.util.InvalidConfigurationException
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.ConfigCategory
object Skills : ConfigCategory("skill", "skills") {
private val registry = Registry<Skill>()
object Skills : CategoryWithRegistry<Skill>("skill", "skills") {
override fun clear(plugin: LibreforgePlugin) {
registry.clear()
}
@@ -26,8 +23,4 @@ object Skills : ConfigCategory("skill", "skills") {
override fun afterReload(plugin: LibreforgePlugin) {
SkillsGUI.update(plugin)
}
fun getByID(id: String): Skill? = registry[id]
fun values(): Set<Skill> = registry.values()
}

View File

@@ -2,18 +2,15 @@ package com.willfp.ecoskills.stats
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.placeholder.PlayerPlaceholder
import com.willfp.eco.core.registry.Registry
import com.willfp.eco.util.toNiceString
import com.willfp.ecoskills.CategoryWithRegistry
import com.willfp.ecoskills.EcoSkillsPlugin
import com.willfp.ecoskills.api.averageSkillLevel
import com.willfp.ecoskills.api.totalSkillLevel
import com.willfp.ecoskills.gui.menus.StatsGUI
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.ConfigCategory
object Stats : ConfigCategory("stat", "stats") {
private val registry = Registry<Stat>()
object Stats : CategoryWithRegistry<Stat>("stat", "stats") {
override val supportsSharing = false
override fun beforeReload(plugin: LibreforgePlugin) {
@@ -37,8 +34,4 @@ object Stats : ConfigCategory("stat", "stats") {
override fun afterReload(plugin: LibreforgePlugin) {
StatsGUI.update(plugin)
}
fun getByID(id: String): Stat? = registry[id]
fun values(): Set<Stat> = registry.values()
}

View File

@@ -0,0 +1,29 @@
package com.willfp.ecoskills.util
import com.willfp.eco.core.command.CommandBase
import com.willfp.eco.util.toSingletonList
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
fun CommandBase.players(args: List<String>, position: Int, key: String): List<Player> {
val arg = this.notifyNull(args.getOrNull(position), key)
return if (arg.equals("all", ignoreCase = true)) {
Bukkit.getOnlinePlayers().toList()
} else {
Bukkit.getPlayer(arg)?.toSingletonList() ?: emptyList()
}
}
fun CommandBase.offlinePlayers(args: List<String>, position: Int, key: String): List<OfflinePlayer> {
val arg = this.notifyNull(args.getOrNull(position), key)
return if (arg.equals("all", ignoreCase = true)) {
Bukkit.getOfflinePlayers().toList()
} else {
Bukkit.getOfflinePlayer(arg)
.takeIf { it.hasPlayedBefore() }
?.toSingletonList() ?: emptyList()
}
}

View File

@@ -11,12 +11,11 @@ messages:
invalid-effect: "&cInvalid effect!"
reset-player: "&fReset player!"
recounted-player: "&fRecounted effect &6%effect% &ffor player &a%player%&f to level &6%level%&f!"
requires-skill: "&cYou must specify a skill!"
invalid-skill: "&cInvalid skill!"
invalid-skill-stat: "&cInvalid skill or stat!"
requires-amount: "&cYou must specify the amount!"
invalid-amount: "&cInvalid amount!"
gave-skill-xp: "Gave %player% %amount% %skill% experience!"
gave-stat: "Gave %player% %amount% %stat%&r!"
gave-skill-xp: "Gave %player% %amount% %obj% experience!"
gave-stat: "Gave %player% %amount% %obj%&r!"
enabled-actionbar: "&fYou have &aenabled &fthe action bar!"
disabled-actionbar: "&fYou have &cdisabled &fthe action bar!"
actionbar-disabled: "&cThe action bar is disabled on this server."