Reimplemented MissingDependencyException

This commit is contained in:
Will FP 2023-12-07 17:35:21 +00:00
parent 942d76f989
commit 32d8ceb775
3 changed files with 62 additions and 6 deletions

View File

@ -44,6 +44,7 @@ object EcoEnchants : RegistrableCategory<EcoEnchant>("enchant", "enchants") {
override fun afterReload(plugin: LibreforgePlugin) {
plugin as EcoEnchantsPlugin
sendPrompts(plugin)
registerHardcodedEnchantments(plugin)
}
@ -54,6 +55,7 @@ object EcoEnchants : RegistrableCategory<EcoEnchant>("enchant", "enchants") {
return
}
try {
val enchant = LibreforgeEcoEnchant(
id,
config,
@ -61,6 +63,9 @@ object EcoEnchants : RegistrableCategory<EcoEnchant>("enchant", "enchants") {
)
doRegister(plugin, enchant)
} catch (e: MissingDependencyException) {
addPluginPrompt(plugin, e.plugins)
}
}
private fun doRegister(plugin: EcoEnchantsPlugin, enchant: EcoEnchant) {

View File

@ -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<String>
) : Exception() {
override val message = "Missing the following plugins: ${plugins.joinToString(", ")}"
}
// Plugin names mapped to enchants that aren't installed.
private val prompts = mutableMapOf<String, Int>()
fun addPluginPrompt(plugin: EcoEnchantsPlugin, plugins: Set<String>) {
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()
}

View File

@ -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<String>()
for (dependency in config.getStrings("dependencies")) {
if (!Bukkit.getPluginManager().plugins.map { it.name }.containsIgnoreCase(dependency)) {
missingPlugins += dependency
}
}
if (missingPlugins.isNotEmpty()) {
throw MissingDependencyException(missingPlugins)
}
}
}