[Util] [Web] Read from a .txt file!

Discussion in 'Resources' started by Wizehh, Jan 18, 2014.

?

Will you ever use this?

  1. Yes, the possibilites are limitless!

    93.3%
  2. No, it's horrible; please go away.

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

    Wizehh

    Hey guys! After about 30 minutes of researching, I came up with this lil' snippet of code that you can use to return text from a .txt file (from a website, not your computer). You could use this to send players the server news, display the rules on a sign, whatever you want!
    It's not much, but I thought it was pretty cool, so I decided I'd share it with everybody:
    Code:Java
    1. // Put this inside a method or a command:
    2. try { // (Something could go wrong)
    3.  
    4. URL news = new URL("[url]http://yourdomain.com/file.txt[/url]");
    5. BufferedReader in = new BufferedReader(new InputStreamReader(news.openStream()));
    6. String inputLine;
    7.  
    8. while ((inputLine = in.readLine()) != null) { // While it still has lines to read from
    9. player.sendMessage(inputLine.replace("&", "\247")); // Sends the message (with chat color support)
    10. }
    11.  
    12. in.close(); // close it!
    13.  
    14. } catch (Exception e) { // The file doesn't exist, it couldn't connect, etc.
    15. player.sendMessage(ChatColor.RED + "an error has occured..");
    16. e.printStackTrace();
    17. }

    View the direct source here.

    Alternate way based off of this by Goblom:
    https://github.com/Goblom/Bukkit-Li...in/java/org/goblom/bukkitlibs/TextReader.java
     
    Goblom likes this.
  2. Offline

    Goblom

    ArthurMaker and DrJava like this.
  3. Goblom
    No hate intended, but how did you take this a bit further? What's the "simpler" way? You basically just used the same code, put it in a class and added a method to get the data.
     
    bobacadodl likes this.
  4. Offline

    Goblom

    Assist Simpler as in less work, I know i used same code and everything, i gave credit to Wizehh and everything.

    I in no way shape or form take credit for it.
     
  5. Offline

    Wizehh

    This is also awesome; I'll add this below my snippet :)
     
    Goblom likes this.
  6. Goblom
    Yours actually requires more work (to set up, not use). I'd have to make a new class, then paste the code in it from your GitHub. When I want to use it, I have to create a new instance of that class, and call one (or more, I didn't check if you closed the stream) of its methods.

    You could just put the code in a method that returns a list/array of strings, along with a parameter for the URL. Or if you're going to use the code only once, or if you don't care how good your code looks, then just place it in wherever you need it.

    I know this isn't much work at all, but still, creating a class for it is unnecessary :p

    Edit: Also, Wizehh , you shouldn't be posting Java tutorials here. I know, this has a potential uses in Bukkit plugins, but so does like 99% of Java methods. If someone needs this, they could just Google search it, I'm sure there's plenty of tutorials and snippets out there.
     
  7. Offline

    Wizehh

    As I said before, I thought I would share it since it did take me some time to find said resource. If you don't want to use it, that's fine; however, I thought it could potentially be useful to a lot of people. If I'm not mistaken, this is the "resource" section, correct?
     
  8. Correct, but as explained in this thread,

    And my point to you is that you made a Java tutorial which in my opinion shouldn't be posted here since this is the bukkit forums in a section related to bukkit

    And he's correct, this sub forum is in a "Bukkit" forum section, therefore everything posted here should have some relation to Bukkit.

    This is the forum for Bukkit API resources and tutorials, everything else is somewhere else.

    I will thank you for the utility, and the time you spent finding/creating it, but same code can be found all over the internet.
     
    DrJava likes this.
  9. Offline

    bobacadodl

    I agree with Assist. Java tutorials are better for external websites, and answers for these things can easily be found on stackoverflow or another website with a quick google search.
    IMO, The resources section should mainly be for bukkit related things.
     
    TfT_02, Cirno, Garris0n and 1 other person like this.
  10. Offline

    Wizehh

    Thank you for the feedback, but I'm not in accord with you.
     
  11. Offline

    Scullyking

    Neat, ty for the snip :)
     
  12. Offline

    Wizehh

    No problem, glad you like it.
     
  13. Offline

    CeramicTitan

    Wizehh
    Something like this would most likely get your project rejected. Reading from external sources other than BukkitDev is against the Plugin Submission Guidelines.
     
  14. Offline

    Wizehh

    CeramicTitan
    Ah; good thing this is for a private plugin then!
     
  15. Offline

    breezeyboy

    My plugin checks my website to do a version check and it was accepted
     
    Wizehh likes this.
  16. Offline

    CeramicTitan

    I don't know why. Name of the plugin?
     
  17. Offline

    breezeyboy

    Player lookup only mods and I can view it
     
  18. Offline

    CeramicTitan

    I am a mod?
     
    Assist likes this.
  19. Offline

    breezeyboy

    I know you are, I was stating it because someone else might try to look it up
     
Thread Status:
Not open for further replies.

Share This Page