Created
November 2, 2018 03:55
-
-
Save mworzala/19fa96fb5a79c9550110ec74e8f6b71b to your computer and use it in GitHub Desktop.
paste this into cngmgr
This file contains hidden or 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 uk.co.netbans.netbans.Config; | |
import ninja.leaping.configurate.commented.CommentedConfigurationNode; | |
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; | |
import ninja.leaping.configurate.loader.ConfigurationLoader; | |
import org.slf4j.Logger; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.function.Function; | |
public class ConfigManager { | |
private final Path location; | |
private final String configName; | |
private final File config; | |
private final Logger logger; | |
private ConfigurationLoader<CommentedConfigurationNode> conf; | |
private CommentedConfigurationNode node; | |
public ConfigManager(Path configLocation, String configName, Logger logger, Function<CommentedConfigurationNode, CommentedConfigurationNode> customOptions) { | |
this.location = configLocation; | |
this.configName = configName; | |
this.logger = logger; | |
if (!location.toFile().exists()) { | |
location.toFile().mkdir(); | |
} | |
config = new File(location.toFile(), this.configName); | |
conf = HoconConfigurationLoader.builder().setFile(config).build(); | |
try { | |
node = conf.load(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
node = customOptions.apply(node); | |
try { | |
saveConfig(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
save(config); | |
} | |
private void saveConfig() { | |
try { | |
conf.save(node); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void save(File input) { | |
conf = HoconConfigurationLoader.builder().setFile(input).build(); | |
} | |
public String getStringOption(Object... nodes) { | |
String option = ""; | |
try { | |
option = node.getNode(nodes).getString(); | |
} catch (NullPointerException e) { | |
e.printStackTrace(); | |
} | |
return option; | |
} | |
public Number getNumberOption(Object... nodes) { | |
Number option = -1; | |
try { | |
option = node.getNode(nodes).getDouble(); | |
} catch (NullPointerException e) { | |
e.printStackTrace(); | |
} | |
return option; | |
} | |
public boolean getBooleanOption(Object... nodes) { | |
boolean option = false; | |
try { | |
option = node.getNode(nodes).getBoolean(); | |
} catch (NullPointerException e) { | |
e.printStackTrace(); | |
} | |
return option; | |
} | |
public ArrayList<String> getArrayListOptions(Object... nodes) { | |
Function<Object, String> stringTransformer = new Function<Object, String>() { | |
public String apply(Object input) { | |
if (input instanceof String) { | |
return (String) input; | |
} else { | |
return null; | |
} | |
} | |
}; | |
ArrayList<String> finallist = new ArrayList(); | |
node.getNode(nodes).getList(stringTransformer).forEach((x) -> { | |
finallist.add(x); | |
}); | |
return finallist; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment