From e620831d1fc6bab17ee1c3bea38ac4560b2bfffd Mon Sep 17 00:00:00 2001 From: NopoTheGamer <40329022+NopoTheGamer@users.noreply.github.com> Date: Sat, 16 Nov 2024 18:49:32 +1100 Subject: [PATCH] Fix pet overlay issues (#1392) --- .../miscfeatures/PetInfoOverlay.java | 150 +++++++++++++++--- 1 file changed, 124 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/PetInfoOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/PetInfoOverlay.java index 510ac0300..6a555b35b 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/PetInfoOverlay.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/PetInfoOverlay.java @@ -37,6 +37,7 @@ import io.github.moulberry.notenoughupdates.overlays.TextOverlay; import io.github.moulberry.notenoughupdates.overlays.TextOverlayStyle; import io.github.moulberry.notenoughupdates.util.Constants; +import io.github.moulberry.notenoughupdates.util.ItemResolutionQuery; import io.github.moulberry.notenoughupdates.util.PetLeveling; import io.github.moulberry.notenoughupdates.util.SBInfo; import io.github.moulberry.notenoughupdates.util.Utils; @@ -50,6 +51,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IChatComponent; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -79,7 +81,8 @@ public class PetInfoOverlay extends TextOverlay { private static final Pattern TAB_LIST_XP = Pattern.compile( "([0-9,]+\\.?[0-9]*)/([0-9,]+\\.?[0-9]*)[kM]? XP \\(\\d+\\.?\\d*%\\)"); private static final Pattern TAB_LIST_XP_OVERFLOW = Pattern.compile("\\+([0-9,]+\\.?[0-9]*) XP"); - private static final Pattern TAB_LIST_PET_NAME = Pattern.compile("\\[Lvl (\\d+)\\] (.+)"); + private static final Pattern TAB_LIST_PET_NAME = Pattern.compile("§.\\[Lvl (\\d+)\\] §(.)(.+)"); + private static final Pattern TAB_LIST_PET_ITEM = Pattern.compile("§[fa956d4][a-zA-Z- ]+"); private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); @@ -359,6 +362,9 @@ public void updateFrequent() { public float getLevelPercent(Pet pet) { if (pet == null) return 0; try { + if (pet.petLevel.getMaxLevel() == pet.petLevel.getCurrentLevel()) { + return 100; + } return Float.parseFloat(StringUtils.formatToTenths(Math.min( pet.petLevel.getPercentageToNextLevel() * 100f, 100f @@ -383,13 +389,20 @@ private List createStringsForPet(Pet currentPet, boolean secondPet) { String lvlString = null; if (levelPercent != 100 || !NotEnoughUpdates.INSTANCE.config.petOverlay.hidePetLevelProgress) { - lvlStringShort = EnumChatFormatting.AQUA + "" + roundFloat(levelXp) + "/" + - roundFloat(currentPet.petLevel.getExpRequiredForNextLevel()) + long xpForNextLevel = currentPet.petLevel.getExpRequiredForNextLevel(); + float visualXp = levelXp; + if (levelPercent == 100) { + if (xpForNextLevel > levelXp) { + visualXp = xpForNextLevel; + } + } + lvlStringShort = EnumChatFormatting.AQUA + "" + roundFloat(visualXp) + "/" + + roundFloat(xpForNextLevel) + EnumChatFormatting.YELLOW + " (" + levelPercent + "%)"; lvlString = EnumChatFormatting.AQUA + "" + - Utils.shortNumberFormat(Math.min(levelXp, currentPet.petLevel.getExpRequiredForNextLevel()), 0) + "/" + - Utils.shortNumberFormat(currentPet.petLevel.getExpRequiredForNextLevel(), 0) + Utils.shortNumberFormat(Math.min(visualXp, xpForNextLevel), 0) + "/" + + Utils.shortNumberFormat(xpForNextLevel, 0) + EnumChatFormatting.YELLOW + " (" + levelPercent + "%)"; } @@ -403,7 +416,7 @@ private List createStringsForPet(Pet currentPet, boolean secondPet) { String xpGainString = EnumChatFormatting.AQUA + "XP/h: " + EnumChatFormatting.YELLOW + roundFloat(xpGain); if (!secondPet && xpGain > 0 && - (levelXp != levelXpLast || System.currentTimeMillis() - lastXpUpdateNonZero > 3500)) { + (levelXp != levelXpLast || System.currentTimeMillis() - lastXpUpdateNonZero > 4500)) { if (pauseCountdown <= 0) { xpGainString += EnumChatFormatting.RED + " (PAUSED)"; } else { @@ -864,7 +877,7 @@ public void onTick(TickEvent.ClientTickEvent event) { } } removeMap.keySet().retainAll(removeSet); - } else if (containerName.equals("Your Equipment")) { + } else if (containerName.startsWith("Your Equipment")) { ItemStack petStack = lower.getStackInSlot(47); if (petStack != null && petStack.getItem() == Items.skull) { NBTTagCompound tag = petStack.getTagCompound(); @@ -892,7 +905,12 @@ public void onTick(TickEvent.ClientTickEvent event) { double petLevel = PetLeveling.getPetLevelingForPet(name, Rarity.valueOf(rarityString)) .getPetLevel(petXp) .getCurrentLevel(); - int index = getClosestPetIndex(name, rarity, "", (float) petLevel); + + String petItem = ""; + if (petInfoObject.has("heldItem")) { + petItem = petInfoObject.get("heldItem").getAsString(); + } + int index = getClosestPetIndex(name, rarity, petItem, (float) petLevel); if (index != config.selectedPet) { clearPet(); setCurrentPet(index); @@ -927,6 +945,7 @@ private boolean isPetMenu(String containerName, ContainerChest container) { private long lastXpUpdate = -1; private long lastXpUpdateNonZero = -1; private long lastPaused = -1; + private long lastPetCorrect = -1; public void updatePetLevels() { float totalGain = 0; @@ -941,30 +960,82 @@ public void updatePetLevels() { if ("rift".equals(SBInfo.getInstance().getLocation())) return; - for (String line : TablistAPI.getWidgetLines(TablistAPI.WidgetNames.PET)) { + List widgetLines = TablistAPI.getWidgetLines(TablistAPI.WidgetNames.PET); + for (int i = 0; i < widgetLines.size(); i++) { + String line = widgetLines.get(i); + String lineWithColours = line.replace("§r", "").trim(); line = Utils.cleanColour(line).trim().replace(",", ""); + Matcher normalXPMatcher = TAB_LIST_XP.matcher(line); Matcher overflowXPMatcher = TAB_LIST_XP_OVERFLOW.matcher(line); - Matcher petNameMatcher = TAB_LIST_PET_NAME.matcher(line); + + Matcher petNameMatcher = TAB_LIST_PET_NAME.matcher(lineWithColours); + Matcher petItemMatcher = TAB_LIST_PET_ITEM.matcher(lineWithColours); + if (petNameMatcher.matches()) { - String petName = petNameMatcher.group(2); + String petName = petNameMatcher.group(3); + int petLevel = 1; + try { + petLevel = Integer.parseInt(petNameMatcher.group(1)); + } catch (NumberFormatException ignored) { + Utils.addChatMessage(EnumChatFormatting.RED + "[NEU] Invalid number in tab list: " + petNameMatcher.group(1)); + } + if (!getPetNameFromId(currentPet.petType, currentPet.petLevel.getCurrentLevel()).equalsIgnoreCase(petName)) { + if (lastPetCorrect == -1 || lastPetCorrect > 0 && System.currentTimeMillis() - lastPetCorrect > 5000) { + int rarity = getRarityByColor(petNameMatcher.group(2)).petId; + String petItem = ""; + if (widgetLines.size() > i) { + String nextLine = widgetLines.get(i + 1).replace("§r", "").trim(); + Matcher nextLinePetItemMatcher = TAB_LIST_PET_ITEM.matcher(nextLine); + if (nextLinePetItemMatcher.matches()) { + petItem = getInternalIdForPetItemDisplayName(nextLinePetItemMatcher.group(0)); + } + } + + String internalName = ItemResolutionQuery.findInternalNameByDisplayName(lineWithColours, true); + String[] split = internalName.split(";"); + if (split.length > 0) { + internalName = split[0]; + } + + int closestPetIndex = getClosestPetIndex(internalName, rarity, petItem, petLevel); + if (closestPetIndex != -1) { + //If it is -1 your petcache is probably outdated and you need to open /pets, but im sure they can work it out + setCurrentPet(closestPetIndex); + } + lastPetCorrect = System.currentTimeMillis(); + } break; + } else { + lastPetCorrect = System.currentTimeMillis(); } - try { - int petLevel = Integer.parseInt(petNameMatcher.group(1)); - PetLeveling.ExpLadder petLadder = PetLeveling.getPetLevelingForPet(currentPet.petType, currentPet.rarity); - if (currentPet.petLevel.getCurrentLevel() != petLevel) { - long baseLevelXp = petLadder.getPetExpForLevel(petLevel); - currentPet.petLevel.setExpTotal(baseLevelXp); - currentPet.petLevel = petLadder.getPetLevel(currentPet.petLevel.getExpTotal()); - } - } catch (NumberFormatException ignored) { - Utils.addChatMessage(EnumChatFormatting.RED + "[NEU] Invalid number in tab list: " + petNameMatcher.group(1)); + PetLeveling.ExpLadder petLadder = PetLeveling.getPetLevelingForPet(currentPet.petType, currentPet.rarity); + if (currentPet.petLevel.getCurrentLevel() != petLevel) { + long baseLevelXp = petLadder.getPetExpForLevel(petLevel); + currentPet.petLevel.setExpTotal(baseLevelXp); + currentPet.petLevel = petLadder.getPetLevel(currentPet.petLevel.getExpTotal()); } } + + if (petItemMatcher.matches()) { + String petItem = getInternalIdForPetItemDisplayName(petItemMatcher.group(0)); + if (!Objects.equals(currentPet.petItem, petItem)) { + int closestPetIndex = getClosestPetIndex( + currentPet.petType, + currentPet.rarity.petId, + petItem, + currentPet.petLevel.getCurrentLevel() + ); + + if (config.selectedPet != closestPetIndex && closestPetIndex != -1) { + setCurrentPet(closestPetIndex); + } + } + } + if (normalXPMatcher.matches() || overflowXPMatcher.matches()) { String xpString; if (normalXPMatcher.matches()) xpString = normalXPMatcher.group(1); @@ -1002,7 +1073,7 @@ public void updatePetLevels() { } } - if (totalGain != 0 || System.currentTimeMillis() - lastXpUpdate > 3500) { + if (totalGain != 0 || System.currentTimeMillis() - lastXpUpdate > 4500) { xpHourMap.put(System.currentTimeMillis(), totalGain); lastXpUpdate = System.currentTimeMillis(); } @@ -1016,7 +1087,7 @@ public void updatePetLevels() { averageXp += value; } - if (!xpHourMap.isEmpty()) xpGainHour = (averageXp / xpHourMap.size()) * ((float) (60 * 60) / seconds); + if (!xpHourMap.isEmpty()) xpGainHour = (averageXp) * ((float) (60 * 60) / seconds); else xpGainHour = 0; } else { lastPaused = System.currentTimeMillis(); @@ -1083,10 +1154,16 @@ public void onChatReceived(ClientChatReceivedEvent event) { String pet = Utils.cleanColour(petName) .replaceAll("[^\\w ]", "").trim() .replace(" ", "_").toUpperCase(Locale.ROOT); - - setCurrentPet(getClosestPetIndex(pet, rarity.petId, "", lastLevelHovered)); + List siblings = event.message.getChatStyle().getChatHoverEvent().getValue().getSiblings(); + String petItem = ""; + if (siblings.size() > 6) { + IChatComponent iChatComponent = siblings.get(6); + String formattedText = iChatComponent.getChatStyle().getColor() + iChatComponent.getUnformattedText(); + petItem = getInternalIdForPetItemDisplayName(formattedText); + } + setCurrentPet(getClosestPetIndex(pet, rarity.petId, petItem, lastLevelHovered)); if (PetInfoOverlay.config.selectedPet == -1) { - setCurrentPet(getClosestPetIndex(pet, rarity.petId - 1, "", lastLevelHovered)); + setCurrentPet(getClosestPetIndex(pet, rarity.petId - 1, petItem, lastLevelHovered)); if (getCurrentPet() != null && !"PET_ITEM_TIER_BOOST".equals(getCurrentPet().petItem)) { PetInfoOverlay.config.selectedPet = -1; Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( @@ -1145,4 +1222,25 @@ private static String getPetNameFromId(String petId, int petLevel) { } return defaultName; } + + private static boolean hasRepoPopupped = false; + + private static String getInternalIdForPetItemDisplayName(String displayName) { + JsonObject pets = Constants.PETS; + String defaultName = displayName.replace(" ", "_").replace("-", "_").toUpperCase(Locale.ROOT); + defaultName = Utils.cleanColour(defaultName).trim(); + if (pets == null) return defaultName; + if (!pets.has("pet_item_display_name_to_id")) { + if (!hasRepoPopupped) Utils.showOutdatedRepoNotification("pets.json pet_item_display_name_to_id"); + hasRepoPopupped = true; + return defaultName; + } + + JsonObject petItemDisplayNameToId = pets.get("pet_item_display_name_to_id").getAsJsonObject(); + if (petItemDisplayNameToId.has(displayName)) { + return petItemDisplayNameToId.get(displayName).getAsString(); + } + + return defaultName; + } }