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

View file

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