1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Fixed Import

This commit is contained in:
TheBusyBiscuit 2016-11-13 18:13:44 +01:00 committed by GitHub
parent 13f208a3b6
commit 24774ec878

View File

@ -1,148 +1,149 @@
package me.mrCookieSlime.CSCoreLibSetup; package me.mrCookieSlime.CSCoreLibSetup;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.HttpURLConnection;
import java.net.URL; import java.net.MalformedURLException;
import java.net.URLConnection; import java.net.URL;
import java.net.URLConnection;
import org.bukkit.plugin.Plugin;
import org.json.simple.JSONArray; import org.bukkit.plugin.Plugin;
import org.json.simple.JSONObject; import org.json.simple.JSONArray;
import org.json.simple.JSONValue; import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class CSCoreLibLoader {
public class CSCoreLibLoader {
Plugin plugin;
URL url; Plugin plugin;
URL download; URL url;
File file; URL download;
File file;
public CSCoreLibLoader(Plugin plugin) {
this.plugin = plugin; public CSCoreLibLoader(Plugin plugin) {
try { this.plugin = plugin;
this.url = new URL("https://api.curseforge.com/servermods/files?projectIds=88802"); try {
} catch (MalformedURLException e) { this.url = new URL("https://api.curseforge.com/servermods/files?projectIds=88802");
} } catch (MalformedURLException e) {
} }
}
public boolean load() {
if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) return true; public boolean load() {
else { if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) return true;
System.err.println(" "); else {
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
System.err.println(plugin.getName() + " could not be properly installed!"); System.err.println(" ");
System.err.println("It appears that you have not installed CS-CoreLib"); System.err.println(plugin.getName() + " could not be properly installed!");
System.err.println("And because of that, CS-CoreLib is now going to be"); System.err.println("It appears that you have not installed CS-CoreLib");
System.err.println("downloaded and installed."); System.err.println("And because of that, CS-CoreLib is now going to be");
System.err.println("But for the time being " + plugin.getName() + " will remain disabled"); System.err.println("downloaded and installed.");
System.err.println("After the installation process has finished,"); System.err.println("But for the time being " + plugin.getName() + " will remain disabled");
System.out.println("you will be asked to restart your Server."); System.err.println("After the installation process has finished,");
System.err.println("- mrCookieSlime"); System.out.println("you will be asked to restart your Server.");
System.err.println(" "); System.err.println("- mrCookieSlime");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { System.err.println(" ");
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() { @Override
if (connect()) install(); public void run() {
} if (connect()) install();
}, 10L); }
return false; }, 10L);
} return false;
} }
}
private boolean connect() {
try { private boolean connect() {
final URLConnection connection = this.url.openConnection(); try {
connection.setConnectTimeout(5000); final URLConnection connection = this.url.openConnection();
connection.addRequestProperty("User-Agent", "CS-CoreLib Loader (by mrCookieSlime)"); connection.setConnectTimeout(5000);
connection.setDoOutput(true); connection.addRequestProperty("User-Agent", "CS-CoreLib Loader (by mrCookieSlime)");
connection.setDoOutput(true);
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final JSONArray array = (JSONArray) JSONValue.parse(reader.readLine()); final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
download = traceURL(((String) ((JSONObject) array.get(array.size() - 1)).get("downloadUrl")).replace("https:", "http:")); final JSONArray array = (JSONArray) JSONValue.parse(reader.readLine());
file = new File("plugins/" + (String) ((JSONObject) array.get(array.size() - 1)).get("name") + ".jar"); download = traceURL(((String) ((JSONObject) array.get(array.size() - 1)).get("downloadUrl")).replace("https:", "http:"));
file = new File("plugins/" + (String) ((JSONObject) array.get(array.size() - 1)).get("name") + ".jar");
return true;
} catch (IOException e) { return true;
System.err.println(" "); } catch (IOException e) {
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
System.err.println("Could not connect to BukkitDev, is it down?"); System.err.println(" ");
System.err.println(" "); System.err.println("Could not connect to BukkitDev, is it down?");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
return false; System.err.println(" ");
} return false;
} }
}
private URL traceURL(String location) throws IOException {
HttpURLConnection connection = null; private URL traceURL(String location) throws IOException {
HttpURLConnection connection = null;
while (true) {
URL url = new URL(location); while (true) {
connection = (HttpURLConnection) url.openConnection(); URL url = new URL(location);
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(5000); connection.setInstanceFollowRedirects(false);
connection.addRequestProperty("User-Agent", "Auto Updater (by mrCookieSlime)"); connection.setConnectTimeout(5000);
connection.addRequestProperty("User-Agent", "Auto Updater (by mrCookieSlime)");
switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM: switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_MOVED_PERM:
String loc = connection.getHeaderField("Location"); case HttpURLConnection.HTTP_MOVED_TEMP:
location = new URL(new URL(location), loc).toExternalForm(); String loc = connection.getHeaderField("Location");
continue; location = new URL(new URL(location), loc).toExternalForm();
} continue;
break; }
} break;
}
return connection.getURL();
} return connection.getURL();
}
private void install() {
BufferedInputStream input = null; private void install() {
FileOutputStream output = null; BufferedInputStream input = null;
try { FileOutputStream output = null;
input = new BufferedInputStream(download.openStream()); try {
output = new FileOutputStream(file); input = new BufferedInputStream(download.openStream());
output = new FileOutputStream(file);
final byte[] data = new byte[1024];
int read; final byte[] data = new byte[1024];
while ((read = input.read(data, 0, 1024)) != -1) { int read;
output.write(data, 0, read); while ((read = input.read(data, 0, 1024)) != -1) {
} output.write(data, 0, read);
} catch (Exception ex) { }
System.err.println(" "); } catch (Exception ex) {
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
System.err.println("Could not download CS-CoreLib"); System.err.println(" ");
System.err.println(" "); System.err.println("Could not download CS-CoreLib");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - FATAL ERROR - ####################");
} finally { System.err.println(" ");
try { } finally {
if (input != null) input.close(); try {
if (output != null) output.close(); if (input != null) input.close();
System.err.println(" "); if (output != null) output.close();
System.err.println("#################### - WARNING - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - WARNING - ####################");
System.err.println("Please restart your Server to finish the Installation"); System.err.println(" ");
System.err.println("of " + plugin.getName() + " and CS-CoreLib"); System.err.println("Please restart your Server to finish the Installation");
System.err.println(" "); System.err.println("of " + plugin.getName() + " and CS-CoreLib");
System.err.println("#################### - WARNING - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - WARNING - ####################");
} catch (IOException e) { System.err.println(" ");
e.printStackTrace(); } catch (IOException e) {
} e.printStackTrace();
} }
} }
}
}
}