Adding value to config

I have trouble,

I want to add “h” (true) value to node b without deleting rest of them

Whats your current code? It is completely possible to do this, so it must be a issue with your code.

No code. I have separated cfg managers to all cases

Im not sure what you mean?

Do you mean you have separated the ConfigurationLoader (in the docs its known as ConfigurationManager) on a per file bases?

And how are you writing a configuration file without code?

Look what I want:
FileNameConfig.class:

  • static void load, setup & save,
  • static void (like FileNameConfig.node.getNode(…)) with node
  • and that manage file filename.conf (sharedroot = false)

Id love no-connection with Main (in general other) classes

Is that possible to do?

I dont think you understand how configs work. Yes you can have the config outside the main. However the config has 2 seperate parts to it, the loader and the nodes. For you to load a config you must have a output of the nodes (aka not void). Your second bullet … Not sure what you mean by that.

I am more then happy to provide you with code that will allow you to create, modify and save config files with ease however it wont be a static method that does it. It will be a whole class. Is that ok?

Of course, I want this yours code

Ok. Writing it now. Be up soon

1 Like

This is my init code

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(File file){
        this.loader = 
HoconConfigurationLoader.builder().setFile(file).build();
        try {
            root = this.loader.load();
        } catch (IOException e) {
            root = this.loader.createEmptyNode();
        }
    }

    public Config set(Object object){
        this.root.setValue(object);
        return this;
    }

    public <T extends Object> Config set(TypeToken<T> type, T obj) throws ObjectMappingException {
        this.root.setValue(type, obj);
        return this;
    }

    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));
    }

    /**
     * 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;
    }
}

Was it tested? I want to know before I’ll run InteliJ

Not tested no. However I have tested similar classes.

Just keep in mind that this code assumes that the file you provided is created. If its not and you pass it in then it will complain

1 Like

And getting File with
@Inject
@ConfigDir(sharedroot = false)
private File dir

??

Just specify the file you want

File file = new File("configuration/<your plugin id>/config.conf");

Remember that in java that the root is the programs directory, so in the case of a sponge plugin, it would be the root of the minecraft directory unless the user has specified otherwise (dont need to worry about user specifying elsewhere)

Oh and if you are worried about not having the right place. You can get sponge to output where it should be. Its something like the following

Path configDir = Sponge.getConfigManager().getPluginConfig(mainPlugin).getDirectory();