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

@ -1,9 +1,13 @@
package xyz.alexcrea.cuanvil.api; package xyz.alexcrea.cuanvil.api;
import io.delilaheve.CustomAnvil;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry; import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
@ -20,6 +24,8 @@ import java.util.Map;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class EnchantmentApi { public class EnchantmentApi {
private static int saveChangeTask = -1;
private EnchantmentApi() {} private EnchantmentApi() {}
/** /**
@ -39,6 +45,9 @@ public class EnchantmentApi {
EnchantLimitConfigGui.getInstance().updateValueForGeneric(enchantment, true); EnchantLimitConfigGui.getInstance().updateValueForGeneric(enchantment, true);
} }
// Write default if do not exist
writeDefaultConfig(enchantment, false);
return true; return true;
} }
@ -138,4 +147,44 @@ public class EnchantmentApi {
return Collections.unmodifiableMap(CAEnchantmentRegistry.getInstance().registeredEnchantments()); 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);
}
} }

View file

@ -20,21 +20,38 @@ public abstract class ConfigHolder {
public static UnitRepairHolder UNIT_REPAIR_HOLDER; public static UnitRepairHolder UNIT_REPAIR_HOLDER;
public static CustomAnvilCraftHolder CUSTOM_RECIPE_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(); 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(); ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
CONFLICT_HOLDER = new ConflictConfigHolder(); CONFLICT_HOLDER = new ConflictConfigHolder();
UNIT_REPAIR_HOLDER = new UnitRepairHolder(); UNIT_REPAIR_HOLDER = new UnitRepairHolder();
CUSTOM_RECIPE_HOLDER = new CustomAnvilCraftHolder(); CUSTOM_RECIPE_HOLDER = new CustomAnvilCraftHolder();
return reloadAllFromDisk(true); return removeNonDefaultFromDisk(true);
} }
public static boolean reloadAllFromDisk(boolean hardfail) { public static boolean reloadAllFromDisk(boolean hardfail) {
boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail); boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail);
if (!sucess) return false; 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; if (!sucess) return false;
sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail); sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
if (!sucess) return false; if (!sucess) return false;

View file

@ -97,19 +97,23 @@ class CustomAnvil : JavaPlugin() {
chatListener = ChatEventListener() chatListener = ChatEventListener()
server.pluginManager.registerEvents(chatListener, this) server.pluginManager.registerEvents(chatListener, this)
// Load dependency
DependencyManager.loadDependency()
// Register anvil events // Register anvil events
server.pluginManager.registerEvents(AnvilEventListener(DependencyManager.packetManager), this) server.pluginManager.registerEvents(AnvilEventListener(DependencyManager.packetManager), this)
// Load metrics // Load metrics
Metrics(this, bstatsPluginId) 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) Bukkit.getScheduler().scheduleSyncDelayedTask(this, {loadEnchantmentSystem()}, 0L)
} }
private fun loadEnchantmentSystem(){ private fun loadEnchantmentSystem(){
// Load dependency // Load default configuration
DependencyManager.loadDependency() if (!ConfigHolder.loadDefaultConfig()) return
// Register enchantments // Register enchantments
CAEnchantmentRegistry.getInstance().registerStartupEnchantments() CAEnchantmentRegistry.getInstance().registerStartupEnchantments()
@ -119,8 +123,7 @@ class CustomAnvil : JavaPlugin() {
server.pluginManager.callEvent(enchantReadyEvent) server.pluginManager.callEvent(enchantReadyEvent)
// Load config // Load config
val success = ConfigHolder.loadConfig() if (!ConfigHolder.loadNonDefaultConfig()) return
if (!success) return
// temporary: handle 1.21 update // temporary: handle 1.21 update
Update_1_21.handleUpdate() Update_1_21.handleUpdate()
@ -133,7 +136,7 @@ class CustomAnvil : JavaPlugin() {
GuiSharedConstant.loadConstants() GuiSharedConstant.loadConstants()
// Register enchantment of compatible plugin and load configuration change. // Register enchantment of compatible plugin and load configuration change.
DependencyManager.handleCompatibilityConfig(this) DependencyManager.handleCompatibilityConfig()
} }
fun reloadResource( fun reloadResource(

View file

@ -1,13 +1,9 @@
package xyz.alexcrea.cuanvil.dependency package xyz.alexcrea.cuanvil.dependency
import org.bukkit.Bukkit 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.NoProtocoLib
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import java.io.File
object DependencyManager { object DependencyManager {
@ -21,7 +17,7 @@ object DependencyManager {
// ProtocolLib dependency // ProtocolLib dependency
packetManager = packetManager =
if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper() if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper()
else NoProtocoLib() else NoProtocoLib()
// Enchantment Squared dependency // Enchantment Squared dependency
if(pluginManager.isPluginEnabled("EnchantsSquared")){ if(pluginManager.isPluginEnabled("EnchantsSquared")){
@ -37,22 +33,11 @@ object DependencyManager {
} }
fun handleCompatibilityConfig(plugin: Plugin) { fun handleCompatibilityConfig() {
val folder = File(plugin.dataFolder, "compatibility")
enchantmentSquaredCompatibility?.registerPluginConfiguration() 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() { fun registerEnchantments() {
enchantmentSquaredCompatibility?.registerEnchantments() enchantmentSquaredCompatibility?.registerEnchantments()
ecoEnchantCompatibility?.registerEnchantments() ecoEnchantCompatibility?.registerEnchantments()

View file

@ -2,13 +2,9 @@ package xyz.alexcrea.cuanvil.dependency
import com.willfp.ecoenchants.enchant.EcoEnchants import com.willfp.ecoenchants.enchant.EcoEnchants
import io.delilaheve.CustomAnvil import io.delilaheve.CustomAnvil
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.plugin.Plugin import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.api.EnchantmentApi 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) { class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
@ -21,49 +17,14 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
} }
fun registerEnchantments() { fun registerEnchantments() {
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
for (ecoEnchant in EcoEnchants.values()) { 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) 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.ConflictBuilder
import xyz.alexcrea.cuanvil.api.EnchantmentApi import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.api.MaterialGroupApi import xyz.alexcrea.cuanvil.api.MaterialGroupApi
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
@ -36,6 +35,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
} }
fun registerEnchantments(){ fun registerEnchantments(){
CustomAnvil.instance.logger.info("Preparing Enchantment Squared compatibility...")
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) { for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
EnchantmentApi.registerEnchantment(CAEnchantSquaredEnchantment(enchant)) EnchantmentApi.registerEnchantment(CAEnchantSquaredEnchantment(enchant))
} }
@ -62,14 +62,8 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!! return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!!
} }
private val IS_READY_PATH = "enchantment_square_ready"
fun registerPluginConfiguration(){ fun registerPluginConfiguration(){
val defaultConfig = ConfigHolder.DEFAULT_CONFIG.config CustomAnvil.instance.logger.info("Preparing Enchantment Squared config...")
val isReady = defaultConfig.getBoolean(IS_READY_PATH, false)
if(isReady) return
CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...")
// Prepare enchantments // Prepare enchantments
val esEnchantments = ArrayList<CAEnchantSquaredEnchantment>() val esEnchantments = ArrayList<CAEnchantSquaredEnchantment>()
@ -77,22 +71,11 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
esEnchantments.add(getWrappedEnchant(enchant) as CAEnchantSquaredEnchantment) 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 // Write groups and conflicts
writeMissingGroups() writeMissingGroups()
writeMaterialRestriction(esEnchantments) writeMaterialRestriction(esEnchantments)
writeEnchantmentConflicts(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 !") CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !")
} }
@ -112,15 +95,15 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
MaterialGroupApi.addMaterialGroup(hoes) MaterialGroupApi.addMaterialGroup(hoes)
val shield = IncludeGroup("shield") val shield = IncludeGroup("shield")
hoes.addToPolicy(Material.SHIELD) shield.addToPolicy(Material.SHIELD)
MaterialGroupApi.addMaterialGroup(shield) MaterialGroupApi.addMaterialGroup(shield)
val elytra = IncludeGroup("elytra") val elytra = IncludeGroup("elytra")
hoes.addToPolicy(Material.ELYTRA) elytra.addToPolicy(Material.ELYTRA)
MaterialGroupApi.addMaterialGroup(elytra) MaterialGroupApi.addMaterialGroup(elytra)
val trinkets = IncludeGroup("trinkets") val trinkets = IncludeGroup("trinkets")
hoes.addToPolicy(Material.ROTTEN_FLESH) trinkets.addToPolicy(Material.ROTTEN_FLESH)
MaterialGroupApi.addMaterialGroup(trinkets) MaterialGroupApi.addMaterialGroup(trinkets)
} }