Writing User Input to file

Discussion in 'Plugin Development' started by keto23, Feb 23, 2012.

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

    keto23

    Hey... Im trying to get user input and put it in a new line in a file... Im having heaps of trouble.

    /post <message that needs to be put on file>

    idk how to do it D;

    Could someone help me with this please? :D
     
  2. Offline

    theguynextdoor

    Code:
        public static void writeToFile(String fileName, String bulletin, Player player) throws IOException {
            FileWriter writer = new FileWriter("plugins/BulletinNextDoor/" + fileName, true);
     
                writer.write("\n" + bulletin);
                writer.close();
        }
    This is a cut down version of what i use to write messages to a file, all you need is the message to put in, in which i would recommend you look up on how to use a string builder for. This method also assumes the file exists first btw.
     
  3. Offline

    keto23

    How would I create the file? Also whats a string builder
     
  4. Offline

    acuddlyheadcrab

    I use a userdata.yml for some of my plugins, and the methods I use to get/load/save it were taken from here (BukkitWiki) and here (WalkerCrouse's tutorial)

    Here I put the methods I use together into one nifty copy&paste-able piece of code.

    (It contains 2 constants, and 4 methods to handle a "non-config" yml file)
    http://pastebin.com/y3SuiAqE

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  5. Offline

    keto23

    But I still don't know how to put the user input with a command(/post <message>) into a file...
     
  6. Offline

    theguynextdoor

    Code:
    String msg;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < args.length; i++) {
        if (i != 0)
        sb.append(' ');
        sb.append(args[i]);
    }
    msg = sb.toString();
    That is the message if its used in a command, which i assume it is.
     
  7. Offline

    keto23

    How do I make the actual command?
     
  8. Offline

    theguynextdoor

    Well i recommend you look on some tutorials, but here is something to get you started.
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("post")) {
                if (sender instanceof Player) {
                    if (args.length == 0) {
                        sender.sendMessage("You muppet");
                    }
     
                    Player bsender = (Player) sender;
     
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < args.length; i++) {
                        if (i != 0)
                            sb.append(' ');
                        sb.append(args[i]);
                    }
                    msg = sb.toString();
               
                    Bukkit.getServer().broadcastMessage(msg);
            }
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page