diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java index 57c3aa8..1c94e25 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java @@ -1,9 +1,13 @@ package xyz.alexcrea.cuanvil.api; +import io.delilaheve.CustomAnvil; +import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; @@ -20,6 +24,8 @@ import java.util.Map; @SuppressWarnings("unused") public class EnchantmentApi { + private static int saveChangeTask = -1; + private EnchantmentApi() {} /** @@ -39,6 +45,9 @@ public class EnchantmentApi { EnchantLimitConfigGui.getInstance().updateValueForGeneric(enchantment, true); } + // Write default if do not exist + writeDefaultConfig(enchantment, false); + return true; } @@ -138,4 +147,44 @@ public class EnchantmentApi { return Collections.unmodifiableMap(CAEnchantmentRegistry.getInstance().registeredEnchantments()); } + /** + * Write the default level and rarity configuration of the enchantment. + * @param enchantment The enchantment to write default configuration + * @param override If it should override old configuration + * @return Return false if override is false and a configuration exist. true otherwise. + */ + public static boolean writeDefaultConfig(CAEnchantment enchantment, boolean override){ + FileConfiguration config = ConfigHolder.DEFAULT_CONFIG.getConfig(); + if(!override && config.contains(enchantment.getName())) return false; + + writeDefaultConfig(config, enchantment); + + prepareSaveTask(); + return true; + } + + + private static void writeDefaultConfig(FileConfiguration defaultConfig, CAEnchantment enchantment) { + defaultConfig.set("enchant_limits." + enchantment.getKey().getKey(), enchantment.defaultMaxLevel()); + + String basePath = "enchant_values." + enchantment.getKey().getKey(); + EnchantmentRarity rarity = enchantment.defaultRarity(); + + defaultConfig.set(basePath + ".item", rarity.getItemValue()); + defaultConfig.set(basePath + ".book", rarity.getBookValue()); + } + + /** + * Prepare a task to save custom recipe configuration. + */ + private static void prepareSaveTask() { + if(saveChangeTask != -1) return; + + saveChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{ + ConfigHolder.DEFAULT_CONFIG.saveToDisk(true); + saveChangeTask = -1; + }, 0L); + } + + } diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java index dc6a14e..3cfaf03 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java +++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java @@ -20,21 +20,38 @@ public abstract class ConfigHolder { public static UnitRepairHolder UNIT_REPAIR_HOLDER; public static CustomAnvilCraftHolder CUSTOM_RECIPE_HOLDER; - public static boolean loadConfig() { + /** + * Load default configuration. + * @return True if successful. + */ + public static boolean loadDefaultConfig() { DEFAULT_CONFIG = new DefaultConfigHolder(); + + return DEFAULT_CONFIG.reloadFromDisk(true); + } + + /** + * Load non default configuration. + * @return True if successful. + */ + public static boolean loadNonDefaultConfig() { ITEM_GROUP_HOLDER = new ItemGroupConfigHolder(); CONFLICT_HOLDER = new ConflictConfigHolder(); UNIT_REPAIR_HOLDER = new UnitRepairHolder(); CUSTOM_RECIPE_HOLDER = new CustomAnvilCraftHolder(); - return reloadAllFromDisk(true); + return removeNonDefaultFromDisk(true); } public static boolean reloadAllFromDisk(boolean hardfail) { - boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail); if (!sucess) return false; - sucess = ITEM_GROUP_HOLDER.reloadFromDisk(hardfail); + + return removeNonDefaultFromDisk(hardfail); + } + + private static boolean removeNonDefaultFromDisk(boolean hardfail){ + boolean sucess = ITEM_GROUP_HOLDER.reloadFromDisk(hardfail); if (!sucess) return false; sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail); if (!sucess) return false; diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index c33cae6..1d5397d 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -97,19 +97,23 @@ class CustomAnvil : JavaPlugin() { chatListener = ChatEventListener() server.pluginManager.registerEvents(chatListener, this) + // Load dependency + DependencyManager.loadDependency() + // Register anvil events server.pluginManager.registerEvents(AnvilEventListener(DependencyManager.packetManager), this) // Load metrics Metrics(this, bstatsPluginId) - // Load other things + // Load other thing later. + // It is so other dependent plugins can implement there event listener before we fire them. Bukkit.getScheduler().scheduleSyncDelayedTask(this, {loadEnchantmentSystem()}, 0L) } private fun loadEnchantmentSystem(){ - // Load dependency - DependencyManager.loadDependency() + // Load default configuration + if (!ConfigHolder.loadDefaultConfig()) return // Register enchantments CAEnchantmentRegistry.getInstance().registerStartupEnchantments() @@ -119,8 +123,7 @@ class CustomAnvil : JavaPlugin() { server.pluginManager.callEvent(enchantReadyEvent) // Load config - val success = ConfigHolder.loadConfig() - if (!success) return + if (!ConfigHolder.loadNonDefaultConfig()) return // temporary: handle 1.21 update Update_1_21.handleUpdate() @@ -133,7 +136,7 @@ class CustomAnvil : JavaPlugin() { GuiSharedConstant.loadConstants() // Register enchantment of compatible plugin and load configuration change. - DependencyManager.handleCompatibilityConfig(this) + DependencyManager.handleCompatibilityConfig() } fun reloadResource( diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt index bede48f..f3962d5 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt @@ -1,13 +1,9 @@ package xyz.alexcrea.cuanvil.dependency import org.bukkit.Bukkit -import org.bukkit.configuration.file.FileConfiguration -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 xyz.alexcrea.cuanvil.enchant.CAEnchantment -import java.io.File object DependencyManager { @@ -21,7 +17,7 @@ object DependencyManager { // ProtocolLib dependency packetManager = if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper() - else NoProtocoLib() + else NoProtocoLib() // Enchantment Squared dependency if(pluginManager.isPluginEnabled("EnchantsSquared")){ @@ -37,22 +33,11 @@ object DependencyManager { } - fun handleCompatibilityConfig(plugin: Plugin) { - val folder = File(plugin.dataFolder, "compatibility") - + fun handleCompatibilityConfig() { enchantmentSquaredCompatibility?.registerPluginConfiguration() - ecoEnchantCompatibility?.registerPluginConfiguration(folder) } - fun writeDefaultConfig(defaultConfig: FileConfiguration, enchantment: CAEnchantment) { - 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 - } - fun registerEnchantments() { enchantmentSquaredCompatibility?.registerEnchantments() ecoEnchantCompatibility?.registerEnchantments() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt index 77e9792..89d0ca8 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EcoEnchantDependency.kt @@ -2,13 +2,9 @@ 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.api.EnchantmentApi -import xyz.alexcrea.cuanvil.config.ConfigHolder -import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry -import java.io.File class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) { @@ -21,49 +17,14 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) { } fun registerEnchantments() { + CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...") + for (ecoEnchant in EcoEnchants.values()) { - EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before ca, we need to unregister old "vanilla" enchant. + EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant. EnchantmentApi.registerEnchantment(ecoEnchant.enchantment) } - } - - fun registerPluginConfiguration(folder: File){ - val compatibilityFile = File(folder, "ecoEnchant.yml") - - if(compatibilityFile.exists()){ - folder.mkdirs() - 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 - - DependencyManager.writeDefaultConfig(defaultConfig, enchantment) //TODO move to api register - } - } - - if(doSave){ - config.save(compatibilityFile) - ConfigHolder.DEFAULT_CONFIG.saveToDisk(true) - - CustomAnvil.instance.logger.info("Saved default for new eco enchant enchantments.") - } + CustomAnvil.instance.logger.info("Eco Enchant should now work as expected !") } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt index f5e0fe1..3eacc2e 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt @@ -11,7 +11,6 @@ import org.bukkit.plugin.Plugin import xyz.alexcrea.cuanvil.api.ConflictBuilder import xyz.alexcrea.cuanvil.api.EnchantmentApi import xyz.alexcrea.cuanvil.api.MaterialGroupApi -import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment @@ -36,6 +35,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) } fun registerEnchantments(){ + CustomAnvil.instance.logger.info("Preparing Enchantment Squared compatibility...") for (enchant in CustomEnchantManager.getInstance().allEnchants.values) { EnchantmentApi.registerEnchantment(CAEnchantSquaredEnchantment(enchant)) } @@ -62,14 +62,8 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!! } - - private val IS_READY_PATH = "enchantment_square_ready" fun registerPluginConfiguration(){ - val defaultConfig = ConfigHolder.DEFAULT_CONFIG.config - val isReady = defaultConfig.getBoolean(IS_READY_PATH, false) - if(isReady) return - - CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...") + CustomAnvil.instance.logger.info("Preparing Enchantment Squared config...") // Prepare enchantments val esEnchantments = ArrayList() @@ -77,22 +71,11 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) esEnchantments.add(getWrappedEnchant(enchant) as CAEnchantSquaredEnchantment) } - // Write default level limit and xp cost - for (enchantment in esEnchantments) { //TODO move to api register - DependencyManager.writeDefaultConfig(defaultConfig, enchantment) - } - // Write groups and conflicts writeMissingGroups() writeMaterialRestriction(esEnchantments) writeEnchantmentConflicts(esEnchantments) - // Set ready - defaultConfig[IS_READY_PATH] = true - - // Save - ConfigHolder.DEFAULT_CONFIG.saveToDisk(true) - CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !") } @@ -112,15 +95,15 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) MaterialGroupApi.addMaterialGroup(hoes) val shield = IncludeGroup("shield") - hoes.addToPolicy(Material.SHIELD) + shield.addToPolicy(Material.SHIELD) MaterialGroupApi.addMaterialGroup(shield) val elytra = IncludeGroup("elytra") - hoes.addToPolicy(Material.ELYTRA) + elytra.addToPolicy(Material.ELYTRA) MaterialGroupApi.addMaterialGroup(elytra) val trinkets = IncludeGroup("trinkets") - hoes.addToPolicy(Material.ROTTEN_FLESH) + trinkets.addToPolicy(Material.ROTTEN_FLESH) MaterialGroupApi.addMaterialGroup(trinkets) }