|
| 1 | +package de.hysky.skyblocker.skyblock.slayers; |
| 2 | + |
| 3 | +import de.hysky.skyblocker.utils.SlayerUtils; |
| 4 | +import net.minecraft.client.gui.hud.ClientBossBar; |
| 5 | +import net.minecraft.entity.boss.BossBar; |
| 6 | +import net.minecraft.entity.decoration.ArmorStandEntity; |
| 7 | +import net.minecraft.text.Text; |
| 8 | + |
| 9 | +import java.util.UUID; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class SlayerBossBars { |
| 14 | + private static final Pattern HEALTH_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?[kM]?)(?=❤)"); |
| 15 | + private static int bossMaxHealth = -1; |
| 16 | + private static long lastUpdateTime = 0; |
| 17 | + private static final long UPDATE_INTERVAL = 400; |
| 18 | + private static ClientBossBar bossBar; |
| 19 | + public static final UUID uuid = UUID.randomUUID(); |
| 20 | + |
| 21 | + /** |
| 22 | + * Determines if the boss bar should be rendered and updates the max health of the boss. |
| 23 | + * Has a 400ms cooldown built-in. |
| 24 | + */ |
| 25 | + public static boolean shouldRenderBossBar() { |
| 26 | + long currentTime = System.currentTimeMillis(); |
| 27 | + if (currentTime - lastUpdateTime < UPDATE_INTERVAL) { |
| 28 | + return bossBar != null; |
| 29 | + } |
| 30 | + lastUpdateTime = currentTime; |
| 31 | + |
| 32 | + // Reset if no slayer |
| 33 | + if (!SlayerUtils.isInSlayer()) { |
| 34 | + bossMaxHealth = -1; |
| 35 | + bossBar = null; |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + // Update boss max health |
| 40 | + if (SlayerUtils.getSlayerArmorStandEntity() != null && bossMaxHealth == -1) { |
| 41 | + Matcher maxHealthMatcher = HEALTH_PATTERN.matcher(SlayerUtils.getSlayerArmorStandEntity().getName().getString()); |
| 42 | + if (maxHealthMatcher.find()) bossMaxHealth = convertToInt(maxHealthMatcher.group(0)); |
| 43 | + } |
| 44 | + |
| 45 | + return bossBar != null || SlayerUtils.getSlayerArmorStandEntity() != null; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Updates the boss bar with the current slayer's health, called every frame. |
| 50 | + * @return The updated boss bar. |
| 51 | + */ |
| 52 | + public static ClientBossBar updateBossBar() { |
| 53 | + ArmorStandEntity slayer = SlayerUtils.getSlayerArmorStandEntity(); |
| 54 | + if (bossBar == null) bossBar = new ClientBossBar(uuid, slayer != null ? slayer.getDisplayName() : Text.of("Attempting to Locate Slayer..."), 1f, BossBar.Color.PURPLE, BossBar.Style.PROGRESS, false, false, false); |
| 55 | + |
| 56 | + // If no slayer armor stand is found, display a red progress bar |
| 57 | + if (slayer == null) { |
| 58 | + bossBar.setStyle(BossBar.Style.PROGRESS); |
| 59 | + bossBar.setColor(BossBar.Color.RED); |
| 60 | + return bossBar; |
| 61 | + } |
| 62 | + |
| 63 | + // Update the boss bar with the current slayer's health |
| 64 | + Matcher healthMatcher = HEALTH_PATTERN.matcher(slayer.getName().getString()); |
| 65 | + if (healthMatcher.find() && slayer.isAlive()) { |
| 66 | + bossBar.setPercent(bossMaxHealth == -1 ? 1f : (float) convertToInt(healthMatcher.group(1)) / bossMaxHealth); |
| 67 | + bossBar.setColor(BossBar.Color.PINK); |
| 68 | + bossBar.setName(slayer.getDisplayName()); |
| 69 | + bossBar.setStyle(BossBar.Style.NOTCHED_10); |
| 70 | + } else { |
| 71 | + bossBar.setColor(BossBar.Color.RED); |
| 72 | + bossBar.setStyle(BossBar.Style.PROGRESS); |
| 73 | + bossBar.setName(slayer.getDisplayName()); |
| 74 | + } |
| 75 | + |
| 76 | + return bossBar; |
| 77 | + } |
| 78 | + |
| 79 | + private static int convertToInt(String value) { |
| 80 | + if (value == null || value.isEmpty()) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + value = value.trim().toLowerCase(); |
| 85 | + double multiplier = 1.0; |
| 86 | + |
| 87 | + if (value.endsWith("m")) { |
| 88 | + multiplier = 1_000_000; |
| 89 | + value = value.substring(0, value.length() - 1); |
| 90 | + } else if (value.endsWith("k")) { |
| 91 | + multiplier = 1_000; |
| 92 | + value = value.substring(0, value.length() - 1); |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + double numericValue = Double.parseDouble(value); |
| 97 | + return (int) (numericValue * multiplier); |
| 98 | + } catch (NumberFormatException e) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments