Last active
August 29, 2015 14:18
-
-
Save Techcable/9609dce212ef3eade53d to your computer and use it in GitHub Desktop.
PluginLoader (Load a plugin from spigotmc, or bukkit-dev)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.FileOutputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.net.HttpURLConnection; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import org.json.simple.JSONArray; | |
| import org.json.simple.JSONObject; | |
| import org.json.simple.parser.JSONParser; | |
| import lombok.*; | |
| /** | |
| * Loads Dependencies from bukkit, spigot, or a maven repo | |
| * <p> | |
| * Only downloading from bukkit compiles with the bukkitdev guidelines. | |
| * </p> | |
| * | |
| * @author Techcable | |
| */ | |
| @NoArgsConstructor(access=AccessLevel.PRIVATE) | |
| public class PluginLoader { | |
| /** | |
| * Load dependencies from the annotations specified in the plugin's class | |
| * | |
| * @param jPlugin plugin to download for | |
| * @throws UnableToDownloadException if unable to donwload a dependency | |
| */ | |
| public static class loadDependencies(Class<? extends JavaPlugin> jPlugin) throws UnableToDonwloadException { | |
| for | |
| } | |
| private static interface Project { | |
| /** | |
| * Return the name of this project as specified in plugin.yml | |
| * | |
| * @return the name of this plugin as specified in plugin.yml | |
| */ | |
| public String getName(); | |
| /** | |
| * Download this project to the specified location | |
| * | |
| * @param file destination file | |
| * @throws UnableToDownloadException if unable to download | |
| */ | |
| public void downloadTo(File file) throws UnableToDownloadException; | |
| } | |
| private static class UnableToDonwloadException extends Exception { | |
| public UnableToDownloadException(String msg, Exception cause) { | |
| super(msg, cause); | |
| } | |
| } | |
| //Spigot Methods () | |
| public String get | |
| //BukkitDev Methods | |
| public List<JSONObject> getFiles(int projectId) { | |
| List<JSONObject> files = new ArrayList<>(); | |
| JSONArray json = getJson("https://api.curseforge.com/servermods/files?projectIds" + projectId) | |
| for (Object obj : json) { | |
| JSONObject file = (JSONObject) obj; | |
| files.add(file); | |
| } | |
| return files; | |
| } | |
| @RequiredArgsConstructor | |
| @Getter | |
| private static class BukkitDevProject implements Project { | |
| private final int id; | |
| private final String name; | |
| @Override | |
| public void downloadTo(File destination) throws UnableToDonwloadException { | |
| try { | |
| List<JSONObject> files = getFiles(getId()); | |
| JSONObject latestFile = null; | |
| for (JSONObject file : files) { | |
| latestFile = file; //Java iterates from first to last, and the last file is the latest release | |
| } | |
| String version = (String) latestFile.get("name"); //Name is really version | |
| URL downloadURL = new URL((String) latestFile.get("downloadUrl")); | |
| Bukkit.getLogger().info("[PluginLoader] loading version " + version + " of " + getName()); | |
| if (!destination.exists()) destination.createNewFile(); | |
| try { | |
| int b; | |
| } finally { | |
| if (in != null) in.close(); | |
| if (out != null) out.close(); | |
| } | |
| } catch (Exception e) { | |
| throw new UnableToDonwloadException("Unable to download " + getName(), e); | |
| } | |
| } | |
| } | |
| //Utils | |
| private static download(URL url, File file) throws IOException { | |
| InputStream in; | |
| OutputStream out; | |
| try { | |
| in = downloadURL.openStream(); | |
| out = new FileOutputStream(destination); | |
| final byte data[] = new byte[1024]; | |
| int count; | |
| while ((count = in.read(data, 0, 1024)) != -1) { | |
| out.write(data, 0, count); | |
| } | |
| } finally { | |
| if (in != null) in.close() | |
| if (out != null) out.close(); | |
| } | |
| } | |
| private static JSONParser PARSER = new JSONParser(); | |
| private static Object getJson(String rawUrl) { | |
| BufferedReader reader = null; | |
| try { | |
| URL url = new URL(rawUrl); | |
| HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| connection.setRequestMethod("GET"); | |
| reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| StringBuffer result = new StringBuffer(); | |
| String line; | |
| while ((line = reader.readLine()) != null) result.append(line); | |
| return PARSER.parse(result.toString()); | |
| } catch (Exception ex) { | |
| return null; | |
| } finally { | |
| try { | |
| if (reader != null) reader.close(); | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| return null; | |
| } | |
| } | |
| } | |
| private static Object postJson(String url, JSONArray body) { | |
| String rawResponse = post(url, body.toJSONString()); | |
| if (rawResponse == null) return null; | |
| try { | |
| return PARSER.parse(rawResponse); | |
| } catch (Exception e) { | |
| return null; | |
| } | |
| } | |
| private static String post(String rawUrl, String body) { | |
| BufferedReader reader = null; | |
| OutputStream out = null; | |
| try { | |
| URL url = new URL(rawUrl); | |
| HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| connection.setRequestMethod("POST"); | |
| connection.setDoOutput(true); | |
| connection.setDoInput(true); | |
| connection.setRequestProperty("Content-Type", "application/json"); | |
| out = connection.getOutputStream(); | |
| out.write(body.getBytes()); | |
| reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| StringBuffer result = new StringBuffer(); | |
| String line; | |
| while ((line = reader.readLine()) != null) result.append(line); | |
| return result.toString(); | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| return null; | |
| } finally { | |
| try { | |
| if (out != null) out.close(); | |
| if (reader != null) reader.close(); | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| return null; | |
| } | |
| } | |
| } | |
| //Annotations | |
| public class @interface RequiredLibrary { | |
| public String groupId(); | |
| public String artifactId(); | |
| public String version(); | |
| public String mavenRepo() default ""; | |
| } | |
| public class @interface RequiredLibraries { | |
| public RequiredLibrary[] value(); | |
| } | |
| public class @interface RequiredPlugin { | |
| /** | |
| * Get the bukkit/spigot project id | |
| * <p> | |
| * Return the spigot project id if {@link #source()} returns SPIGOT | |
| * Return the bukkit project id if {@link #source()} returns BUKKIT_DEV | |
| * </p> | |
| * | |
| * @return the bukkit/spigot project id | |
| */ | |
| public int projectId(); | |
| /** | |
| * Return the plugin name as specified in plugin.yml | |
| * <p> | |
| * Used to determine whether the plugin is already installed | |
| * </p> | |
| * | |
| * @return the plugin name as specified in plugin.yml | |
| */ | |
| public String name(); | |
| public String version(); | |
| /** | |
| * Return where to download from | |
| * <p> | |
| * Currently only supports SPIGOT and BUKKIT_DEV | |
| * </p> | |
| * | |
| * @return the download source | |
| */ | |
| public PluginSource source(); | |
| } | |
| public enum PluginSource { | |
| SPIGOT, | |
| BUKKIT_DEV; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment