How do you read a string from DropBox and output it.

Discussion in 'Plugin Development' started by SleepyDog, Oct 19, 2014.

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

    SleepyDog

    Okay, here is what i need to do:
    I need the plugin to read the first line of a text file in my dropbox then set it to a variable so i can use it in a broadcast or print it to the console.

    Here is my attempt:
    Code:
     public void onEnable()
      {
        getLogger().info("To clear the chat do /clearchat");
        saveDefaultConfig();
     
      try { URL e = new URL("https://www.dropbox.com/s/8haw1eivk46759u/ClearChat.txt?dl=0");
        BufferedReader in = new BufferedReader(new InputStreamReader(e.openStream()));
        String str = in.readLine();
        getLogger().info("Test:"+str);
        in.close();}
      catch (IOException e) { }
      }
    And this is the output:
    [​IMG]


    How could i make this mumboJumbo of an output be the simple line in my dropbox that says 'test'?

    Thankyou for the help!

    EDIT:
    If i could take the text form the TextFile in the Dropbox and set it into a local file that would be amazing, is there a way i could just download the txt file to the config folder and read it from there?
     
  2. Offline

    FerusGrim

    In regards to your edit, maybe something like this:
    Code:java
    1. try {
    2. List<String> tempArray = new ArrayList<String>();
    3. URL url = new URL(link.to.file);
    4. URLConnection con = url.openConnection();
    5. InputStream stream = con.getInputStream();
    6. String cont = "";
    7. int ch;
    8. byte[] bytes = new byte[1];
    9. while ((ch = stream.read()) != -1) {
    10. bytes[0] = (byte) ch;
    11. cont = cont + new String(bytes);
    12. }
    13. String[] nl = cont.split("\\/");
    14. if (nl.length > 0) {
    15. for (String string : nl) {
    16. getLogger().info(string);
    17. }
    18. }
    19. stream.close();
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }
     
  3. Offline

    SmooshCakez

  4. Offline

    FerusGrim

    Assuming I understand the doc, this is a much better suggestion than mine. xD
     
  5. Offline

    Dudemister1999

    SleepyDog FerusGrim

    Here's how I get text from Dropbox (I set the MOTD with ProtocolLib)

    Code:java
    1.  
    2. public static String getMOTD()
    3. {
    4. URL site;
    5. StringBuilder motd = new StringBuilder(StringUtil.colour("&0 &4Crimson &8Deadworks&0 "));
    6. try
    7. {
    8. site = new URL("[url]https://www.dropbox.com/s/npu53an6sfmtnih/motd.txt?dl=1[/url]");
    9.  
    10. try (BufferedReader in = new BufferedReader(new InputStreamReader(site.openStream())))
    11. {
    12. String line;
    13. while ((line = in.readLine()) != null)
    14. {
    15. motd.append("\n").append(StringUtil.colour(line));
    16. }
    17. }
    18. }
    19. catch(IOException ex)
    20. {
    21. Logger.getLogger(NewsCollector.class.getName()).log(Level.SEVERE, null, ex);
    22. }
    23.  
    24. return motd.toString();
    25. }
    26.  


    Note that it'll throw an exception if it can't find it, so make sure you check the link is valid too.
     
  6. Offline

    SleepyDog

    I got this error:
    [​IMG]
    should the link to file be in ""?
     
  7. Offline

    FerusGrim

    Change:
    Code:java
    1. import java.awt.List;
    to:
    Code:java
    1. import java.util.List;
     
  8. Offline

    SleepyDog

    How would i make this set the ClearChat.txt to the config?
     
  9. Offline

    Dudemister1999

    SleepyDog

    That depends on what you mean. Do you want to save the text collected to a file, or get the text from the file and do something with it?
     
  10. Offline

    SleepyDog


    [​IMG]
    Know a fix?

    Ether, i would rather do something with it, eg. broadcast it
    or save it and then load it from the config and broadcast it.
    Witch is easier?

    Could you rewrite (or show me how to) the code so it will broadcast the content of the .txt?

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

    Dudemister1999

    SleepyDog Personally, I'd just make a scheduled task every half hour or so to "refresh" it. On startup, get it and save it to config, then broadcast it all you want. Then, when the half hour comes around, check from the dropbox file again, replace the saved text with that.

    So, there's two ways you can go about this:

    1- You can just collect it when you want to broadcast it (If it's in a regular interval)
    2- Save it to config, and load it whenever you want (For commands n' stuff)

    SleepyDog I'll write it really quick. Didn't see that post :p

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

    FerusGrim

    There's nothing really wrong with it. I just forgot to remove that piece of unused code.
     
  13. Offline

    Dudemister1999

    SleepyDog

    Note this will need some changes to reflect your needs.

    Show Spoiler

    Code:java
    1. public static String getMOTD()
    2. {
    3. URL site;
    4. StringBuilder motd = new StringBuilder(StringUtil.colour("&0 &4Crimson &8Deadworks&0 "));
    5. try
    6. {
    7. site = new URL("[URL]https://www.dropbox.com/s/npu53an6sfmtnih/motd.txt?dl=1[/URL]");
    8.  
    9. try (BufferedReader in = new BufferedReader(new InputStreamReader(site.openStream())))
    10. {
    11. String line;
    12. while ((line = in.readLine()) != null)
    13. {
    14. motd.append("\n").append(StringUtil.colour(line));
    15. }
    16. }
    17. }
    18. catch(IOException ex)
    19. {
    20. Logger.getLogger(NewsCollector.class.getName()).log(Level.SEVERE, null, ex);
    21. }
    22.  
    23. return motd.toString();
    24. }
    25.  
    26. public static String message;
    27. static JavaPlugin mainPlugin;
    28.  
    29. public static void setup()
    30. {
    31. Bukkit.getScheduler().runTaskTimerAsynchronously(mainPlugin, new Runnable()
    32. {
    33. @Override
    34. public void run()
    35. {
    36. message = getMOTD();
    37. }
    38. }, 0L, ((20*60) * 30 /* Minutes (Set to 30 minutes here) */));
    39. }
    40.  
    41. public static void broadcast()
    42. {
    43. Bukkit.broadcastMessage(ChatColor.GREEN + "Scheduled message:");
    44. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', message));
    45. }



    And with that code, I even added support for color codes! (&aGreen, &bBlue, &6Gold, etc.)

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

    SleepyDog

    Thankyou, i will try it. Am i okay to message you in the future if there is something i don't understand about this bit of code?

    Where would i put this code?
    In then onEnable()?

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

    Gater12

    SleepyDog
    It's a snippet of a method. Review the syntax of Java if you are further confused.
     
  16. Offline

    SleepyDog

    huh?
     
  17. Offline

    Gater12

  18. Offline

    SleepyDog

  19. Offline

    Gater12

    SleepyDog
    Depends on what you want to do with it. Put in your main class, create a different class, etc.
     
  20. Offline

    SleepyDog

    I am new to this, basically i want to ping the ClearChat.txt in my dropbox every hour then set the data in the txt file to a file in the config folder...
     
  21. Offline

    Gater12

    SleepyDog
    Well I can't make all the decisions for you. What works for me might not work well for you.

    I suggest creating a class and putting the method in.
     
  22. Offline

    SleepyDog

    Server --> Dropbox -->server -->config
    I honestly thought this would be easier to do.

    How would i put it in a new class? Will this be auto run or do i need to start it?

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

    Gater12

    SleepyDog
    It is easy. The method is already done for you (You need to tweak it so it will work with you asformentioned. I'm just confusing you is the most probable cause on why you do not understand.

    To reiterate myself you can put the method anywhere you want (Well not anywhere but in any class). It's up to you.
     
  24. Offline

    SleepyDog

    So i made a class:
    [​IMG]
    Do i just paste the code into this?
     
  25. Offline

    Gater12

    Here's an example to clear things up.

    You have your method:
    Code:java
    1. public void foo(){
    2. /* Code code code would go go go here */
    3. }


    You can put in a class.

    Code:java
    1. public class BarClass {
    2. public void foo(){
    3.  
    4. }
    5. }


    and to invoke, or run, it you would do:
    Code:java
    1. BarClass newInstance = new BarClass();
    2.  
    3. newInstance.foo();
     
  26. Offline

    SleepyDog

    So how would i start this?
    [​IMG]
     
  27. Offline

    Gater12

  28. Offline

    SleepyDog

    How would i start it though?
     
  29. Offline

    Gater12

    SleepyDog
    Because it is a static method, it belongs to the class and not to the instance. In simple terms, you don't need to make a new instance of the class to run the method.

    To run it you would do:
    Code:java
    1. DeveloperMessages.getMOTD()
     
  30. Offline

    SleepyDog

    did not work


    Code:java
    1. package me.sleepydog935.clearchat;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.IOException;
    5. import java.io.InputStreamReader;
    6. import java.net.URL;
    7. import java.util.logging.Level;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.util.StringUtil;
    13.  
    14. public class DeveloperMessages {
    15. public static String getMOTD(){
    16. URL site;
    17. StringBuilder motd = new StringBuilder(StringUtil.colour("&0 &4Crimson &8Deadworks&0 "));
    18. try
    19. {
    20. site = new URL("[URL]https://www.dropbox.com/s/8haw1eivk46759u/ClearChat.txt?dl=0[/URL]");
    21.  
    22. try (BufferedReader in = new BufferedReader(new InputStreamReader(site.openStream())))
    23. {
    24. String line;
    25. while ((line = in.readLine()) != null)
    26. {
    27. motd.append("\n").append(StringUtil.colour(line));
    28. }
    29. }
    30. }
    31. catch(IOException ex)
    32. {
    33. Logger.getLogger(NewsCollector.class.getName()).log(Level.SEVERE, null, ex);
    34. }
    35.  
    36. return motd.toString();
    37. }
    38.  
    39. public static String message;
    40. static JavaPlugin mainPlugin;
    41.  
    42. public static void setup()
    43. {
    44. Bukkit.getScheduler().runTaskTimerAsynchronously(mainPlugin, new Runnable()
    45. {
    46. @Override
    47. public void run()
    48. {
    49. message = getMOTD();
    50. }
    51. }, 0L, ((20*60) * 30 /* Minutes (Set to 30 minutes here) */));
    52. }
    53.  
    54. public static void broadcast()
    55. {
    56. Bukkit.broadcastMessage(ChatColor.GREEN + "Scheduled message:");
    57. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', message));
    58. }
    59. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page