Question

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

Was it helpful?

Solution 2

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).

OTHER TIPS

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")

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top