Added lore conversion

This commit is contained in:
Auxilor 2022-08-30 19:34:35 +01:00
parent 5aec63ffc0
commit 76d8ce7c86
3 changed files with 126 additions and 1 deletions

View File

@ -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)
)
}

View File

@ -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<Enchantment, Int>()
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
}
}

View File

@ -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: