Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import com.google.common.reflect.TypeToken;
- import ninja.leaping.configurate.ConfigurationNode;
- import ninja.leaping.configurate.commented.CommentedConfigurationNode;
- import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
- import ninja.leaping.configurate.loader.ConfigurationLoader;
- import ninja.leaping.configurate.objectmapping.ObjectMappingException;
- import java.io.File;
- import java.io.IOException;
- import java.util.Collection;
- import java.util.List;
- import java.util.function.Function;
- import java.util.stream.Collector;
- import java.util.stream.Collectors;
- public class Config {
- private ConfigurationLoader<CommentedConfigurationNode> loader;
- private CommentedConfigurationNode root;
- public Config(ConfigurationLoader<CommentedConfigurationNode> loader, CommentedConfigurationNode root){
- this.loader = loader;
- this.root = root;
- }
- public Config(File file) {
- this.loader = HoconConfigurationLoader.builder().setFile(file).build();
- try {
- root = this.loader.load();
- } catch (IOException e) {
- root = this.loader.createEmptyNode();
- }
- }
- /**
- * Removes a node from the config file, this includes child nodes
- * @param node node to remove
- * @return itself for chaining
- */
- public Config remove(Object... node){
- return set(null, node);
- }
- /**
- * Sets a object directly into the node
- * @param object the object to set
- * @param node the location
- * @return itself for chaining
- */
- public Config set(Object object, Object... node) {
- this.root.getNode(node).setValue(object);
- return this;
- }
- /**
- * Sets the object directly into the node
- * @param type the type the object is
- * @param obj the object to set
- * @param node the location
- * @param <T> the object type
- * @return itself for chaining
- * @throws ObjectMappingException
- */
- public <T extends Object> Config set(TypeToken<T> type, T obj, Object... node) throws ObjectMappingException {
- this.root.getNode(node).setValue(type, obj);
- return this;
- }
- /**
- * Gets the value in the location however if none is found then the other is used
- * @param function the function to get the value
- * @param other the failsafe object
- * @param node the location
- * @param <T> the type of what to get
- * @return the value at the location or other if no value is found
- */
- public <T extends Object> T get(Function<ConfigurationNode, T> function, T other, Object... node) {
- T value = function.apply(this.root.getNode(node));
- if (value == null) {
- return other;
- }
- return value;
- }
- /**
- * Gets a specified value from the node
- *
- * @param function the conversion from node to T
- * @param node the location
- * @param <T> the type
- * @return the value
- */
- public <T extends Object> T get(Function<ConfigurationNode, T> function, Object... node) {
- return function.apply(this.root.getNode(node));
- }
- /**
- * Checks if the location is real or not
- * @param node the location to check
- * @return if the location is not real
- */
- public boolean isVirtual(Object... node){
- return get(ConfigurationNode::isVirtual, node);
- }
- /**
- * Gets a integer value from the node
- *
- * @param node the location
- * @return the value
- */
- public int getInteger(Object... node) {
- return get(ConfigurationNode::getInt, node);
- }
- /**
- * Gets a double value from the node
- *
- * @param node the location
- * @return the value
- */
- public double getDouble(Object... node) {
- return get(ConfigurationNode::getDouble, node);
- }
- /**
- * Gets a string value from the node
- *
- * @param node the location
- * @return the value
- */
- public String getString(Object... node) {
- return get(ConfigurationNode::getString, node);
- }
- /**
- * Gets a boolean value from the node
- *
- * @param node the location
- * @return the value
- */
- public boolean getBoolean(Object... node) {
- return get(ConfigurationNode::getBoolean, node);
- }
- /**
- * Gets a specified type of collection from the configuration file
- *
- * @param collector The type of collection @see Collectors
- * @param function the conversion from node to T
- * @param node the location of the position
- * @param <T> the class type of the collection
- * @param <A> the type of collection
- * @return A specified type of collection with all values from the node
- */
- public <T, A extends Collection<T>> A getCollection(Collector<T, ?, A> collector, Function<ConfigurationNode, T> function, Object... node) {
- return this.root.getNode(node).getChildrenList().stream().map(c -> function.apply(c)).collect(collector);
- }
- /**
- * Gets a list of T from the configuration file
- *
- * @param function The conversion from node to T
- * @param node The location of the position
- * @param <T> The class type of the list
- * @return The list with all values
- */
- public <T> List<T> getList(Function<ConfigurationNode, T> function, Object... node) {
- return getCollection(Collectors.toList(), function, node);
- }
- /**
- * Saves the changes you have made to the config
- *
- * @return itself for chaining
- * @throws IOException if it can not save for X reason
- */
- public Config save() throws IOException {
- this.loader.save(this.root);
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement