Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package me.Bentipa.updater;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;
  12.  
  13. import org.bukkit.plugin.java.JavaPlugin;
  14. import org.w3c.dom.Document;
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17. import org.xml.sax.SAXException;
  18.  
  19. /**
  20. *
  21. * @author Benjamin | Bentipa
  22. * @version 1.0
  23. * Created on 03.04.2015
  24. *
  25. */
  26. public class SpigotPluginUpdater {
  27.  
  28.  
  29. private URL url;
  30. private JavaPlugin plugin;
  31. private String pluginurl;
  32.  
  33.  
  34. /**
  35. * Create a new SpigotPluginUpdate to check and update your plugin
  36. * @param plugin - your plugin
  37. * @param pluginurl - the url to your plugin.html on your webserver
  38. */
  39. public SpigotPluginUpdater(JavaPlugin plugin, String pluginurl){
  40. try {
  41. url = new URL(pluginurl);
  42. } catch (MalformedURLException e) {
  43. System.out.println("[SpigotPluginUpdater] Error in checking update for: '" +pluginurl+"' invalid Pluginname!");
  44. System.out.println(" -- StackTrace --");
  45. e.printStackTrace();
  46. System.out.println(" -- End of StackTrace --");
  47. }
  48. this.plugin = plugin;
  49. this.pluginurl = pluginurl;
  50. }
  51.  
  52.  
  53. private String version = "";
  54. private String downloadURL = "";
  55. private String changeLOG = "";
  56.  
  57. private boolean out = true;
  58. /**
  59. * Enable a console output if new Version is availible
  60. */
  61. public void enableOut(){
  62. out = true;
  63. }
  64.  
  65. /**
  66. * Disable a console output if new Version is availible
  67. */
  68. public void disableOut(){
  69. out = false;
  70. }
  71.  
  72. /**
  73. * Check for a new Update
  74. * @return if new update is availible
  75. */
  76. public boolean needsUpdate(){
  77.  
  78. try {
  79. URLConnection con = url.openConnection();
  80. InputStream _in = con.getInputStream();
  81. Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(_in);
  82.  
  83. Node nod = doc.getElementsByTagName("item").item(0);
  84. NodeList children = nod.getChildNodes();
  85.  
  86. version = children.item(1).getTextContent();
  87. downloadURL = children.item(3).getTextContent();
  88. changeLOG = children.item(5).getTextContent();
  89. if(!version.replaceAll("[a-zA-z ]", "").equals(plugin.getDescription().getVersion())) {
  90. if(out){
  91. System.out.println(" New Version found: " + version.replaceAll("[a-zA-z ]", ""));
  92. System.out.println(" Download it here: " + downloadURL.toString());
  93. System.out.println(" Changelog: " + changeLOG);
  94. }
  95.  
  96. return true;
  97. }
  98.  
  99. } catch (IOException | SAXException | ParserConfigurationException e) {
  100. System.out.println("[SpigotPluginUpdater] Error in checking update for: '" +pluginurl+"' (invalid URL?) !");
  101. System.out.println(" -- StackTrace --");
  102. e.printStackTrace();
  103. System.out.println(" -- End of StackTrace --");
  104. }
  105.  
  106.  
  107. return false;
  108. }
  109.  
  110. /**
  111. * Executes the Update and trys to install it
  112. */
  113. public void update(){
  114. try {
  115. URL download = new URL(getFolder(pluginurl)+downloadURL);
  116.  
  117. BufferedInputStream in = null;
  118. FileOutputStream fout = null;
  119. try {
  120. if(out){
  121. plugin.getLogger().info("Trying to download from: " + getFolder(pluginurl)+downloadURL);
  122. }
  123. in = new BufferedInputStream(download.openStream());
  124. fout = new FileOutputStream("plugins/"+downloadURL);
  125.  
  126. final byte data[] = new byte[1024];
  127. int count;
  128. while ((count = in.read(data, 0, 1024)) != -1) {
  129. fout.write(data, 0, count);
  130. }
  131. } finally {
  132. if (in != null) {
  133. in.close();
  134. }
  135. if (fout != null) {
  136. fout.close();
  137. }
  138. }
  139.  
  140. if(out){
  141. plugin.getLogger().info("Succesfully downloaded file: " + downloadURL);
  142. plugin.getLogger().info("To have the Features, you have to reload your Server now!");
  143. }
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. private String getFolder(String s){
  155. String path = s;
  156. int lastslash = path.lastIndexOf("/");
  157. // for(int i = 0; i < path.length(); i++){
  158. //
  159. // if(c == '/'){
  160. // lastslash++;
  161. // }
  162. // }
  163. String folder = path.substring(0, lastslash+1);
  164. return folder;
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement