From e79711245f98ac1d4470f9602cc8816cdaefde04 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 3 Oct 2022 17:26:35 +0100 Subject: [PATCH] Fixed placeholders in enchantment descriptions --- .../display/EnchantmentFormatting.kt | 38 ++++++++++++++++++- .../resources/enchants/ecoskills/vicious.yml | 2 - .../main/resources/enchants/rebounding.yml | 1 + 3 files changed, 38 insertions(+), 3 deletions(-) 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 2ee0a69c..d50636f6 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 @@ -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 { 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() + val mockPlaceholderIDs = mutableMapOf() + + 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 + } } } diff --git a/eco-core/core-plugin/src/main/resources/enchants/ecoskills/vicious.yml b/eco-core/core-plugin/src/main/resources/enchants/ecoskills/vicious.yml index 44888a21..de0f5979 100644 --- a/eco-core/core-plugin/src/main/resources/enchants/ecoskills/vicious.yml +++ b/eco-core/core-plugin/src/main/resources/enchants/ecoskills/vicious.yml @@ -18,8 +18,6 @@ effects: args: stat: ferocity amount: "5 + 5 * %level%" - triggers: - - alt_click conditions: [ ] diff --git a/eco-core/core-plugin/src/main/resources/enchants/rebounding.yml b/eco-core/core-plugin/src/main/resources/enchants/rebounding.yml index 6f059b38..bd6de0ae 100644 --- a/eco-core/core-plugin/src/main/resources/enchants/rebounding.yml +++ b/eco-core/core-plugin/src/main/resources/enchants/rebounding.yml @@ -17,6 +17,7 @@ effects: - id: damage_victim args: damage: "%v% * (0.2 + %level% * 0.1)" + use-source: false triggers: - shield_block