custom load config

This commit is contained in:
Tyler Murphy 2022-05-05 12:58:57 -04:00
parent 36027e007e
commit acb7da8337
4 changed files with 49 additions and 52 deletions

View file

@ -144,9 +144,9 @@ public class Config {
public static void loadConfig() { public static void loadConfig() {
config = new ConfigManager("config.yml"); config = ConfigManager.create("config.yml");
config.saveConfig(); config.saveConfig();
leaderboard = new ConfigManager("leaderboard.yml"); leaderboard = ConfigManager.create("leaderboard.yml");
//Spawn //Spawn
spawnPosition = new Vector( spawnPosition = new Vector(

View file

@ -21,6 +21,7 @@ package net.tylermurphy.hideAndSeek.configuration;
import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.*; import java.io.*;
@ -35,9 +36,18 @@ public class ConfigManager {
private YamlConfiguration config,defaultConfig; private YamlConfiguration config,defaultConfig;
private String defaultFilename; private String defaultFilename;
public ConfigManager(String filename){ public static ConfigManager create(String filename){
return new ConfigManager(filename, filename);
}
public static ConfigManager create(String filename, String defaultFilename){
return new ConfigManager(filename, defaultFilename);
}
private ConfigManager(String filename, String defaultFilename){
this.defaultFilename = defaultFilename;
this.file = new File(Main.data, filename); this.file = new File(Main.data, filename);
this.defaultFilename = file.getName();
File folder = Main.data; File folder = Main.data;
if(!folder.exists()){ if(!folder.exists()){
@ -47,55 +57,6 @@ public class ConfigManager {
} }
if(!file.exists()){ if(!file.exists()){
saveDefaultConfiguration();
}
this.config = YamlConfiguration.loadConfiguration(file);
FileInputStream input = null;
try{
input = new FileInputStream(file);
} catch (Exception e){
throw new RuntimeException("Could not create input stream for "+file.getPath());
}
InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8);
this.defaultConfig = YamlConfiguration.loadConfiguration(reader);
try{
input.close();
reader.close();
} catch (IOException ignored){
}
}
public ConfigManager(String filename, String defaultFilename){
this.defaultFilename = defaultFilename;
this.file = new File(Main.data, filename);
if(!file.exists()){
saveDefaultConfiguration();
}
this.config = YamlConfiguration.loadConfiguration(file);
InputStream input = Main.plugin.getResource(defaultFilename);
if(input == null){
throw new RuntimeException("Could not create input stream for "+defaultFilename);
}
InputStreamReader reader = new InputStreamReader(input);
this.defaultConfig = YamlConfiguration.loadConfiguration(reader);
try{
input.close();
reader.close();
} catch (IOException e){
Main.plugin.getLogger().severe("Couldn't find "+defaultFilename+" internally. Did you set an incorrect local?");
Main.plugin.getServer().getPluginManager().disablePlugin(Main.plugin);
throw new RuntimeException();
}
}
private void saveDefaultConfiguration(){
try{ try{
InputStream input = Main.plugin.getResource(defaultFilename); InputStream input = Main.plugin.getResource(defaultFilename);
if(input == null){ if(input == null){
@ -108,6 +69,42 @@ public class ConfigManager {
} }
} }
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e){
throw new RuntimeException("Could not create input stream for "+file.getPath());
}
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
this.config = new YamlConfiguration();
try {
this.config.load(reader);
} catch(InvalidConfigurationException | IOException e){
throw new RuntimeException("Invalid configuration in config file: "+file.getPath());
}
InputStream input = this.getClass().getClassLoader().getResourceAsStream(defaultFilename);
if(input == null){
throw new RuntimeException("Could not create input stream for "+defaultFilename);
}
InputStreamReader default_reader = new InputStreamReader(input, StandardCharsets.UTF_8);
this.defaultConfig = new YamlConfiguration();
try {
this.defaultConfig.load(reader);
} catch(InvalidConfigurationException | IOException e){
throw new RuntimeException("Invalid configuration in config file: "+file.getPath());
}
try{
input.close();
fileInputStream.close();
reader.close();
default_reader.close();
} catch (IOException e){
throw new RuntimeException("Unable to finalize loading of config files.");
}
}
public boolean contains(String path){ public boolean contains(String path){
return config.contains(path); return config.contains(path);
} }
@ -136,7 +133,7 @@ public class ConfigManager {
public float getFloat(String path){ public float getFloat(String path){
float value = (float) config.getDouble(path); float value = (float) config.getDouble(path);
if(value == 0){ if(value == 0.0F){
return (float) defaultConfig.getDouble(path); return (float) defaultConfig.getDouble(path);
} else { } else {
return value; return value;

View file

@ -38,7 +38,7 @@ public class Items {
public static void loadItems() { public static void loadItems() {
ConfigManager manager = new ConfigManager("items.yml"); ConfigManager manager = ConfigManager.create("items.yml");
SEEKER_ITEMS = new ArrayList<>(); SEEKER_ITEMS = new ArrayList<>();
ConfigurationSection SeekerItems = manager.getConfigurationSection("items.seeker"); ConfigurationSection SeekerItems = manager.getConfigurationSection("items.seeker");

View file

@ -36,7 +36,7 @@ public class Localization {
public static void loadLocalization() { public static void loadLocalization() {
ConfigManager manager = new ConfigManager("localization.yml", "lang/localization_"+Config.locale +".yml"); ConfigManager manager = ConfigManager.create("localization.yml", "lang/localization_"+Config.locale +".yml");
int PLUGIN_VERSION = manager.getDefaultInt("version"); int PLUGIN_VERSION = manager.getDefaultInt("version");
int VERSION = manager.getInt("version"); int VERSION = manager.getInt("version");