diff options
| author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-09-22 14:04:56 -0400 |
|---|---|---|
| committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-09-22 14:04:56 -0400 |
| commit | f8665b96427e382808295f6664886ef56a45d2f2 (patch) | |
| tree | 3a2091d9eb664652425c8e934fddd8a48a1fdd99 /src/main/java | |
| parent | 0ef339b506839c641df5e47bcecebc697fd4a09e (diff) | |
| download | Skyblocker-f8665b96427e382808295f6664886ef56a45d2f2.tar.gz Skyblocker-f8665b96427e382808295f6664886ef56a45d2f2.tar.bz2 Skyblocker-f8665b96427e382808295f6664886ef56a45d2f2.zip | |
Refactor bars updating
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/StatusBarTracker.java | 122 |
1 files changed, 59 insertions, 63 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/StatusBarTracker.java b/src/main/java/de/hysky/skyblocker/skyblock/StatusBarTracker.java index 18448ea5..ebf052f7 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/StatusBarTracker.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/StatusBarTracker.java @@ -53,6 +53,65 @@ public class StatusBarTracker { updateSpeed(); } + private boolean allowOverlayMessage(Text text, boolean overlay) { + onOverlayMessage(text, overlay); + return true; + } + + private Text onOverlayMessage(Text text, boolean overlay) { + if (!overlay || !Utils.isOnSkyblock() || !SkyblockerConfigManager.get().uiAndVisuals.bars.enableBars || Utils.isInTheRift()) { + return text; + } + return Text.of(update(text.getString(), SkyblockerConfigManager.get().chat.hideMana)); + } + + public String update(String actionBar, boolean filterManaUse) { + var sb = new StringBuilder(); + + // Match health and don't add it to the string builder + // Append healing to the string builder if there is any healing + Matcher matcher = STATUS_HEALTH.matcher(actionBar); + if (!matcher.find()) return actionBar; + updateHealth(matcher); + if (matcher.group("healing") != null) { + sb.append("§c❤"); + } + matcher.appendReplacement(sb, "$3"); + + // Match defense or mana use and don't add it to the string builder + if (matcher.usePattern(DEFENSE_STATUS).find()) { + defense = RegexUtils.parseIntFromMatcher(matcher, 1); + matcher.appendReplacement(sb, ""); + } else if (filterManaUse && matcher.usePattern(MANA_USE).find()) { + matcher.appendReplacement(sb, ""); + } + + // Match mana and don't add it to the string builder + if (matcher.usePattern(MANA_STATUS).find()) { + updateMana(matcher); + matcher.appendReplacement(sb, ""); + } + + // Append the rest of the message to the string builder + matcher.appendTail(sb); + String res = sb.toString().trim(); + return res.isEmpty() ? null : res; + } + + private void updateHealth(Matcher matcher) { + int health = RegexUtils.parseIntFromMatcher(matcher, "health"); + int max = RegexUtils.parseIntFromMatcher(matcher, "max"); + updateHealth(health, max, Math.max(0, health - max)); + } + + private void updateHealth(int value, int max, int overflow) { + if (client != null && client.player != null) { + value = (int) (client.player.getHealth() * max / client.player.getMaxHealth()); + overflow = (int) (client.player.getAbsorptionAmount() * max / client.player.getMaxHealth()); + } + health = new Resource(Math.min(value, max), max, Math.min(overflow, max)); + } + private void updateMana(Matcher m) { int value = RegexUtils.parseIntFromMatcher(m, 1); int max = RegexUtils.parseIntFromMatcher(m, 3); @@ -93,68 +152,5 @@ public class StatusBarTracker { this.speed = new Resource(value, max, 0); } - private void updateHealth(Matcher matcher) { - int health = RegexUtils.parseIntFromMatcher(matcher, "health"); - int max = RegexUtils.parseIntFromMatcher(matcher, "max"); - updateHealth(health, max, Math.max(0, health - max)); - } - - private void updateHealth(int value, int max, int overflow) { - if (client != null && client.player != null) { - value = (int) (client.player.getHealth() * max / client.player.getMaxHealth()); - overflow = (int) (client.player.getAbsorptionAmount() * max / client.player.getMaxHealth()); - } - health = new Resource(Math.min(value, max), max, Math.min(overflow, max)); - } - - private String reset(String str, Matcher m) { - str = str.substring(m.end()); - m.reset(str); - return str; - } - - private boolean allowOverlayMessage(Text text, boolean overlay) { - onOverlayMessage(text, overlay); - return true; - } - - private Text onOverlayMessage(Text text, boolean overlay) { - if (!overlay || !Utils.isOnSkyblock() || !SkyblockerConfigManager.get().uiAndVisuals.bars.enableBars || Utils.isInTheRift()) { - return text; - } - return Text.of(update(text.getString(), SkyblockerConfigManager.get().chat.hideMana)); - } - - public String update(String actionBar, boolean filterManaUse) { - var sb = new StringBuilder(); - Matcher matcher = STATUS_HEALTH.matcher(actionBar); - if (!matcher.lookingAt()) return actionBar; - updateHealth(matcher); - if (matcher.group("healing") != null) { - sb.append("§c❤"); - sb.append(matcher.group("healing")); - } - actionBar = reset(actionBar, matcher); - if (matcher.usePattern(MANA_STATUS).lookingAt()) { - defense = 0; - updateMana(matcher); - actionBar = reset(actionBar, matcher); - } else { - if (matcher.usePattern(DEFENSE_STATUS).lookingAt()) { - defense = RegexUtils.parseIntFromMatcher(matcher, 1); - actionBar = reset(actionBar, matcher); - } else if (filterManaUse && matcher.usePattern(MANA_USE).lookingAt()) { - actionBar = reset(actionBar, matcher); - } - if (matcher.usePattern(MANA_STATUS).find()) { - updateMana(matcher); - matcher.appendReplacement(sb, ""); - } - } - matcher.appendTail(sb); - String res = sb.toString().trim(); - return res.isEmpty() ? null : res; - } - public record Resource(int value, int max, int overflow) {} } |
