[SOLVED]Configuration Use

so…i know how to right the config…and it works…and i know how to right the Event…which also works…but how do i get the two to communicate properly?

I’m trying to make it so if the config node is false then another situation happens…however it won’t work…Help? here’s the code part i’m working on

    @Listener
    public void onCreeperExplode(ExplosionEvent.Pre event) {
        if (event.getCause().first(Creeper.class).isPresent()) {
            if (ConfigurationManager.getInstance().getConfig().getNode("Creeper").getValue() == "true"){
                event.setCancelled(true);
            }
            if ((Boolean) ConfigurationManager.getInstance().getConfig().getNode("Creeper").getValue("false")){
                event.setCancelled(false);
            }
        }
    }

now whiout the second and thrid if statements…and the second event.setcancelled(false) this stops creepers from exploding…however the rest of it is not working any ideas?

here’s the working part for stopping the explosing from a creeper:
@Listener
public void onCreeperExplode(ExplosionEvent.Pre event) {
if (event.getCause().first(Creeper.class).isPresent()) {
event.setCancelled(true);
}
}

but i want to add configuration into it

When comparing Strings for equality you should use the .equals() method instead of == see here. That first if statement works properly. I’m not sure what you’re using for your configuration manger class, but I suspect the issue lies in how you’re reading the value of nodes from your config file. Luckily, the sponge docs team has created some sweet documentation about getting the value of a node which you can find here! Hope that helps!

ok so here’s the configmanager…

package me.Cleardragonf.ExplosionGuard;

import java.io.File;
import java.io.IOException;

import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;

public class ConfigurationManager {

    private static ConfigurationManager instance = new ConfigurationManager();
   
    public static ConfigurationManager getInstance() {
            return instance;
    }
   
    private ConfigurationLoader<CommentedConfigurationNode> configLoader;
    private CommentedConfigurationNode config;
   
    public void setup(File configFile, ConfigurationLoader<CommentedConfigurationNode> configLoader) {
            this.configLoader = configLoader;
           
            if (!configFile.exists()) {
                    try {
                            configFile.createNewFile();
                            loadConfig();
                            config.getNode("Creeper").setComment("Disable Explosiong?").setValue("True");
                            saveConfig();
                    }
                   
                    catch (Exception e) {
                            e.printStackTrace();
                    }
            }
           
            else {
                    loadConfig();
            }
    }
   
    public CommentedConfigurationNode getConfig() {
            return config;
    }
   
    public void saveConfig() {
            try {
                    configLoader.save(config);
            }
           
            catch (IOException e) {
                    e.printStackTrace();
            }
    }
   
    public void loadConfig() {
            try {
                    config = configLoader.load();
            }
           
            catch (IOException e) {
                    e.printStackTrace();
            }
    }

}

…it’s because my value is case sensitive…isn’t it? lol cause i just changed the value’s both to "T"rue instead of “True” and “true” lol and it seems to be working for default now…

Yes if you are getting and setting strings they are case sensitive. If you are loading and setting boolean values may I suggest using the getBoolean() method. When you go to set the value you could just set the value to a boolean, something like valueNode.setValue(true); That way you eliminate doing any string comparisons and can make your listener a fancy one liner like:

event.setCancelled(ConfigurationManager.getInstance().getConfig().getNode("Creeper").getBoolean(false));

If you are worried about case sensitivity you could always utilize getString() and the equalsIgnoreCase() method that String has and do something like:

event.setCancelled(ConfigurationManager.getInstance().getConfig().getNode("Creeper").getString("False").equalsIgnoreCase("True"));

Hope that helps!

1 Like