From f411297515c4f4b5ae437a57c25a8b02cc9122eb Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:17:42 +0100 Subject: [PATCH] Backend: SpecialColor translation (#2854) Co-authored-by: Cal Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> --- .../fishing/ThunderSparksHighlight.kt | 6 +- .../features/fishing/trophy/GeyserFishing.kt | 7 +- .../features/minion/MinionFeatures.kt | 30 +++++--- .../features/misc/PartyMemberOutlines.kt | 4 +- .../features/nether/SulphurSkitterBox.kt | 7 +- .../rift/area/dreadfarm/VoltHighlighter.kt | 9 ++- .../features/skillprogress/SkillProgress.kt | 9 ++- .../at/hannibal2/skyhanni/utils/ColorUtils.kt | 10 ++- .../skyhanni/utils/SpecialColor.java | 72 ------------------- .../hannibal2/skyhanni/utils/SpecialColor.kt | 31 ++++++++ versions/1.8.9/detekt/baseline.xml | 3 - 11 files changed, 77 insertions(+), 111 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.java create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.kt diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/ThunderSparksHighlight.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/ThunderSparksHighlight.kt index 96dc4159f835..0bd155afd8ef 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/ThunderSparksHighlight.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/ThunderSparksHighlight.kt @@ -16,12 +16,11 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.drawWaypointFilled import at.hannibal2.skyhanni.utils.SkullTextureHolder -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import at.hannibal2.skyhanni.utils.getLorenzVec import net.minecraft.entity.item.EntityArmorStand import net.minecraft.init.Blocks import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color @SkyHanniModule object ThunderSparksHighlight { @@ -43,8 +42,7 @@ object ThunderSparksHighlight { fun onRenderWorld(event: LorenzRenderWorldEvent) { if (!isEnabled()) return - val special = config.color - val color = Color(SpecialColor.specialToChromaRGB(special), true) + val color = config.color.toSpecialColor() for (spark in sparks) { if (spark.isDead) continue diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/GeyserFishing.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/GeyserFishing.kt index 48ea25c46e3d..f92bcc1d8f4b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/GeyserFishing.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/GeyserFishing.kt @@ -13,12 +13,11 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.RenderUtils.drawFilledBoundingBoxNea -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import net.minecraft.util.AxisAlignedBB import net.minecraft.util.EnumParticleTypes import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color @SkyHanniModule object GeyserFishing { @@ -40,7 +39,7 @@ object GeyserFishing { geyserBox = AxisAlignedBB( potentialGeyser.x - 2, 118.0 - 0.1, potentialGeyser.z - 2, - potentialGeyser.x + 2, 118.0 - 0.09, potentialGeyser.z + 2 + potentialGeyser.x + 2, 118.0 - 0.09, potentialGeyser.z + 2, ) if (config.hideParticles && FishingAPI.bobber != null) { @@ -63,7 +62,7 @@ object GeyserFishing { if (!IslandType.CRIMSON_ISLE.isInIsland()) return if (config.onlyWithRod && !FishingAPI.holdingLavaRod) return - val color = Color(SpecialColor.specialToChromaRGB(config.boxColor), true) + val color = config.boxColor.toSpecialColor() event.drawFilledBoundingBoxNea(geyserBox, color) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/minion/MinionFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/minion/MinionFeatures.kt index 5b8b7d41d043..d413f89b1ddb 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/minion/MinionFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/minion/MinionFeatures.kt @@ -47,7 +47,7 @@ import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.drawWaypointFilled import at.hannibal2.skyhanni.utils.RenderUtils.renderString import at.hannibal2.skyhanni.utils.SimpleTimeMark -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import at.hannibal2.skyhanni.utils.TimeUtils.format import at.hannibal2.skyhanni.utils.getLorenzVec import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern @@ -60,7 +60,6 @@ import net.minecraft.init.Blocks import net.minecraftforge.event.entity.player.PlayerInteractEvent import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color @SkyHanniModule object MinionFeatures { @@ -75,21 +74,35 @@ object MinionFeatures { private var coinsPerDay = "" private val patternGroup = RepoPattern.group("minion") + + /** + * REGEX-TEST: §aYou have upgraded your Minion to Tier V + */ private val minionUpgradePattern by patternGroup.pattern( "chat.upgrade", - "§aYou have upgraded your Minion to Tier (?.*)" + "§aYou have upgraded your Minion to Tier (?.*)", ) + + /** + * REGEX-TEST: §aYou received §r§64 coins§r§a! + * REGEX-TEST: §aYou received §r§610.5 coins§r§a! + */ private val minionCoinPattern by patternGroup.pattern( "chat.coin", - "§aYou received §r§6.* coins§r§a!" + "§aYou received §r§6.* coins§r§a!", ) + + /** + * REGEX-TEST: Redstone Minion IV + * REGEX-TEST: Chicken Minion XI + */ private val minionTitlePattern by patternGroup.pattern( "title", - "Minion [^➜]" + "Minion [^➜]", ) private val minionCollectItemPattern by patternGroup.pattern( "item.collect", - "^§aCollect All$" + "^§aCollect All$", ) var lastMinion: LorenzVec? = null @@ -143,8 +156,7 @@ object MinionFeatures { if (!enableWithHub()) return if (!config.lastClickedMinion.display) return - val special = config.lastClickedMinion.color - val color = Color(SpecialColor.specialToChromaRGB(special), true) + val color = config.lastClickedMinion.color.toSpecialColor() val loc = lastMinion if (loc != null) { @@ -156,7 +168,7 @@ object MinionFeatures { true, extraSize = -0.25, extraSizeTopY = 0.2, - extraSizeBottomY = 0.0 + extraSizeBottomY = 0.0, ) } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PartyMemberOutlines.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PartyMemberOutlines.kt index 0d1f96d135a2..4fead757e041 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/PartyMemberOutlines.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/PartyMemberOutlines.kt @@ -7,7 +7,7 @@ import at.hannibal2.skyhanni.events.RenderEntityOutlineEvent import at.hannibal2.skyhanni.features.dungeon.DungeonAPI import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColorInt import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraft.entity.Entity import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -30,6 +30,6 @@ object PartyMemberOutlines { private fun getEntityOutlineColor(entity: Entity): Int? { if (entity !is EntityOtherPlayerMP || !PartyAPI.partyMembers.contains(entity.name)) return null - return SpecialColor.specialToChromaRGB(config.outlineColor) + return config.outlineColor.toSpecialColorInt() } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/nether/SulphurSkitterBox.kt b/src/main/java/at/hannibal2/skyhanni/features/nether/SulphurSkitterBox.kt index d9d1efc0e59f..633e9b8e8aae 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/nether/SulphurSkitterBox.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/nether/SulphurSkitterBox.kt @@ -15,13 +15,12 @@ import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.RenderUtils import at.hannibal2.skyhanni.utils.RenderUtils.expandBlock -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import at.hannibal2.skyhanni.utils.toLorenzVec import net.minecraft.init.Blocks import net.minecraft.util.AxisAlignedBB import net.minecraft.util.BlockPos import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color @SkyHanniModule object SulphurSkitterBox { @@ -78,14 +77,14 @@ object SulphurSkitterBox { } private fun drawBox(axis: AxisAlignedBB, partialTicks: Float) { - val color = Color(SpecialColor.specialToChromaRGB(config.boxColor), true) + val color = config.boxColor.toSpecialColor() when (config.boxType) { SulphurSkitterBoxConfig.BoxType.FULL -> { RenderUtils.drawFilledBoundingBoxNea( axis, color, partialTicks = partialTicks, - renderRelativeToCamera = false + renderRelativeToCamera = false, ) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/rift/area/dreadfarm/VoltHighlighter.kt b/src/main/java/at/hannibal2/skyhanni/features/rift/area/dreadfarm/VoltHighlighter.kt index 354c2b5a31dc..69cc830d2baf 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/rift/area/dreadfarm/VoltHighlighter.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/rift/area/dreadfarm/VoltHighlighter.kt @@ -13,7 +13,7 @@ import at.hannibal2.skyhanni.utils.RenderUtils.drawDynamicText import at.hannibal2.skyhanni.utils.RenderUtils.exactLocation import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SkullTextureHolder -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import at.hannibal2.skyhanni.utils.TimeUtils.format import at.hannibal2.skyhanni.utils.compat.getStandHelmet import net.minecraft.client.Minecraft @@ -22,7 +22,6 @@ import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.item.EntityArmorStand import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -67,17 +66,17 @@ object VoltHighlighter { VoltState.DOING_LIGHTNING -> 0x800000FF.toInt() VoltState.HOSTILE -> 0x80FF0000.toInt() else -> 0 - } + }, ) { config.voltMoodMeter } if (state == VoltState.DOING_LIGHTNING && config.voltRange) { RenderUtils.drawCylinderInWorld( - Color(SpecialColor.specialToChromaRGB(config.voltColour), true), + config.voltColour.toSpecialColor(), entity.posX, entity.posY - 4f, entity.posZ, radius = LIGHTNING_DISTANCE, partialTicks = event.partialTicks, - height = 20F + height = 20F, ) val dischargingSince = chargingSince.getOrDefault(entity, SimpleTimeMark.farPast()) val dischargeTimeLeft = CHARGE_TIME - dischargingSince.passedSince() diff --git a/src/main/java/at/hannibal2/skyhanni/features/skillprogress/SkillProgress.kt b/src/main/java/at/hannibal2/skyhanni/features/skillprogress/SkillProgress.kt index 70c369219e24..a7ac10d11676 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/skillprogress/SkillProgress.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/skillprogress/SkillProgress.kt @@ -33,14 +33,13 @@ import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SoundUtils import at.hannibal2.skyhanni.utils.SoundUtils.playSound -import at.hannibal2.skyhanni.utils.SpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor import at.hannibal2.skyhanni.utils.TimeUnit import at.hannibal2.skyhanni.utils.TimeUtils.format import at.hannibal2.skyhanni.utils.renderables.Renderable import at.hannibal2.skyhanni.utils.renderables.Renderable.Companion.horizontalContainer import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.awt.Color import kotlin.math.ceil import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds @@ -115,7 +114,7 @@ object SkillProgress { maxWidth = 182 Renderable.progressBar( percent = factor.toDouble(), - startColor = Color(SpecialColor.specialToChromaRGB(barConfig.barStartColor)), + startColor = barConfig.barStartColor.toSpecialColor(), texture = barConfig.texturedBar.usedTexture.get(), useChroma = barConfig.useChroma.get(), ) @@ -125,8 +124,8 @@ object SkillProgress { val factor = skillExpPercentage.coerceAtMost(1.0) Renderable.progressBar( percent = factor, - startColor = Color(SpecialColor.specialToChromaRGB(barConfig.barStartColor)), - endColor = Color(SpecialColor.specialToChromaRGB(barConfig.barStartColor)), + startColor = barConfig.barStartColor.toSpecialColor(), + endColor = barConfig.barStartColor.toSpecialColor(), width = maxWidth, height = barConfig.regularBar.height, useChroma = barConfig.useChroma.get(), diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt index d0650462e182..1d61741b4cd9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt @@ -1,12 +1,16 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColor +import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColorInt import java.awt.Color object ColorUtils { - /** Transfer string colors from the config to [Color] */ - fun String.toChromaColor() = Color(toChromaColorInt(), true) - fun String.toChromaColorInt() = SpecialColor.specialToChromaRGB(this) + @Deprecated("Use toSpecialColor() instead", ReplaceWith("this.toSpecialColor()")) + fun String.toChromaColor() = this.toSpecialColor() + + @Deprecated("Use toSpecialColorInt() instead", ReplaceWith("this.toSpecialColorInt()")) + fun String.toChromaColorInt() = this.toSpecialColorInt() fun String.getFirstColorCode() = takeIf { it.firstOrNull() == '§' }?.getOrNull(1) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.java b/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.java deleted file mode 100644 index 7fc733c2b3e6..000000000000 --- a/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.java +++ /dev/null @@ -1,72 +0,0 @@ -package at.hannibal2.skyhanni.utils; - -import java.awt.Color; - -/** - * Taken from NotEnoughUpdates - */ -public class SpecialColor { - - private static final int RADIX = 10; - - private static int[] decompose(String csv) { - String[] split = csv.split(":"); - - int[] arr = new int[split.length]; - - for (int i = 0; i < split.length; i++) { - arr[i] = Integer.parseInt(split[split.length - 1 - i], RADIX); - } - return arr; - } - - public static int getSpeed(String special) { - return decompose(special)[4]; - } - - public static float getSecondsForSpeed(int speed) { - return (255 - speed) / 254f * (MAX_CHROMA_SECS - MIN_CHROMA_SECS) + MIN_CHROMA_SECS; - } - - private static final int MIN_CHROMA_SECS = 1; - private static final int MAX_CHROMA_SECS = 60; - - public static long startTime = -1; - - @Deprecated // use String.toChromaColor() - public static int specialToChromaRGB(String special) { - if (startTime < 0) startTime = System.currentTimeMillis(); - - int[] d = decompose(special); - int chr = d[4]; - int a = d[3]; - int r = d[2]; - int g = d[1]; - int b = d[0]; - - float[] hsv = Color.RGBtoHSB(r, g, b, null); - - if (chr > 0) { - float seconds = getSecondsForSpeed(chr); - hsv[0] += (System.currentTimeMillis() - startTime) / 1000f / seconds; - hsv[0] %= 1; - if (hsv[0] < 0) hsv[0] += 1; - } - - return (a & 0xFF) << 24 | (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) & 0x00FFFFFF); - } - - public static int rotateHue(int argb, int degrees) { - int a = (argb >> 24) & 0xFF; - int r = (argb >> 16) & 0xFF; - int g = (argb >> 8) & 0xFF; - int b = (argb) & 0xFF; - - float[] hsv = Color.RGBtoHSB(r, g, b, null); - - hsv[0] += degrees / 360f; - hsv[0] %= 1; - - return (a & 0xFF) << 24 | (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) & 0x00FFFFFF); - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.kt b/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.kt new file mode 100644 index 000000000000..d4cf776888ce --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/SpecialColor.kt @@ -0,0 +1,31 @@ +package at.hannibal2.skyhanni.utils + +import java.awt.Color + +/** + * Taken from NotEnoughUpdates, + * translated to Kotlin and modified. + */ +object SpecialColor { + private const val MIN_CHROMA_SECS = 1 + private const val MAX_CHROMA_SECS = 60 + private var startTime = SimpleTimeMark.farPast() + + fun String.toSpecialColor() = Color(toSpecialColorInt(), true) + + fun String.toSpecialColorInt(): Int { + if (startTime.isFarPast()) startTime = SimpleTimeMark.now() + + val (chroma, alpha, red, green, blue) = decompose(this) + val (hue, sat, bri) = Color.RGBtoHSB(red, green, blue, null) + + val adjustedHue = if (chroma > 0) (hue + (startTime.passedSince().inWholeMilliseconds / 1000f / chromaSpeed(chroma) % 1)).let { + if (it < 0) it + 1f else it + } else hue + + return (alpha and 0xFF) shl 24 or (Color.HSBtoRGB(adjustedHue, sat, bri) and 0x00FFFFFF) + } + + private fun decompose(csv: String) = csv.split(":").mapNotNull { it.toIntOrNull() }.toIntArray() + private fun chromaSpeed(speed: Int) = (255 - speed) / 254f * (MAX_CHROMA_SECS - MIN_CHROMA_SECS) + MIN_CHROMA_SECS +} diff --git a/versions/1.8.9/detekt/baseline.xml b/versions/1.8.9/detekt/baseline.xml index 87b1f4709896..8ecb254c9664 100644 --- a/versions/1.8.9/detekt/baseline.xml +++ b/versions/1.8.9/detekt/baseline.xml @@ -209,9 +209,6 @@ RepoPatternRegexTest:MaxwellAPI.kt$MaxwellAPI$by patternGroup.pattern( "gui.noselectedpower", "(?:§.)*Visit Maxwell in the Hub to learn", ) RepoPatternRegexTest:MaxwellAPI.kt$MaxwellAPI$by patternGroup.pattern( "gui.thaumaturgy.data", "§(?<color>.)\\+(?<amount>[^ ]+)(?<icon>.) (?<name>.+)", ) RepoPatternRegexTest:MinionCraftHelper.kt$MinionCraftHelper$by RepoPattern.pattern( "bingo.minion.name", "(?<name>.*) Minion (?<number>.*)" ) - RepoPatternRegexTest:MinionFeatures.kt$MinionFeatures$by patternGroup.pattern( "chat.coin", "§aYou received §r§6.* coins§r§a!" ) - RepoPatternRegexTest:MinionFeatures.kt$MinionFeatures$by patternGroup.pattern( "chat.upgrade", "§aYou have upgraded your Minion to Tier (?<tier>.*)" ) - RepoPatternRegexTest:MinionFeatures.kt$MinionFeatures$by patternGroup.pattern( "title", "Minion [^➜]" ) RepoPatternRegexTest:MobFilter.kt$MobFilter$by repoGroup.pattern( "filter.dojo", "^(?:(?<points>\\d+) pts|(?<empty>\\w+))$", ) RepoPatternRegexTest:MobFilter.kt$MobFilter$by repoGroup.pattern( "filter.summon", "^(?<owner>\\w+)'s (?<name>.*) \\d+.*", ) RepoPatternRegexTest:MobFilter.kt$MobFilter$by repoGroup.pattern( "jerry", "(?:\\[\\w+(?<level>\\d+)\\] )?(?<owner>\\w+)'s (?<name>\\w+ Jerry) \\d+ Hits", )