diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt index d93403a9..f22c3623 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt @@ -18,6 +18,7 @@ import com.willfp.ecoenchants.enchant.EcoEnchantLevel import com.willfp.ecoenchants.enchant.EcoEnchants import com.willfp.ecoenchants.enchant.EnchantGUI import com.willfp.ecoenchants.enchant.FoundEcoEnchantLevel +import com.willfp.ecoenchants.enchant.LoreConversion import com.willfp.ecoenchants.enchant.legacyRegisterVanillaEnchantmentData import com.willfp.ecoenchants.enchant.registration.EnchantmentRegisterer import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer @@ -117,7 +118,7 @@ class EcoEnchantsPlugin : LibreforgePlugin() { EnchantingTableSupport(this), LootSupport(this), AnvilSupport(this), - //LoreConversion(this), + LoreConversion(this), GrindstoneSupport(this) ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchantInfo.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchantInfo.kt index 2079236f..a48b8edd 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchantInfo.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEnchantInfo.kt @@ -2,7 +2,8 @@ package com.willfp.ecoenchants.commands import com.willfp.eco.core.command.impl.PluginCommand import com.willfp.ecoenchants.EcoEnchantsPlugin -import com.willfp.ecoenchants.enchants.EcoEnchants +import com.willfp.ecoenchants.display.getFormattedName +import com.willfp.ecoenchants.enchant.EcoEnchants import com.willfp.ecoenchants.enchant.EnchantGUI import org.bukkit.ChatColor import org.bukkit.command.CommandSender @@ -41,7 +42,7 @@ class CommandEnchantInfo(plugin: EcoEnchantsPlugin) : PluginCommand( override fun tabComplete(sender: CommandSender, args: List): List { val completions = mutableListOf() - val names = EcoEnchants.values().mapNotNull { ChatColor.stripColor(it.displayName) } + val names = EcoEnchants.values().mapNotNull { ChatColor.stripColor(it.getFormattedName(0)) } if (args.isEmpty()) { // Currently, this case is not ever reached diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt index e74b5b56..006b29e7 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt @@ -1,7 +1,9 @@ package com.willfp.ecoenchants.enchant +import com.google.common.collect.HashBiMap import com.willfp.eco.core.config.interfaces.Config import com.willfp.ecoenchants.EcoEnchantsPlugin +import com.willfp.ecoenchants.display.getFormattedName import com.willfp.ecoenchants.enchant.impl.LibreforgeEcoEnchant import com.willfp.ecoenchants.enchant.impl.hardcoded.EnchantmentPermanenceCurse import com.willfp.ecoenchants.enchant.impl.hardcoded.EnchantmentRepairing @@ -13,15 +15,19 @@ import com.willfp.ecoenchants.target.EnchantmentTargets import com.willfp.ecoenchants.type.EnchantmentTypes import com.willfp.libreforge.loader.LibreforgePlugin import com.willfp.libreforge.loader.configs.RegistrableCategory +import org.bukkit.ChatColor @Suppress("UNUSED") object EcoEnchants : RegistrableCategory("enchant", "enchants") { + private val BY_NAME = HashBiMap.create() + override fun clear(plugin: LibreforgePlugin) { plugin as EcoEnchantsPlugin for (enchant in registry.values()) { plugin.enchantmentRegisterer.unregister(enchant) EnchantRegistrations.removeEnchant(enchant) + BY_NAME.remove(enchant.getFormattedName(0)) } registry.clear() @@ -61,6 +67,7 @@ object EcoEnchants : RegistrableCategory("enchant", "enchants") { val enchantment = plugin.enchantmentRegisterer.register(enchant) // Register delegated versions registry.register(enchantment as EcoEnchant) + BY_NAME[ChatColor.stripColor(enchant.getFormattedName(0))] = enchantment as EcoEnchant EnchantRegistrations.registerEnchantments() } @@ -80,4 +87,10 @@ object EcoEnchants : RegistrableCategory("enchant", "enchants") { } } } + + fun getByName(name: String?): EcoEnchant? { + return if (name == null) { + null + } else BY_NAME[name] + } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/LoreConversion.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/LoreConversion.kt new file mode 100644 index 00000000..b85120e3 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/LoreConversion.kt @@ -0,0 +1,115 @@ +package com.willfp.ecoenchants.enchant + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.fast.fast +import com.willfp.eco.util.NumberUtils +import org.bukkit.ChatColor +import org.bukkit.enchantments.Enchantment +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.inventory.InventoryOpenEvent +import org.bukkit.event.player.PlayerItemHeldEvent +import org.bukkit.inventory.BlockInventoryHolder +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.EnchantmentStorageMeta + +class LoreConversion( + private val plugin: EcoPlugin +) : Listener { + @EventHandler + fun loreConverter(event: PlayerItemHeldEvent) { + if (!plugin.configYml.getBool("lore-conversion.enabled")) { + return + } + + convertLore(event.player.inventory.getItem(event.newSlot)) + } + + @EventHandler + fun aggressiveLoreConverter(event: InventoryOpenEvent) { + if (!plugin.configYml.getBool("lore-conversion.enabled")) { + return + } + if (!plugin.configYml.getBool("lore-conversion.aggressive")) { + return + } + + val inventory = event.inventory + + if (inventory.holder !is BlockInventoryHolder) { + return + } + + for (itemStack in inventory.contents) { + convertLore(itemStack) + } + } + + private fun convertLore(itemStack: ItemStack?) { + if (itemStack == null) { + return + } + + val meta = itemStack.itemMeta ?: return + + val toAdd = mutableMapOf() + + val lore = itemStack.fast().lore.toMutableList() + + for (line in lore.toList()) { + val uncolored = ChatColor.stripColor(line) ?: continue + + var enchant: EcoEnchant? + var level: Int + val split = uncolored.split(" ").toMutableList() + + if (split.isEmpty()) { + continue + } + + if (split.size == 1) { + enchant = EcoEnchants.getByName(split[0]) + level = 1 + } else { + val attemptFullLine = EcoEnchants.getByName(line) + if (attemptFullLine != null) { + enchant = attemptFullLine + level = 1 + } else { + var levelString = split.last() + split.remove(levelString) + levelString = levelString.trim { it <= ' ' } + level = try { + NumberUtils.fromNumeral(levelString) + } catch (e: IllegalArgumentException) { + continue + } + val enchantName = split.joinToString(" ") + enchant = EcoEnchants.getByName(enchantName) + } + } + + if (enchant == null) { + continue + } + + toAdd[enchant.enchantment] = level + } + + + if (meta is EnchantmentStorageMeta) { + lore.clear() + for ((enchant, level) in toAdd) { + meta.addStoredEnchant(enchant, level, true) + } + } else { + lore.clear() + for ((enchant, level) in toAdd) { + meta.addEnchant(enchant, level, true) + } + } + + itemStack.itemMeta = meta + itemStack.fast().lore = lore + } +} \ No newline at end of file