Add default for eco enchant enchantments.

This commit is contained in:
alexcrea 2024-06-23 22:13:55 +02:00
parent 1e15bc7ab3
commit a4dda06602
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
4 changed files with 57 additions and 4 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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.")
}
}
}