1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Fixed CSCoreLibLoader

This commit is contained in:
TheBusyBiscuit 2017-03-24 14:04:38 +01:00
parent f760dec589
commit fc00c5356c
2 changed files with 156 additions and 153 deletions

View File

@ -1,149 +1,152 @@
package me.mrCookieSlime.CSCoreLibSetup; package me.mrCookieSlime.Slimefun.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.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
public class CSCoreLibLoader { public class CSCoreLibLoader {
Plugin plugin; Plugin plugin;
URL url; URL url;
URL download; URL download;
File file; File file;
public CSCoreLibLoader(Plugin plugin) { public CSCoreLibLoader(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
try { try {
this.url = new URL("https://api.curseforge.com/servermods/files?projectIds=88802"); this.url = new URL("https://api.curseforge.com/servermods/files?projectIds=88802");
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
} }
} }
public boolean load() { public boolean load() {
if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) return true; if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) return true;
else { else {
System.err.println(" "); System.err.println(" ");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println("#################### - INFO - ####################");
System.err.println(" "); System.err.println(" ");
System.err.println(plugin.getName() + " could not be properly installed!"); System.err.println(plugin.getName() + " could not be loaded.");
System.err.println("It appears that you have not installed CS-CoreLib"); System.err.println("It appears that you have not installed CS-CoreLib");
System.err.println("And because of that, CS-CoreLib is now going to be"); System.err.println("Your Server will now try to download and install");
System.err.println("downloaded and installed."); System.err.println("CS-CoreLib for you.");
System.err.println("But for the time being " + plugin.getName() + " will remain disabled"); System.err.println("You will be asked to restart your Server when it's finished.");
System.err.println("After the installation process has finished,"); System.err.println("If this somehow fails, please download and install CS-CoreLib manually:");
System.out.println("you will be asked to restart your Server."); System.err.println("https://dev.bukkit.org/projects/cs-corelib");
System.err.println("- mrCookieSlime"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - INFO - ####################");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
@Override public void run() {
public void run() { if (connect()) install();
if (connect()) install(); }
} }, 10L);
}, 10L); return false;
return false; }
} }
}
private boolean connect() {
private boolean connect() { try {
try { final URLConnection connection = this.url.openConnection();
final URLConnection connection = this.url.openConnection(); connection.setConnectTimeout(5000);
connection.setConnectTimeout(5000); connection.addRequestProperty("User-Agent", "CS-CoreLib Loader (by mrCookieSlime)");
connection.addRequestProperty("User-Agent", "CS-CoreLib Loader (by mrCookieSlime)"); connection.setDoOutput(true);
connection.setDoOutput(true);
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); final JSONArray array = (JSONArray) JSONValue.parse(reader.readLine());
final JSONArray array = (JSONArray) JSONValue.parse(reader.readLine()); download = traceURL(((String) ((JSONObject) array.get(array.size() - 1)).get("downloadUrl")).replace("https:", "http:"));
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");
file = new File("plugins/" + (String) ((JSONObject) array.get(array.size() - 1)).get("name") + ".jar");
return true;
return true; } catch (IOException e) {
} catch (IOException e) { System.err.println(" ");
System.err.println(" "); System.err.println("#################### - WARNING - ####################");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("Could not connect to BukkitDev.");
System.err.println("Could not connect to BukkitDev, is it down?"); System.err.println("Please download & install CS-CoreLib manually:");
System.err.println(" "); System.err.println("https://dev.bukkit.org/projects/cs-corelib");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println(" ");
System.err.println(" "); System.err.println("#################### - WARNING - ####################");
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 new URL(connection.getURL().toString().replaceAll(" ", "%20"));
}
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("#################### - WARNING - ####################");
System.err.println("Could not download CS-CoreLib"); System.err.println(" ");
System.err.println(" "); System.err.println("Failed to download CS-CoreLib");
System.err.println("#################### - FATAL ERROR - ####################"); System.err.println("Please download & install CS-CoreLib manually:");
System.err.println(" "); System.err.println("https://dev.bukkit.org/projects/cs-corelib");
} finally { System.err.println(" ");
try { System.err.println("#################### - WARNING - ####################");
if (input != null) input.close(); System.err.println(" ");
if (output != null) output.close(); } finally {
System.err.println(" "); try {
System.err.println("#################### - WARNING - ####################"); if (input != null) input.close();
System.err.println(" "); if (output != null) output.close();
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("#################### - INFO - ####################");
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");
} catch (IOException e) { System.err.println(" ");
e.printStackTrace(); System.err.println("#################### - INFO - ####################");
} System.err.println(" ");
} } catch (IOException e) {
} e.printStackTrace();
}
} }
}
}

View File

@ -15,9 +15,6 @@ import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.AutoEnchanter;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -39,8 +36,8 @@ import me.mrCookieSlime.CSCoreLibPlugin.PluginUtils;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Clock; import me.mrCookieSlime.CSCoreLibPlugin.general.Clock;
import me.mrCookieSlime.CSCoreLibPlugin.general.Reflection.ReflectionUtils; import me.mrCookieSlime.CSCoreLibPlugin.general.Reflection.ReflectionUtils;
import me.mrCookieSlime.CSCoreLibSetup.CSCoreLibLoader;
import me.mrCookieSlime.Slimefun.AncientAltar.Pedestals; import me.mrCookieSlime.Slimefun.AncientAltar.Pedestals;
import me.mrCookieSlime.Slimefun.CSCoreLibSetup.CSCoreLibLoader;
import me.mrCookieSlime.Slimefun.Commands.SlimefunCommand; import me.mrCookieSlime.Slimefun.Commands.SlimefunCommand;
import me.mrCookieSlime.Slimefun.Commands.SlimefunTabCompleter; import me.mrCookieSlime.Slimefun.Commands.SlimefunTabCompleter;
import me.mrCookieSlime.Slimefun.GEO.OreGenSystem; import me.mrCookieSlime.Slimefun.GEO.OreGenSystem;
@ -55,6 +52,7 @@ import me.mrCookieSlime.Slimefun.Objects.Research;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunArmorPiece; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunArmorPiece;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.AutoEnchanter;
import me.mrCookieSlime.Slimefun.Setup.Files; import me.mrCookieSlime.Slimefun.Setup.Files;
import me.mrCookieSlime.Slimefun.Setup.Messages; import me.mrCookieSlime.Slimefun.Setup.Messages;
import me.mrCookieSlime.Slimefun.Setup.MiscSetup; import me.mrCookieSlime.Slimefun.Setup.MiscSetup;
@ -89,6 +87,8 @@ import me.mrCookieSlime.Slimefun.listeners.ItemListener;
import me.mrCookieSlime.Slimefun.listeners.TalismanListener; import me.mrCookieSlime.Slimefun.listeners.TalismanListener;
import me.mrCookieSlime.Slimefun.listeners.TeleporterListener; import me.mrCookieSlime.Slimefun.listeners.TeleporterListener;
import me.mrCookieSlime.Slimefun.listeners.ToolListener; import me.mrCookieSlime.Slimefun.listeners.ToolListener;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
public class SlimefunStartup extends JavaPlugin { public class SlimefunStartup extends JavaPlugin {