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 1713104f..3202baa3 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 @@ -11,6 +11,7 @@ import com.willfp.ecoenchants.config.TypesYml import com.willfp.ecoenchants.config.VanillaEnchantsYml import com.willfp.ecoenchants.display.EnchantDisplay import com.willfp.ecoenchants.enchants.EcoEnchants +import com.willfp.ecoenchants.enchants.LoreConversion import com.willfp.ecoenchants.enchants.impl.EnchantmentTelekinesis import com.willfp.ecoenchants.enchants.registerVanillaEnchants import com.willfp.ecoenchants.integrations.EnchantRegistrations @@ -55,7 +56,8 @@ class EcoEnchantsPlugin : LibReforgePlugin() { VillagerSupport(this), EnchantingTableSupport(this), LootSupport(this), - AnvilSupport(this) + AnvilSupport(this), + LoreConversion(this) ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LoreConversion.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LoreConversion.kt new file mode 100644 index 00000000..d13ca4b2 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LoreConversion.kt @@ -0,0 +1,116 @@ +package com.willfp.ecoenchants.enchants + +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] = 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 + } +} diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 7493a38d..a4675b8e 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -95,6 +95,13 @@ enchantinfo: - "&fApplicable to: &a%targets%" - "&fConflicts with: &a%conflicts%" +# Options for converting lore-based enchants (from other plugins) with EcoEnchants enchantments +# with the same names. If you're switching over from another plugin and don't want your players to +# lose their enchantments, just switch this on. +lore-conversion: + enabled: false # If lore conversion should be enabled + aggressive: false # Will convert all items in all inventories when opened, likely to use a lot of performance + cooldown: in-actionbar: true sound: