use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class CMLMain method downloadAssets.
protected static void downloadAssets() throws IOException {
File defaultAssetsFolder = new File("InteractiveChatDiscordSrvAddon/built-in", "Default");
defaultAssetsFolder.mkdirs();
System.out.println("Available Minecraft Versions:");
for (String version : ResourceDownloadManager.getMinecraftVersions()) {
System.out.println(version);
}
System.out.println();
System.out.println("Select Minecraft Version: (Type the version string)");
String input;
while (true) {
input = IN.readLine();
if (ResourceDownloadManager.getMinecraftVersions().contains(input)) {
break;
}
System.out.println("That is not a valid Minecraft version!");
System.out.println();
}
ResourceDownloadManager downloadManager = new ResourceDownloadManager(input, defaultAssetsFolder);
CompletableFuture<Void> future = new CompletableFuture<>();
new Thread(() -> {
downloadManager.downloadResources((type, fileName, percentage) -> {
switch(type) {
case CLIENT_DOWNLOAD:
System.out.println("Downloading client jar");
break;
case EXTRACT:
System.out.println("Extracting " + fileName);
break;
case DOWNLOAD:
System.out.println("Downloading " + fileName);
break;
case DONE:
System.out.println("Done!");
break;
}
});
future.complete(null);
}).start();
future.join();
System.out.println("Assets saved at: " + defaultAssetsFolder.getAbsolutePath());
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AssetsDownloader method loadExtras.
public static void loadExtras() {
ResourceDownloadManager downloadManager = new ResourceDownloadManager(InteractiveChat.exactMinecraftVersion, null);
downloadManager.downloadExtras(() -> {
InteractiveChatDiscordSrvAddon.plugin.extras.clear();
}, (key, dataBytes) -> {
InteractiveChatDiscordSrvAddon.plugin.extras.put(key, dataBytes);
});
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AssetsDownloader method loadAssets.
@SuppressWarnings("deprecation")
public static void loadAssets(File rootFolder, boolean force, boolean clean, CommandSender... senders) throws Exception {
if (!Arrays.asList(senders).contains(Bukkit.getConsoleSender())) {
List<CommandSender> senderList = new ArrayList<>(Arrays.asList(senders));
senderList.add(Bukkit.getConsoleSender());
senders = senderList.toArray(new CommandSender[senderList.size()]);
}
if (!LOCK.tryLock(0, TimeUnit.MILLISECONDS)) {
return;
}
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("Default") ? json.get("Default").toString() : "EMPTY";
String oldVersion = json.containsKey("version") ? json.get("version").toString() : "EMPTY";
File defaultAssetsFolder = new File(rootFolder + "/built-in", "Default");
defaultAssetsFolder.mkdirs();
ResourceDownloadManager downloadManager = new ResourceDownloadManager(InteractiveChat.exactMinecraftVersion, defaultAssetsFolder);
String hash = downloadManager.getHash();
if (force || !hash.equals(oldHash) || !InteractiveChatDiscordSrvAddon.plugin.getDescription().getVersion().equals(oldVersion)) {
if (clean) {
InteractiveChatDiscordSrvAddon.plugin.sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Cleaning old default resources!", senders);
FileUtils.removeFolderRecursively(defaultAssetsFolder);
defaultAssetsFolder.mkdirs();
}
if (force) {
InteractiveChatDiscordSrvAddon.plugin.sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Forcibly re-downloading default resources! Please wait... (" + oldHash + " -> " + hash + ")", senders);
} else if (!hash.equals(oldHash)) {
InteractiveChatDiscordSrvAddon.plugin.sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Hash changed! Re-downloading default resources! Please wait... (" + oldHash + " -> " + hash + ")", senders);
} else {
InteractiveChatDiscordSrvAddon.plugin.sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Plugin version changed! Re-downloading default resources! Please wait... (" + oldHash + " -> " + hash + ")", senders);
}
downloadManager.downloadResources((type, fileName, percentage) -> {
switch(type) {
case CLIENT_DOWNLOAD:
if (!InteractiveChatDiscordSrvAddon.plugin.reducedAssetsDownloadInfo) {
Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "[ICDiscordSrvAddon] Downloading client jar");
}
break;
case EXTRACT:
if (!InteractiveChatDiscordSrvAddon.plugin.reducedAssetsDownloadInfo) {
Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "[ICDiscordSrvAddon] Extracting " + fileName);
}
break;
case DOWNLOAD:
if (!InteractiveChatDiscordSrvAddon.plugin.reducedAssetsDownloadInfo) {
Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "[ICDiscordSrvAddon] Downloading " + fileName + " (" + FORMAT.format(percentage) + "%)");
}
break;
case DONE:
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Done!");
break;
}
});
}
downloadManager.downloadExtras(() -> {
InteractiveChatDiscordSrvAddon.plugin.extras.clear();
}, (key, dataBytes) -> {
InteractiveChatDiscordSrvAddon.plugin.extras.put(key, dataBytes);
});
InteractiveChatDiscordSrvAddon.plugin.defaultResourceHash = hash;
json.put("Default", 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();
}
LOCK.unlock();
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager 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();
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceDownloadManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class MinecraftFontRenderer method downloadAllLanguages.
protected void downloadAllLanguages(String title, BufferedImage image, Icon icon) {
File defaultAssetsFolder = new File("InteractiveChatDiscordSrvAddon/built-in", "Default");
defaultAssetsFolder.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 downloadManager = new ResourceDownloadManager((String) options.getSelectedItem(), defaultAssetsFolder);
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(() -> {
downloadManager.downloadLanguages((type, fileName, percentage) -> {
switch(type) {
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;
}
});
future.complete(null);
}).start();
future.join();
progressBar.setValue(9999);
JOptionPane.showMessageDialog(null, GUIMain.createLabel("Assets saved at: " + defaultAssetsFolder.getAbsolutePath(), 15), title, JOptionPane.INFORMATION_MESSAGE, icon);
frame.setVisible(false);
frame.dispose();
}
Aggregations