Write default config on enchant registering.

Fix Enchantment Squared group not adding element.
This commit is contained in:
alexcrea 2024-07-10 23:26:55 +02:00
parent a5c647776c
commit fc7e85529c
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
6 changed files with 90 additions and 92 deletions

View file

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

View file

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

View file

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

View file

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