I am quite new to coding in Java and I am trying to make a plugin which creates a character template. The user will type the command /char name . I want it to then check if the command sender's file exists and if so it will write under the field "Name: " in their .dat file. If the file does not exist then it will create a file and write in that field. The only problem I am having is creating and writing and reading from the file. At one point I managed to get it to create the user file and write the name that they set but I couldn't write more as it would write something stupid like "Name: Gender" in the file. I also have no idea how to read from the file as well as it needs to be able to get their set name to change the name tag above their head.
This is an example of the code I am using on the command "/char gender":
if (label.equalsIgnoreCase("char gender")) {
if (args.length < 2) {
sender.sendMessage("/char gender <Male or Female>");
return false;
}
if (args.equals("male")) {
PrintWriter writer1;
try {
writer1 = new PrintWriter("plugins/Guildplate/PlayerData/" + sender.getName() + ".yml", "UTF-8");
writer1.println("Gender: Male");
writer1.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
File file = new File("plugins/Guildplate/PlayerData/" + sender.getName() + ".yml");
}
}
else if (args.equals("female")) {
PrintWriter writer1;
try {
writer1 = new PrintWriter("plugins/Guildplate/PlayerData/" + sender.getName() + ".yml", "UTF-8");
writer1.println("Gender: Female");
writer1.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
File file = new File("plugins/Guildplate/PlayerData/" + sender.getName() + ".yml");
}
}
else {
sender.sendMessage("/char gender <male or female>");
}
I am using a .yml file to test if it works because some reason, it will not create the player.dat