Saving a string to a notepad

Discussion in 'Plugin Development' started by Shakti, Jan 18, 2015.

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

    Shakti

    How would I save a string to a note pad
    Example : /save 123 and ot saves 123 to a notepad
     
  2. Offline

    teej107

    @Shakti What does this have to do with Bukkit? You're problem has nothing to do with Bukkit. What have you researched?
     
  3. Offline

    Shakti

    @teej107 It has to do with coding bukkit. I mean how do I code were I can do /save (what I want to save) to a yml. And I meant save an argument
     
    Last edited: Jan 18, 2015
  4. Offline

    1Rogue

  5. Here we go :
    Code:java
    1.  
    2. //First you have to create a file in default package called config.yml
    3. @Oveeride
    4. public void onEnable() {
    5. //Now on enable save the config, the config only will be saved if the file does not exist
    6. saveDefaultConfig();
    7. }
    8. //Now we gonna create our command
    9. @Override
    10. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    11. if(cmd.getName().equalsIgnoreCase("save") {
    12. if(args.length == 0) {
    13. sender.sendMessage("/save <something>");
    14. return true;
    15. } else {
    16. if(getConfig().getStringList("Saves") == null) { //WIth this we can check if there's something saved
    17. //if there's nothing saved we create a new list
    18. List<String> list = new ArraList<String>();
    19. list.add(args[0]); //If you wanna save all args you have to use a string build
    20. getConfig().set("Saves", list);
    21. saveConfig();
    22. sender.sendMessage(args[0]+"has been add to the config");
    23. return true;
    24. } else {
    25. //if there0s something save get the list
    26. List<String> list = getConfig().getStringList("Saves");
    27. list.add(args[0]); //If you wanna save all args you have to use a string build
    28. //Add new string to the list
    29. getConfig().set("Saves", list); //Set the new list to the path
    30. //Save the config
    31. saveConfig();
    32. sender.sendMessage(args[0]+"has been add to the config");
    33. return true;
    34. }
    35. }
    36. return true;
    37. }
    38.  
     
    Synapz likes this.
  6. Offline

    1Rogue

    If you're not going to type this up in an IDE at least proof-read some things (and make sure there aren't typos):

    Code:java
    1. getConfig().getStringList("Saves") == null


    This will never be true. If there is no value, .getStringList returns an empty list. Furthermore this whole thing can be simplified to about one line:

    Code:java
    1. public boolean onCommand(/* arguments */, String[] args) {
    2. if (args.length > 0) {
    3. getConfig().set("saves", getConfig().getStringList("saves").add(StringUtils.join(args, " ")));
    4. return true;
    5. }
    6. return false;
    7. }
     
    Last edited: Jan 24, 2015
    Synapz likes this.
  7. "the best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer."
    PS: Nice void on command
     
  8. Offline

    1Rogue

    Or to not spread bad examples and hinder other people's ability to make reliable, clean code, and instead learn from what other people / online resources are saying and advance your own knowledge on the subject.
     
  9. Offline

    Shakti

    Shouldn't it be public boolean onCommand
     
  10. Offline

    Skionz

    I don't think you can do this with Bukkit.
    Code:
    File file = new File(this.getDataFolder() + File.seperator + "stuff.txt");
    BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
    writer.append("123");
    writer.close();
     
Thread Status:
Not open for further replies.

Share This Page