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 a14114d8..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).map { Display.PREFIX + it }) + enchantLore.addAll(enchant.getFormattedDescription(level, player) + .filter { it.isNotEmpty() }.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 0a2ec32a..6a20ad60 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 { @@ -72,14 +73,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) { @@ -91,3 +92,6 @@ fun EcoEnchantLike.getFormattedDescription(level: Int): List { )), 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 846daa76..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 @@ -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 + } } - } + ) ) ) } @@ -79,6 +85,11 @@ interface EcoEnchantLike { return description } + + // Java backwards compatibility + fun getUnformattedDescription(level: Int): String { + return getUnformattedDescription(level, null) + } } private val ecoEnchantLikes = Caffeine.newBuilder() 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 { 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: [ ]