-
-
Save FerusGrim/f63692f0e93c6717c1d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.voxelwars.config; | |
import com.voxelwars.util.proxy.VoxelWars; | |
import ninja.leaping.configurate.ConfigurationNode; | |
import ninja.leaping.configurate.commented.CommentedConfigurationNode; | |
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; | |
import ninja.leaping.configurate.loader.ConfigurationLoader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URL; | |
public class Config { | |
private final String fileName; | |
private final File file; | |
private ConfigurationLoader<CommentedConfigurationNode> configLoader; | |
private ConfigurationNode rootNode; | |
public Config(String fileName) { | |
this.fileName = fileName; | |
if (!VoxelWars.isPresent()) { | |
throw new IllegalStateException("VoxelWars hasn't been initialized, yet!"); | |
} | |
this.file = new File(VoxelWars.getConfigDir().get(), fileName); | |
this.configLoader = this.getLoader(); | |
this.rootNode = this.get(ConfigPath.ROOT); | |
} | |
public String getFileName() { | |
return this.fileName; | |
} | |
public File getFile() { | |
return this.file; | |
} | |
public ConfigurationLoader<CommentedConfigurationNode> getLoader() { | |
if (this.configLoader != null) { | |
return this.configLoader; | |
} | |
if (this.file.exists()) { | |
this.configLoader = HoconConfigurationLoader.builder().setFile(this.file).build(); | |
return this.configLoader; | |
} | |
this.file.getParentFile().mkdirs(); | |
URL url = this.getClass().getClassLoader().getResource(this.fileName); | |
if (url == null) { | |
this.configLoader = HoconConfigurationLoader.builder().setFile(this.file).build(); | |
return this.configLoader; | |
} | |
this.configLoader = HoconConfigurationLoader.builder().setURL(url).build(); | |
return this.configLoader; | |
} | |
public ConfigurationNode get(ConfigPath path) { | |
if (path == ConfigPath.ROOT) { | |
if (this.rootNode != null) { | |
return this.rootNode; | |
} | |
try { | |
this.rootNode = this.getLoader().load(); | |
return this.rootNode; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
return this.rootNode.getNode(path.getPath()); | |
} | |
public void set(ConfigPath path, Object value) { | |
if (path == ConfigPath.ROOT) { | |
throw new IllegalArgumentException("Cannot \"set\" the ROOT path."); | |
} | |
this.rootNode.getNode(path.getPath()).setValue(value); | |
try { | |
this.getLoader().save(this.rootNode); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.voxelwars.config; | |
public enum ConfigPath { | |
ROOT(), | |
TESTING("config", "testing"), | |
TESTING_AGAIN("config", "testingAgain"), | |
TESTING_INT("config", "testingInt"), | |
TESTING_STRING("config", "testingString"), | |
NEW_VALUE("config", "newValue"), | |
; | |
private final String[] path; | |
ConfigPath(String... path) { | |
this.path = path; | |
} | |
public String[] getPath() { | |
return this.path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment