Updated to use eco native line wrapping

This commit is contained in:
Auxilor 2023-05-11 17:23:39 +01:00
parent 9d95144be8
commit 96242461f4
2 changed files with 8 additions and 44 deletions

View File

@ -40,7 +40,7 @@ allprojects {
} }
dependencies { dependencies {
compileOnly("com.willfp:eco:6.56.0") compileOnly("com.willfp:eco:6.58.0")
compileOnly("org.jetbrains:annotations:23.0.0") compileOnly("org.jetbrains:annotations:23.0.0")
compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10") compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10")
compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.0") compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.0")

View File

@ -3,14 +3,15 @@ package com.willfp.ecoenchants.display
import com.github.benmanes.caffeine.cache.Cache import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine import com.github.benmanes.caffeine.cache.Caffeine
import com.willfp.eco.core.config.updating.ConfigUpdater import com.willfp.eco.core.config.updating.ConfigUpdater
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
import com.willfp.eco.core.placeholder.context.placeholderContext
import com.willfp.eco.util.NumberUtils import com.willfp.eco.util.NumberUtils
import com.willfp.eco.util.StringUtils import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.formatEco
import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.EcoEnchantsPlugin
import com.willfp.ecoenchants.enchants.EcoEnchantLike import com.willfp.ecoenchants.enchants.EcoEnchantLike
import org.apache.commons.lang.WordUtils import net.kyori.adventure.text.Component
import org.bukkit.ChatColor import net.kyori.adventure.text.JoinConfiguration
import net.kyori.adventure.text.TextComponent
import java.util.stream.Collectors
// This is an object to be able to invalidate the cache on reload // This is an object to be able to invalidate the cache on reload
object DisplayCache { object DisplayCache {
@ -83,50 +84,13 @@ fun EcoEnchantLike.getFormattedDescription(level: Int): List<String> {
val descriptionFormat = plugin.configYml.getString("display.descriptions.format") val descriptionFormat = plugin.configYml.getString("display.descriptions.format")
val wrap = plugin.configYml.getInt("display.descriptions.word-wrap") val wrap = plugin.configYml.getInt("display.descriptions.word-wrap")
var description = this.getUnformattedDescription(level) var description = descriptionFormat + 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)
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 // Replace reset tags with description format
for (tag in resetTags) { for (tag in resetTags) {
description = description.replace(tag, tag + descriptionFormat) description = description.replace(tag, tag + descriptionFormat)
} }
// Wrap the lines StringUtils.lineWrap(description.formatEco(), wrap)
val wrapped = WordUtils.wrap(description, wrap, "\n", false)
.lines()
.map {
// Swap back in placeholders
var string = it
for ((mock, id) in mockPlaceholderIDs) {
string = string.replace(mock, mockPlaceholderMap[id] ?: "")
}
StringUtils.format(
descriptionFormat + string
)
}
wrapped
} }
} }