Filled [REQUEST] Run a Command and Display Text Written in a .TXT File

Discussion in 'Archived: Plugin Requests' started by billytheDOLPHIN, Jun 5, 2014.

  1. Offline

    timtower Administrator Administrator Moderator

    It is coded as a single line, but with line breaks...
    sb.append("\n");
     
  2. Offline

    Vortex20000

    unrealdesign could I use some of the source code you used, or would you like to keep it All Rights Reserved?
     
  3. Offline

    unrealdesign

    Yup.

    Sure!

    Here is the whole class if anyone is interested. It's really sloppy code, but it didn't need to be well written because it's so simple and was made so fast :p.

    Code:java
    1.  
    2. package org.fragzone.cmdtxt;
    3.  
    4. import java.io.BufferedReader;
    5. import java.io.File;
    6. import java.io.FileReader;
    7. import java.io.IOException;
    8. import java.util.logging.Level;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class MainClass extends JavaPlugin {
    17.  
    18. public void onEnable()
    19. {
    20. getLogger().info("has successfully been enabled!");
    21. if(!new File("plugins/CmdTxt").exists()) new File("plugins/CmdTxt").mkdir();
    22. }
    23.  
    24. public void onDisable()
    25. {
    26. getLogger().info("has successfully been disabled!");
    27. }
    28.  
    29. @Override
    30. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    31. {
    32. if (cmd.getName().equalsIgnoreCase("cmdtxt"))
    33. {
    34. if(sender instanceof Player)
    35. {
    36. if(args.length <= 0)
    37. {
    38. sender.sendMessage(ChatColor.RED+"/CmdTxt <filename>.txt");
    39. }
    40. else
    41. {
    42. String fileName = args[0];
    43. File file = new File("plugins/CmdTxt/"+fileName);
    44. if(file.exists())
    45. {
    46. try {
    47. String message = readFile(fileName);
    48. String coloredMessage = ChatColor.translateAlternateColorCodes('&', message);
    49. sender.sendMessage(coloredMessage);
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. }
    53. }
    54. else
    55. {
    56. sender.sendMessage(ChatColor.RED+"Sorry, there is no file: \""+fileName+"\"");
    57. }
    58. }
    59. }
    60. else
    61. {
    62. getLogger().log(Level.WARNING, "This command can only be run by a player.");
    63. }
    64. return true;
    65. }
    66. return false;
    67. }
    68.  
    69. public String readFile(String fileName) throws IOException {
    70. BufferedReader br = new BufferedReader(new FileReader("plugins/CmdTxt/"+fileName));
    71. try {
    72. StringBuilder sb = new StringBuilder();
    73. String line = br.readLine();
    74.  
    75. while (line != null) {
    76. sb.append(line);
    77. sb.append("\n");
    78. line = br.readLine();
    79. }
    80. return sb.toString();
    81. } finally {
    82. br.close();
    83. }
    84. }
    85. }
    86.  
     

Share This Page