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 f271120f..2d5bc8cd 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 @@ -21,7 +21,6 @@ import com.willfp.ecoenchants.mechanics.EnchantingTableSupport import com.willfp.ecoenchants.mechanics.GrindstoneSupport import com.willfp.ecoenchants.mechanics.LootSupport import com.willfp.ecoenchants.mechanics.VillagerSupport -import com.willfp.ecoenchants.target.ActiveEnchantUpdateListeners import com.willfp.ecoenchants.target.EnchantLookup.heldEnchantLevels import com.willfp.libreforge.loader.LibreforgePlugin import com.willfp.libreforge.loader.configs.ConfigCategory @@ -62,7 +61,6 @@ class EcoEnchantsPlugin : LibreforgePlugin() { override fun loadListeners(): List { return listOf( - ActiveEnchantUpdateListeners(this), VillagerSupport(this), EnchantingTableSupport(this), LootSupport(this), diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt index faf4289f..3e853a85 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt @@ -1,6 +1,7 @@ package com.willfp.ecoenchants.enchants import com.github.benmanes.caffeine.cache.Caffeine +import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.ConfigType import com.willfp.eco.core.config.config import com.willfp.eco.core.config.interfaces.Config @@ -43,7 +44,7 @@ import java.util.Objects abstract class EcoEnchant( val id: String, configProvider: (EcoEnchant) -> Config, - protected val plugin: EcoEnchantsPlugin + protected val plugin: EcoPlugin ) : Enchantment(NamespacedKey.minecraft(id)), EcoEnchantLike { final override val config by lazy { configProvider(this) } override val enchant by lazy { this } @@ -84,19 +85,19 @@ abstract class EcoEnchant( constructor( config: Config, - plugin: EcoEnchantsPlugin + plugin: EcoPlugin ) : this(config.getString("id"), { config }, plugin) constructor( id: String, config: Config, - plugin: EcoEnchantsPlugin + plugin: EcoPlugin ) : this(id, { config }, plugin) @JvmOverloads constructor( id: String, - plugin: EcoEnchantsPlugin, + plugin: EcoPlugin, force: Boolean = true ) : this( id, @@ -127,10 +128,10 @@ abstract class EcoEnchant( } ) - conditions = if (plugin.isLoaded) Conditions.compile( + conditions = Conditions.compile( config.getSubsections("conditions"), ViolationContext(plugin, "Enchantment $id") - ) else emptyConditionList() + ) if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable.$id") == null) { val permission = Permission( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLevel.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLevel.kt new file mode 100644 index 00000000..d15ba4a7 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLevel.kt @@ -0,0 +1,33 @@ +package com.willfp.ecoenchants.enchants + +import com.willfp.eco.core.EcoPlugin +import com.willfp.libreforge.Holder +import com.willfp.libreforge.conditions.ConditionList +import com.willfp.libreforge.effects.EffectList +import java.util.Objects + +class EcoEnchantLevel( + parent: EcoEnchant, + level: Int, + override val effects: EffectList, + override val conditions: ConditionList, + plugin: EcoPlugin +) : Holder { + override val id = plugin.createNamespacedKey("${parent.id}_$level") + + override fun equals(other: Any?): Boolean { + if (other !is EcoEnchantLevel) { + return false + } + + return this.id == other.id + } + + override fun toString(): String { + return id.toString() + } + + override fun hashCode(): Int { + return Objects.hash(this.id) + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LibReforgeEcoEnchant.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LibReforgeEcoEnchant.kt index de3db453..febedc82 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LibReforgeEcoEnchant.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/LibReforgeEcoEnchant.kt @@ -1,5 +1,6 @@ package com.willfp.ecoenchants.enchants +import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.libreforge.Holder @@ -14,47 +15,17 @@ import java.util.Objects class LibReforgeEcoEnchant( id: String, config: Config, - plugin: EcoEnchantsPlugin + plugin: EcoPlugin ) : EcoEnchant( id, config, plugin ) { - private val effects: EffectList - - init { - effects = if (plugin.isLoaded) Effects.compile( - config.getSubsections("effects"), - ViolationContext(plugin, "Enchantment $id") - ) else emptyEffectList() - } + private val effects = Effects.compile( + config.getSubsections("effects"), + ViolationContext(plugin, "Enchantment $id") + ) override fun createLevel(level: Int): EcoEnchantLevel = EcoEnchantLevel(this, level, effects, conditions, plugin) } - -class EcoEnchantLevel( - parent: EcoEnchant, - level: Int, - override val effects: EffectList, - override val conditions: ConditionList, - plugin: LibreforgePlugin -) : Holder { - override val id = plugin.createNamespacedKey("${parent.id}_$level") - - override fun equals(other: Any?): Boolean { - if (other !is EcoEnchantLevel) { - return false - } - - return this.id == other.id - } - - override fun toString(): String { - return id.toString() - } - - override fun hashCode(): Int { - return Objects.hash(this.id) - } -} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentReplenish.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentReplenish.kt index 480d0bae..551f15c9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentReplenish.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentReplenish.kt @@ -1,5 +1,6 @@ package com.willfp.ecoenchants.enchants.impl +import com.willfp.eco.core.EcoPlugin import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.enchants.EcoEnchant import com.willfp.ecoenchants.target.EnchantLookup.hasEnchantActive @@ -27,7 +28,7 @@ class EnchantmentReplenish( private class ReplenishHandler( private val enchant: EcoEnchant, - private val plugin: EcoEnchantsPlugin + private val plugin: EcoPlugin ) : Listener { @EventHandler( ignoreCancelled = true diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentSoulbound.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentSoulbound.kt index 419d3603..895bd189 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentSoulbound.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/impl/EnchantmentSoulbound.kt @@ -1,5 +1,6 @@ package com.willfp.ecoenchants.enchants.impl +import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.data.keys.PersistentDataKey import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.core.data.profile @@ -32,7 +33,7 @@ class EnchantmentSoulbound( } private class SoulboundHandler( - private val plugin: EcoEnchantsPlugin, + private val plugin: EcoPlugin, private val enchant: EcoEnchant ) : Listener { private val savedSoulboundItems = PersistentDataKey( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/ActiveEnchantUpdateListeners.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/ActiveEnchantUpdateListeners.kt deleted file mode 100644 index 2e72760e..00000000 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/ActiveEnchantUpdateListeners.kt +++ /dev/null @@ -1,74 +0,0 @@ -package com.willfp.ecoenchants.target - -import com.willfp.eco.core.EcoPlugin -import com.willfp.eco.core.events.ArmorChangeEvent -import com.willfp.ecoenchants.target.EnchantLookup.clearEnchantCache -import com.willfp.ecoenchants.target.EnchantmentTargets.isEnchantable -import com.willfp.libreforge.updateEffects -import org.bukkit.entity.Player -import org.bukkit.event.EventHandler -import org.bukkit.event.Listener -import org.bukkit.event.entity.EntityPickupItemEvent -import org.bukkit.event.inventory.InventoryClickEvent -import org.bukkit.event.player.PlayerDropItemEvent -import org.bukkit.event.player.PlayerItemHeldEvent -import org.bukkit.event.player.PlayerJoinEvent - -@Suppress("UNUSED", "UNUSED_PARAMETER") -class ActiveEnchantUpdateListeners(private val plugin: EcoPlugin) : Listener { - @EventHandler - fun onItemPickup(event: EntityPickupItemEvent) { - if (event.entity !is Player) { - return - } - val player = event.entity as Player - - if (!event.item.itemStack.isEnchantable) { - return - } - - refreshPlayer(player) - } - - @EventHandler - fun onPlayerJoin(event: PlayerJoinEvent) { - refresh() - } - - @EventHandler - fun onInventoryDrop(event: PlayerDropItemEvent) { - if (!event.itemDrop.itemStack.isEnchantable) { - return - } - - refreshPlayer(event.player) - } - - @EventHandler - fun onChangeSlot(event: PlayerItemHeldEvent) { - refreshPlayer(event.player) - plugin.scheduler.run { refreshPlayer(event.player) } - } - - @EventHandler - fun onArmorChange(event: ArmorChangeEvent) { - refreshPlayer(event.player) - } - - @EventHandler - fun onInventoryClick(event: InventoryClickEvent) { - if (event.whoClicked !is Player) { - return - } - refreshPlayer(event.whoClicked as Player) - } - - private fun refresh() { - plugin.server.onlinePlayers.forEach { player: Player -> refreshPlayer(player) } - } - - private fun refreshPlayer(player: Player) { - player.clearEnchantCache() - player.updateEffects() - } -}