diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java index a4f1f68..c00c7c2 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentRegistry.java @@ -44,6 +44,9 @@ public class CAEnchantmentRegistry { if(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility() != null){ DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility().registerEnchantments(); } + if(DependencyManager.INSTANCE.getEcoEnchantCompatibility() != null){ + DependencyManager.INSTANCE.getEcoEnchantCompatibility().registerEnchantments(); + } } diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index ff8fef3..02423a6 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -108,7 +108,7 @@ class CustomAnvil : JavaPlugin() { Update_1_21.handleUpdate() // Handle custom enchant config - DependencyManager.handleConfigChanges() + DependencyManager.handleConfigChanges(this) // Load gui constants //TODO maybe something better later MainConfigGui.getInstance().init(DependencyManager.packetManager) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt index 311da9d..57e27d7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt @@ -1,9 +1,11 @@ package xyz.alexcrea.cuanvil.dependency import org.bukkit.Bukkit +import org.bukkit.plugin.Plugin import xyz.alexcrea.cuanvil.dependency.protocolib.NoProtocoLib import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper +import java.io.File object DependencyManager { @@ -33,9 +35,11 @@ object DependencyManager { } - fun handleConfigChanges() { + fun handleConfigChanges(plugin: Plugin) { + val folder = File(plugin.dataFolder, "compatibility") + enchantmentSquaredCompatibility?.registerPluginConfiguration() - ecoEnchantCompatibility?.registerEnchantments() + ecoEnchantCompatibility?.registerPluginConfiguration(folder) } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt index 87e050d..a128132 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt @@ -2,11 +2,14 @@ package xyz.alexcrea.cuanvil.dependency import com.willfp.ecoenchants.enchant.EcoEnchants import io.delilaheve.CustomAnvil +import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.plugin.Plugin +import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant +import java.io.File class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) { @@ -23,9 +26,52 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) { for (ecoEnchant in EcoEnchants.values()) { val enchantments: CAEnchantment = CAEcoEnchant(ecoEnchant) - registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As + registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As eco enchants are considered real enchantment, we need to unregister it. registery.register(enchantments) } } + fun registerPluginConfiguration(folder: File){ + val compatibilityFile = File(folder, "ecoEnchant.yml") + + if(compatibilityFile.exists()){ + compatibilityFile.createNewFile() + } + + val config = YamlConfiguration.loadConfiguration(compatibilityFile) + val defaultConfig = ConfigHolder.DEFAULT_CONFIG.config + var doSave = false + + for (ecoEnchant in EcoEnchants.values()) { + val enchantment = CAEnchantmentRegistry.getInstance().getByKey(ecoEnchant.enchantmentKey) + + if(enchantment == null){ + CustomAnvil.instance.logger.warning("Could not find " + ecoEnchant.enchantmentKey + "testing compatibility.") + continue + } + + // Write enchantment value if needed + val testPath = "default.${enchantment.key.key}" + if(!config.getBoolean(testPath, false)){ + doSave = true + config[testPath] = true + + defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel() + + val rarity = enchantment.defaultRarity() + defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue + defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue + + } + } + + if(doSave){ + config.save(compatibilityFile) + ConfigHolder.DEFAULT_CONFIG.saveToDisk(true) + + CustomAnvil.instance.logger.info("Saved default for new eco enchant enchantments.") + } + + } + }