aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLulonaut <lulonaut@lulonaut.tech>2023-08-30 13:51:35 +0200
committerGitHub <noreply@github.com>2023-08-30 13:51:35 +0200
commit2f768fbe0e87debd50ac2ea6d929fa6867a73c0f (patch)
tree36de54f68a76f29a4eae9848c7a0015eec5c4779
parentde22a446533d682ee87c18f0406560a868c3ea79 (diff)
downloadNotEnoughUpdates-2f768fbe0e87debd50ac2ea6d929fa6867a73c0f.tar.gz
NotEnoughUpdates-2f768fbe0e87debd50ac2ea6d929fa6867a73c0f.tar.bz2
NotEnoughUpdates-2f768fbe0e87debd50ac2ea6d929fa6867a73c0f.zip
parse tab list for skill info (#780)
Co-authored-by: nopo <nopotheemail@gmail.com>
-rw-r--r--config/detekt/detekt.yml4
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java5
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/profileviewer/SkyblockProfiles.java1
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java47
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/util/TabSkillInfoParser.kt114
5 files changed, 122 insertions, 49 deletions
diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml
index 80ff2814..dfea9a5e 100644
--- a/config/detekt/detekt.yml
+++ b/config/detekt/detekt.yml
@@ -9,3 +9,7 @@ forge:
active: true
InvalidSubscribeEvent:
active: true
+
+style:
+ MagicNumber:
+ active: false
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java b/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
index ff776025..0b4e3934 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
+ * Copyright (C) 2022-2023 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
@@ -45,6 +45,7 @@ import io.github.moulberry.notenoughupdates.util.Constants;
import io.github.moulberry.notenoughupdates.util.NotificationHandler;
import io.github.moulberry.notenoughupdates.util.ProfileApiSyncer;
import io.github.moulberry.notenoughupdates.util.SBInfo;
+import io.github.moulberry.notenoughupdates.util.TabSkillInfoParser;
import io.github.moulberry.notenoughupdates.util.Utils;
import io.github.moulberry.notenoughupdates.util.XPInformation;
import net.minecraft.client.Minecraft;
@@ -171,7 +172,7 @@ public class NEUEventListener {
CrystalOverlay.tick();
FairySouls.getInstance().tick();
- XPInformation.getInstance().tick();
+ TabSkillInfoParser.parseSkillInfo();
ItemCustomizeManager.tick();
BackgroundBlur.markDirty();
NPCRetexturing.getInstance().tick();
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/SkyblockProfiles.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/SkyblockProfiles.java
index 566e0c07..f2f9c6ad 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/SkyblockProfiles.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/SkyblockProfiles.java
@@ -865,7 +865,6 @@ public class SkyblockProfiles {
if (skillName.equals("farming")) {
maxLevel += Utils.getElementAsInt(Utils.getElement(profileJson, "jacob2.perks.farming_level_cap"), 0);
}
-
out.put(skillName, ProfileViewerUtils.getLevel(levelingArray, skillExperience, maxLevel, false));
}
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 0a501415..117545c9 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/XPInformation.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
+ * Copyright (C) 2022-2023 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
@@ -24,15 +24,12 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.core.util.StringUtils;
-import io.github.moulberry.notenoughupdates.profileviewer.ProfileViewer;
-import io.github.moulberry.notenoughupdates.profileviewer.SkyblockProfiles;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -210,46 +207,4 @@ public class XPInformation {
}
updateWithPercentage.clear();
}
-
- public void tick() {
- ProfileApiSyncer.getInstance().requestResync("xpinformation", 5 * 60 * 1000,
- () -> {
- }, this::onApiUpdated
- );
- }
-
- private static final String[] skills = {
- "taming",
- "mining",
- "foraging",
- "enchanting",
- "carpentry",
- "farming",
- "combat",
- "fishing",
- "alchemy",
- "runecrafting"
- };
-
- private void onApiUpdated(SkyblockProfiles profile) {
- Map<String, ProfileViewer.Level> skyblockInfo = profile.getLatestProfile().getLevelingInfo();
- if (skyblockInfo == null) {
- return;
- }
-
- for (String skill : skills) {
- SkillInfo info = new SkillInfo();
-
- ProfileViewer.Level levelInfo = skyblockInfo.get(skill);
- float level = levelInfo.level;
-
- info.totalXp = levelInfo.totalXp;
- info.currentXpMax = levelInfo.maxXpForLevel;
- info.level = (int) level;
- info.currentXp = (level % 1) * info.currentXpMax;
- info.fromApi = true;
-
- skillInfoMap.put(skill.toLowerCase(), info);
- }
- }
}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/TabSkillInfoParser.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/TabSkillInfoParser.kt
new file mode 100644
index 00000000..b4bfdf28
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/TabSkillInfoParser.kt
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2023 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.util
+
+import com.google.gson.JsonArray
+import net.minecraft.util.EnumChatFormatting
+import java.util.regex.Pattern
+import kotlin.math.abs
+
+object TabSkillInfoParser {
+ private val skillTabPattern: Pattern =
+ Pattern.compile("^§r§e§lSkills: §r§a(?<type>\\w+) (?<level>\\d+): §r§3(?<progress>.+)%§r\$")
+ private val maxSkillTabPattern: Pattern =
+ Pattern.compile("^§r§e§lSkills: §r§a(?<type>\\w+) (?<level>\\d+): §r§c§lMAX§r\$")
+ private var sentErrorOnce = false
+
+ private fun calculateLevelXp(levelingArray: JsonArray, level: Int): Double {
+ var totalXp = 0.0
+ for (i in 0 until level + 1) {
+ val xp = levelingArray[i].asDouble
+ totalXp += xp
+ }
+ return totalXp
+ }
+
+ private fun isWithinPercentageRange(xp: Double, existingXp: Double, percentage: Double): Boolean {
+ val diff = (abs(xp - existingXp) / existingXp) * 100
+ return diff <= percentage
+ }
+
+ private fun sendError(message: String) {
+ if (!sentErrorOnce) {
+ Utils.addChatMessage(message)
+ sentErrorOnce = true
+ }
+ }
+
+ private fun levelArray(skillType: String) =
+ if (skillType == "runecrafting") Utils.getElement(Constants.LEVELING, "runecrafting_xp").asJsonArray
+ else Utils.getElement(Constants.LEVELING, "leveling_xp").asJsonArray
+
+ @JvmStatic
+ fun parseSkillInfo() {
+ if (Constants.LEVELING == null) {
+ sendError("${EnumChatFormatting.RED}[NEU] There is an error with your repo, please report this in the discord at ${EnumChatFormatting.AQUA}discord.gg/moulberry")
+ return
+ }
+
+ for (s in TabListUtils.getTabList()) {
+ val matcher = skillTabPattern.matcher(s)
+ val maxLevelMatcher = maxSkillTabPattern.matcher(s)
+ if (matcher.matches()) {
+ // All the groups are guaranteed to match
+ val name = matcher.group("type")!!.lowercase()
+ val level = matcher.group("level")!!.toInt()
+ val progress = matcher.group("progress")!!.toFloatOrNull()
+ if (progress == null) {
+ sendError("${EnumChatFormatting.RED}[NEU] Error while parsing skill level from tab list")
+ return
+ }
+ val levelingArray = levelArray(name)
+ val levelXp = calculateLevelXp(levelingArray, level - 1)
+ // This *should* not cause problems, since skills that are max Level won't be picked up
+ val nextLevelDiff = levelingArray[level].asDouble
+ val nextLevelProgress = nextLevelDiff * progress / 100
+
+ val totalXp = levelXp + nextLevelProgress
+ val existingLevel = XPInformation.getInstance().getSkillInfo(name) ?: XPInformation.SkillInfo()
+
+ // Only update if the numbers are substantially different
+ if (!isWithinPercentageRange(totalXp, existingLevel.totalXp.toDouble(), 1.0)) {
+ existingLevel.level = level
+ existingLevel.totalXp = totalXp.toFloat()
+ existingLevel.currentXp = nextLevelProgress.toFloat()
+ existingLevel.currentXpMax = nextLevelDiff.toFloat()
+ XPInformation.getInstance().skillInfoMap[name] = existingLevel
+ }
+
+ // There is only one skill at a time in the tab list
+ break
+ } else if (maxLevelMatcher.matches()) {
+ val name = maxLevelMatcher.group("type")!!.lowercase()
+ val level = maxLevelMatcher.group("level")!!.toInt()
+
+ val existingLevel = XPInformation.getInstance().getSkillInfo(name) ?: XPInformation.SkillInfo()
+ if (existingLevel.level != level) {
+ existingLevel.level = level
+ val levelingArray = levelArray(name)
+
+ val totalXp = calculateLevelXp(levelingArray, level - 1)
+ existingLevel.totalXp = totalXp.toFloat()
+ XPInformation.getInstance().skillInfoMap[name] = existingLevel
+ }
+ }
+ }
+ }
+}