diff options
author | Soopyboo32 <49228220+Soopyboo32@users.noreply.github.com> | 2022-10-21 15:51:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 09:51:44 +0200 |
commit | 44fad457f0a066b0378b3cb615fd389d8d2a0850 (patch) | |
tree | cdf1c1efb35432e52c4448ed23152c50051148ed | |
parent | 280a89cdbf086e53bcd6985e1ddd48b6aa718087 (diff) | |
download | NotEnoughUpdates-44fad457f0a066b0378b3cb615fd389d8d2a0850.tar.gz NotEnoughUpdates-44fad457f0a066b0378b3cb615fd389d8d2a0850.tar.bz2 NotEnoughUpdates-44fad457f0a066b0378b3cb615fd389d8d2a0850.zip |
Soopy networth option in pv (#303)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
5 files changed, 235 insertions, 7 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/commands/profile/PeekCommand.java b/src/main/java/io/github/moulberry/notenoughupdates/commands/profile/PeekCommand.java index c1c184f6..77377917 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/commands/profile/PeekCommand.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/commands/profile/PeekCommand.java @@ -39,6 +39,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -72,7 +73,7 @@ public class PeekCommand extends ClientCommandBase { } else { profile.resetCache(); - if (peekCommandExecutorService == null) { + if (peekCommandExecutorService == null || peekCommandExecutorService.isTerminated()) { peekCommandExecutorService = Executors.newSingleThreadScheduledExecutor(); } @@ -105,13 +106,32 @@ public class PeekCommand extends ClientCommandBase { boolean isMe = name.equalsIgnoreCase("moulberry"); PlayerStats.Stats stats = profile.getStats(null); - if (stats == null) return; + if (stats == null) { + peekScheduledFuture = peekCommandExecutorService.schedule(this, 200, TimeUnit.MILLISECONDS); + return; + } Map<String, ProfileViewer.Level> skyblockInfo = profile.getSkyblockInfo(null); + if (NotEnoughUpdates.INSTANCE.config.profileViewer.useSoopyNetworth) { + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessageWithOptionalDeletion(new ChatComponentText( + EnumChatFormatting.YELLOW + "[PEEK] Getting the player's Skyblock networth..."), id); + + CountDownLatch countDownLatch = new CountDownLatch(1); + + profile.getSoopyNetworth(null, () -> countDownLatch.countDown()); + + try { //Wait for async network request + countDownLatch.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + //Now it's waited for network request the data should be cached (accessed in nw section) + } + Minecraft.getMinecraft().ingameGUI .getChatGUI() .printChatMessageWithOptionalDeletion(new ChatComponentText(EnumChatFormatting.GREEN + " " + - EnumChatFormatting.STRIKETHROUGH + "-=-" + EnumChatFormatting.RESET + EnumChatFormatting.GREEN + " " + + EnumChatFormatting.STRIKETHROUGH + "-=-" + EnumChatFormatting.RESET + EnumChatFormatting.GREEN + + " " + Utils.getElementAsString(profile.getHypixelProfile().get("displayname"), name) + "'s Info " + EnumChatFormatting.STRIKETHROUGH + "-=-"), id); @@ -195,7 +215,7 @@ public class PeekCommand extends ClientCommandBase { Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( g + "Slayer: " + zombiePrefix + (int) Math.floor(zombie) + g + "-" + spiderPrefix + (int) Math.floor(spider) + g + "-" + - wolfPrefix + (int) Math.floor(wolf) + g+ "-" + + wolfPrefix + (int) Math.floor(wolf) + g + "-" + endermanPrefix + (int) Math.floor(enderman) + g + "-" + blazePrefix + (int) Math.floor(blaze))); } @@ -238,7 +258,18 @@ public class PeekCommand extends ClientCommandBase { float bankBalance = Utils.getElementAsFloat(Utils.getElement(profileInfo, "banking.balance"), -1); float purseBalance = Utils.getElementAsFloat(Utils.getElement(profileInfo, "coin_purse"), 0); - long networth = profile.getNetWorth(null); + long networth; + if (NotEnoughUpdates.INSTANCE.config.profileViewer.useSoopyNetworth) { + ProfileViewer.Profile.SoopyNetworthData nwData = profile.getSoopyNetworth(null, () -> {}); + if (nwData == null) { + networth = -2l; + } else { + networth = nwData.getTotal(); + } + } else { + networth = profile.getNetWorth(null); + } + float money = Math.max(bankBalance + purseBalance, networth); EnumChatFormatting moneyPrefix = money > 50 * 1000 * 1000 ? (money > 200 * 1000 * 1000 diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/ProfileViewer.java b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/ProfileViewer.java index 673e1015..951438fb 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/ProfileViewer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/ProfileViewer.java @@ -94,4 +94,12 @@ public class ProfileViewer { ) @ConfigEditorBoolean public boolean showPronounsInPv = BuildFlags.ENABLE_PRONOUNS_IN_PV_BY_DEFAULT; + + @Expose + @ConfigOption( + name = "Use Soopy Networth", + desc = "Replaces NEU networth with Soopy networth in /pv and /peek" + ) + @ConfigEditorBoolean + public boolean useSoopyNetworth = true; } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java index 806b1bff..a0a78938 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java @@ -49,6 +49,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.ResourceLocation; +import org.apache.commons.lang3.text.WordUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -225,7 +226,31 @@ public class BasicPage extends GuiProfileViewerPage { } } - long networth = profile.getNetWorth(profileId); + long networth; + ArrayList<String> nwCategoryHover = new ArrayList<>(); + if (NotEnoughUpdates.INSTANCE.config.profileViewer.useSoopyNetworth) { + ProfileViewer.Profile.SoopyNetworthData nwData = profile.getSoopyNetworth(profileId, () -> {}); + if (nwData == null) { + networth = -2l; + } else { + networth = nwData.getTotal(); + + for (String category : nwData.getCategories()) { + if (nwData.getCategory(category) == 0) continue; + + nwCategoryHover.add(EnumChatFormatting.GREEN + + WordUtils.capitalizeFully(category.replace("_", " ")) + + ": " + + EnumChatFormatting.GOLD + + GuiProfileViewer.numberFormat.format(nwData.getCategory(category))); + } + + nwCategoryHover.add(""); + } + } else { + networth = profile.getNetWorth(profileId); + } + if (networth > 0) { Utils.drawStringCentered( EnumChatFormatting.GREEN + "Net Worth: " + EnumChatFormatting.GOLD + @@ -245,7 +270,7 @@ public class BasicPage extends GuiProfileViewerPage { .get("avg_buy") .getAsDouble() ); - String networthIRLMoney = Long.toString(Math.round(((networthInCookies * 325) / 675) * 4.99)); + String networthIRLMoney = GuiProfileViewer.numberFormat.format(Math.round(((networthInCookies * 325) / 675) * 4.99)); if ( mouseX > guiLeft + 8 && mouseX < guiLeft + 8 + fr.getStringWidth("Net Worth: " + GuiProfileViewer.numberFormat.format(networth)) @@ -262,7 +287,9 @@ public class BasicPage extends GuiProfileViewerPage { networthIRLMoney ); getInstance().tooltipToDisplay.add(""); + if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) { + getInstance().tooltipToDisplay.addAll(nwCategoryHover); getInstance().tooltipToDisplay.add(EnumChatFormatting.RED + "This is calculated using the current"); getInstance().tooltipToDisplay.add( EnumChatFormatting.RED + "price of booster cookies on bazaar and the price"); @@ -282,7 +309,26 @@ public class BasicPage extends GuiProfileViewerPage { } } } catch (Exception ignored) { + ignored.printStackTrace(); } + } else { + //Networth is under 0 + //If = -1 -> an error occured + //If = -2 -> still loading networth + + String stateStr = EnumChatFormatting.RED + "An error occured"; + if (networth == -2) { + stateStr = EnumChatFormatting.YELLOW + "Loading..."; + } + + Utils.drawStringCentered( + EnumChatFormatting.GREEN + "Net Worth: " + stateStr, + fr, + guiLeft + 63, + guiTop + 38, + true, + 0 + ); } if (status != null) { 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 a43e2b98..b32d3648 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java @@ -43,6 +43,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -646,9 +647,11 @@ public class ProfileViewer { private final HashMap<String, PlayerStats.Stats> stats = new HashMap<>(); private final HashMap<String, PlayerStats.Stats> passiveStats = new HashMap<>(); private final HashMap<String, Long> networth = new HashMap<>(); + private final HashMap<String, SoopyNetworthData> soopyNetworth = new HashMap<>(); private final AtomicBoolean updatingSkyblockProfilesState = new AtomicBoolean(false); private final AtomicBoolean updatingGuildInfoState = new AtomicBoolean(false); private final AtomicBoolean updatingPlayerStatusState = new AtomicBoolean(false); + private final AtomicBoolean updatingSoopyNetworth = new AtomicBoolean(false); private final AtomicBoolean updatingBingoInfo = new AtomicBoolean(false); private final Pattern COLL_TIER_PATTERN = Pattern.compile("_(-?\\d+)"); private String latestProfile = null; @@ -717,6 +720,125 @@ public class ProfileViewer { return bingoInformation != null ? bingoInformation : null; } + public class SoopyNetworthData { + private HashMap<String, Long> categoryWorth; + private Long totalWorth; + private String[] keys; + + SoopyNetworthData(JsonObject nwData) { + categoryWorth = new HashMap<>(); + + if (nwData == null || nwData.isJsonNull()) { + totalWorth = -1l; + keys = new String[0]; + return; + } + if (nwData.get("total").isJsonNull()) { + totalWorth = -1l; + keys = new String[0]; + return; + } + + totalWorth = nwData.get("total").getAsLong(); + for (Map.Entry<String, JsonElement> entry : nwData.get("categories").getAsJsonObject().entrySet()) { + if (entry.getValue().isJsonNull()) { + continue; + } + categoryWorth.put(entry.getKey(), entry.getValue().getAsLong()); + } + + //Sort keys based on category value + keys = categoryWorth.keySet().stream().sorted(Comparator.comparingLong(k->getCategory((String) k)).reversed()).toArray(String[]::new); + } + + private SoopyNetworthData setLoading() { + totalWorth = -2l; + return this; + } + + public long getTotal() { + return totalWorth; + } + + public long getCategory(String name) { + if (categoryWorth.containsKey(name)) return categoryWorth.get(name); + return 0; + } + + public String[] getCategories() { + return keys; + } + } + + /** + * Returns SoopyNetworthData with total = -1 if error + * Returns null if still loading + */ + public SoopyNetworthData getSoopyNetworth(String profileName, Runnable callback) { + if (profileName == null) profileName = latestProfile; + if (soopyNetworth.get(profileName) != null) { + callback.run(); + return soopyNetworth.get(profileName); + } + + JsonArray playerInfo = getSkyblockProfiles(() -> {}); + if (playerInfo == null) return null; //Not sure how to support the callback in these cases + if (updatingSoopyNetworth.get()) return new SoopyNetworthData(null).setLoading(); //It shouldent really matter tho as these should never occur in /peek + updatingSoopyNetworth.set(true); + + manager.apiUtils + .request() + .url("https://soopy.dev/api/v2/player_networth/" + this.uuid) + .method("POST") + .postData("application/json", skyblockProfiles.toString()) + .requestJson() + .handle((jsonObject, throwable) -> { + if (throwable != null) throwable.printStackTrace(); + if (throwable != null || !jsonObject.has("success") || !jsonObject.get("success").getAsBoolean()) { + //Something went wrong + //Set profile networths to null to indicate that + for (int i = 0; i < skyblockProfiles.size(); i++) { + if (!skyblockProfiles.get(i).isJsonObject()) { + return null; + } + JsonObject profile = skyblockProfiles.get(i).getAsJsonObject(); + + String cuteName = profile.get("cute_name").getAsString(); + + soopyNetworth.put(cuteName, new SoopyNetworthData(null)); + } + updatingSoopyNetworth.set(false); + callback.run(); + return null; + } + + //Success, update networth data + for (int i = 0; i < skyblockProfiles.size(); i++) { + if (!skyblockProfiles.get(i).isJsonObject()) { + return null; + } + JsonObject profile = skyblockProfiles.get(i).getAsJsonObject(); + + String cuteName = profile.get("cute_name").getAsString(); + String profileId = profile.get("profile_id").getAsString(); + + SoopyNetworthData networth; + if (jsonObject.getAsJsonObject("data").get(profileId).isJsonNull()) { + networth = new SoopyNetworthData(null); + } else { + networth = new SoopyNetworthData(jsonObject.getAsJsonObject("data").get(profileId).getAsJsonObject()); + } + + soopyNetworth.put(cuteName, networth); + } + + updatingSoopyNetworth.set(false); + callback.run(); + return null; + }); + return null; + } + public long getNetWorth(String profileName) { if (profileName == null) profileName = latestProfile; if (networth.get(profileName) != null) return networth.get(profileName); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java index 8c594911..7818c3c0 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java @@ -33,6 +33,7 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -82,6 +83,8 @@ public class ApiUtil { private String baseUrl = null; private boolean shouldGunzip = false; private String method = "GET"; + private String postData = null; + private String postContentType = null; public Request method(String method) { this.method = method; @@ -108,6 +111,12 @@ public class ApiUtil { return this; } + public Request postData(String contentType, String data) { + this.postContentType = contentType; + this.postData = data; + return this; + } + private CompletableFuture<URL> buildUrl() { CompletableFuture<URL> fut = new CompletableFuture<>(); try { @@ -140,6 +149,18 @@ public class ApiUtil { conn.setConnectTimeout(10000); conn.setReadTimeout(10000); conn.setRequestProperty("User-Agent", USER_AGENT); + if (this.postContentType != null) { + conn.setRequestProperty("Content-Type", this.postContentType); + } + if (this.postData != null) { + conn.setDoOutput(true); + OutputStream os = conn.getOutputStream(); + try { + os.write(this.postData.getBytes("utf-8")); + } finally { + os.close(); + } + } inputStream = conn.getInputStream(); |