diff options
author | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-07-29 12:21:03 +1000 |
---|---|---|
committer | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-07-29 12:21:03 +1000 |
commit | 6115246bf36fb98037ca0df2faac869eba7845c3 (patch) | |
tree | 4e659cf3d0bae323346ef251c995179c490c1115 /src/main/java/io | |
parent | 9716e11a253a370e16dd1f0bdfdd70ac21ee4102 (diff) | |
download | NotEnoughUpdates-6115246bf36fb98037ca0df2faac869eba7845c3.tar.gz NotEnoughUpdates-6115246bf36fb98037ca0df2faac869eba7845c3.tar.bz2 NotEnoughUpdates-6115246bf36fb98037ca0df2faac869eba7845c3.zip |
1.11.10.5
Diffstat (limited to 'src/main/java/io')
8 files changed, 375 insertions, 249 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); } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java index f5475123..9e4e851a 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java @@ -411,6 +411,7 @@ public class NotEnoughUpdates { return true; } catch(Exception ignored) {} + return true; } return false; } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java index 07c21304..ea6d2f9f 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java @@ -19,6 +19,8 @@ import net.minecraft.util.EnumChatFormatting; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class APIManager { @@ -142,23 +144,25 @@ public class APIManager { } public void tick() { + if(manager.config.disableAH.value || manager.config.apiKey.value == null || manager.config.apiKey.value.isEmpty()) return; + customAH.tick(); long currentTime = System.currentTimeMillis(); if(currentTime - lastPageUpdate > 5*1000) { - lastPageUpdate = System.currentTimeMillis(); + lastPageUpdate = currentTime; updatePageTick(); ahNotification(); } - if(currentTime - lastProfileUpdate > 10*1000) { + /*if(currentTime - lastProfileUpdate > 10*1000) { lastProfileUpdate = System.currentTimeMillis(); updateProfiles(Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")); - } + }*/ if(currentTime - lastCleanup > 120*1000) { - lastCleanup = System.currentTimeMillis(); + lastCleanup = currentTime; cleanup(); } if(currentTime - lastCustomAHSearch > 60*1000) { - lastCustomAHSearch = System.currentTimeMillis(); + lastCustomAHSearch = currentTime; if(Minecraft.getMinecraft().currentScreen instanceof CustomAHGui || customAH.isRenderOverAuctionView()) { customAH.updateSearch(); calculateStats(); @@ -263,38 +267,41 @@ public class APIManager { } } + private ExecutorService es = Executors.newSingleThreadExecutor(); private void cleanup() { - try { - long currTime = System.currentTimeMillis(); - Set<String> toRemove = new HashSet<>(); - for(Map.Entry<String, Auction> entry : auctionMap.entrySet()) { - long timeToEnd = entry.getValue().end - currTime; - if(timeToEnd < -60) { - toRemove.add(entry.getKey()); - } else if(currTime - entry.getValue().lastUpdate > 5*60*1000) { - toRemove.add(entry.getKey()); + es.submit(() -> { + try { + long currTime = System.currentTimeMillis(); + Set<String> toRemove = new HashSet<>(); + for(Map.Entry<String, Auction> entry : auctionMap.entrySet()) { + long timeToEnd = entry.getValue().end - currTime; + if(timeToEnd < -60) { + toRemove.add(entry.getKey()); + } else if(currTime - entry.getValue().lastUpdate > 5*60*1000) { + toRemove.add(entry.getKey()); + } } - } - toRemove.removeAll(playerBids); - for(String aucid : toRemove) { - auctionMap.remove(aucid); - } - for(HashMap<Integer, HashSet<String>> extrasMap : extrasToAucIdMap.values()) { - for(HashSet<String> aucids : extrasMap.values()) { - for(String aucid : toRemove) { - aucids.remove(aucid); + toRemove.removeAll(playerBids); + for(String aucid : toRemove) { + auctionMap.remove(aucid); + } + for(HashMap<Integer, HashSet<String>> extrasMap : extrasToAucIdMap.values()) { + for(HashSet<String> aucids : extrasMap.values()) { + for(String aucid : toRemove) { + aucids.remove(aucid); + } } } + for(HashSet<String> aucids : internalnameToAucIdMap.values()) { + aucids.removeAll(toRemove); + } + for(TreeMap<Integer, String> lowestBINs : internalnameToLowestBIN.values()) { + lowestBINs.values().removeAll(toRemove); + } + } catch(ConcurrentModificationException e) { + lastCleanup = System.currentTimeMillis() - 110*1000; } - for(HashSet<String> aucids : internalnameToAucIdMap.values()) { - aucids.removeAll(toRemove); - } - for(TreeMap<Integer, String> lowestBINs : internalnameToLowestBIN.values()) { - lowestBINs.values().removeAll(toRemove); - } - } catch(ConcurrentModificationException e) { - cleanup(); - } + }); } private void updatePageTick() { diff --git a/src/main/java/io/github/moulberry/notenoughupdates/auction/CustomAH.java b/src/main/java/io/github/moulberry/notenoughupdates/auction/CustomAH.java index 2bbabd64..f7409c97 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/auction/CustomAH.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/auction/CustomAH.java @@ -34,6 +34,8 @@ import java.awt.datatransfer.StringSelection; import java.text.NumberFormat; import java.util.*; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1038,109 +1040,107 @@ public class CustomAH extends Gui { return matches; } + private ExecutorService es = Executors.newSingleThreadExecutor(); public void updateSearch() { if(searchField == null || priceField == null) init(); + long currentTime = System.currentTimeMillis(); - /*if(searchField.getText().length() > 0 && searchField.getText().length() <= 2 && - System.currentTimeMillis() - lastSearchFieldUpdate < 200) { - shouldUpdateSearch = true; - return; - }*/ - - if(System.currentTimeMillis() - lastUpdateSearch < 500) { - shouldUpdateSearch = true; - return; - } + es.submit(() -> { + if(currentTime - lastUpdateSearch < 500) { + shouldUpdateSearch = true; + return; + } - lastUpdateSearch = System.currentTimeMillis(); - shouldUpdateSearch = false; + lastUpdateSearch = currentTime; + shouldUpdateSearch = false; - scrollAmount = 0; - try { - auctionIds.clear(); - if(filterMyAuctions) { - for(String aucid : manager.auctionManager.getPlayerBids()) { - APIManager.Auction auc = manager.auctionManager.getAuctionItems().get(aucid); - if(doesAucMatch(auc)) { - auctionIds.add(aucid); - } - } - } else if(searchField.getText().length() == 0) { - for(Map.Entry<String, APIManager.Auction> entry : manager.auctionManager.getAuctionItems().entrySet()) { - if(doesAucMatch(entry.getValue())) { - auctionIds.add(entry.getKey()); + scrollAmount = 0; + try { + auctionIds.clear(); + if(filterMyAuctions) { + for(String aucid : manager.auctionManager.getPlayerBids()) { + APIManager.Auction auc = manager.auctionManager.getAuctionItems().get(aucid); + if(doesAucMatch(auc)) { + auctionIds.add(aucid); + } } - } - } else { - String query = searchField.getText(); - Set<String> dontMatch = new HashSet<>(); - - HashSet<String> allMatch = new HashSet<>(); - if(query.contains("!")) { //only used for inverted queries, so dont need to populate unless ! in query + } else if(searchField.getText().length() == 0) { for(Map.Entry<String, APIManager.Auction> entry : manager.auctionManager.getAuctionItems().entrySet()) { if(doesAucMatch(entry.getValue())) { - allMatch.add(entry.getKey()); - } else { - dontMatch.add(entry.getKey()); + auctionIds.add(entry.getKey()); } } - } - - boolean invert = false; - - StringBuilder query2 = new StringBuilder(); - char lastOp = '|'; - for(char c : query.toCharArray()) { - if(query2.toString().trim().isEmpty() && c == '!') { - invert = true; - } else if(c == '|' || c == '&') { - if(lastOp == '|') { - HashSet<String> result = search(query2.toString(), dontMatch); - if(!invert) { - auctionIds.addAll(result); - } else { - HashSet<String> allClone = (HashSet<String>) allMatch.clone(); - allClone.removeAll(result); - auctionIds.addAll(allClone); - } - } else if(lastOp == '&') { - HashSet<String> result = search(query2.toString(), dontMatch); - if(!invert) { - auctionIds.retainAll(result); + } else { + String query = searchField.getText(); + Set<String> dontMatch = new HashSet<>(); + + HashSet<String> allMatch = new HashSet<>(); + if(query.contains("!")) { //only used for inverted queries, so dont need to populate unless ! in query + for(Map.Entry<String, APIManager.Auction> entry : manager.auctionManager.getAuctionItems().entrySet()) { + if(doesAucMatch(entry.getValue())) { + allMatch.add(entry.getKey()); } else { - auctionIds.removeAll(result); + dontMatch.add(entry.getKey()); } } - - query2 = new StringBuilder(); - invert = false; - lastOp = c; - } else { - query2.append(c); } - } - if(lastOp == '|') { - HashSet<String> result = search(query2.toString(), dontMatch); - if(!invert) { - auctionIds.addAll(result); - } else { - HashSet<String> allClone = (HashSet<String>) allMatch.clone(); - allClone.removeAll(result); - auctionIds.addAll(allClone); + + boolean invert = false; + + StringBuilder query2 = new StringBuilder(); + char lastOp = '|'; + for(char c : query.toCharArray()) { + if(query2.toString().trim().isEmpty() && c == '!') { + invert = true; + } else if(c == '|' || c == '&') { + if(lastOp == '|') { + HashSet<String> result = search(query2.toString(), dontMatch); + if(!invert) { + auctionIds.addAll(result); + } else { + HashSet<String> allClone = (HashSet<String>) allMatch.clone(); + allClone.removeAll(result); + auctionIds.addAll(allClone); + } + } else if(lastOp == '&') { + HashSet<String> result = search(query2.toString(), dontMatch); + if(!invert) { + auctionIds.retainAll(result); + } else { + auctionIds.removeAll(result); + } + } + + query2 = new StringBuilder(); + invert = false; + lastOp = c; + } else { + query2.append(c); + } } - } else if(lastOp == '&') { - HashSet<String> result = search(query2.toString(), dontMatch); - if(!invert) { - auctionIds.retainAll(result); - } else { - auctionIds.removeAll(result); + if(lastOp == '|') { + HashSet<String> result = search(query2.toString(), dontMatch); + if(!invert) { + auctionIds.addAll(result); + } else { + HashSet<String> allClone = (HashSet<String>) allMatch.clone(); + allClone.removeAll(result); + auctionIds.addAll(allClone); + } + } else if(lastOp == '&') { + HashSet<String> result = search(query2.toString(), dontMatch); + if(!invert) { + auctionIds.retainAll(result); + } else { + auctionIds.removeAll(result); + } } } + sortItems(); + } catch(Exception e) { + shouldUpdateSearch = true; } - sortItems(); - } catch(Exception e) { - shouldUpdateSearch = true; - } + }); } public void sortItems() throws ConcurrentModificationException { diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/Options.java b/src/main/java/io/github/moulberry/notenoughupdates/options/Options.java index 0de6cafc..8fee7b6e 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/Options.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/Options.java @@ -93,6 +93,11 @@ public class Options { "Api Key", false, "Type /api new to receive key and put it here."); + public Option<Boolean> disableAH = new Option( + true, + "Disable AH", + false, + "Disables all AH-related features. Reduces network usage and may improve performance"); public Option<Boolean> autoupdate = new Option( true, "Automatically Update Items", @@ -292,6 +297,7 @@ public class Options { tryAddOption(tooltipBorderColours, options); tryAddOption(hideApiKey, options); tryAddOption(streamerMode, options); + tryAddOption(disableAH, options); tryAddOption(quickAHUpdate, options); tryAddOption(autoupdate, options); tryAddOption(cacheRenderedItempane, options); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/GuiProfileViewer.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/GuiProfileViewer.java index 295bce12..8224bb6d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/GuiProfileViewer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/GuiProfileViewer.java @@ -337,19 +337,17 @@ public class GuiProfileViewer extends GuiScreen { } break; case PETS: - JsonObject petsInfo = profile.getPetsInfo(profileId); - if(petsInfo == null) break; - JsonArray pets = petsInfo.get("pets").getAsJsonArray(); - for(int i=petsPage*20; i<Math.min(petsPage*20+20, pets.size()); i++) { - int xIndex = i % COLLS_XCOUNT; - int yIndex = i / COLLS_XCOUNT; + if(sortedPets == null) break; + for(int i=petsPage*20; i<Math.min(petsPage*20+20, sortedPets.size()); i++) { + int xIndex = (i%20) % COLLS_XCOUNT; + int yIndex = (i%20) / COLLS_XCOUNT; float x = 5 + COLLS_XPADDING + (COLLS_XPADDING + 20) * xIndex; float y = 7 + COLLS_YPADDING + (COLLS_YPADDING + 20) * yIndex; if(mouseX > guiLeft+x && mouseX < guiLeft+x+20) { if(mouseY > guiTop+y && mouseY < guiTop+y+20) { - selectedPet = pets.get(i).getAsJsonObject(); + selectedPet = i; return; } } @@ -447,6 +445,9 @@ public class GuiProfileViewer extends GuiScreen { break; case COLS: mouseReleasedCols(mouseX, mouseY, mouseButton); + break; + case PETS: + mouseReleasedPets(mouseX, mouseY, mouseButton); } } @@ -500,6 +501,22 @@ public class GuiProfileViewer extends GuiScreen { Utils.playPressSound(); } + private void mouseReleasedPets(int mouseX, int mouseY, int mouseButton) { + if(mouseY > guiTop+6 && mouseY < guiTop+22) { + if(mouseX > guiLeft+100-15-12 && mouseX < guiLeft+100-20) { + if(petsPage > 0) { + petsPage--; + } + return; + } else if(mouseX > guiLeft+100+15 && mouseX < guiLeft+100+20+12) { + if(sortedPets != null && petsPage < Math.ceil(sortedPets.size()/25f)-1) { + petsPage++; + } + return; + } + } + } + private void mouseReleasedInvs(int mouseX, int mouseY, int mouseButton) { if(mouseButton == 0) { int i=0; @@ -568,31 +585,45 @@ public class GuiProfileViewer extends GuiScreen { } } - public int getLevel(JsonArray levels, int offset, float exp) { + private class Level { + float level; + float currentLevelRequirement; + } + + public Level getLevel(JsonArray levels, int offset, float exp) { float xpTotal = 0; - int level = 1; + float level = 1; + float currentLevelRequirement = 0; + float remainingToNextLevel = 0; for(int i=offset; i<offset+99; i++) { - xpTotal += levels.get(i).getAsFloat(); + currentLevelRequirement = levels.get(i).getAsFloat(); + xpTotal += currentLevelRequirement; if(xpTotal > exp) { + remainingToNextLevel = (xpTotal-currentLevelRequirement+exp)/currentLevelRequirement; break; } else { level += 1; } } + level += remainingToNextLevel; if(level <= 0) { level = 1; } else if(level > 100) { level = 100; } - return level; + Level levelObj = new Level(); + levelObj.level = level; + levelObj.currentLevelRequirement = currentLevelRequirement; + return levelObj; } - private JsonObject selectedPet = null; + private int selectedPet = -1; private int petsPage = 0; private List<JsonObject> sortedPets = null; + private List<ItemStack> sortedPetsStack = null; private static HashMap<String, String> minionRarityToNumMap = new HashMap<>(); static { minionRarityToNumMap.put("COMMON", "0"); @@ -624,20 +655,10 @@ public class GuiProfileViewer extends GuiScreen { } } - Panorama.drawPanorama(-backgroundRotation, guiLeft+212, guiTop+44, 81, 108, -0.37f, 0.6f, - getPanoramasForLocation(location==null?"dynamic":location, panoramaIdentifier)); - - Minecraft.getMinecraft().getTextureManager().bindTexture(pv_pets); - Utils.drawTexturedRect(guiLeft, guiTop, sizeX, sizeY, GL11.GL_NEAREST); - - Utils.drawStringCentered(EnumChatFormatting.DARK_PURPLE+"Pets", Minecraft.getMinecraft().fontRendererObj, - guiLeft+100, guiTop+14, true, 4210752); - GlStateManager.color(1, 1, 1, 1); - JsonArray pets = petsInfo.get("pets").getAsJsonArray(); - if(sortedPets == null) { sortedPets = new ArrayList<>(); + sortedPetsStack = new ArrayList<>(); for(int i=0; i<pets.size(); i++) { sortedPets.add(pets.get(i).getAsJsonObject()); } @@ -658,11 +679,7 @@ public class GuiProfileViewer extends GuiScreen { return (int)(exp2 - exp1); } }); - } - - for(int i=petsPage*20; i<Math.min(petsPage*20+20, sortedPets.size()); i++) { - JsonObject pet = sortedPets.get(i); - if(pet != null) { + for(JsonObject pet : sortedPets) { String petname = pet.get("type").getAsString(); String tier = pet.get("tier").getAsString(); String tierNum = minionRarityToNumMap.get(tier); @@ -672,11 +689,15 @@ public class GuiProfileViewer extends GuiScreen { int petRarityOffset = petsJson.get("pet_rarity_offset").getAsJsonObject().get(tier).getAsInt(); JsonArray levelsArr = petsJson.get("pet_levels").getAsJsonArray(); - int level = getLevel(levelsArr, petRarityOffset, exp); + Level levelObj = getLevel(levelsArr, petRarityOffset, exp); + float level = levelObj.level; + float currentLevelRequirement = levelObj.currentLevelRequirement; + pet.addProperty("level", level); + pet.addProperty("currentLevelRequirement", currentLevelRequirement); JsonObject petItem = NotEnoughUpdates.INSTANCE.manager.getItemInformation().get(petname+";"+tierNum); ItemStack stack = NotEnoughUpdates.INSTANCE.manager.jsonToStack(petItem, false, false); - HashMap<String, String> replacements = NotEnoughUpdates.INSTANCE.manager.getLoreReplacements(petname, tier, level); + HashMap<String, String> replacements = NotEnoughUpdates.INSTANCE.manager.getLoreReplacements(petname, tier, (int)Math.floor(level)); NBTTagCompound tag = stack.getTagCompound()==null?new NBTTagCompound():stack.getTagCompound(); if(tag.hasKey("display", 10)) { @@ -704,14 +725,65 @@ public class GuiProfileViewer extends GuiScreen { } stack.setTagCompound(tag); - int xIndex = i % COLLS_XCOUNT; - int yIndex = i / COLLS_XCOUNT; + sortedPetsStack.add(stack); + } + } + + Panorama.drawPanorama(-backgroundRotation, guiLeft+212, guiTop+44, 81, 108, -0.37f, 0.6f, + getPanoramasForLocation(location==null?"dynamic":location, panoramaIdentifier)); + + Minecraft.getMinecraft().getTextureManager().bindTexture(pv_pets); + Utils.drawTexturedRect(guiLeft, guiTop, sizeX, sizeY, GL11.GL_NEAREST); + + Utils.drawStringCentered(EnumChatFormatting.DARK_PURPLE+"Pets", Minecraft.getMinecraft().fontRendererObj, + guiLeft+100, guiTop+14, true, 4210752); + GlStateManager.color(1, 1, 1, 1); + + JsonElement activePetElement = petsInfo.get("active_pet"); + if(selectedPet == -1 && activePetElement != null && activePetElement.isJsonObject()) { + JsonObject active = activePetElement.getAsJsonObject(); + for(int i=0; i<sortedPets.size(); i++) { + if(sortedPets.get(i) == active) { + selectedPet = i; + break; + } + } + } + + boolean leftHovered = false; + boolean rightHovered = false; + if(Mouse.isButtonDown(0)) { + if(mouseY > guiTop+6 && mouseY < guiTop+22) { + if(mouseX > guiLeft+100-20-12 && mouseX < guiLeft+100-20) { + leftHovered = true; + } else if(mouseX > guiLeft+100+20 && mouseX < guiLeft+100+20+12) { + rightHovered = true; + } + } + } + Minecraft.getMinecraft().getTextureManager().bindTexture(resource_packs); + + if(petsPage > 0) { + Utils.drawTexturedRect(guiLeft+100-15-12, guiTop+6, 12, 16, + 29/256f, 53/256f, !leftHovered?0:32/256f, !leftHovered?32/256f:64/256f, GL11.GL_NEAREST); + } + if(petsPage < Math.ceil(pets.size()/25f)-1) { + Utils.drawTexturedRect( guiLeft+100+15, guiTop+6, 12, 16, + 5/256f, 29/256f, !rightHovered?0:32/256f, !rightHovered?32/256f:64/256f, GL11.GL_NEAREST); + } + + for(int i=petsPage*20; i<Math.min(petsPage*20+20, sortedPets.size()); i++) { + JsonObject pet = sortedPets.get(i); + ItemStack stack = sortedPetsStack.get(i); + if(pet != null) { + int xIndex = (i%20) % COLLS_XCOUNT; + int yIndex = (i%20) / COLLS_XCOUNT; float x = 5 + COLLS_XPADDING + (COLLS_XPADDING + 20) * xIndex; float y = 7 + COLLS_YPADDING + (COLLS_YPADDING + 20) * yIndex; Minecraft.getMinecraft().getTextureManager().bindTexture(pv_elements); - if(pet == selectedPet) { + if(i == selectedPet) { GlStateManager.color(1, 185/255f, 0, 1); Utils.drawTexturedRect(guiLeft+x, guiTop+y, 20, 20, 0, 20/256f, 0, 20/256f, GL11.GL_NEAREST); @@ -731,15 +803,27 @@ public class GuiProfileViewer extends GuiScreen { } } - if(selectedPet != null) { - String type = selectedPet.get("type").getAsString(); + if(selectedPet >= 0) { + ItemStack petStack = sortedPetsStack.get(selectedPet); + String display = petStack.getDisplayName(); + JsonObject pet = sortedPets.get(selectedPet); + String type = pet.get("type").getAsString(); for(int i=0; i<4; i++) { JsonObject item = NotEnoughUpdates.INSTANCE.manager.getItemInformation().get(type+";"+i); if(item != null) { int x = guiLeft+280; float y = guiTop+67+15*(float)Math.sin(((currentTime-startTime)/800f)%(2*Math.PI)); + + int displayLen = Minecraft.getMinecraft().fontRendererObj.getStringWidth(display); + int halfDisplayLen = displayLen/2; + GlStateManager.translate(x, y, 0); + + drawRect(-halfDisplayLen-1-28, -1, halfDisplayLen+1-28, 8, new Color(0, 0, 0, 100).getRGB()); + + Minecraft.getMinecraft().fontRendererObj.drawString(display, -halfDisplayLen-28, 0, 0, true); + ItemStack stack = NotEnoughUpdates.INSTANCE.manager.jsonToStack(item); GlStateManager.scale(-3.5f, 3.5f, 1); GlStateManager.enableDepth(); @@ -749,7 +833,19 @@ public class GuiProfileViewer extends GuiScreen { break; } } + + float level = pet.get("level").getAsFloat(); + float currentLevelRequirement = pet.get("currentLevelRequirement").getAsFloat(); + + Utils.drawStringCenteredScaledMaxWidth(display, Minecraft.getMinecraft().fontRendererObj, guiLeft+368, guiTop+28+4, true, 98, 0); + //renderAlignedString(display, EnumChatFormatting.YELLOW+"[LVL "+Math.floor(level)+"]", guiLeft+319, guiTop+28, 98); + renderBar(guiLeft+319, guiTop+38, 98, (float)Math.floor(level)/100f); + + renderAlignedString("To Next LVL", EnumChatFormatting.WHITE.toString()+(int)(level%1*100)+"%", guiLeft+319, guiTop+46, 98); + + renderBar(guiLeft+319, guiTop+56, 98, level%1); } + } private String[] romans = new String[]{"I","II","III","IV","V","VI","VII","VIII","IX","X","XI", diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java index d463f87e..a5b474c6 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java @@ -330,7 +330,10 @@ public class ProfileViewer { JsonObject member = members.get(uuid).getAsJsonObject(); if(member.has("coop_invitation")) { - continue; + JsonObject coop_invitation = member.get("coop_invitation").getAsJsonObject(); + if(!coop_invitation.get("confirmed").getAsBoolean()) { + continue; + } } String cute_name = profile.get("cute_name").getAsString(); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java index f0a4853f..2769e18e 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java @@ -2,6 +2,7 @@ package io.github.moulberry.notenoughupdates.util; import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.apache.commons.io.IOUtils; import java.io.BufferedReader; import java.io.IOException; @@ -24,7 +25,7 @@ public class HypixelApi { */ private Gson gson = new Gson(); - private ExecutorService es = Executors.newCachedThreadPool(); + private ExecutorService es = Executors.newFixedThreadPool(3); public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer) { getHypixelApiAsync(apiKey, method, args, consumer, () -> {}); @@ -49,17 +50,10 @@ public class HypixelApi { URLConnection connection = url.openConnection(); connection.setConnectTimeout(3000); - try(BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { - StringBuilder builder = new StringBuilder(); - int codePoint; - while((codePoint = reader.read()) != -1) { - builder.append(((char)codePoint)); - } - String response = builder.toString(); + String response = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8); - JsonObject json = gson.fromJson(response, JsonObject.class); - return json; - } + JsonObject json = gson.fromJson(response, JsonObject.class); + return json; } public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) { |