From da7690ee96cf6b8828734b527469024d1a044527 Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Fri, 14 Jul 2023 18:11:24 +0300 Subject: [PATCH 1/4] Enchantment descriptions will now always parse against players --- .../ecoenchants/display/EnchantDisplay.kt | 2 +- .../display/EnchantmentFormatting.kt | 5 ++- .../ecoenchants/enchants/EcoEnchantLike.kt | 38 +++++++++++-------- .../willfp/ecoenchants/enchants/EnchantGUI.kt | 12 ++---- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt index 389dd356..6171ad7b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt @@ -106,7 +106,7 @@ class EnchantDisplay(private val plugin: EcoEnchantsPlugin) : DisplayModule(plug enchantLore.add(Display.PREFIX + formattedName) if (shouldDescribe) { - enchantLore.addAll(enchant.getFormattedDescription(level).map { Display.PREFIX + it }) + enchantLore.addAll(enchant.getFormattedDescription(level, player).map { Display.PREFIX + it }) } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt index af738174..069d7269 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt @@ -8,6 +8,7 @@ import com.willfp.eco.util.StringUtils import com.willfp.eco.util.formatEco import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.enchants.EcoEnchantLike +import org.bukkit.entity.Player // This is an object to be able to invalidate the cache on reload object DisplayCache { @@ -71,14 +72,14 @@ private val resetTags = arrayOf( "§r" ) -fun EcoEnchantLike.getFormattedDescription(level: Int): List { +fun EcoEnchantLike.getFormattedDescription(level: Int, player: Player? = null): List { val plugin = EcoEnchantsPlugin.instance return DisplayCache.descriptionCache.get(DisplayableEnchant(this, level)) { val descriptionFormat = plugin.configYml.getString("display.descriptions.format") val wrap = plugin.configYml.getInt("display.descriptions.word-wrap") - var description = descriptionFormat + this.getUnformattedDescription(level) + var description = descriptionFormat + this.getUnformattedDescription(level, player) // Replace reset tags with description format for (tag in resetTags) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt index 846daa76..0ee7c5f6 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt @@ -6,6 +6,7 @@ import com.willfp.eco.core.fast.fast import com.willfp.eco.core.placeholder.InjectablePlaceholder import com.willfp.eco.core.placeholder.PlaceholderInjectable import com.willfp.eco.core.placeholder.StaticPlaceholder +import com.willfp.eco.core.placeholder.context.placeholderContext import com.willfp.eco.util.NumberUtils import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.mechanics.infiniteIfNegative @@ -18,6 +19,7 @@ import com.willfp.ecoenchants.vanilla.VanillaEnchantmentData import org.bukkit.Material import org.bukkit.NamespacedKey import org.bukkit.enchantments.Enchantment +import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.* @@ -33,7 +35,7 @@ interface EcoEnchantLike { fun canEnchantItem(item: ItemStack): Boolean // Method body goes here - fun getUnformattedDescription(level: Int): String { + fun getUnformattedDescription(level: Int, player: Player? = null): String { // Fetch custom placeholders other than %placeholder% val uncompiledPlaceholders = config.getSubsection("placeholders").getKeys(false).associateWith { config.getString("placeholders.$it") @@ -42,30 +44,34 @@ interface EcoEnchantLike { // Add %placeholder% placeholder in uncompiledPlaceholders["placeholder"] = config.getString("placeholder") + + // Evaluate each placeholder val placeholders = uncompiledPlaceholders.map { (id, expr) -> DescriptionPlaceholder( id, NumberUtils.evaluateExpression( expr, - null, - object : PlaceholderInjectable { - override fun getPlaceholderInjections(): List { - return listOf( - StaticPlaceholder( - "level", - ) { level.toString() } - ) - } + placeholderContext( + player = player, + injectable = object : PlaceholderInjectable { + override fun getPlaceholderInjections(): List { + return listOf( + StaticPlaceholder( + "level", + ) { level.toString() } + ) + } - override fun addInjectablePlaceholder(p0: MutableIterable) { - // Do nothing - } + override fun addInjectablePlaceholder(p0: MutableIterable) { + // Do nothing + } - override fun clearInjectedPlaceholders() { - // Do nothing + override fun clearInjectedPlaceholders() { + // Do nothing + } } - } + ) ) ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EnchantGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EnchantGUI.kt index 28feade8..e7bf455a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EnchantGUI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EnchantGUI.kt @@ -19,7 +19,6 @@ import com.willfp.eco.core.items.Items import com.willfp.eco.core.items.builder.EnchantedBookBuilder import com.willfp.eco.core.items.builder.ItemStackBuilder import com.willfp.eco.core.items.isEmpty -import com.willfp.eco.util.StringUtils import com.willfp.eco.util.formatEco import com.willfp.eco.util.lineWrap import com.willfp.ecoenchants.EcoEnchantsPlugin @@ -27,13 +26,10 @@ import com.willfp.ecoenchants.display.EnchantSorter.sortForDisplay import com.willfp.ecoenchants.display.getFormattedDescription import com.willfp.ecoenchants.display.getFormattedName import com.willfp.ecoenchants.target.EnchantmentTargets.applicableEnchantments -import org.apache.commons.lang.WordUtils -import org.bukkit.ChatColor import org.bukkit.Material import org.bukkit.entity.Player import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack -import org.bukkit.persistence.PersistentDataType import kotlin.math.ceil object EnchantGUI { @@ -158,7 +154,7 @@ object EnchantGUI { setSlot( plugin.configYml.getInt("enchantinfo.item.row"), plugin.configYml.getInt("enchantinfo.item.column"), - enchant.getInformationSlot(plugin) + enchant.getInformationSlot(plugin, player) ) setMask( @@ -196,7 +192,7 @@ private class EnchantmentScrollPane( val enchant = enchants.getOrNull(index + size * (page - 1)) ?: return defaultSlot - return enchant.getInformationSlot(plugin) + return enchant.getInformationSlot(plugin, player) } override fun getRows() = plugin.configYml.getInt("enchant-gui.enchant-area.height") @@ -208,7 +204,7 @@ private class EnchantmentScrollPane( private val cachedEnchantmentSlots = Caffeine.newBuilder() .build() -private fun EcoEnchant.getInformationSlot(plugin: EcoEnchantsPlugin): Slot { +private fun EcoEnchant.getInformationSlot(plugin: EcoEnchantsPlugin, player: Player): Slot { return cachedEnchantmentSlots.get(this) { val level = if (plugin.configYml.getBool("enchantinfo.item.show-max-level")) { enchant.maxLevel @@ -221,7 +217,7 @@ private fun EcoEnchant.getInformationSlot(plugin: EcoEnchantsPlugin): Slot { .addStoredEnchantment(enchant, level) .addItemFlag(ItemFlag.HIDE_ENCHANTS) .setDisplayName(enchant.getFormattedName(level)) - .addLoreLines(enchant.getFormattedDescription(level)) + .addLoreLines(enchant.getFormattedDescription(level, player)) .addLoreLines { plugin.configYml.getStrings("enchantinfo.item.lore") .map { From 0680ecefd4083915f6d74a81c7a1e7cbefb6c5fd Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Fri, 14 Jul 2023 18:21:22 +0300 Subject: [PATCH 2/4] Backwards compatibility --- .../com/willfp/ecoenchants/display/EnchantmentFormatting.kt | 3 +++ .../kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt index 069d7269..63b5f874 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantmentFormatting.kt @@ -91,3 +91,6 @@ fun EcoEnchantLike.getFormattedDescription(level: Int, player: Player? = null): )), wrap) } } + +// Java backwards compatibility +fun EcoEnchantLike.getFormattedDescription(level: Int): List = getFormattedDescription(level, null) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt index 0ee7c5f6..f0db000e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt @@ -85,6 +85,11 @@ interface EcoEnchantLike { return description } + + // Java backwards compatibility + fun getUnformattedDescription(level: Int): String { + return getUnformattedDescription(level, null) + } } private val ecoEnchantLikes = Caffeine.newBuilder() From 1a54695076b2cccd4351adcd7b356faa643105be Mon Sep 17 00:00:00 2001 From: Exanthiax <107284021+Exanthiax@users.noreply.github.com> Date: Sun, 13 Aug 2023 18:11:21 +0100 Subject: [PATCH 3/4] Fixes two enchants damage_cause not damageCause --- .../core-plugin/src/main/resources/enchants/arcane_defence.yml | 2 +- .../core-plugin/src/main/resources/enchants/block_breather.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eco-core/core-plugin/src/main/resources/enchants/arcane_defence.yml b/eco-core/core-plugin/src/main/resources/enchants/arcane_defence.yml index 0c7c0a4d..64b22c06 100644 --- a/eco-core/core-plugin/src/main/resources/enchants/arcane_defence.yml +++ b/eco-core/core-plugin/src/main/resources/enchants/arcane_defence.yml @@ -20,7 +20,7 @@ effects: triggers: - take_damage filters: - damageCause: + damage_cause: - poison - magic diff --git a/eco-core/core-plugin/src/main/resources/enchants/block_breather.yml b/eco-core/core-plugin/src/main/resources/enchants/block_breather.yml index 4f6b8a72..96d66772 100644 --- a/eco-core/core-plugin/src/main/resources/enchants/block_breather.yml +++ b/eco-core/core-plugin/src/main/resources/enchants/block_breather.yml @@ -20,7 +20,7 @@ effects: triggers: - take_damage filters: - damageCause: + damage_cause: - suffocation conditions: [ ] From ab6bc81f6d0c694d7e1ebae4b6174b86d49e36af Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Sun, 3 Dec 2023 03:37:11 +0300 Subject: [PATCH 4/4] Making enchantment description empty will no longer leave a blank line in the lore --- .../kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt index 7d0c3117..791205b9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt @@ -106,7 +106,8 @@ class EnchantDisplay(private val plugin: EcoEnchantsPlugin) : DisplayModule(plug enchantLore.add(Display.PREFIX + formattedName) if (shouldDescribe) { - enchantLore.addAll(enchant.getFormattedDescription(level, player).map { Display.PREFIX + it }) + enchantLore.addAll(enchant.getFormattedDescription(level, player) + .filter { it.isNotEmpty() }.map { Display.PREFIX + it }) } } }