Skip to content

Instantly share code, notes, and snippets.

@mattmess1221
Last active August 29, 2015 14:06
Show Gist options
  • Save mattmess1221/0701ad483f334dd20687 to your computer and use it in GitHub Desktop.
Save mattmess1221/0701ad483f334dd20687 to your computer and use it in GitHub Desktop.
Usage for config system.
package mnm.plugins.test;
import org.spongepowered.api.plugin.config.*;
public class ConfigTest {
public void loadConfig(Configuration config) {
// loads any changes
config.load();
// Sets a comment
config.setComment("Example config file");
String string = config.getPrimitive("string", "I am a String.",
"This is a String element").getValue();
ConfigObject confObj = config.getObject("object",
"This is an object element");
int integer = confObj.getPrimitive("integer", Integer.class).getValue();
ConfigArray<String> array = confObj.getArray("array", String.class);
for (String elem : array) { // We can iterate arrays
System.out.println(elem);
}
ConfigObject object2 = confObj.getObject("object");
for (ConfigElement<?> eleme : object2) { // objects, too
ConfigElementType type = eleme.getType();
switch(type){
case ARRAY:
eleme.toConfigArray();
break;
case NULL:
eleme.toConfigNull();
break;
case OBJECT:
eleme.toConfigObject();
break;
case PRIMITIVE:
eleme.toConfigPrimitive();
break;
}
}
// This will work for saving, but will raise issues when loading.
ConfigPrimitive<Object> object = config.getPrimitive("jobject", Object.class);
// Calls toString() to save.
object.setValue(this);
// There is no fromString()
// Will always return null :(
object.getValue();
// save changes
config.save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment