Допустим, у меня был следующий основной класс: < /p>
Код: Выделить всё
package ...
import ...
// My main class
public class MyClass extends JavaPlugin {
// I instantiate my File and FileConfiguration here
// Should I do this? I need them for my other classes.
public FileConfiguration myFileConfig = null;
public File myFile;
@Override
public void onEnable() {
// Get/Create File
myFile = new File(getDataFolder(), "myfile.yml");
if (!myFile.exists()) {
try {
myFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// Load myfile.yml file configuration
FileConfiguration myFileConfig = YamlConfiguration.loadConfiguration(myFile);
// Register my command executor class
getCommand("test").setExecutor(new myCommandExecutor());
}
@Override
public void onDisable() {
// Irrelevant stuff here
}
}
Код: Выделить всё
package ...
import ...
public class myCommandExecutor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("test")) {
if (args.length > 0) {
// RIGHT HERE I WOULD ADD ALL THE COMMAND ARGUMENTS
// IMAGINE THE FOLLOWING USAGE FOR THE COMMAND
// USAGE: /test
// IF THE USER EXECUTED THE FOLLOWING, THE CODE BELOW WOULD BE THE FINAL RESULT
// EXECUTED: /test add two hello
YamlClass.addToFile(args[1], args[2]);
} else {
sender.sendMessage("Not enough arguments!");
}
}
}
}
Код: Выделить всё
test:
one:
two:
- hello
three:
< /code>
И если пользователь затем RAN /Test Добавьте три Goodbye < /code>, файл будет выглядеть следующим образом: < /p>
test:
one:
two:
- hello
three:
- goodbye
Подробнее здесь: https://stackoverflow.com/questions/201 ... -yaml-file
Мобильная версия