From 683e95f5d3a65d5a8a4e82272915bc9b16e77536 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 14 May 2023 17:12:46 +0100 Subject: [PATCH] Fixes and improvements --- .../com/willfp/ecoskills/EcoSkillsPlugin.kt | 7 +- .../kotlin/com/willfp/ecoskills/Levellable.kt | 3 +- .../ecoskills/commands/CommandSkills.kt | 1 + .../commands/CommandToggleActionBar.kt | 1 - .../willfp/ecoskills/commands/CommandTop.kt | 87 +++++++++++++++++++ .../skills/EcoSkillsTopPlaceholder.kt | 33 ++++++- .../com/willfp/ecoskills/skills/Skills.kt | 28 ++++++ .../core-plugin/src/main/resources/lang.yml | 9 +- 8 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlugin.kt index ed05026..f5410a7 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlugin.kt @@ -23,6 +23,7 @@ import com.willfp.ecoskills.libreforge.TriggerGainSkillXp import com.willfp.ecoskills.libreforge.TriggerLevelUpSkill import com.willfp.ecoskills.mana.MagicHandler import com.willfp.ecoskills.mana.MagicTypes +import com.willfp.ecoskills.skills.EcoSkillsSkillTopPlaceholder import com.willfp.ecoskills.skills.EcoSkillsTopPlaceholder import com.willfp.ecoskills.skills.SkillCritListener import com.willfp.ecoskills.skills.Skills @@ -79,10 +80,14 @@ class EcoSkillsPlugin : LibreforgePlugin() { Filters.register(FilterSkillCrit) EcoSkillsTopPlaceholder(this).register() + EcoSkillsSkillTopPlaceholder(this).register() } override fun handleReload() { - ActionBarHandler(this).startTicking() + if (this.configYml.getBool("persistent-action-bar.enabled")) { + ActionBarHandler(this).startTicking() + } + TemporaryBossBarHandler(this).startTicking() MagicHandler(this).startTicking() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/Levellable.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/Levellable.kt index 5fb27a2..b13397a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/Levellable.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/Levellable.kt @@ -92,7 +92,8 @@ abstract class Levellable( val uuid = leaderboardCache.get(true).getOrNull(position - 1) ?: return null - val player = Bukkit.getOfflinePlayer(uuid) + val player = Bukkit.getOfflinePlayer(uuid).takeIf { it.hasPlayedBefore() } ?: return null + return LeaderboardEntry( player, getActualLevel(player) 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 aef5254..2fe4154 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 @@ -16,6 +16,7 @@ class CommandSkills(plugin: EcoPlugin) : PluginCommand( ) { init { this.addSubcommand(CommandToggleActionBar(plugin)) + .addSubcommand(CommandTop(plugin)) } override fun onExecute(player: Player, args: 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 index e7d32c0..eaa74ba 100644 --- 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 @@ -14,7 +14,6 @@ class CommandToggleActionBar(plugin: EcoPlugin) : Subcommand( ) { override fun onExecute(player: Player, args: List) { - when (player.isPersistentActionBarEnabled) { true -> { player.sendCompatibleActionBarMessage("") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt new file mode 100644 index 0000000..34a2819 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt @@ -0,0 +1,87 @@ +package com.willfp.ecoskills.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.core.placeholder.context.placeholderContext +import com.willfp.eco.util.formatEco +import com.willfp.eco.util.savedDisplayName +import com.willfp.ecoskills.skills.Skills +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.util.StringUtil + + +class CommandTop(plugin: EcoPlugin) : + Subcommand( + plugin, + "top", + "ecoskills.command.top", + false + ) { + + override fun onExecute(sender: CommandSender, args: List) { + val skill = Skills.getByID(args.getOrNull(0)) + + val pageIndex = if (skill == null) 0 else 1 + val page = args.getOrNull(pageIndex)?.toIntOrNull() ?: 1 + + if (skill == null && args.getOrNull(pageIndex)?.toIntOrNull() == null) { + sender.sendMessage(plugin.langYml.getMessage("invalid-skill")) + return + } + + val positions = ((page)..(page + 9)).toList() + + val top = if (skill == null) { + positions.mapNotNull { Skills.getTop(it) } + } else { + positions.mapNotNull { skill.getTop(it) } + } + + val messages = plugin.langYml.getStrings("top.format") + val lines = mutableListOf() + + for ((index, entry) in top.withIndex()) { + val (player, level) = entry + + val line = plugin.langYml.getString("top-line-format") + .replace("%rank%", positions[index].toString()) + .replace("%level%", level.toString()) + .replace("%player%", player.savedDisplayName) + + lines.add(line) + } + + val linesIndex = messages.indexOf("%lines%") + + if (linesIndex != -1) { + messages.removeAt(linesIndex) + messages.addAll(linesIndex, lines) + } + + for (message in messages) { + sender.sendMessage( + message.formatEco( + placeholderContext( + player = sender as? Player + ) + ) + ) + } + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + listOf(1, 2, 3, 4, 5).map { it.toString() }, + completions + ) + return completions + } + + return emptyList() + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/EcoSkillsTopPlaceholder.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/EcoSkillsTopPlaceholder.kt index f9b7486..3afe6ea 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/EcoSkillsTopPlaceholder.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/EcoSkillsTopPlaceholder.kt @@ -6,7 +6,7 @@ import com.willfp.eco.core.placeholder.context.PlaceholderContext import com.willfp.eco.util.savedDisplayName import java.util.regex.Pattern -class EcoSkillsTopPlaceholder( +class EcoSkillsSkillTopPlaceholder( private val plugin: EcoPlugin ) : RegistrablePlaceholder { private val pattern = Pattern.compile("(top_)[a-z]+_[0-9]+_[a-z]+") @@ -31,7 +31,36 @@ class EcoSkillsTopPlaceholder( return when (args.last()) { "name" -> skill.getTop(place)?.player?.savedDisplayName - "amount" -> skill.getTop(place)?.level?.toString() + "level", "amount" -> skill.getTop(place)?.level?.toString() + else -> null + } + } +} + +class EcoSkillsTopPlaceholder( + private val plugin: EcoPlugin +) : RegistrablePlaceholder { + private val pattern = Pattern.compile("(top_)[0-9]+_[a-z]+") + + override fun getPattern(): Pattern = pattern + override fun getPlugin(): EcoPlugin = plugin + + override fun getValue(params: String, ctx: PlaceholderContext): String? { + val args = params.split("_") + + if (args.size < 2) { + return null + } + + if (args[0] != "top") { + return null + } + + val place = args[1].toIntOrNull() ?: return null + + return when (args.last()) { + "name" -> Skills.getTop(place)?.player?.savedDisplayName + "level", "amount" -> Skills.getTop(place)?.level?.toString() else -> null } } 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 71f7699..b301e2e 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,13 +1,41 @@ package com.willfp.ecoskills.skills +import com.github.benmanes.caffeine.cache.Caffeine import com.willfp.eco.core.config.interfaces.Config import com.willfp.ecoskills.CategoryWithRegistry import com.willfp.ecoskills.EcoSkillsPlugin +import com.willfp.ecoskills.api.totalSkillLevel import com.willfp.ecoskills.gui.menus.SkillsGUI import com.willfp.ecoskills.util.InvalidConfigurationException +import com.willfp.ecoskills.util.LeaderboardEntry import com.willfp.libreforge.loader.LibreforgePlugin +import org.bukkit.Bukkit +import java.util.UUID +import java.util.concurrent.TimeUnit object Skills : CategoryWithRegistry("skill", "skills") { + // Totally not copied over from Levellable + private val leaderboardCache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.MINUTES) + .build> { + Bukkit.getOfflinePlayers().sortedByDescending { + it.totalSkillLevel + }.map { it.uniqueId } + } + + fun getTop(position: Int): LeaderboardEntry? { + require(position > 0) { "Position must be greater than 0" } + + val uuid = leaderboardCache.get(true).getOrNull(position - 1) ?: return null + + val player = Bukkit.getOfflinePlayer(uuid).takeIf { it.hasPlayedBefore() } ?: return null + + return LeaderboardEntry( + player, + player.totalSkillLevel + ) + } + override fun clear(plugin: LibreforgePlugin) { registry.clear() } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 18a2d6e..0c1145c 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -5,23 +5,16 @@ messages: invalid-command: "&cUnknown subcommand!" reloaded: "Reloaded!" - requires-player: "&cYou must specify a player!" invalid-player: "&cInvalid player!" - requires-effect: "&cYou must specify an effect!" - invalid-effect: "&cInvalid effect!" + invalid-skill: "&cInvalid skill!" reset-player: "&fReset player!" - recounted-player: "&fRecounted effect &6%effect% &ffor player &a%player%&f to level &6%level%&f!" invalid-skill-stat: "&cInvalid skill or stat!" - requires-amount: "&cYou must specify the amount!" invalid-amount: "&cInvalid amount!" 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." - enabled-xp-gain-sound: "&fYou have &aenabled &fskill XP gain sound!" - disabled-xp-gain-sound: "&fYou have &cdisabled &fskill XP gain sound!" - xp-gain-sound-disabled: "&cSkill XP gain sound is disabled on this server." resetting-all-players: "&fResetting all players... (this may take a while)" reset-all-players: "&fReset all players!"