aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
diff options
context:
space:
mode:
authorMoulberry <james.jenour@student.scotch.wa.edu.au>2020-07-29 12:21:03 +1000
committerMoulberry <james.jenour@student.scotch.wa.edu.au>2020-07-29 12:21:03 +1000
commit6115246bf36fb98037ca0df2faac869eba7845c3 (patch)
tree4e659cf3d0bae323346ef251c995179c490c1115 /src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
parent9716e11a253a370e16dd1f0bdfdd70ac21ee4102 (diff)
downloadnotenoughupdates-6115246bf36fb98037ca0df2faac869eba7845c3.tar.gz
notenoughupdates-6115246bf36fb98037ca0df2faac869eba7845c3.tar.bz2
notenoughupdates-6115246bf36fb98037ca0df2faac869eba7845c3.zip
1.11.10.5
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java189
1 files changed, 104 insertions, 85 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
index ffd57b41..14dc61fb 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
@@ -21,6 +21,7 @@ import org.lwjgl.opengl.Display;
import javax.swing.*;
import java.io.*;
import java.net.URL;
+import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ExecutorService;
@@ -367,121 +368,139 @@ public class NEUManager {
* repository.
*/
public void loadItemInformation() {
- try {
- if(config.autoupdate.value) {
- JOptionPane pane = new JOptionPane("Getting items to download from remote repository.");
- JDialog dialog = pane.createDialog("NotEnoughUpdates Remote Sync");
- dialog.setModal(false);
- //dialog.setVisible(true);
+ Thread thread = new Thread(() -> {
+ try {
+ if(config.autoupdate.value) {
+ JOptionPane pane = new JOptionPane("Getting items to download from remote repository.");
+ JDialog dialog = pane.createDialog("NotEnoughUpdates Remote Sync");
+ dialog.setModal(false);
+ //dialog.setVisible(true);
- if (Display.isActive()) dialog.toFront();
+ if (Display.isActive()) dialog.toFront();
- HashMap<String, String> oldShas = new HashMap<>();
- for (Map.Entry<String, JsonElement> entry : itemShaConfig.entrySet()) {
- if (new File(repoLocation, entry.getKey() + ".json").exists()) {
- oldShas.put(entry.getKey() + ".json", entry.getValue().getAsString());
+ HashMap<String, String> oldShas = new HashMap<>();
+ for (Map.Entry<String, JsonElement> entry : itemShaConfig.entrySet()) {
+ if (new File(repoLocation, entry.getKey() + ".json").exists()) {
+ oldShas.put(entry.getKey() + ".json", entry.getValue().getAsString());
+ }
}
- }
- Map<String, String> changedFiles = neuio.getChangedItems(oldShas);
+ Map<String, String> changedFiles = neuio.getChangedItems(oldShas);
- if (changedFiles != null) {
- for (Map.Entry<String, String> changedFile : changedFiles.entrySet()) {
- itemShaConfig.addProperty(changedFile.getKey().substring(0, changedFile.getKey().length() - 5),
- changedFile.getValue());
- }
- try {
- writeJson(itemShaConfig, itemShaLocation);
- } catch (IOException e) {
- }
- }
- if (Display.isActive()) dialog.toFront();
- if (changedFiles != null && changedFiles.size() <= 20) {
- String startMessage = "NotEnoughUpdates: Syncing with remote repository (";
- int downloaded = 0;
+ if (Display.isActive()) dialog.toFront();
+
+ if (changedFiles != null && changedFiles.size() <= 20) {
+ String startMessage = "NotEnoughUpdates: Syncing with remote repository (";
+ int downloaded = 0;
+
+ String dlUrl = "https://raw.githubusercontent.com/Moulberry/NotEnoughUpdates-REPO/master/";
+
+ for (String name : changedFiles.keySet()) {
+ pane.setMessage(startMessage + (++downloaded) + "/" + changedFiles.size() + ")\nCurrent: " + name);
+ dialog.pack();
+ //dialog.setVisible(true);
+ if (Display.isActive()) dialog.toFront();
+
+ File item = new File(repoLocation, name);
+ try {
+ item.getParentFile().mkdirs();
+ item.createNewFile();
+ } catch (IOException e) {
+ continue;
+ }
+ URL url = new URL(dlUrl+name);
+ URLConnection urlConnection = url.openConnection();
+ urlConnection.setConnectTimeout(3000);
+ urlConnection.setReadTimeout(3000);
+ try (BufferedInputStream inStream = new BufferedInputStream(urlConnection.getInputStream());
+ FileOutputStream fileOutputStream = new FileOutputStream(item)) {
+ byte dataBuffer[] = new byte[1024];
+ int bytesRead;
+ while ((bytesRead = inStream.read(dataBuffer, 0, 1024)) != -1) {
+ fileOutputStream.write(dataBuffer, 0, bytesRead);
+ }
+ itemShaConfig.addProperty(name.substring(0, name.length() - 5),
+ changedFiles.get(name));
+ } catch (IOException e) {
+ }
+ }
+ try {
+ writeJson(itemShaConfig, itemShaLocation);
+ } catch (IOException e) {
+ }
+ } else {
+ Utils.recursiveDelete(repoLocation);
+ repoLocation.mkdirs();
- String dlUrl = "https://raw.githubusercontent.com/Moulberry/NotEnoughUpdates-REPO/master/";
+ //TODO: Store hard-coded value somewhere else
+ String dlUrl = "https://github.com/Moulberry/NotEnoughUpdates-REPO/archive/master.zip";
- for (String name : changedFiles.keySet()) {
- pane.setMessage(startMessage + (++downloaded) + "/" + changedFiles.size() + ")\nCurrent: " + name);
+ pane.setMessage("Downloading NEU Master Archive. (DL# >20)");
dialog.pack();
- dialog.setVisible(true);
+ //dialog.setVisible(true);
if (Display.isActive()) dialog.toFront();
- File item = new File(repoLocation, name);
+ File itemsZip = new File(repoLocation, "neu-items-master.zip");
try {
- item.getParentFile().mkdirs();
- item.createNewFile();
+ itemsZip.createNewFile();
} catch (IOException e) {
+ return;
}
- try (BufferedInputStream inStream = new BufferedInputStream(new URL(dlUrl+name).openStream());
- FileOutputStream fileOutputStream = new FileOutputStream(item)) {
+ try (BufferedInputStream inStream = new BufferedInputStream(new URL(dlUrl).openStream());
+ FileOutputStream fileOutputStream = new FileOutputStream(itemsZip)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = inStream.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
+ return;
}
- }
- } else {
- Utils.recursiveDelete(repoLocation);
- repoLocation.mkdirs();
- //TODO: Store hard-coded value somewhere else
- String dlUrl = "https://github.com/Moulberry/NotEnoughUpdates-REPO/archive/master.zip";
+ pane.setMessage("Unzipping NEU Master Archive.");
+ dialog.pack();
+ //dialog.setVisible(true);
+ if (Display.isActive()) dialog.toFront();
- pane.setMessage("Downloading NEU Master Archive. (DL# >20)");
- dialog.pack();
- dialog.setVisible(true);
- if (Display.isActive()) dialog.toFront();
+ unzipIgnoreFirstFolder(itemsZip.getAbsolutePath(), repoLocation.getAbsolutePath());
- File itemsZip = new File(repoLocation, "neu-items-master.zip");
- try {
- itemsZip.createNewFile();
- } catch (IOException e) {
- }
- try (BufferedInputStream inStream = new BufferedInputStream(new URL(dlUrl).openStream());
- FileOutputStream fileOutputStream = new FileOutputStream(itemsZip)) {
- byte dataBuffer[] = new byte[1024];
- int bytesRead;
- while ((bytesRead = inStream.read(dataBuffer, 0, 1024)) != -1) {
- fileOutputStream.write(dataBuffer, 0, bytesRead);
+ if (changedFiles != null) {
+ for (Map.Entry<String, String> changedFile : changedFiles.entrySet()) {
+ itemShaConfig.addProperty(changedFile.getKey().substring(0, changedFile.getKey().length() - 5),
+ changedFile.getValue());
+ }
+ try {
+ writeJson(itemShaConfig, itemShaLocation);
+ } catch (IOException e) {
+ }
}
- } catch (IOException e) {
- e.printStackTrace();
}
- pane.setMessage("Unzipping NEU Master Archive.");
- dialog.pack();
- dialog.setVisible(true);
- if (Display.isActive()) dialog.toFront();
-
- unzipIgnoreFirstFolder(itemsZip.getAbsolutePath(), repoLocation.getAbsolutePath());
+ dialog.dispose();
}
+ } catch(Exception e) {}
- dialog.dispose();
+ Set<String> currentlyInstalledItems = new HashSet<>();
+ for(File f : new File(repoLocation, "items").listFiles()) {
+ currentlyInstalledItems.add(f.getName().substring(0, f.getName().length()-5));
}
- } catch(Exception e) {}
- Set<String> currentlyInstalledItems = new HashSet<>();
- for(File f : new File(repoLocation, "items").listFiles()) {
- currentlyInstalledItems.add(f.getName().substring(0, f.getName().length()-5));
- }
-
- Set<String> removedItems;
- if(config.autoupdate.value) {
- removedItems = neuio.getRemovedItems(currentlyInstalledItems);
- } else {
- removedItems = new HashSet<>();
- }
- for(File f : new File(repoLocation, "items").listFiles()) {
- String internalname = f.getName().substring(0, f.getName().length()-5);
- if(!removedItems.contains(internalname)) {
- loadItem(internalname);
+ Set<String> removedItems;
+ if(config.autoupdate.value) {
+ removedItems = neuio.getRemovedItems(currentlyInstalledItems);
+ } else {
+ removedItems = new HashSet<>();
}
- }
+ for(File f : new File(repoLocation, "items").listFiles()) {
+ String internalname = f.getName().substring(0, f.getName().length()-5);
+ if(!removedItems.contains(internalname)) {
+ loadItem(internalname);
+ }
+ }
+ });
+
+ thread.start();
}
/**
@@ -1408,7 +1427,7 @@ public class NEUManager {
for(Map.Entry<String, JsonElement> entry : max.get("statNums").getAsJsonObject().entrySet()) {
int statMax = (int)Math.floor(entry.getValue().getAsFloat());
int statMin = (int)Math.floor(min.get("statNums").getAsJsonObject().get(entry.getKey()).getAsFloat());
- String statStr = "+"+statMin+"\u27A1"+statMax;
+ String statStr = (statMin>0?"+":"")+statMin+"\u27A1"+statMax;
replacements.put(entry.getKey(), statStr);
}
} else {
@@ -1426,7 +1445,7 @@ public class NEUManager {
float statMax = entry.getValue().getAsFloat();
float statMin = min.get("statNums").getAsJsonObject().get(entry.getKey()).getAsFloat();
float val = statMin*minMix + statMax*maxMix;
- String statStr = "+"+(int)Math.floor(val);
+ String statStr = (statMin>0?"+":"")+(int)Math.floor(val);
replacements.put(entry.getKey(), statStr);
}
}