use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class MinecraftFontRenderer method loadResources.
public void loadResources() {
try {
if (!resourceLock.tryLock(0, TimeUnit.MILLISECONDS)) {
return;
}
} catch (InterruptedException e) {
}
reloadResourcesButton.setEnabled(false);
resourceBar.setValue(0);
resourceBar.setVisible(true);
textAreaResources.setText("Loading Resources...");
updateTextAreaInputSize();
List<String> resourceOrder;
int valuePerPack;
try {
YamlConfiguration yaml = new YamlConfiguration(new FileInputStream("InteractiveChatDiscordSrvAddon/config.yml"));
resourceOrder = yaml.getStringList("Resources.Order");
Collections.reverse(resourceOrder);
valuePerPack = (int) ((1.0 / (double) (resourceOrder.size() + 1)) * 10000);
} catch (IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, GUIMain.createLabel("There is an error while loading from config:\n" + e.getMessage(), 13, Color.RED), title, JOptionPane.ERROR_MESSAGE);
return;
}
if (resourceManager != null) {
resourceManager.close();
}
PrintStream original = System.err;
try {
resourceManager = new ResourceManager();
resourceManager.loadResources(new File("InteractiveChatDiscordSrvAddon/built-in", "Default"));
resourceBar.setValue(valuePerPack);
for (String resourceName : resourceOrder) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setErr(new PrintStream(baos));
try {
File resourcePackFile = new File("InteractiveChatDiscordSrvAddon/resourcepacks/" + resourceName);
ResourcePackInfo info = resourceManager.loadResources(resourcePackFile);
} catch (Exception e) {
e.printStackTrace();
}
String error = baos.toString();
if (!error.isEmpty()) {
ForkJoinPool.commonPool().execute(() -> JOptionPane.showMessageDialog(null, GUIMain.createLabel("There are errors while loading \"" + resourceName + "\":\n" + error, 13, Color.RED), title, JOptionPane.ERROR_MESSAGE));
}
resourceBar.setValue(resourceBar.getValue() + valuePerPack);
}
} catch (Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, GUIMain.createLabel("An error occurred!\n" + sw, 13, Color.RED), title, JOptionPane.ERROR_MESSAGE);
}
System.setErr(original);
textAreaResources.setText("Loaded Resources:\n");
for (ResourcePackInfo info : resourceManager.getResourcePackInfo()) {
textAreaResources.append(" - " + info.getName());
if (!info.getStatus()) {
textAreaResources.append(" (Failed)");
}
textAreaResources.append("\n");
}
List<LanguageData> languages = getAllLanguageData(resourceManager.getLanguageManager());
String lastSelected = comboBoxLanguages.getSelectedItem() == null ? null : ((LanguageData) comboBoxLanguages.getSelectedItem()).getLanguage();
comboBoxLanguages.removeAllItems();
for (LanguageData language : languages) {
comboBoxLanguages.addItem(language);
}
Optional<LanguageData> optLanguage = languages.stream().filter(each -> each.getLanguage().equalsIgnoreCase(lastSelected == null ? "en_us" : lastSelected)).findFirst();
if (optLanguage.isPresent()) {
comboBoxLanguages.setSelectedItem(optLanguage.get());
} else {
comboBoxLanguages.setSelectedIndex(0);
}
reloadResourcesButton.setEnabled(true);
resourceBar.setVisible(false);
updateTextAreaInputSize();
resourceLock.unlock();
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class BlockModelRenderer method loadResources.
public void loadResources() {
try {
if (!lock.tryLock(0, TimeUnit.MILLISECONDS)) {
return;
}
} catch (InterruptedException e) {
}
renderModelButton.setEnabled(false);
reloadResourcesButton.setEnabled(false);
resourceBar.setValue(0);
resourceBar.setVisible(true);
textAreaResources.setText("Loading Resources...");
List<String> resourceOrder;
int valuePerPack;
try {
YamlConfiguration yaml = new YamlConfiguration(new FileInputStream("InteractiveChatDiscordSrvAddon/config.yml"));
resourceOrder = yaml.getStringList("Resources.Order");
Collections.reverse(resourceOrder);
valuePerPack = (int) ((1.0 / (double) (resourceOrder.size() + 1)) * 10000);
} catch (IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, GUIMain.createLabel("There is an error while loading from config:\n" + e.getMessage(), 13, Color.RED), title, JOptionPane.ERROR_MESSAGE);
return;
}
if (resourceManager != null) {
resourceManager.close();
}
PrintStream original = System.err;
try {
resourceManager = new ResourceManager();
resourceManager.loadResources(new File("InteractiveChatDiscordSrvAddon/built-in", "Default"));
resourceBar.setValue(valuePerPack);
for (String resourceName : resourceOrder) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setErr(new PrintStream(baos));
try {
File resourcePackFile = new File("InteractiveChatDiscordSrvAddon/resourcepacks/" + resourceName);
ResourcePackInfo info = resourceManager.loadResources(resourcePackFile);
} catch (Exception e) {
e.printStackTrace();
}
String error = baos.toString();
if (!error.isEmpty()) {
ForkJoinPool.commonPool().execute(() -> JOptionPane.showMessageDialog(null, GUIMain.createLabel("There are errors while loading \"" + resourceName + "\":\n" + error, 13, Color.RED), title, JOptionPane.ERROR_MESSAGE));
}
resourceBar.setValue(resourceBar.getValue() + valuePerPack);
}
} catch (Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, GUIMain.createLabel("An error occurred!\n" + sw, 13, Color.RED), title, JOptionPane.ERROR_MESSAGE);
}
System.setErr(original);
textAreaResources.setText("Loaded Resources:\n");
for (ResourcePackInfo info : resourceManager.getResourcePackInfo()) {
textAreaResources.append(" - " + info.getName());
if (!info.getStatus()) {
textAreaResources.append(" (Failed)");
}
textAreaResources.append("\n");
}
renderModelButton.setEnabled(true);
reloadResourcesButton.setEnabled(true);
resourceBar.setVisible(false);
lock.unlock();
}
use of com.loohp.interactivechatdiscordsrvaddon.resources.ResourceManager in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class InteractiveChatDiscordSrvAddon method reloadTextures.
public void reloadTextures(boolean redownload, boolean clean, CommandSender... receivers) {
CommandSender[] senders;
if (Stream.of(receivers).noneMatch(each -> each.equals(Bukkit.getConsoleSender()))) {
senders = new CommandSender[receivers.length + 1];
System.arraycopy(receivers, 0, senders, 0, receivers.length);
senders[senders.length - 1] = Bukkit.getConsoleSender();
} else {
senders = receivers;
}
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
try {
if (!resourceReloadLock.tryLock(0, TimeUnit.MILLISECONDS)) {
sendMessage(ChatColor.YELLOW + "Resource reloading already in progress!", senders);
return;
}
isReady = false;
if (InteractiveChatDiscordSrvAddon.plugin.resourceManager != null) {
Bukkit.getScheduler().callSyncMethod(plugin, () -> {
InteractiveChatDiscordSrvAddon.plugin.resourceManager.close();
return null;
}).get();
}
try {
AssetsDownloader.loadAssets(getDataFolder(), redownload, clean, receivers);
} catch (Exception e) {
e.printStackTrace();
}
List<String> resourceList = new ArrayList<>();
resourceList.add("Default");
resourceList.addAll(resourceOrder);
sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Reloading ResourceManager: " + ChatColor.YELLOW + String.join(", ", resourceList), senders);
ResourceManager resourceManager = new ResourceManager();
resourceManager.getLanguageManager().setTranslateFunction((translateKey, language) -> LanguageUtils.getTranslation(translateKey, language));
resourceManager.getLanguageManager().setAvailableLanguagesSupplier(() -> LanguageUtils.getLoadedLanguages());
resourceManager.getLanguageManager().registerReloadListener(e -> {
LanguageUtils.clearPluginTranslations(InteractiveChatDiscordSrvAddon.plugin);
for (Entry<String, Map<String, String>> entry : e.getTranslations().entrySet()) {
LanguageUtils.loadPluginTranslations(InteractiveChatDiscordSrvAddon.plugin, entry.getKey(), entry.getValue());
}
});
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Loading \"Default\" resources...");
resourceManager.loadResources(new File(getDataFolder() + "/built-in", "Default"));
for (String resourceName : resourceOrder) {
try {
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Loading \"" + resourceName + "\" resources...");
File resourcePackFile = new File(getDataFolder(), "resourcepacks/" + resourceName);
ResourcePackInfo info = resourceManager.loadResources(resourcePackFile);
if (info.getStatus()) {
if (info.comparePackFormat() > 0) {
sendMessage(ChatColor.YELLOW + "[ICDiscordSrvAddon] Warning: \"" + resourceName + "\" was made for a newer version of Minecraft!", senders);
} else if (info.comparePackFormat() < 0) {
sendMessage(ChatColor.YELLOW + "[ICDiscordSrvAddon] Warning: \"" + resourceName + "\" was made for an older version of Minecraft!", senders);
}
} else {
if (info.getRejectedReason() == null) {
sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load \"" + resourceName + "\"", senders);
} else {
sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load \"" + resourceName + "\", Reason: " + info.getRejectedReason(), senders);
}
}
} catch (Exception e) {
sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] Unable to load \"" + resourceName + "\"", senders);
e.printStackTrace();
}
}
Bukkit.getScheduler().callSyncMethod(plugin, () -> {
InteractiveChatDiscordSrvAddon.plugin.resourceManager = resourceManager;
Cache.clearAllCache();
if (resourceManager.getResourcePackInfo().stream().allMatch(each -> each.getStatus())) {
sendMessage(ChatColor.AQUA + "[ICDiscordSrvAddon] Loaded all resources!", senders);
isReady = true;
} else {
sendMessage(ChatColor.RED + "[ICDiscordSrvAddon] There is a problem while loading resources.", senders);
}
return null;
}).get();
resourceReloadLock.unlock();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
Aggregations