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 006b29e7..9724802b 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 @@ -44,6 +44,7 @@ object EcoEnchants : RegistrableCategory("enchant", "enchants") { override fun afterReload(plugin: LibreforgePlugin) { plugin as EcoEnchantsPlugin + sendPrompts(plugin) registerHardcodedEnchantments(plugin) } @@ -54,13 +55,17 @@ object EcoEnchants : RegistrableCategory("enchant", "enchants") { return } - val enchant = LibreforgeEcoEnchant( - id, - config, - plugin - ) + try { + val enchant = LibreforgeEcoEnchant( + id, + config, + plugin + ) - doRegister(plugin, enchant) + doRegister(plugin, enchant) + } catch (e: MissingDependencyException) { + addPluginPrompt(plugin, e.plugins) + } } private fun doRegister(plugin: EcoEnchantsPlugin, enchant: EcoEnchant) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/MissingDependencyException.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/MissingDependencyException.kt new file mode 100644 index 00000000..7df6ac91 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/MissingDependencyException.kt @@ -0,0 +1,34 @@ +package com.willfp.ecoenchants.enchant + +import com.willfp.eco.core.EcoPlugin +import com.willfp.ecoenchants.EcoEnchantsPlugin + +class MissingDependencyException( + val plugins: Set +) : Exception() { + override val message = "Missing the following plugins: ${plugins.joinToString(", ")}" +} + +// Plugin names mapped to enchants that aren't installed. +private val prompts = mutableMapOf() + +fun addPluginPrompt(plugin: EcoEnchantsPlugin, plugins: Set) { + if (!plugin.isLoaded) { + return + } + + for (pluginName in plugins) { + prompts[pluginName] = prompts.getOrDefault(pluginName, 0) + 1 + } +} + +fun sendPrompts(plugin: EcoPlugin) { + for ((pl, amount) in prompts) { + plugin.logger.apply { + warning("$amount enchantments were not loaded because they need $pl to be installed!") + warning("Either download $pl or delete the folder at /plugins/EcoEnchants/enchants/${pl.lowercase()} to remove this message") + } + } + + prompts.clear() +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/impl/LibreforgeEcoEnchant.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/impl/LibreforgeEcoEnchant.kt index bffa6c08..471f0158 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/impl/LibreforgeEcoEnchant.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/impl/LibreforgeEcoEnchant.kt @@ -1,12 +1,15 @@ package com.willfp.ecoenchants.enchant.impl import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.util.containsIgnoreCase import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.enchant.EcoEnchantLevel +import com.willfp.ecoenchants.enchant.MissingDependencyException import com.willfp.libreforge.BlankHolder.conditions import com.willfp.libreforge.BlankHolder.effects import com.willfp.libreforge.SilentViolationContext import com.willfp.libreforge.effects.Effects +import org.bukkit.Bukkit class LibreforgeEcoEnchant( id: String, @@ -21,4 +24,18 @@ class LibreforgeEcoEnchant( override fun createLevel(level: Int): EcoEnchantLevel { return EcoEnchantLevel(this, level, effects, conditions, plugin) } + + init { + val missingPlugins = mutableSetOf() + + for (dependency in config.getStrings("dependencies")) { + if (!Bukkit.getPluginManager().plugins.map { it.name }.containsIgnoreCase(dependency)) { + missingPlugins += dependency + } + } + + if (missingPlugins.isNotEmpty()) { + throw MissingDependencyException(missingPlugins) + } + } }