diff --git a/.gitignore b/.gitignore index 1cdc9f7f..08ff0d1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ target/ +.idea/ +songodaUpdater.iml pom.xml.tag pom.xml.releaseBackup pom.xml.versionsBackup diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..d18b9e99 --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + com.songoda + SongodaUpdater + 4.0.0 + 1 + + clean install + SongodaUpdater-${project.version} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + + + + private + http://repo.songoda.com/artifactory/private/ + + + + + org.spigotmc + spigot + 1.14-pre5-2 + + + diff --git a/src/main/java/com/songoda/update/Module.java b/src/main/java/com/songoda/update/Module.java new file mode 100644 index 00000000..0ac93f84 --- /dev/null +++ b/src/main/java/com/songoda/update/Module.java @@ -0,0 +1,7 @@ +package com.songoda.update; + +public interface Module { + + void run(Plugin plugin); + +} diff --git a/src/main/java/com/songoda/update/Plugin.java b/src/main/java/com/songoda/update/Plugin.java new file mode 100644 index 00000000..f93ed946 --- /dev/null +++ b/src/main/java/com/songoda/update/Plugin.java @@ -0,0 +1,63 @@ +package com.songoda.update; + +import org.bukkit.plugin.java.JavaPlugin; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class Plugin { + + private JavaPlugin javaPlugin; + private int songodaId; + private List modules = new ArrayList<>(); + private String latestVersion; + private String notification; + private JSONObject json; + + public Plugin(JavaPlugin javaPlugin, int songodaId) { + this.javaPlugin = javaPlugin; + this.songodaId = songodaId; + } + + public String getLatestVersion() { + return latestVersion; + } + + public void setLatestVersion(String latestVersion) { + this.latestVersion = latestVersion; + } + + public String getNotification() { + return notification; + } + + public void setNotification(String notification) { + this.notification = notification; + } + + public JSONObject getJson() { + return json; + } + + public void setJson(JSONObject json) { + this.json = json; + } + + public Module addModule(Module module) { + modules.add(module); + return module; + } + + public List getModules() { + return new ArrayList<>(modules); + } + + public JavaPlugin getJavaPlugin() { + return javaPlugin; + } + + public int getSongodaId() { + return songodaId; + } +} diff --git a/src/main/java/com/songoda/update/SongodaUpdate.java b/src/main/java/com/songoda/update/SongodaUpdate.java new file mode 100644 index 00000000..d403933d --- /dev/null +++ b/src/main/java/com/songoda/update/SongodaUpdate.java @@ -0,0 +1,80 @@ +package com.songoda.update; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +public class SongodaUpdate { + + private static int version = 1; + + private static List registeredPlugins = new ArrayList<>(); + + private static SongodaUpdate INSTANCE; + + public SongodaUpdate() { + Bukkit.getScheduler().scheduleSyncDelayedTask(registeredPlugins.get(0).getJavaPlugin(), this::update, 20L); + } + + private void update() { + for (Plugin plugin : registeredPlugins) { + try { + JavaPlugin javaPlugin = plugin.getJavaPlugin(); + System.out.println("Establishing connection with the Songoda update server."); + URL url = new URL("http://update.songoda.com/index.php?plugin=" + javaPlugin.getName() + + "&version=" + javaPlugin.getDescription().getVersion()); + URLConnection urlConnection = url.openConnection(); + InputStream is = urlConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + + int numCharsRead; + char[] charArray = new char[1024]; + StringBuffer sb = new StringBuffer(); + while ((numCharsRead = isr.read(charArray)) > 0) { + sb.append(charArray, 0, numCharsRead); + } + String jsonString = sb.toString(); + JSONObject json = (JSONObject) new JSONParser().parse(jsonString); + + plugin.setLatestVersion((String) json.get("latestVersion")); + plugin.setNotification((String) json.get("notification")); + + plugin.setJson(json); + + for (Module module : plugin.getModules()) { + module.run(plugin); + } + } catch (IOException e) { + System.out.println("Connection failed..."); + e.printStackTrace(); //ToDo: This cannot be here in final. + } catch (ParseException e) { + System.out.println("Failed to parse json."); + e.printStackTrace(); //ToDo: This cannot be here in final. + } + } + } + + public static void load(Plugin plugin) { + registeredPlugins.add(plugin); + System.out.println("Hooked " + plugin.getJavaPlugin().getName() + "."); + if (INSTANCE == null) INSTANCE = new SongodaUpdate(); + } + + public static int getVersion() { + return version; + } + + public static SongodaUpdate getInstance() { + return INSTANCE; + } +}