Reputation: 23
i have started 1 day ago with scripting java and i am working with bukkit.
my question is how i can get&set values( variables ) into a external file (plugin.yml) ?
like this
plugin.yml
commandforx: ABC
main.java
if(cmd.getName().equalsIgnoreCase( *commandforx* )) ......
so my target is to get the var commandforx from plugin.yml to main.java
and heres my second question how i can save coordinates?
also set&get the coordinates over the plugin.yml...
thanks ahead
Upvotes: 1
Views: 220
Reputation: 51
You should use the YamlConfiguration which is awsome for stuff like that.
File folder = new File("plugins//YourFolder");
File file = new File("plugins//YourFolder//YourFile.yml");
YamlConfiguration cfg = YamlConfiguration.loadFile(file);
if(!file.exist()) {
try {
file.createNewFile();
} catch(IOException e) {
e.printStackTrace();
}
}
cfg.set("commandforx", "ABC");
try {
cfg.save(file);
} catch(IOException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 23616
If you would like to set commandforx
to the string ABC
you could do this in your main file:
this.getConfig().set("commandforx", "abc");
then to get it, you could do
String commandforx = this.getConfig().getString("commandforx");
Just make sure to add a null check before you try to get the string, or you'll get a NullPointer
:
if(this.getConfig().contains("commandforx")
Upvotes: 1
Reputation: 76
Don't store variables in plugin.yml. Instead you can use the FileConfiguration API that is in the Bukkit API by default! Here is a great tutorial on doing this: https://forums.bukkit.org/threads/tut-bukkits-new-fileconfiguration-api-create-a-yaml-configuration.42775/
Or alternatively, you can just read and write from a defined file (Tutorials on this are everywhere online) and parse the input. This is more difficult then using the FileConfiguration API, but it's still very simple and gives you more control over the file (I always use this method).
Upvotes: 0