diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/CategoryWithRegistry.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/CategoryWithRegistry.kt new file mode 100644 index 0000000..2fbe1bf --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/CategoryWithRegistry.kt @@ -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( + id: String, + directory: String +) : ConfigCategory(id, directory) { + protected val registry = Registry() + + fun getByID(id: String?): T? = id?.let { registry[id] } + + fun values(): Set = registry.values() +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/actionbar/ActionBarHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/actionbar/ActionBarHandler.kt index 5ed6e80..8490789 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/actionbar/ActionBarHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/actionbar/ActionBarHandler.kt @@ -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() private val whitelist = mutableMapOf() +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 } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt index 4d11fd5..67bac52 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt @@ -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) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt index 8cc122a..b8ee1e8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt @@ -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) { - 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() ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt index 7da7705..e26a8af 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt @@ -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) { - 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): List { @@ -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() } -} \ No newline at end of file +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandSkills.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandSkills.kt index f4277f3..aef5254 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandSkills.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandSkills.kt @@ -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) { - 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): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + Skills.values().map { it.id }, + completions + ) + return completions + } + + return emptyList() } override fun getAliases(): List { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandToggleActionBar.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandToggleActionBar.kt new file mode 100644 index 0000000..e7d32c0 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandToggleActionBar.kt @@ -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) { + + when (player.isPersistentActionBarEnabled) { + true -> { + player.sendCompatibleActionBarMessage("") + player.sendMessage(plugin.langYml.getMessage("disabled-actionbar")) + } + + false -> player.sendMessage(plugin.langYml.getMessage("enabled-actionbar")) + } + + player.togglePersistentActionBar() + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effects.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effects.kt index d859d27..f043c9a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effects.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effects.kt @@ -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() +object Effects : CategoryWithRegistry("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 = registry.values() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/mana/MagicTypes.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/mana/MagicTypes.kt index 0139892..dda72f9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/mana/MagicTypes.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/mana/MagicTypes.kt @@ -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() +object MagicTypes : CategoryWithRegistry("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 = registry.values() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skills.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skills.kt index f3bfa00..71f7699 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skills.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skills.kt @@ -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() +object Skills : CategoryWithRegistry("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 = registry.values() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/Stats.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/Stats.kt index 6393a5f..cb6990a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/Stats.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/Stats.kt @@ -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() +object Stats : CategoryWithRegistry("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 = registry.values() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/MultiPlayerSelector.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/MultiPlayerSelector.kt new file mode 100644 index 0000000..d25210a --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/MultiPlayerSelector.kt @@ -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, position: Int, key: String): List { + 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, position: Int, key: String): List { + 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() + } +} diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 7604dcb..18a2d6e 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -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."