mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
Write default config on enchant registering.
Fix Enchantment Squared group not adding element.
This commit is contained in:
parent
a5c647776c
commit
fc7e85529c
6 changed files with 90 additions and 92 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue