From 0e303a39e83861a31af9be9e4d1d453928de430b Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 21 May 2023 18:38:10 +0100 Subject: [PATCH] Improved GUI and command output --- .../ecoenchants/commands/CommandEnchant.kt | 7 ++- .../commands/CommandGiveRandomBook.kt | 3 +- .../ecoenchants/display/EnchantDisplay.kt | 1 - .../willfp/ecoenchants/enchants/EnchantGUI.kt | 56 +++++-------------- .../core-plugin/src/main/resources/config.yml | 1 - .../core-plugin/src/main/resources/lang.yml | 4 +- 6 files changed, 24 insertions(+), 48 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchant.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchant.kt index 18e28639..f5809a76 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchant.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchant.kt @@ -2,6 +2,7 @@ package com.willfp.ecoenchants.commands import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.util.StringUtils import com.willfp.eco.util.savedDisplayName import com.willfp.ecoenchants.enchants.wrap import com.willfp.ecoenchants.rarity.EnchantmentRarities @@ -46,7 +47,7 @@ class CommandEnchant(plugin: EcoPlugin) : PluginCommand( meta.addEnchant(enchant, level, true) sender.sendMessage( - plugin.langYml.getMessage("added-enchant") + plugin.langYml.getMessage("added-enchant", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) .replace("%enchant%", enchant.wrap().displayName) .replace("%player%", player.savedDisplayName) ) @@ -57,11 +58,13 @@ class CommandEnchant(plugin: EcoPlugin) : PluginCommand( meta.removeEnchant(enchant) sender.sendMessage( - plugin.langYml.getMessage("removed-enchant") + plugin.langYml.getMessage("removed-enchant", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) .replace("%enchant%", enchant.wrap().displayName) .replace("%player%", player.savedDisplayName) ) } + + item.itemMeta = meta } override fun tabComplete(sender: CommandSender, rawArgs: List): List { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt index 1191e025..3e0ea185 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt @@ -5,6 +5,7 @@ import com.willfp.eco.core.command.impl.PluginCommand import com.willfp.eco.core.drops.DropQueue import com.willfp.eco.core.items.builder.EnchantedBookBuilder import com.willfp.eco.util.NumberUtils +import com.willfp.eco.util.StringUtils import com.willfp.ecoenchants.display.getFormattedName import com.willfp.ecoenchants.enchants.EcoEnchants import com.willfp.ecoenchants.enchants.wrap @@ -72,7 +73,7 @@ class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand( .push() sender.sendMessage( - plugin.langYml.getMessage("gave-random-book") + plugin.langYml.getMessage("gave-random-book", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) .replace("%player%", player.name) .replace("%enchantment%", enchantment.wrap().getFormattedName(level)) ) 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 26ce08c9..389dd356 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 @@ -67,7 +67,6 @@ class EnchantDisplay(private val plugin: EcoEnchantsPlugin) : DisplayModule(plug val shouldDescribe = (plugin.configYml.getBool("display.descriptions.enabled") && enchants.size <= plugin.configYml.getInt("display.descriptions.threshold") && player?.seesEnchantmentDescriptions ?: true) - || pdc.has(plugin.namespacedKeyFactory.create("force-describe"), PersistentDataType.INTEGER) val formattedNames = mutableMapOf() 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 f0cbf8dc..28feade8 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 @@ -21,14 +21,17 @@ 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 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 @@ -216,65 +219,36 @@ private fun EcoEnchant.getInformationSlot(plugin: EcoEnchantsPlugin): Slot { slot( EnchantedBookBuilder() .addStoredEnchantment(enchant, level) - .writeMetaKey( - plugin.namespacedKeyFactory.create("force-describe"), - PersistentDataType.INTEGER, - 1 - ) + .addItemFlag(ItemFlag.HIDE_ENCHANTS) + .setDisplayName(enchant.getFormattedName(level)) + .addLoreLines(enchant.getFormattedDescription(level)) .addLoreLines { - // This is horrific and I should refactor it. - val wrappableMap = mutableMapOf() - - fun String.toWrappable(): String { - val unspaced = this.replace(" ", "{_}") - val uncolored = ChatColor.stripColor(unspaced)!! - - wrappableMap[uncolored] = this - return uncolored - } - - fun String.replaceInWrappable(): String { - var processed = this - for ((wrappable, original) in wrappableMap) { - processed = processed.replace(wrappable, original) - } - return processed - } - plugin.configYml.getStrings("enchantinfo.item.lore") .map { it.replace("%max_level%", enchant.maxLevel.toString()) - .replace("%rarity%", enchant.enchantmentRarity.displayName.toWrappable()) + .replace("%rarity%", enchant.enchantmentRarity.displayName) .replace( "%targets%", - enchant.targets.joinToString(", ") { target -> target.displayName.toWrappable() } + enchant.targets.joinToString(", ") { target -> target.displayName } ) .replace( "%conflicts%", if (enchant.conflictsWithEverything) { - plugin.langYml.getFormattedString("all-conflicts").toWrappable() + plugin.langYml.getFormattedString("all-conflicts") } else { enchant.conflicts.joinToString(", ") { conflict -> - conflict.wrap().getFormattedName(0).toWrappable() - }.ifEmpty { plugin.langYml.getFormattedString("no-conflicts").toWrappable() } + conflict.wrap().getFormattedName(0) + }.ifEmpty { plugin.langYml.getFormattedString("no-conflicts") } } ) .replace("%tradeable%", this.isTradeable.parseYesOrNo(plugin.langYml)) .replace("%discoverable%", this.isDiscoverable.parseYesOrNo(plugin.langYml)) .replace("%enchantable%", this.isEnchantable.parseYesOrNo(plugin.langYml)) } - .flatMap { - WordUtils.wrap(it, 45, "\n", false) - .lines() - .map { s -> s.replaceInWrappable() } - .mapIndexed { index, s -> - if (index == 0) s - else StringUtils.format( - plugin.configYml.getString("enchantinfo.item.line-wrap-format") + s - ) - } - } .formatEco() + .flatMap { + it.lineWrap(32, true) + } } .build() ) @@ -282,5 +256,5 @@ private fun EcoEnchant.getInformationSlot(plugin: EcoEnchantsPlugin): Slot { } fun Boolean.parseYesOrNo(langYml: LangYml): String { - return if (this) langYml.getFormattedString("yes") else langYml.getFormattedString("no") + return if (this) langYml.getFormattedString("yes") else langYml.getFormattedString("no") } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index b63be9c2..79f510f9 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -96,7 +96,6 @@ enchantinfo: row: 2 column: 5 show-max-level: true # Whether the book should be the max level or level 1 - line-wrap-format: "&a" # Color for line wrapping lore: # The description is automatically appended - "" - "&fMax Level: &a%max_level%" diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 5c90d699..12632eeb 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -15,8 +15,8 @@ messages: no-enchantments-found: "&cNo enchantments found!" invalid-levels: "&cMinimum level can't be higher than maximum level!" invalid-enchantment: "&cInvalid enchantment!" - added-enchant: "&fAdded &a%enchantment%&f to &a%player%&f's held item!" - removed-enchant: "&fRemoved &a%enchantment%&f from &a%player%&f's held item!" + added-enchant: "&fAdded &a%enchant%&f to &a%player%&f's held item!" + removed-enchant: "&fRemoved &a%enchant%&f from &a%player%&f's held item!" all: "All"