Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # AUTOMATIC UPDATER & DOWNLOADER OF BUNGEE-THEAPI
- # Our discord: https://discord.gg/8YtfC234dA
- public void onLoad() {
- downloadAndUpdate();
- }
- public static void downloadAndUpdate() {
- if(ProxyServer.getInstance().getPluginManager().getPlugin("BungeeTheAPI")==null) {
- try {
- HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/87143/download").openConnection();
- conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
- conn.setRequestProperty("Accept-Encoding", "gzip");
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(20000);
- conn.setReadTimeout(20000);
- conn.connect();
- File f = new File("plugins/BungeeTheAPI.jar");
- if(!f.exists()) {
- f.getParentFile().mkdirs();
- f.createNewFile();
- }
- ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
- FileOutputStream fos = new FileOutputStream(f);
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- fos.close();
- rbc.close();
- //load TheAPI
- File file = new File("plugins/BungeeTheAPI.jar");
- try (JarFile jar = new JarFile(file)) {
- JarEntry pdf = jar.getJarEntry("bungee.yml");
- if (pdf == null)
- pdf = jar.getJarEntry("plugin.yml");
- try (InputStream in = jar.getInputStream(pdf)) {
- PluginDescription desc = (PluginDescription)((Yaml)Ref.get(ProxyServer.getInstance().getPluginManager(), "yaml")).loadAs(in, PluginDescription.class);
- desc.setFile(file);
- if (!(boolean)Ref.invoke(ProxyServer.getInstance().getPluginManager(), Ref.method(PluginManager.class, "enablePlugin", Map.class, Stack.class, PluginDescription.class), new HashMap<>(), new Stack<>(), desc))
- ProxyServer.getInstance().getLogger().log(Level.WARNING, "Failed to enable {0}", desc.getName());
- else {
- Plugin plugin = ProxyServer.getInstance().getPluginManager().getPlugin("BungeeTheAPI");
- try {
- plugin.onEnable();
- ProxyServer.getInstance().getLogger().log(Level.INFO, "Enabled plugin {0} version {1} by {2}", new Object[] { plugin.getDescription().getName(), plugin.getDescription().getVersion(), plugin.getDescription().getAuthor() });
- } catch (Throwable t) {
- ProxyServer.getInstance().getLogger().log(Level.WARNING, "Exception encountered when loading plugin: " + plugin.getDescription().getName(), t);
- }
- }
- }
- } catch (Exception ex) {
- ProxyServer.getInstance().getLogger().log(Level.WARNING, "Could not load plugin from file " + file, ex);
- }
- }catch(Exception e) {
- }
- } //else download update if needed
- else {
- try {
- if(new SpigotUpdateChecker(ProxyServer.getInstance().getPluginManager().getPlugin("BukkitTheAPI").getDescription().getVersion(), 87143).checkForUpdates()==Version.NEW) {
- try {
- HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/87143/download").openConnection();
- conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
- conn.setRequestProperty("Accept-Encoding", "gzip");
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(20000);
- conn.setReadTimeout(20000);
- conn.connect();
- File f = new File("plugins/update/BungeeTheAPI.jar");
- if(!f.exists()) {
- f.getParentFile().mkdirs();
- f.createNewFile();
- }
- ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
- FileOutputStream fos = new FileOutputStream(f);
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- fos.close();
- rbc.close();
- }catch(Exception e) {
- }
- }
- }catch(Exception e) {
- }
- }
- }
- public static String fromStream(InputStream stream) {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8), 8192);
- StringBuilder sb = new StringBuilder(512);
- String content;
- while ((content = br.readLine()) != null)
- sb.append(content).append(System.lineSeparator());
- br.close();
- return sb.toString();
- } catch (Exception e) {
- return null;
- }
- }
Add Comment
Please, Sign In to add comment