use of com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AssetsDownloader method loadLibraries.
public static void loadLibraries(File rootFolder) {
try {
File hashes = new File(rootFolder, "hashes.json");
if (!hashes.exists()) {
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
pw.println("{}");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
JSONObject json;
try (InputStreamReader hashReader = new InputStreamReader(new FileInputStream(hashes), StandardCharsets.UTF_8)) {
json = (JSONObject) new JSONParser().parse(hashReader);
} catch (Throwable e) {
new RuntimeException("Invalid hashes.json! It will be reset.", e).printStackTrace();
json = new JSONObject();
}
String oldHash = InteractiveChatDiscordSrvAddon.plugin.defaultResourceHash = json.containsKey("libs") ? json.get("libs").toString() : "EMPTY";
String oldVersion = json.containsKey("version") ? json.get("version").toString() : "EMPTY";
File libsFolder = new File(rootFolder, "libs");
libsFolder.mkdirs();
LibraryDownloadManager downloadManager = new LibraryDownloadManager(libsFolder);
String hash = downloadManager.getHash();
if (!hash.equals(oldHash) || !InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion().equals(oldVersion)) {
downloadManager.downloadLibraries((result, jarName) -> {
if (result) {
Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Downloaded library \"" + jarName + "\"");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to download library \"" + jarName + "\"");
}
});
}
LibraryLoader.loadLibraries(libsFolder, (file, e) -> {
String jarName = file.getName();
if (e == null) {
Bukkit.getConsoleSender().sendMessage("[ICDiscordSrvAddon] Loaded library \"" + jarName + "\"");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load library \"" + jarName + "\"");
e.printStackTrace();
}
});
json.put("libs", hash);
json.put("version", InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion());
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(hashes), StandardCharsets.UTF_8))) {
Gson g = new GsonBuilder().setPrettyPrinting().create();
pw.println(g.toJson(new JsonParser().parse(json.toString())));
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.loohp.interactivechatdiscordsrvaddon.libs.LibraryDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class GUIMain method downloadAssets.
protected static void downloadAssets(String title, BufferedImage image, Icon icon) {
File defaultAssetsFolder = new File("InteractiveChatDiscordSrvAddon/built-in", "Default");
defaultAssetsFolder.mkdirs();
File libsFolder = new File("InteractiveChatDiscordSrvAddon", "libs");
libsFolder.mkdirs();
JPanel panel = new JPanel();
panel.add(GUIMain.createLabel("Select Minecraft Version: ", 13));
JComboBox<String> options = new JComboBox<>();
for (String version : ResourceDownloadManager.getMinecraftVersions()) {
options.addItem(version);
}
panel.add(options);
int result = JOptionPane.showOptionDialog(null, panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icon, null, null);
if (result < 0) {
return;
}
ResourceDownloadManager resourceDownloadManager = new ResourceDownloadManager((String) options.getSelectedItem(), defaultAssetsFolder);
LibraryDownloadManager libraryDownloadManager = new LibraryDownloadManager(libsFolder);
JFrame frame = new JFrame(title);
frame.setIconImage(image);
frame.setSize(800, 175);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
panel.setLayout(new GridLayout(0, 1));
JLabel label = GUIMain.createLabel("<html>Downloading Assets:<html/>", 13);
label.setSize(800, 125);
panel.add(label);
JProgressBar progressBar = new JProgressBar(0, 10000);
panel.add(progressBar);
frame.add(panel, BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
CompletableFuture<Void> future = new CompletableFuture<>();
new Thread(() -> {
resourceDownloadManager.downloadResources((type, fileName, percentage) -> {
switch(type) {
case CLIENT_DOWNLOAD:
label.setText("<html>Downloading Assets:<br>Downloading client jar<html/>");
break;
case EXTRACT:
label.setText("<html>Downloading Assets:<br>Extracting " + fileName + "<html/>");
break;
case DOWNLOAD:
label.setText("<html>Downloading Assets:<br>Downloading " + fileName + "<html/>");
progressBar.setValue(Math.min(9999, (int) (percentage * 100)));
break;
case DONE:
label.setText("<html>Done!<html/>");
break;
}
});
libraryDownloadManager.downloadLibraries((downloadResult, jarName) -> {
if (downloadResult) {
label.setText("<html>Downloaded library \"" + jarName + "\"<html/>");
}
});
future.complete(null);
}).start();
future.join();
progressBar.setValue(9999);
JOptionPane.showMessageDialog(null, createLabel("Assets saved at: " + defaultAssetsFolder.getAbsolutePath(), 15), title, JOptionPane.INFORMATION_MESSAGE, icon);
frame.setVisible(false);
frame.dispose();
}
Aggregations