My plugin won't work

Discussion in 'Plugin Development' started by mighty2361, Oct 20, 2012.

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

    mighty2361

    Hello everyone!
    I recently wrote a plugin called MightyReport which allows players to report someone if he is breaking rules. But when I do /report player reason, it should write the command sender and reason in folder plugins/MightyReport/data/playername.txt but it won't work. There isn't any error in console or anything, it just won't write to file for some reason.



    Code:
    Code:
    package tk.mighty2361;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("unused")
    public final class Main extends JavaPlugin {
     
    private static final String bufferedWriter = null;
        @Override
    public void onEnable(){
    getLogger().info("MightyReport has been enabled!");
    }
     
        @Override
    public void onDisable(){
    getLogger().info("MightyReport has been disabled.");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("report")){
    if (args.length > 2) {
    sender.sendMessage(ChatColor.RED+"Too many arguments. Usage: /report player reason");
    }
    else if (args.length < 2){
    sender.sendMessage(ChatColor.RED+"Not enough arguments. Usage: /report player reason");
    }
    else {
           try {
               
           getLogger().info("Attempting to write to file");
              @SuppressWarnings("resource")
    BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter("plugins/MightyReport/data/"+args[0]+".txt", true));
              try {
              File file = new File(args[0]+".txt");
     
              // Create file if it does not exist
              boolean success = file.createNewFile();
              if (success) {
                  bufferedwriter.write("Reported for: "+args[1]+"by: "+sender.getName());
                  bufferedwriter.newLine();
                  getLogger().info("Successfully created a new file and wrote to it.");
              } 
              else {
               bufferedwriter.write("Reported for: "+args[1]+"by: "+sender.getName());
                  bufferedwriter.newLine();
                  getLogger().info("Successfully wrote to file");
              }
           } catch (IOException e) {
           }
     
     
           } catch (FileNotFoundException ex) {
           } catch (IOException ex) {
           } finally {
           
             
           }
     
     
    return true; 
    }
    }
    return false; 
    }{
     
    }
    }

    EDIT: IT CREATES THE FILE NOW, BUT IT STILL WON'T WRITE TO IT.
     
  2. Offline

    Hoolean

    1. Please use a code tag
    2. Have you registered the command in the plugin.yml?
     
  3. Offline

    Infamous Jeezy

    Code:java
    1.  
    2. package tk.mighty2361;
    3.  
    4. import java.io.BufferedWriter;
    5. import java.io.File;
    6. import java.io.FileNotFoundException;
    7. import java.io.FileWriter;
    8. import java.io.IOException;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. @SuppressWarnings("unused")
    14. public final class Main extends JavaPlugin {
    15.  
    16. private static final String bufferedWriter = null;
    17. @Override
    18. public void onEnable(){
    19. getLogger().info("MightyReport has been enabled!");
    20. }
    21.  
    22. @Override
    23. public void onDisable(){
    24. getLogger().info("MightyReport has been disabled.");
    25. }
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    27. if(cmd.getName().equalsIgnoreCase("report")){
    28. if (args.length > 2) {
    29. sender.sendMessage("Not enough arguments. Usage: /report player reason");
    30. }
    31. else if (args.length < 2){
    32. sender.sendMessage("Too many arguments!");
    33. }
    34. else {
    35. try {
    36.  
    37. getLogger().info("Attempting to write to file");
    38. @SuppressWarnings("resource")
    39. BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter("plugins/MightyReport/data/", true));
    40. try {
    41. File file = new File(args[0]+".txt");
    42.  
    43. // Create file if it does not exist
    44. boolean success = file.createNewFile();
    45. if (success) {
    46. bufferedwriter.write("Reported for: "+args[1]+"by: "+sender.getName());
    47. bufferedwriter.newLine();
    48. }
    49. else {
    50. bufferedwriter.write("Reported for: "+args[1]+"by: "+sender.getName());
    51. bufferedwriter.newLine();
    52. }
    53. } catch (IOException e) {
    54. }
    55.  
    56.  
    57. } catch (FileNotFoundException ex) {
    58. } catch (IOException ex) {
    59. } finally {
    60.  
    61.  
    62. }
    63.  
    64.  
    65. return true;
    66. }
    67. }
    68. return false;
    69. }
    70.  
    71. }
    72.  


    That's better, my eyes aren't burning as much.
    Anyways, make sure your plugin.yml exists and is written correctly.
    One problem that could be possible, although it's just a guess, your first argument is the name, the second argument is the reason. The problem with that is, if the person puts spaces in the reason, its gonna be more than 2 arguments, therefore you must gather all the arguments.
     
    MrBluebear3 likes this.
  4. Offline

    mighty2361

    I registered the command in plugin.yml. The command works, but it won't write to file
     
  5. Offline

    Hoolean

    Could you please re-post it in a code tag so it has indentation? It would be a lot easier to find the problem...
     
  6. Offline

    mighty2361

    done
     
  7. Offline

    Hoolean

    There are no tabs or any form of indentation... This will make it very hard to read!
     
  8. Offline

    Woobie

    Code:
    package tk.mighty2361;
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("unused")
    public final class Main extends JavaPlugin {
     
        private static final String bufferedWriter = null;
     
        @Override
        public void onEnable() {
            getLogger().info("MightyReport has been enabled!");
        }
     
        @Override
        public void onDisable() {
            getLogger().info("MightyReport has been disabled.");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (cmd.getName().equalsIgnoreCase("report")) {
                if (args.length > 2) {
                    sender.sendMessage("Not enough arguments. Usage: /report player reason");
                } else if (args.length < 2) {
                    sender.sendMessage("Too many arguments!");
                } else {
                    try {
     
                        getLogger().info("Attempting to write to file");
                        @SuppressWarnings("resource")
                        BufferedWriter bufferedwriter = new BufferedWriter(
                                new FileWriter("plugins/MightyReport/data/", true));
                        try {
                            File file = new File(args[0] + ".txt");
     
                            // Create file if it does not exist
                            boolean success = file.createNewFile();
                            if (success) {
                                bufferedwriter.write("Reported for: " + args[1]
                                        + "by: " + sender.getName());
                                bufferedwriter.newLine();
                            } else {
                                bufferedwriter.write("Reported for: " + args[1]
                                        + "by: " + sender.getName());
                                bufferedwriter.newLine();
                            }
                        } catch (IOException e) {
                        }
     
                    } catch (FileNotFoundException ex) {
                    } catch (IOException ex) {
                    } finally {
     
                    }
     
                    return true;
                }
            }
            return false;
        }
     
    }
    Looks a bit weird, used the Ctrl Shift F trick in eclipse.
     
    MrBluebear3 likes this.
  9. Offline

    Hoolean

    :D Thanks!

    mighty2361

    Add in a few debugs, to see where it is not working!

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

    Lolmewn

    You are writing to "plugins/MightyReport/data/"
     
  11. Offline

    mighty2361

    I added a few debugs. The command works, it just won't write to file. I tried getLogger().info("");
    and I found out it wont create a file and write to it for some reason
     
  12. Offline

    Hoolean

    What's the problem with that?

    EDIT: Ninja'd!
     
  13. Offline

    Woobie

    I think he is creating .txt files inside plugins /MightyReport/Data
    Code:
    File file = new File(args[0]+".txt");
     
    // Create file if it does not exist
    boolean success = file.createNewFile();
    if (success) {
    bufferedwriter.write("Reported for: "+args[1]+"by: "+sender.getName());
    bufferedwriter.newLine();
    }
    I might be horribly wrong, since I've never worked with anything like this, and probably never will.
     
  14. Offline

    Lolmewn

    You might just want to do file.getParentDir().mkdirs(); or whatever it was to be sure that the directories exist.

    He is, yes, but the problem is that his BufferedWriter is pointing to /data, and not /data/args[0].txt

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    MrBluebear3 likes this.
  15. Offline

    Hoolean

    What he said :D
     
  16. Offline

    mighty2361

    ^^ I edited the code

    It creates a file now (I pointed bufferedwriter to plugins/mightyreport/data/args[0].txt) but it is empty. I use the command again and it still wont write to the file.
     
Thread Status:
Not open for further replies.

Share This Page