Merge pull request #386

Making enchantment description empty will no longer leave a blank line in the lore
This commit is contained in:
Will FP 2023-12-07 12:37:28 +00:00 committed by GitHub
commit d8582e6ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 27 deletions

View File

@ -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 })
}
}
}

View File

@ -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<String> {
fun EcoEnchantLike.getFormattedDescription(level: Int, player: Player? = null): List<String> {
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<String> {
)), wrap)
}
}
// Java backwards compatibility
fun EcoEnchantLike.getFormattedDescription(level: Int): List<String> = getFormattedDescription(level, null)

View File

@ -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<InjectablePlaceholder> {
return listOf(
StaticPlaceholder(
"level",
) { level.toString() }
)
}
placeholderContext(
player = player,
injectable = object : PlaceholderInjectable {
override fun getPlaceholderInjections(): List<InjectablePlaceholder> {
return listOf(
StaticPlaceholder(
"level",
) { level.toString() }
)
}
override fun addInjectablePlaceholder(p0: MutableIterable<InjectablePlaceholder>) {
// Do nothing
}
override fun addInjectablePlaceholder(p0: MutableIterable<InjectablePlaceholder>) {
// 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()

View File

@ -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<EcoEnchant, Slot>()
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 {