'Delete' a line from a file

Discussion in 'Plugin Development' started by xDeeKay, Oct 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    xDeeKay

    These are user .yml files created when a player joins the game.
    The command '/noteadd <player> <message>' is used to add 'notes' to the specified players .yml, each time writing the message on a new line.

    So what I'm trying to accomplish is deleting a line from a file on command. The command '/notedelete <player> <note no.>' is used to delete a 'note', each line in the players .yml is represented by <note no.> When a line is deleted, every line below the ones deleted needs to be moved up as well, to prevent gaps.

    Here is what I have so far, the comment is where I've tried different things:
    Code:java
    1. public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("notedelete")) {
    3. if (args.length == 2) {
    4. try {
    5. File playerNote = new File(plugin.getDataFolder() + File.separator + "playernotes", args[0] + ".yml");
    6. if (playerNote.exists()) {
    7. if (args.length > 1) {
    8. // Delete line # from player.yml
    9. } else {
    10. cs.sendMessage("§cIncorrect syntax. Usage: /notedelete <player> <note no.>");
    11. }
    12. } else {
    13. cs.sendMessage("§cThis player does not exist.");
    14. }
    15. } catch(IOException e) {
    16. e.printStackTrace();
    17. cs.sendMessage("§cPlayer note could not be deleted. See server.log");
    18. }
    19. } else {
    20. cs.sendMessage("§cIncorrect syntax. Usage: /notedelete <player> <note no.>");
    21. }
    22. }
    23. return true;
    24. }

    Thanks.
     
  2. Offline

    beastman3226

    Create a Note object per player. This note object has a file that can be written to. It would be used just like a config except that it would be straight text. The note would hold each line. When you do an update method you loop through all your files updating every single note with every single line. Or you could do update(File name) and it would update just that file.
     
  3. Offline

    Chinwe

    I did almost exactly this here, using separate Note objects as beastman3226 suggested :)
     
  4. Offline

    xDeeKay

Thread Status:
Not open for further replies.

Share This Page