diff options
| author | BuildTools <james.jenour@protonmail.com> | 2021-06-04 01:18:28 +0800 |
|---|---|---|
| committer | BuildTools <james.jenour@protonmail.com> | 2021-06-04 01:18:28 +0800 |
| commit | a1fa5a67caebf754a0fcc43168672823ede0db93 (patch) | |
| tree | c0d7d78dfffaa659835a59952cac6edd2592be87 /src/main/java/io/github/moulberry/notenoughupdates/util | |
| parent | 1b172089ce502803f7644611afd618ce00dcb860 (diff) | |
| download | notenoughupdates-a1fa5a67caebf754a0fcc43168672823ede0db93.tar.gz notenoughupdates-a1fa5a67caebf754a0fcc43168672823ede0db93.tar.bz2 notenoughupdates-a1fa5a67caebf754a0fcc43168672823ede0db93.zip | |
merge is pain
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util')
5 files changed, 121 insertions, 245 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Constants.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Constants.java index 485ca19c..f095fc64 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Constants.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Constants.java @@ -1,9 +1,28 @@ package io.github.moulberry.notenoughupdates.util; -import com.google.gson.JsonObject; +import com.google.gson.*; +import io.github.moulberry.notenoughupdates.collectionlog.CollectionConstant; + +import java.lang.reflect.Type; +import java.util.concurrent.locks.ReentrantLock; +import java.util.regex.Pattern; public class Constants { + private static class PatternSerializer implements JsonDeserializer<Pattern>, JsonSerializer<Pattern> { + @Override + public Pattern deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return Pattern.compile(json.getAsString()); + } + @Override + public JsonElement serialize(Pattern src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.pattern()); + } + } + + private static final Gson gson = new GsonBuilder().setPrettyPrinting() + .registerTypeAdapter(Pattern.class, new PatternSerializer()).create(); + public static JsonObject BONUSES; public static JsonObject DISABLE; public static JsonObject ENCHANTS; @@ -15,23 +34,29 @@ public class Constants { public static JsonObject ESSENCECOSTS; public static JsonObject FAIRYSOULS; public static JsonObject REFORGESTONES; + public static CollectionConstant COLLECTIONLOG; + + private static final ReentrantLock lock = new ReentrantLock(); public static void reload() { - BONUSES = Utils.getConstant("bonuses"); - DISABLE = Utils.getConstant("disable"); - ENCHANTS = Utils.getConstant("enchants"); - LEVELING = Utils.getConstant("leveling"); - MISC = Utils.getConstant("misc"); - PETNUMS = Utils.getConstant("petnums"); - PETS = Utils.getConstant("pets"); - PARENTS = Utils.getConstant("parents"); - ESSENCECOSTS = Utils.getConstant("essencecosts"); - FAIRYSOULS = Utils.getConstant("fairy_souls"); - REFORGESTONES = Utils.getConstant("reforgestones"); - } + try { + lock.lock(); - static { - reload(); + BONUSES = Utils.getConstant("bonuses", gson); + DISABLE = Utils.getConstant("disable", gson); + ENCHANTS = Utils.getConstant("enchants", gson); + LEVELING = Utils.getConstant("leveling", gson); + MISC = Utils.getConstant("misc", gson); + PETNUMS = Utils.getConstant("petnums", gson); + PETS = Utils.getConstant("pets", gson); + PARENTS = Utils.getConstant("parents", gson); + ESSENCECOSTS = Utils.getConstant("essencecosts", gson); + FAIRYSOULS = Utils.getConstant("fairy_souls", gson); + REFORGESTONES = Utils.getConstant("reforgestones", gson); + //COLLECTIONLOG = Utils.getConstant("collectionlog", gson, CollectionConstant.class); + } finally { + lock.unlock(); + } } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/SBAIntegration.java b/src/main/java/io/github/moulberry/notenoughupdates/util/SBAIntegration.java deleted file mode 100644 index 04f53bf3..00000000 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/SBAIntegration.java +++ /dev/null @@ -1,217 +0,0 @@ -package io.github.moulberry.notenoughupdates.util; - -import io.github.moulberry.notenoughupdates.util.Utils; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.gui.inventory.GuiContainer; -import net.minecraft.client.settings.KeyBinding; -import net.minecraft.item.ItemStack; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.List; - -public class SBAIntegration { - - private static boolean hasSBA = true; - private static Class<?> skyblockAddonsClass = null; - private static Method skyblockAddons_getInstance = null; - private static Method skyblockAddons_getUtils = null; - private static Class<?> backpackManagerClass = null; - private static Method backpackManager_getFromItem = null; - private static Class<?> backpackClass = null; - private static Method backpackClass_setX= null; - private static Method backpackClass_setY = null; - private static Class<?> utilsClass = null; - private static Method utils_setBackpackToPreview = null; - public static boolean setActiveBackpack(ItemStack stack, int mouseX, int mouseY) { - if(!hasSBA) return false; - try { - if(skyblockAddonsClass == null) { - skyblockAddonsClass = Class.forName("codes.biscuit.skyblockaddons.SkyblockAddons"); - } - if(skyblockAddons_getInstance == null) { - skyblockAddons_getInstance = skyblockAddonsClass.getDeclaredMethod("getInstance"); - } - if(skyblockAddons_getUtils == null) { - skyblockAddons_getUtils = skyblockAddonsClass.getDeclaredMethod("getUtils"); - } - if(backpackManagerClass == null) { - backpackManagerClass = Class.forName("codes.biscuit.skyblockaddons.features.backpacks.BackpackManager"); - } - if(backpackManager_getFromItem == null) { - backpackManager_getFromItem = backpackManagerClass.getDeclaredMethod("getFromItem", ItemStack.class); - } - if(backpackClass == null) { - try { backpackClass = Class.forName("codes.biscuit.skyblockaddons.features.backpacks.Backpack"); } catch(Exception ignored){} - } - if(backpackClass == null) { - backpackClass = Class.forName("codes.biscuit.skyblockaddons.features.backpacks.ContainerPreview"); - } - if(backpackClass_setX == null) { - backpackClass_setX = backpackClass.getDeclaredMethod("setX", int.class); - } - if(backpackClass_setY == null) { - backpackClass_setY = backpackClass.getDeclaredMethod("setY", int.class); - } - if(utilsClass == null) { - utilsClass = Class.forName("codes.biscuit.skyblockaddons.utils.Utils"); - } - if(utils_setBackpackToPreview == null) { - try { utils_setBackpackToPreview = utilsClass.getDeclaredMethod("setBackpackToPreview", backpackClass); } catch(Exception ignored){} - } - if(utils_setBackpackToPreview == null) { - utils_setBackpackToPreview = utilsClass.getDeclaredMethod("setContainerPreviewToRender", backpackClass);; - } - } catch(Exception e) { - e.printStackTrace(); - hasSBA = false; - return false; - } - try { - Object skyblockAddons = skyblockAddons_getInstance.invoke(null); - Object utils = skyblockAddons_getUtils.invoke(skyblockAddons); - if(stack == null) { - utils_setBackpackToPreview.invoke(utils, (Object) null); - } else { - Object backpack = backpackManager_getFromItem.invoke(null, stack); - backpackClass_setX.invoke(backpack, mouseX); - backpackClass_setY.invoke(backpack, mouseY); - utils_setBackpackToPreview.invoke(utils, backpack); - } - } catch(Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - private static Field guiContainerHook_freezeBackpack = null; - public static boolean isFreezeBackpack() { - if(!hasSBA) return false; - try { - if(guiContainerHookClass == null) { - guiContainerHookClass = Class.forName("codes.biscuit.skyblockaddons.asm.hooks.GuiContainerHook"); - } - if(guiContainerHook_freezeBackpack == null) { - guiContainerHook_freezeBackpack = guiContainerHookClass.getDeclaredField("freezeBackpack"); - guiContainerHook_freezeBackpack.setAccessible(true); - } - } catch(Exception e) { - e.printStackTrace(); - hasSBA = false; - return false; - } - try { - return (boolean) guiContainerHook_freezeBackpack.get(null); - } catch(Exception e) { - e.printStackTrace(); - return false; - } - } - - public static boolean setFreezeBackpack(boolean freezeBackpack) { - if(!hasSBA) return false; - try { - if(guiContainerHookClass == null) { - guiContainerHookClass = Class.forName("codes.biscuit.skyblockaddons.asm.hooks.GuiContainerHook"); - } - if(guiContainerHook_freezeBackpack == null) { - guiContainerHook_freezeBackpack = guiContainerHookClass.getDeclaredField("freezeBackpack"); - guiContainerHook_freezeBackpack.setAccessible(true); - } - } catch(Exception e) { - e.printStackTrace(); - hasSBA = false; - return false; - } - try { - guiContainerHook_freezeBackpack.set(null, freezeBackpack); - return true; - } catch(Exception e) { - e.printStackTrace(); - return false; - } - } - - private static Method guiContainerHook_keyTyped = null; - private static Method skyblockAddons_getFreezeBackpackKey = null; - public static boolean keyTyped(int keyCode) { - if(!hasSBA) return false; - try { - if(skyblockAddonsClass == null) { - skyblockAddonsClass = Class.forName("codes.biscuit.skyblockaddons.SkyblockAddons"); - } - if(skyblockAddons_getInstance == null) { - skyblockAddons_getInstance = skyblockAddonsClass.getDeclaredMethod("getInstance"); - } - if(skyblockAddons_getFreezeBackpackKey == null) { - skyblockAddons_getFreezeBackpackKey = skyblockAddonsClass.getDeclaredMethod("getFreezeBackpackKey"); - } - if(guiContainerHookClass == null) { - guiContainerHookClass = Class.forName("codes.biscuit.skyblockaddons.asm.hooks.GuiContainerHook"); - } - if(guiContainerHook_keyTyped == null) { - guiContainerHook_keyTyped = guiContainerHookClass.getDeclaredMethod("keyTyped", int.class); - } - } catch(Exception e) { - e.printStackTrace(); - hasSBA = false; - return false; - } - try { - Object skyblockAddons = skyblockAddons_getInstance.invoke(null); - if(!isFreezeBackpack() && ((KeyBinding)skyblockAddons_getFreezeBackpackKey.invoke(skyblockAddons)).getKeyCode() == keyCode) { - setFreezeBackpack(true); - } else { - guiContainerHook_keyTyped.invoke(null, keyCode); - } - } catch(Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - private static Class<?> guiContainerHookClass = null; - private static Method guiContainerHook_drawBackpacks = null; - public static boolean renderActiveBackpack(int mouseX, int mouseY, FontRenderer fontRendererObj) { - if(!hasSBA) return false; - try { - if(guiContainerHookClass == null) { - guiContainerHookClass = Class.forName("codes.biscuit.skyblockaddons.asm.hooks.GuiContainerHook"); - } - if(guiContainerHook_drawBackpacks == null) { - guiContainerHook_drawBackpacks = guiContainerHookClass.getDeclaredMethod("drawBackpacks", - GuiContainer.class, int.class, int.class, FontRenderer.class); - } - } catch(Exception e) { - e.printStackTrace(); - hasSBA = false; - return false; - } - try { - ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft()); - int width = scaledResolution.getScaledWidth(); - int height = scaledResolution.getScaledHeight(); - - if(Minecraft.getMinecraft().currentScreen instanceof GuiContainer) { - guiContainerHook_drawBackpacks.invoke(null, Minecraft.getMinecraft().currentScreen, mouseX, mouseY, fontRendererObj); - } else { - GuiContainer container = new GuiContainer(null) { - protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { - } - }; - container.setWorldAndResolution(Minecraft.getMinecraft(), width, height); - - guiContainerHook_drawBackpacks.invoke(null, container, mouseX, mouseY, fontRendererObj); - } - } catch(Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - -} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java b/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java index 73d61a1e..df981359 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java @@ -2,7 +2,6 @@ package io.github.moulberry.notenoughupdates.util; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; -import io.github.moulberry.notenoughupdates.miscfeatures.EnchantingSolvers; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiChest; import net.minecraft.client.network.NetworkPlayerInfo; @@ -17,7 +16,6 @@ import net.minecraftforge.client.event.GuiOpenEvent; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -115,7 +113,14 @@ public class SBInfo { return mode; } + private static final String profilePrefix = "\u00a7r\u00a7e\u00a7lProfile: \u00a7r\u00a7a"; + private static final String skillsPrefix = "\u00a7r\u00a7e\u00a7lSkills: \u00a7r\u00a7a"; + + private static final Pattern SKILL_LEVEL_PATTERN = Pattern.compile("([^0-9:]+) (\\d{1,2})"); + public void tick() { + isInDungeon = false; + long currentTime = System.currentTimeMillis(); if(Minecraft.getMinecraft().thePlayer != null && @@ -130,9 +135,17 @@ public class SBInfo { try { for(NetworkPlayerInfo info : Minecraft.getMinecraft().thePlayer.sendQueue.getPlayerInfoMap()) { String name = Minecraft.getMinecraft().ingameGUI.getTabList().getPlayerName(info); - final String profilePrefix = "\u00a7r\u00a7e\u00a7lProfile: \u00a7r\u00a7a"; if(name.startsWith(profilePrefix)) { currentProfile = Utils.cleanColour(name.substring(profilePrefix.length())); + } else if(name.startsWith(skillsPrefix)) { + String levelInfo = name.substring(skillsPrefix.length()).trim(); + Matcher matcher = SKILL_LEVEL_PATTERN.matcher(Utils.cleanColour(levelInfo).split(":")[0]); + if(matcher.find()) { + try { + int level = Integer.parseInt(matcher.group(2).trim()); + XPInformation.getInstance().updateLevel(matcher.group(1).toLowerCase().trim(), level); + } catch(Exception ignored) {} + } } } } catch(Exception e) { diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java index d77f3c9e..4c32c49c 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java @@ -1,10 +1,7 @@ package io.github.moulberry.notenoughupdates.util; import com.google.common.base.Splitter; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; +import com.google.gson.*; import com.mojang.authlib.Agent; import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService; import com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication; @@ -43,12 +40,16 @@ import org.lwjgl.util.glu.Project; import javax.swing.*; import java.awt.*; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.Proxy; import java.nio.FloatBuffer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.*; import java.util.List; @@ -848,14 +849,18 @@ public class Utils { drawHoveringText(textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font, true); } - public static JsonObject getConstant(String constant) { + public static JsonObject getConstant(String constant, Gson gson) { + return getConstant(constant, gson, JsonObject.class); + } + + public static <T> T getConstant(String constant, Gson gson, Class<T> clazz) { File repo = NotEnoughUpdates.INSTANCE.manager.repoLocation; if(repo.exists()) { File jsonFile = new File(repo, "constants/"+constant+".json"); - try { - return NotEnoughUpdates.INSTANCE.manager.getJsonFromFile(jsonFile); - } catch (Exception ignored) { - } + try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8))) { + T obj = gson.fromJson(reader, clazz); + return obj; + } catch(Exception e) { return null; } } return null; } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java b/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java index c3535ab4..cde93ddf 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java @@ -31,9 +31,13 @@ public class XPInformation { } private HashMap<String, SkillInfo> skillInfoMap = new HashMap<>(); + public HashMap<String, Float> updateWithPercentage = new HashMap<>(); + + public int correctionCounter = 0; private static Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults(); private static Pattern SKILL_PATTERN = Pattern.compile("\\+(\\d+(?:,\\d+)*(?:\\.\\d+)?) (.+) \\((\\d+(?:,\\d+)*(?:\\.\\d+)?)/(\\d+(?:,\\d+)*(?:\\.\\d+)?)\\)"); + private static Pattern SKILL_PATTERN_ALT = Pattern.compile("\\+(\\d+(?:,\\d+)*(?:\\.\\d+)?) (.+) \\((\\d\\d?(?:\\.\\d\\d?)?)%\\)"); public HashMap<String, SkillInfo> getSkillInfoMap() { return skillInfoMap; @@ -43,7 +47,7 @@ public class XPInformation { return skillInfoMap.get(skillName.toLowerCase()); } - @SubscribeEvent(priority = EventPriority.HIGHEST) + @SubscribeEvent(priority = EventPriority.HIGHEST, receiveCanceled = true) public void onChatReceived(ClientChatReceivedEvent event) { if(event.type == 2) { JsonObject leveling = Constants.LEVELING; @@ -78,8 +82,54 @@ public class XPInformation { } skillInfoMap.put(skillS.toLowerCase(), skillInfo); + return; + } + matcher = SKILL_PATTERN_ALT.matcher(component); + if(matcher.matches()) { + String skillS = matcher.group(2); + String xpPercentageS = matcher.group(3).replace(",",""); + + float xpPercentage = Float.parseFloat(xpPercentageS); + updateWithPercentage.put(skillS.toLowerCase(), xpPercentage); + } + } + } + } + + public void updateLevel(String skill, int level) { + if(updateWithPercentage.containsKey(skill)) { + JsonObject leveling = Constants.LEVELING; + if(leveling == null) return; + + SkillInfo skillInfo = new SkillInfo(); + skillInfo.totalXp = 0; + skillInfo.level = level; + + JsonArray levelingArray = leveling.getAsJsonArray("leveling_xp"); + for(int i=0; i<levelingArray.size(); i++) { + float cap = levelingArray.get(i).getAsFloat(); + if(i == level) { + skillInfo.currentXp += updateWithPercentage.get(skill)/100f * cap; + skillInfo.totalXp += skillInfo.currentXp; + skillInfo.currentXpMax = cap; + } else { + skillInfo.totalXp += cap; } } + + SkillInfo old = skillInfoMap.get(skill.toLowerCase()); + + if(old.totalXp <= skillInfo.totalXp) { + correctionCounter--; + if(correctionCounter < 0) correctionCounter = 0; + + skillInfoMap.put(skill.toLowerCase(), skillInfo); + } else if(++correctionCounter >= 10) { + correctionCounter = 0; + skillInfoMap.put(skill.toLowerCase(), skillInfo); + } + + updateWithPercentage.remove(skill); } } |
