Getting Player Input

Discussion in 'Plugin Development' started by Dilmorak, Aug 7, 2013.

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

    Dilmorak

    Okay, so I have a very basic idea for a plugin on my server but I've ran into a huge problem... I've never actually saved user input into a .txt file. Here is my code so far, and I really need some help, please explain how to get user input... And PLEASE don't just put down some code.

    Code:java
    1. package me.dulexzach.EmailAsker;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Server;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14.  
    15. import java.io.*;
    16. import java.lang.*;
    17. import java.util.*;
    18. import java.util.logging.Logger;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.ChatColor;
    22. import org.bukkit.OfflinePlayer;
    23. import org.bukkit.command.Command;
    24. import org.bukkit.command.CommandSender;
    25. import org.bukkit.entity.Player;
    26. import org.bukkit.event.EventHandler;
    27. import org.bukkit.event.Listener;
    28. import org.bukkit.event.player.PlayerJoinEvent;
    29. import org.bukkit.plugin.PluginDescriptionFile;
    30. import org.bukkit.plugin.java.JavaPlugin;
    31.  
    32.  
    33. public class EmailAskerExecuter implements CommandExecutor{
    34.  
    35. private Formatter x;
    36.  
    37. private EmailAsker plugin;
    38.  
    39. public EmailAskerExecuter(EmailAsker plugin){
    40. this.plugin = plugin;
    41. }
    42.  
    43. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    44. Player player = (Player) sender;
    45. String playerName = player.getName();
    46.  
    47. if(sender instanceof Player == true){
    48. if (args.length <= 1)
    49. {
    50. player.sendMessage(ChatColor.RED + "Usage: /EnterEmail <Email>");
    51. }
    52. else
    53. {
    54.  
    55. }
    56. sender.sendMessage(ChatColor.AQUA + ("It seems the plugin is working so far."));
    57. }else{
    58. sender.sendMessage(ChatColor.RED + ("Only players can enter their Email!"));
    59. }
    60. return false;
    61. }
    62.  
    63. @EventHandler
    64. public void onJoin(PlayerJoinEvent e) {
    65. Player player = e.getPlayer();
    66. if(!player.hasPlayedBefore()) {
    67. player.sendMessage(ChatColor.AQUA + "Hello, welcome to our server! Please type /EnterEmail {Email} so that we contact you if we need to. We will not spam your EMail. Thank you.");
    68. }
    69.  
    70.  
    71. }
    72. public void openFile(PlayerJoinEvent e){
    73. Player player = e.getPlayer();
    74. try {
    75. x = new Formatter(player + ".txt");
    76. } catch (Exception y) {
    77. }
    78. }
    79. }
     
  2. Offline

    david_rosales

    for the public void openFile(PlayerJoinEvent e), i would actually remove. I would probably make one like
    Code:java
    1.  
    2. public void createFile(Player p, String email){
    3. //First create the file variable. I also recommend .yml
    4. File file = new File(plugin.getDataFolder(), p.getName() + ".yml");
    5. //Then check to see if it exists
    6. if(file.exists()){
    7. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    8. config.set("Email", email);
    9. config.save();
    10. }else{
    11. try{
    12. file.create();
    13. //Now treating the file like a yml
    14. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    15. config.set("Email", email);
    16. config.save();
    17. }catch(Exception e){
    18. }
    19. }

    I would call this method on the command.
    This is the first thing that came to my mind and im pretty sure it works.. If someone else has a better way of doing it... please comment lol. Hope it works for what you need it

    Forgot one thing! Surround the config.save() in the if(file.exists) with a try and catch similar to the one in the else

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

    zack6849

    Code:
    public static HashMap<File, BufferedWriter> writers = new HashMap<File, BufferedWriter>();
    public void log(File log, String message) {
            try {
                if (!log.exists()) {
                    debug("file " + log.getName() + " didn't exist, creating new one");
                    log.getParentFile().mkdirs();
                    log.createNewFile();
                }
                BufferedWriter br = getBufferedWriter(log);
                br.write(message);
                br.newLine();
                br.flush();
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public BufferedWriter getBufferedWriter(File f) {
            try {
                if (writers.containsKey(f)) {
                    debug("found writer for file " + f.getName());
                    return writers.get(f);
                } else {
                    debug("Couldn't find writer for file " + f.getName());
                    BufferedWriter returns = new BufferedWriter(new FileWriter(f, true));
                    writers.put(f, returns);
                    return returns;
                }
            } catch (Exception e) {
                 e.printStackTrace();
                return null;
            }
        }
    
    Probably a bit more complex than it needs to be, but it's efficient.
     
  4. Offline

    Dilmorak

    Now it's creating the file, but it's named KingzCraftMC (my username) but doesn't actually contain the Email.

    When I tried calling on it, all it let me put was createFile(player, playerName);

    Never mind :D I fixed the plugin.

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

    Trevor1134

    Dilmorak Make sure to mark this thread as solved :)
     
Thread Status:
Not open for further replies.

Share This Page