I'm currently programming a Minecraft plugin where I need a LocationManager
I used the one myself that I always use but it does not work with this project the folder + the file is not created and a NullPointerException is output here my class:
private static File file = new File ("plugins //" + Data.folder + "//locations.yml");
private static final YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration (file);
public void applyLocation (String name, Player player) {
if (file.exists ()) {
yamlConfiguration.set (name + ".world", player.getLocation (). GetWorld ());
yamlConfiguration.set (name + ".x", player.getLocation (). GetX ());
yamlConfiguration.set (name + ".y", player.getLocation (). GetY ());
yamlConfiguration.set (name + ".z", player.getLocation (). GetZ ());
yamlConfiguration.set (name + ".yaw", player.getLocation (). GetYaw ());
yamlConfiguration.set (name + ".pitch", player.getLocation (). GetPitch ());
try {
yamlConfiguration.save (file);
} catch (IOException ioException) {
ioException.printStackTrace ();
}
;
} else {
try {
file.mkdir ();
} catch (exception exception) {
exception.printStackTrace ();
}
}
}
public location getLocation (String name) {
double x = yamlConfiguration.getDouble (name + ".x");
double y = yamlConfiguration.getDouble (name + ".y");
double z = yamlConfiguration.getDouble (name + ".z");
double yaw = yamlConfiguration.getDouble (name + ".yaw");
double pitch = yamlConfiguration.getDouble (name + ".pitch");
String WorldName = yamlConfiguration.getString (name + ".world");
World world = Bukkit.getWorld (WorldName);
return new Location (world, x, y, z, (float) yaw, (float) pitch);
}
public boolean existsLocation (String Location) {
return yamlConfiguration. Get (Location.toLowerCase () + ".x")! = null;
}
The error message would be something…
PS:
The file does not necessarily have to exist for the set method of the file configuration.
The file is created at save