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,64 +57,51 @@ public class ConfigManager {
} }
if(!file.exists()){ if(!file.exists()){
saveDefaultConfiguration(); try{
InputStream input = Main.plugin.getResource(defaultFilename);
if(input == null){
throw new RuntimeException("Could not create input stream for "+defaultFilename);
}
java.nio.file.Files.copy(input, file.toPath());
input.close();
} catch(IOException e){
e.printStackTrace();
}
} }
this.config = YamlConfiguration.loadConfiguration(file); FileInputStream fileInputStream;
try {
FileInputStream input = null; fileInputStream = new FileInputStream(file);
try{ } catch (FileNotFoundException e){
input = new FileInputStream(file);
} catch (Exception e){
throw new RuntimeException("Could not create input stream for "+file.getPath()); throw new RuntimeException("Could not create input stream for "+file.getPath());
} }
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8); this.config = new YamlConfiguration();
this.defaultConfig = YamlConfiguration.loadConfiguration(reader); try {
try{ this.config.load(reader);
input.close(); } catch(InvalidConfigurationException | IOException e){
reader.close(); throw new RuntimeException("Invalid configuration in config file: "+file.getPath());
} 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 = this.getClass().getClassLoader().getResourceAsStream(defaultFilename);
InputStream input = Main.plugin.getResource(defaultFilename);
if(input == null){ if(input == null){
throw new RuntimeException("Could not create input stream for "+defaultFilename); throw new RuntimeException("Could not create input stream for "+defaultFilename);
} }
InputStreamReader reader = new InputStreamReader(input); InputStreamReader default_reader = new InputStreamReader(input, StandardCharsets.UTF_8);
this.defaultConfig = YamlConfiguration.loadConfiguration(reader); this.defaultConfig = new YamlConfiguration();
try{ try {
input.close(); this.defaultConfig.load(reader);
reader.close(); } catch(InvalidConfigurationException | IOException e){
} catch (IOException e){ throw new RuntimeException("Invalid configuration in config file: "+file.getPath());
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);
if(input == null){
throw new RuntimeException("Could not create input stream for "+defaultFilename);
}
java.nio.file.Files.copy(input, file.toPath());
input.close(); input.close();
} catch(IOException e){ fileInputStream.close();
e.printStackTrace(); reader.close();
default_reader.close();
} catch (IOException e){
throw new RuntimeException("Unable to finalize loading of config files.");
} }
} }
@ -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");