Guest User

Submitting a http request with EasyScript/Bukkit

a guest
Mar 8th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. importClass(java.net.URL)
  2. importClass(java.io.BufferedReader)
  3. importClass(java.io.InputStreamReader)
  4. importClass(java.io.FileNotFoundException)
  5. importClass(java.util.logging.Level)
  6.  
  7. // In the javascript engine we can implement Java interfaces such as Runnable,
  8. // this makes it possible to use some of the bukkit scheduler API, which
  9. // is required for doing http requests without hanging your server.
  10.  
  11. // To send messages to players we really should be on the main thread.
  12. var sendResponse = function(s) {
  13.     server.getScheduler().runTask(plugin, new java.lang.Runnable({
  14.         run : function() {
  15.             sender.sendMessage(s);
  16.         }
  17.     }));
  18. }
  19.  
  20. // To do http or anything slow we must not be on the main thread.
  21. server.getScheduler().runTaskAsynchronously(plugin, new java.lang.Runnable({
  22.     run : function () {
  23.         try {
  24.             var url = new URL("http://cyberiantiger.org/EasyScript/http.txt");
  25.             var content = new BufferedReader(new InputStreamReader(url.openConnection().getContent()));
  26.             // We only read the first line here.
  27.             var message = content.readLine();
  28.             content.close();
  29.             sendResponse(message);
  30.         } catch (e) {
  31.             if (e.javaException instanceof FileNotFoundException) {
  32.                 sendResponse("Error: not found");
  33.             } else {
  34.                 sendResponse("An error occured, check logs.");
  35.                 throw e
  36.             }
  37.         }
  38.     }
  39. }));
Advertisement
Add Comment
Please, Sign In to add comment