Fixed placeholders in enchantment descriptions

This commit is contained in:
Auxilor 2022-10-03 17:26:35 +01:00
parent 65555ddbee
commit e79711245f
3 changed files with 38 additions and 3 deletions

View File

@ -3,11 +3,13 @@ package com.willfp.ecoenchants.display
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.willfp.eco.core.config.updating.ConfigUpdater
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
import com.willfp.eco.util.NumberUtils
import com.willfp.eco.util.StringUtils
import com.willfp.ecoenchants.EcoEnchantsPlugin
import com.willfp.ecoenchants.enchants.EcoEnchantLike
import org.apache.commons.lang.WordUtils
import org.bukkit.ChatColor
// This is an object to be able to invalidate the cache on reload
object DisplayCache {
@ -75,12 +77,46 @@ fun EcoEnchantLike.getFormattedDescription(level: Int): List<String> {
val wrap = plugin.configYml.getInt("display.descriptions.word-wrap")
var description = this.getUnformattedDescription(level)
/*
Essentially, to work with word wrapping (and colored placeholders),
placeholders are translated first, then replaced with a unique mock
placeholder the length of the translated value, and then swapped back in
at the end.
*/
val placeholders = PlaceholderManager.findPlaceholdersIn(description)
val mockPlaceholderMap = mutableMapOf<Int, String>()
val mockPlaceholderIDs = mutableMapOf<String, Int>()
var i = 1
for (placeholder in placeholders) {
val translated = PlaceholderManager.translatePlaceholders(placeholder, null)
mockPlaceholderMap[i] = translated
val length = ChatColor.stripColor(translated)!!.length
val toReplaceWith = "[${i.toString().repeat((length - 2).coerceAtLeast(1))}]"
mockPlaceholderIDs[toReplaceWith] = i
description = description.replace(placeholder, toReplaceWith)
i++
}
/** Replace reset tags with description format. */
for (tag in resetTags) {
description = description.replace(tag, tag + descriptionFormat)
}
WordUtils.wrap(description, wrap, "\n", false)
/** Wrap the lines. */
val wrapped = WordUtils.wrap(description, wrap, "\n", false)
.lines()
.map { StringUtils.format(descriptionFormat + it) }
/** Swap back in the original placeholder. */
wrapped.map {
var string = it
for ((mock, id) in mockPlaceholderIDs) {
string = string.replace(mock, mockPlaceholderMap[id] ?: "")
}
string
}
}
}

View File

@ -18,8 +18,6 @@ effects:
args:
stat: ferocity
amount: "5 + 5 * %level%"
triggers:
- alt_click
conditions: [ ]

View File

@ -17,6 +17,7 @@ effects:
- id: damage_victim
args:
damage: "%v% * (0.2 + %level% * 0.1)"
use-source: false
triggers:
- shield_block