Reformated awful code

This commit is contained in:
Jonas_Jones 2022-06-25 00:53:29 +02:00
parent 79fa4fbabc
commit 4ebfc27d0c

View file

@ -40,9 +40,9 @@ public class SimpleConfig {
private boolean broken = false; private boolean broken = false;
public interface DefaultConfig { public interface DefaultConfig {
String get( String namespace ); String get(String namespace);
static String empty( String namespace ) { static String empty(String namespace) {
return ""; return "";
} }
} }
@ -53,7 +53,7 @@ public class SimpleConfig {
private final String filename; private final String filename;
private DefaultConfig provider; private DefaultConfig provider;
private ConfigRequest(File file, String filename ) { private ConfigRequest(File file, String filename) {
this.file = file; this.file = file;
this.filename = filename; this.filename = filename;
this.provider = DefaultConfig::empty; this.provider = DefaultConfig::empty;
@ -67,7 +67,7 @@ public class SimpleConfig {
* @return current config request object * @return current config request object
* @see DefaultConfig * @see DefaultConfig
*/ */
public ConfigRequest provider( DefaultConfig provider ) { public ConfigRequest provider(DefaultConfig provider) {
this.provider = provider; this.provider = provider;
return this; return this;
} }
@ -79,11 +79,11 @@ public class SimpleConfig {
* @see SimpleConfig * @see SimpleConfig
*/ */
public SimpleConfig request() { public SimpleConfig request() {
return new SimpleConfig( this ); return new SimpleConfig(this);
} }
private String getConfig() { private String getConfig() {
return provider.get( filename ) + "\n"; return provider.get(filename) + "\n";
} }
} }
@ -95,65 +95,65 @@ public class SimpleConfig {
* @param filename - name of the config file * @param filename - name of the config file
* @return new config request object * @return new config request object
*/ */
public static ConfigRequest of( String filename ) { public static ConfigRequest of(String filename) {
Path path = FabricLoader.getInstance().getConfigDir(); Path path = FabricLoader.getInstance().getConfigDir();
return new ConfigRequest( path.resolve( filename + ".properties" ).toFile(), filename ); return new ConfigRequest(path.resolve(filename + ".properties").toFile(), filename);
} }
private void createConfig() throws IOException { private void createConfig() throws IOException {
// try creating missing files // try creating missing files
request.file.getParentFile().mkdirs(); request.file.getParentFile().mkdirs();
Files.createFile( request.file.toPath() ); Files.createFile(request.file.toPath());
// write default config data // write default config data
PrintWriter writer = new PrintWriter(request.file, "UTF-8"); PrintWriter writer = new PrintWriter(request.file, "UTF-8");
writer.write( request.getConfig() ); writer.write(request.getConfig());
writer.close(); writer.close();
} }
private void loadConfig() throws IOException { private void loadConfig() throws IOException {
Scanner reader = new Scanner( request.file ); Scanner reader = new Scanner(request.file);
for( int line = 1; reader.hasNextLine(); line ++ ) { for (int line = 1; reader.hasNextLine(); line++) {
parseConfigEntry( reader.nextLine(), line ); parseConfigEntry(reader.nextLine(), line);
} }
} }
private void parseConfigEntry( String entry, int line ) { private void parseConfigEntry(String entry, int line) {
if( !entry.isEmpty() && !entry.startsWith( "#" ) ) { if (!entry.isEmpty() && !entry.startsWith("#")) {
String[] parts = entry.split("=", 2); String[] parts = entry.split("=", 2);
if( parts.length == 2 ) { if (parts.length == 2) {
//Recognise comments after a value //Recognise comments after a value
String temp = parts[1].split(" #")[0]; String temp = parts[1].split(" #")[0];
config.put( parts[0], temp); config.put(parts[0], temp);
}else{ } else {
throw new RuntimeException("Syntax error in config file on line " + line + "!"); throw new RuntimeException("Syntax error in config file on line " + line + "!");
} }
} }
} }
private SimpleConfig( ConfigRequest request ) { private SimpleConfig(ConfigRequest request) {
this.request = request; this.request = request;
String identifier = "Config '" + request.filename + "'"; String identifier = "Config '" + request.filename + "'";
if( !request.file.exists() ) { if (!request.file.exists()) {
ArduinoControls.LOGGER.info( identifier + " is missing, generating default one..." ); ArduinoControls.LOGGER.info(identifier + " is missing, generating default one...");
try { try {
createConfig(); createConfig();
} catch (IOException e) { } catch (IOException e) {
ArduinoControls.LOGGER.error( identifier + " failed to generate!" ); ArduinoControls.LOGGER.error(identifier + " failed to generate!");
ArduinoControls.LOGGER.trace(String.valueOf(e)); ArduinoControls.LOGGER.trace(String.valueOf(e));
broken = true; broken = true;
} }
} }
if( !broken ) { if (!broken) {
try { try {
loadConfig(); loadConfig();
} catch (Exception e) { } catch (Exception e) {
ArduinoControls.LOGGER.error( identifier + " failed to load!" ); ArduinoControls.LOGGER.error(identifier + " failed to load!");
ArduinoControls.LOGGER.trace(String.valueOf(e)); ArduinoControls.LOGGER.trace(String.valueOf(e));
broken = true; broken = true;
} }
@ -169,8 +169,8 @@ public class SimpleConfig {
* @see SimpleConfig#getOrDefault * @see SimpleConfig#getOrDefault
*/ */
@Deprecated @Deprecated
public String get( String key ) { public String get(String key) {
return config.get( key ); return config.get(key);
} }
/** /**
@ -179,7 +179,7 @@ public class SimpleConfig {
* *
* @return value corresponding to the given key, or the default value * @return value corresponding to the given key, or the default value
*/ */
public String getOrDefault( String key, String def ) { public String getOrDefault(String key, String def) {
String val = get(key); String val = get(key);
return val == null ? def : val; return val == null ? def : val;
} }
@ -190,9 +190,9 @@ public class SimpleConfig {
* *
* @return value corresponding to the given key, or the default value * @return value corresponding to the given key, or the default value
*/ */
public int getOrDefault( String key, int def ) { public int getOrDefault(String key, int def) {
try { try {
return Integer.parseInt( get(key) ); return Integer.parseInt(get(key));
} catch (Exception e) { } catch (Exception e) {
return def; return def;
} }
@ -204,9 +204,9 @@ public class SimpleConfig {
* *
* @return value corresponding to the given key, or the default value * @return value corresponding to the given key, or the default value
*/ */
public boolean getOrDefault( String key, boolean def ) { public boolean getOrDefault(String key, boolean def) {
String val = get(key); String val = get(key);
if( val != null ) { if (val != null) {
return val.equalsIgnoreCase("true"); return val.equalsIgnoreCase("true");
} }
@ -219,9 +219,9 @@ public class SimpleConfig {
* *
* @return value corresponding to the given key, or the default value * @return value corresponding to the given key, or the default value
*/ */
public double getOrDefault( String key, double def ) { public double getOrDefault(String key, double def) {
try { try {
return Double.parseDouble( get(key) ); return Double.parseDouble(get(key));
} catch (Exception e) { } catch (Exception e) {
return def; return def;
} }
@ -244,7 +244,7 @@ public class SimpleConfig {
* @return true if the operation was successful * @return true if the operation was successful
*/ */
public boolean delete() { public boolean delete() {
VerboseLogger.warn( "Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it." ); VerboseLogger.warn("Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it.");
return request.file.delete(); return request.file.delete();
} }