mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
alpha version up & cleanup code.
This commit is contained in:
parent
1b83c8db81
commit
00fba2f4b0
40 changed files with 893 additions and 718 deletions
|
|
@ -19,27 +19,27 @@ public abstract class ConfigHolder {
|
|||
public static ConflictConfigHolder CONFLICT_HOLDER;
|
||||
public static UnitRepairHolder UNIT_REPAIR_HOLDER;
|
||||
|
||||
public static boolean loadConfig(){
|
||||
public static boolean loadConfig() {
|
||||
DEFAULT_CONFIG = new DefaultConfigHolder();
|
||||
ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
|
||||
CONFLICT_HOLDER = new ConflictConfigHolder();
|
||||
UNIT_REPAIR_HOLDER = new UnitRepairHolder();
|
||||
|
||||
boolean result = reloadAllFromDisk(true);
|
||||
if(result){
|
||||
if (result) {
|
||||
MetricsUtil.INSTANCE.testIfConfigIsDefault();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean reloadAllFromDisk(boolean hardfail){
|
||||
public static boolean reloadAllFromDisk(boolean hardfail) {
|
||||
|
||||
boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail);
|
||||
if(!sucess) return false;
|
||||
if (!sucess) return false;
|
||||
sucess = ITEM_GROUP_HOLDER.reloadFromDisk(hardfail);
|
||||
if(!sucess) return false;
|
||||
if (!sucess) return false;
|
||||
sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
|
||||
if(!sucess) return false;
|
||||
if (!sucess) return false;
|
||||
sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail);
|
||||
return sucess;
|
||||
}
|
||||
|
|
@ -49,43 +49,49 @@ public abstract class ConfigHolder {
|
|||
private static final File BACKUP_FOLDER = new File(CustomAnvil.instance.getDataFolder(), "backup");
|
||||
|
||||
protected FileConfiguration configuration;
|
||||
protected ConfigHolder(){
|
||||
|
||||
protected ConfigHolder() {
|
||||
|
||||
}
|
||||
|
||||
public abstract boolean reloadFromDisk(boolean hardFail);
|
||||
|
||||
public abstract void reload();
|
||||
public FileConfiguration getConfig(){
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
// Config name and files
|
||||
protected abstract String getConfigFileName();
|
||||
|
||||
protected String getConfigFileExtension(){
|
||||
protected String getConfigFileExtension() {
|
||||
return ".yml";
|
||||
}
|
||||
protected File getConfigFile(){
|
||||
return new File(CustomAnvil.instance.getDataFolder(), getConfigFileName()+getConfigFileExtension());
|
||||
|
||||
protected File getConfigFile() {
|
||||
return new File(CustomAnvil.instance.getDataFolder(), getConfigFileName() + getConfigFileExtension());
|
||||
}
|
||||
protected File getFirstBackup(){
|
||||
return new File(BACKUP_FOLDER, getConfigFileName()+"-first"+getConfigFileExtension());
|
||||
|
||||
protected File getFirstBackup() {
|
||||
return new File(BACKUP_FOLDER, getConfigFileName() + "-first" + getConfigFileExtension());
|
||||
}
|
||||
protected File getLastBackup(){
|
||||
return new File(BACKUP_FOLDER, getConfigFileName()+"-latest"+getConfigFileExtension());
|
||||
|
||||
protected File getLastBackup() {
|
||||
return new File(BACKUP_FOLDER, getConfigFileName() + "-latest" + getConfigFileExtension());
|
||||
}
|
||||
|
||||
// Save logic
|
||||
public boolean saveToDisk(boolean doBackup){
|
||||
if(doBackup){
|
||||
if(!saveBackup()){
|
||||
public boolean saveToDisk(boolean doBackup) {
|
||||
if (doBackup) {
|
||||
if (!saveBackup()) {
|
||||
CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
File base = getConfigFile();
|
||||
// if file exist and can't be deleted the file, then we gave up.
|
||||
if(base.exists() && !base.delete()) {
|
||||
if (base.exists() && !base.delete()) {
|
||||
CustomAnvil.instance.getLogger().severe("Could not save config: can't delete existing file.");
|
||||
return false;
|
||||
}
|
||||
|
|
@ -101,15 +107,15 @@ public abstract class ConfigHolder {
|
|||
return true;
|
||||
}
|
||||
|
||||
protected boolean saveBackup(){
|
||||
protected boolean saveBackup() {
|
||||
File base = getConfigFile();
|
||||
if(!base.exists()) return true; // We did back up everything we had to (nothing in this case)
|
||||
if (!base.exists()) return true; // We did back up everything we had to (nothing in this case)
|
||||
boolean sufficientSuccess = false;
|
||||
|
||||
BACKUP_FOLDER.mkdirs();
|
||||
// save first backup if do not exist
|
||||
File firstBackup = getFirstBackup();
|
||||
if(!firstBackup.exists()){
|
||||
if (!firstBackup.exists()) {
|
||||
try {
|
||||
Files.copy(base, firstBackup);
|
||||
sufficientSuccess = true;
|
||||
|
|
@ -120,7 +126,7 @@ public abstract class ConfigHolder {
|
|||
// save last backup
|
||||
File lastBackup = getLastBackup();
|
||||
// if file exist and can't be deleted the file, then we gave up.
|
||||
if(lastBackup.exists() && !lastBackup.delete()){
|
||||
if (lastBackup.exists() && !lastBackup.delete()) {
|
||||
return sufficientSuccess;
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +140,7 @@ public abstract class ConfigHolder {
|
|||
return sufficientSuccess;
|
||||
}
|
||||
|
||||
public static class DefaultConfigHolder extends ConfigHolder{
|
||||
public static class DefaultConfigHolder extends ConfigHolder {
|
||||
|
||||
@Override
|
||||
protected String getConfigFileName() {
|
||||
|
|
@ -150,15 +156,17 @@ public abstract class ConfigHolder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void reload() {}// Nothing to do
|
||||
public void reload() {
|
||||
}// Nothing to do
|
||||
|
||||
}
|
||||
|
||||
// Abstract class for non default config
|
||||
public abstract static class ResourceConfigHolder extends ConfigHolder{
|
||||
public abstract static class ResourceConfigHolder extends ConfigHolder {
|
||||
|
||||
String resourceName;
|
||||
private ResourceConfigHolder(String resourceName){
|
||||
|
||||
private ResourceConfigHolder(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
}
|
||||
|
||||
|
|
@ -170,8 +178,8 @@ public abstract class ConfigHolder {
|
|||
@Override
|
||||
public boolean reloadFromDisk(boolean hardFail) {
|
||||
YamlConfiguration configuration = CustomAnvil.instance.reloadResource(
|
||||
getConfigFileName()+getConfigFileExtension(), hardFail);
|
||||
if(configuration == null) return false;
|
||||
getConfigFileName() + getConfigFileExtension(), hardFail);
|
||||
if (configuration == null) return false;
|
||||
this.configuration = configuration;
|
||||
reload();
|
||||
return true;
|
||||
|
|
@ -180,10 +188,11 @@ public abstract class ConfigHolder {
|
|||
}
|
||||
|
||||
// Class for itemGroupsManager config
|
||||
public static class ItemGroupConfigHolder extends ResourceConfigHolder{
|
||||
public static class ItemGroupConfigHolder extends ResourceConfigHolder {
|
||||
private final static String FILE_NAME = "item_groups";
|
||||
|
||||
ItemGroupManager itemGroupsManager;
|
||||
|
||||
private ItemGroupConfigHolder() {
|
||||
super(FILE_NAME);
|
||||
}
|
||||
|
|
@ -198,7 +207,7 @@ public abstract class ConfigHolder {
|
|||
this.itemGroupsManager = new ItemGroupManager();
|
||||
this.itemGroupsManager.prepareGroups(this.configuration);
|
||||
|
||||
if(CONFLICT_HOLDER.getConfig() != null){
|
||||
if (CONFLICT_HOLDER.getConfig() != null) {
|
||||
CONFLICT_HOLDER.reload();
|
||||
}
|
||||
}
|
||||
|
|
@ -206,10 +215,11 @@ public abstract class ConfigHolder {
|
|||
}
|
||||
|
||||
// Class for enchant conflict config
|
||||
public static class ConflictConfigHolder extends ResourceConfigHolder{
|
||||
public static class ConflictConfigHolder extends ResourceConfigHolder {
|
||||
private final static String FILE_NAME = "enchant_conflict";
|
||||
|
||||
EnchantConflictManager conflictManager;
|
||||
|
||||
private ConflictConfigHolder() {
|
||||
super(FILE_NAME);
|
||||
}
|
||||
|
|
@ -229,14 +239,16 @@ public abstract class ConfigHolder {
|
|||
}
|
||||
|
||||
// Class for unit repair config
|
||||
public static class UnitRepairHolder extends ResourceConfigHolder{
|
||||
public static class UnitRepairHolder extends ResourceConfigHolder {
|
||||
private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item";
|
||||
|
||||
private UnitRepairHolder() {
|
||||
super(ITEM_GROUP_FILE_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {} // Do nothing
|
||||
public void reload() {
|
||||
} // Do nothing
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue