From b1a516c0a297d2fc205edd68ca7c8b8d401f29ad Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Fri, 27 Jan 2023 05:20:45 +0300 Subject: [PATCH] Added %ecoskillstop__name% and %ecoskillstop__amount% placeholders --- build.gradle | 2 + .../com/willfp/ecoskills/EcoSkillsPlugin.java | 4 +- .../willfp/ecoskills/commands/CommandTop.kt | 2 +- .../placeholders/EcoSkillsTopExpansion.kt | 53 +++++++++++++++++++ .../com/willfp/ecoskills/skills/Skill.kt | 19 +++++++ .../core-plugin/src/main/resources/config.yml | 1 + .../core-plugin/src/main/resources/lang.yml | 7 ++- 7 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/placeholders/EcoSkillsTopExpansion.kt diff --git a/build.gradle b/build.gradle index c6cb72e..2a3bbe4 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,7 @@ allprojects { maven { url 'https://repo.codemc.io/repository/maven-public/' } maven { url 'https://repo.dmulloy2.net/repository/public/' } maven { url 'https://repo.essentialsx.net/releases/' } + maven { url 'https://repo.extendedclip.com/content/repositories/placeholderapi/' } } jar { @@ -49,6 +50,7 @@ allprojects { compileOnly 'com.willfp:eco:6.44.0' implementation 'com.willfp:libreforge:3.128.0' implementation 'org.joml:joml:1.10.4' + compileOnly 'me.clip:placeholderapi:2.11.2' compileOnly fileTree(dir: '../../lib', include: ['*.jar']) compileOnly 'com.github.LegameMc:EnchantGui-API:1.0' diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java index bfebac2..966a694 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java @@ -19,6 +19,7 @@ import com.willfp.ecoskills.effects.Effect; import com.willfp.ecoskills.effects.Effects; import com.willfp.ecoskills.integrations.EcoEnchantsEnchantingLeveller; import com.willfp.ecoskills.integrations.enchantgui.EnchantGuiHandler; +import com.willfp.ecoskills.placeholders.EcoSkillsTopExpansion; import com.willfp.ecoskills.skills.CustomSkillTriggerXPGainListener; import com.willfp.ecoskills.skills.Skill; import com.willfp.ecoskills.skills.SkillDisplayListener; @@ -149,7 +150,8 @@ public class EcoSkillsPlugin extends LibReforgePlugin { public List loadAdditionalIntegrations() { return List.of( new IntegrationLoader("EcoEnchants", () -> this.getEventManager().registerListener(new EcoEnchantsEnchantingLeveller(this))), - new IntegrationLoader("EnchantGui", () -> this.getEventManager().registerListener(new EnchantGuiHandler())) + new IntegrationLoader("EnchantGui", () -> this.getEventManager().registerListener(new EnchantGuiHandler())), + new IntegrationLoader("PlaceholderAPI", () -> new EcoSkillsTopExpansion(this).register()) ); } } 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 index 8542136..9723fdf 100644 --- 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 @@ -22,7 +22,7 @@ class CommandTop(plugin: EcoPlugin) : val page = args.firstOrNull()?.toIntOrNull() ?: 1 val top = LeaderboardHandler.getPage(page) - val messages = plugin.langYml.getStrings("top") + val messages = plugin.langYml.getStrings("top.format") val lines = mutableListOf() val useDisplayName = plugin.configYml.getBool("commands.top.use-display-name") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/placeholders/EcoSkillsTopExpansion.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/placeholders/EcoSkillsTopExpansion.kt new file mode 100644 index 0000000..148d956 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/placeholders/EcoSkillsTopExpansion.kt @@ -0,0 +1,53 @@ +package com.willfp.ecoskills.placeholders + +import com.willfp.eco.util.savedDisplayName +import com.willfp.ecoskills.EcoSkillsPlugin +import com.willfp.ecoskills.skills.Skills +import me.clip.placeholderapi.expansion.PlaceholderExpansion +import org.bukkit.OfflinePlayer + +class EcoSkillsTopExpansion(val plugin: EcoSkillsPlugin): PlaceholderExpansion() { + /** + * The placeholder identifier of this expansion. May not contain %, + * {} or _ + * + * @return placeholder identifier that is associated with this expansion + */ + override fun getIdentifier(): String { + return "ecoskillstop" + } + + /** + * The author of this expansion + * + * @return name of the author for this expansion + */ + override fun getAuthor(): String { + return "_OfTeN_" + } + + /** + * The version of this expansion + * + * @return current version of this expansion + */ + override fun getVersion(): String { + return "1.0.0" + } + + override fun onRequest(player: OfflinePlayer?, params: String): String? { + val args = params.split("_") + val skill = Skills.getByID(args.firstOrNull()?.lowercase() ?: return "Invalid skill") + val place = args.getOrNull(1)?.toIntOrNull() ?: return "Invalid place (must be an integer)" + + return when (args.lastOrNull() ?: return "Invalid type: ${args.lastOrNull()}. Must be name/amount") { + "name" -> skill?.getTop(place)?.player?.savedDisplayName ?: plugin.langYml.getFormattedString( + "top.name-empty" + ) + "amount" -> skill?.getTop(place)?.amount?.toString() ?: plugin.langYml.getFormattedString( + "top.amount-empty" + ) + else -> null + } + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt index b88d913..cfc24ab 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt @@ -1,5 +1,6 @@ package com.willfp.ecoskills.skills +import com.github.benmanes.caffeine.cache.Cache import com.github.benmanes.caffeine.cache.Caffeine import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config @@ -24,14 +25,19 @@ import com.willfp.ecoskills.stats.Stats import org.bukkit.Bukkit import org.bukkit.GameMode import org.bukkit.NamespacedKey +import org.bukkit.OfflinePlayer import org.bukkit.entity.Player import org.bukkit.event.Listener +import java.time.Duration abstract class Skill @JvmOverloads constructor( val id: String, forceConfig: Config? = null ) : Listener { protected val plugin: EcoPlugin = EcoSkillsPlugin.getInstance() + val leaderBoardCache: Cache = Caffeine.newBuilder() + .expireAfterWrite(Duration.ofSeconds(plugin.configYml.getInt("cache-expire-after").toLong())) + .build() val key: NamespacedKey = plugin.namespacedKeyFactory.create(id) @@ -240,6 +246,17 @@ abstract class Skill @JvmOverloads constructor( return levels } + fun getTop(place: Int): LeaderboardCacheEntry? { + return leaderBoardCache.get(place) { + val top = Bukkit.getOfflinePlayers() + .sortedByDescending { it.getSkillLevel(this) }.getOrNull(place - 1) + + if (top == null) { + null + } else LeaderboardCacheEntry(top, top.getSkillLevel(this)) + } + } + fun getRewardsMessages(player: Player?, level: Int): List { val raw = messagesCache.get(level) { val parentSection = this.config.getSubsection("rewards.chat-messages") @@ -351,3 +368,5 @@ abstract class Skill @JvmOverloads constructor( } } } + +data class LeaderboardCacheEntry(val player: OfflinePlayer, val amount: Int) diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index eeb7229..c1da2b9 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -549,6 +549,7 @@ point-names: # If you have point names that look ugly (eg g_souls) then you can use-faster-move-trigger: true # Disable if you want move trigger to detect sub-1-block movements raytrace-distance: 80 # The distance that alt_click should check for a location +cache-expire-after: 180 # The amount of seconds to cache a player's leaderboard stats for potions: icon: diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index f00a6a1..847011a 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -42,8 +42,11 @@ line-wrap-color: "&8" top-line-format: "%rank%. %playername% - %level%" top: - - "---- Skills Leaderboard ----" - - "%lines%" + format: + - "---- Skills Leaderboard ----" + - "%lines%" + name-empty: "&cEmpty" + amount-empty: "&0" menu: title: "Your Skills"