From 0da7414dd8000f2b544ea4192fa36233a8413389 Mon Sep 17 00:00:00 2001 From: David Cole <40234707+DavidArthurCole@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:19:35 -0500 Subject: [PATCH] Improvement: CF Stray Filter (#2907) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../ChocolateFactoryRabbitWarningConfig.java | 24 ++++---- .../repo/HoppityRabbitTexturesJson.kt | 15 +++++ .../event/hoppity/HoppityTextureHandler.kt | 57 +++++++++++++++++++ .../ChocolateFactoryDataLoader.kt | 33 ++++++++--- .../ChocolateFactoryScreenFlash.kt | 41 ++++++++----- 5 files changed, 135 insertions(+), 35 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/HoppityRabbitTexturesJson.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityTextureHandler.kt diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/inventory/chocolatefactory/ChocolateFactoryRabbitWarningConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/inventory/chocolatefactory/ChocolateFactoryRabbitWarningConfig.java index ce0ef8ed4c82..fcbd88cf3095 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/inventory/chocolatefactory/ChocolateFactoryRabbitWarningConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/inventory/chocolatefactory/ChocolateFactoryRabbitWarningConfig.java @@ -15,14 +15,9 @@ public class ChocolateFactoryRabbitWarningConfig { @Expose - @ConfigOption(name = "Rabbit Warning", desc = "Warn when the rabbit that needs to be clicked appears.") - @ConfigEditorBoolean - public boolean rabbitWarning = true; - - @Expose - @ConfigOption(name = "Special Rabbit Warning", desc = "Warn when a special rabbit that needs to be clicked appears. (Rabbit The Fish and Golden Rabbits)") - @ConfigEditorBoolean - public boolean specialRabbitWarning = true; + @ConfigOption(name = "Rabbit Warning", desc = "Warn when stray rabbits of a certain tier appear.") + @ConfigEditorDropdown + public StrayTypeEntry rabbitWarningLevel = StrayTypeEntry.ALL; @Expose @ConfigOption(name = "Warning Sound", desc = "The sound that plays for a special rabbit.\n" + @@ -40,17 +35,20 @@ public class ChocolateFactoryRabbitWarningConfig { @Expose @ConfigOption(name = "Flash Screen", desc = "Choose the stray rabbit type to flash the screen for.") @ConfigEditorDropdown - @NotNull - public FlashScreenTypeEntry flashScreenType = FlashScreenTypeEntry.SPECIAL; + public StrayTypeEntry flashScreenLevel = StrayTypeEntry.SPECIAL; - public enum FlashScreenTypeEntry { - SPECIAL("Special"), + public enum StrayTypeEntry { + SPECIAL("Special Only"), + LEGENDARY_P("§6Legendary§7+"), + EPIC_P("§5Epic§7+"), + RARE_P("§9Rare§7+"), + UNCOMMON_P("§aUncommon§7+"), ALL("All"), NONE("None"), ; private final String str; - FlashScreenTypeEntry(String str) { + StrayTypeEntry(String str) { this.str = str; } diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/HoppityRabbitTexturesJson.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/HoppityRabbitTexturesJson.kt new file mode 100644 index 000000000000..a4b0867e8e49 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/HoppityRabbitTexturesJson.kt @@ -0,0 +1,15 @@ +package at.hannibal2.skyhanni.data.jsonobjects.repo + +import com.google.gson.annotations.Expose +import com.google.gson.annotations.SerializedName + +data class HoppityRabbitTexturesJson( + @Expose @SerializedName("textures") val textures: Map>, +) + +data class HoppityRabbitTextureEntry( + @Expose @SerializedName("rabbits") val rabbits: List, + @Expose @SerializedName("texture_value_b64") val textureValueB64: String, + @Expose @SerializedName("skull_id") val skullId: String, + @Expose @SerializedName("texture_id") val textureId: String, +) diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityTextureHandler.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityTextureHandler.kt new file mode 100644 index 000000000000..31766dae3326 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityTextureHandler.kt @@ -0,0 +1,57 @@ +package at.hannibal2.skyhanni.features.event.hoppity + +import at.hannibal2.skyhanni.data.jsonobjects.repo.HoppityRabbitTextureEntry +import at.hannibal2.skyhanni.data.jsonobjects.repo.HoppityRabbitTexturesJson +import at.hannibal2.skyhanni.events.RepositoryReloadEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.LorenzRarity +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object HoppityTextureHandler { + + private var hoppityRabbitTextures = mutableMapOf>() + + @SubscribeEvent + fun onRepoReload(event: RepositoryReloadEvent) { + val data = event.getConstant("HoppityRabbitTextures") + hoppityRabbitTextures = data.textures.mapNotNull { (key, entries) -> + val rarity = LorenzRarity.getByName(key) ?: return@mapNotNull null + rarity to entries + }.toMap().toMutableMap() + } + + /** + * Get the rarity for a given Skull ID. + * @param skullId The Skull ID to search for. + * @return The rarity or null if no rabbit was found. + */ + fun getRarityBySkullId(skullId: String): LorenzRarity? { + return hoppityRabbitTextures.entries.firstOrNull { (_, entries) -> + entries.any { it.skullId == skullId } + }?.key + } + + /** + * Get the rabbit for a given Skull ID. If more than one rabbit is found, null is returned. + * @param skullId The Skull ID to search for. + * @return The rabbit name or null if no rabbit was found or more than one rabbit was found. + */ + fun getRabbitBySkullId(skullId: String): String? = + hoppityRabbitTextures.values.flatten().firstOrNull { + it.skullId == skullId + }?.rabbits?.takeIf { + it.size == 1 + }?.firstOrNull() + + /** + * Get the Texture ID for a given Rabbit name. + * @param rabbit The Rabbit name to search for. + * @return The Texture ID or null if no rabbit was found. + */ + fun getTextureIdByRabbit(rabbit: String): String? = + hoppityRabbitTextures.values.flatten().firstOrNull { + rabbit in it.rabbits + }?.textureId + +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryDataLoader.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryDataLoader.kt index d1ff2c29e51b..c1b5b497b3cc 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryDataLoader.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryDataLoader.kt @@ -1,11 +1,14 @@ package at.hannibal2.skyhanni.features.inventory.chocolatefactory import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.config.features.inventory.chocolatefactory.ChocolateFactoryRabbitWarningConfig.StrayTypeEntry import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.InventoryCloseEvent import at.hannibal2.skyhanni.events.InventoryUpdatedEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI.specialRabbitTextures +import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryScreenFlash.isRarityOrHigher +import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryScreenFlash.isSpecial import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ConditionalUtils @@ -14,6 +17,7 @@ import at.hannibal2.skyhanni.utils.InventoryUtils import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LorenzRarity import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble import at.hannibal2.skyhanni.utils.NumberUtil.formatInt import at.hannibal2.skyhanni.utils.NumberUtil.formatLong @@ -559,17 +563,28 @@ object ChocolateFactoryDataLoader { val isGoldenRabbit = clickMeGoldenRabbitPattern.matches(item.name) val warningConfig = config.rabbitWarning - if (clickMeRabbitPattern.matches(item.name) || isGoldenRabbit) { - if (config.rabbitWarning.rabbitWarning) { - SoundUtils.playBeepSound() - } - - if (warningConfig.specialRabbitWarning && (isGoldenRabbit || item.getSkullTexture() in specialRabbitTextures)) { + if (!clickMeRabbitPattern.matches(item.name) && !isGoldenRabbit) return + if (shouldWarnAboutStray(item)) { + if (isGoldenRabbit || item.getSkullTexture() in specialRabbitTextures) { SoundUtils.repeatSound(100, warningConfig.repeatSound, ChocolateFactoryAPI.warningSound) - } - - ChocolateFactoryAPI.clickRabbitSlot = slotIndex + } else SoundUtils.playBeepSound() } + + ChocolateFactoryAPI.clickRabbitSlot = slotIndex + } + + private fun shouldWarnAboutStray(item: ItemStack) = when (config.rabbitWarning.rabbitWarningLevel) { + StrayTypeEntry.SPECIAL -> isSpecial(item) + + StrayTypeEntry.LEGENDARY_P -> isRarityOrHigher(item, LorenzRarity.LEGENDARY) + StrayTypeEntry.EPIC_P -> isRarityOrHigher(item, LorenzRarity.EPIC) + StrayTypeEntry.RARE_P -> isRarityOrHigher(item, LorenzRarity.RARE) + StrayTypeEntry.UNCOMMON_P -> isRarityOrHigher(item, LorenzRarity.UNCOMMON) + + StrayTypeEntry.ALL -> clickMeRabbitPattern.matches(item.name) || isSpecial(item) + + StrayTypeEntry.NONE -> false + else -> false } private fun findBestUpgrades(list: List) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryScreenFlash.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryScreenFlash.kt index 2cbd6057ab4e..cf5e6c5d350e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryScreenFlash.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryScreenFlash.kt @@ -1,11 +1,12 @@ package at.hannibal2.skyhanni.features.inventory.chocolatefactory import at.hannibal2.skyhanni.api.event.HandleEvent -import at.hannibal2.skyhanni.config.features.inventory.chocolatefactory.ChocolateFactoryRabbitWarningConfig.FlashScreenTypeEntry +import at.hannibal2.skyhanni.config.features.inventory.chocolatefactory.ChocolateFactoryRabbitWarningConfig.StrayTypeEntry import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.SecondPassedEvent import at.hannibal2.skyhanni.events.hoppity.RabbitFoundEvent import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggType +import at.hannibal2.skyhanni.features.event.hoppity.HoppityTextureHandler import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI.specialRabbitTextures import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryDataLoader.clickMeGoldenRabbitPattern import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryDataLoader.clickMeRabbitPattern @@ -13,12 +14,14 @@ import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.InventoryUtils import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LorenzRarity import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SpecialColor.toSpecialColorInt import net.minecraft.client.Minecraft import net.minecraft.client.gui.Gui import net.minecraft.client.renderer.GlStateManager import net.minecraft.inventory.Slot +import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.math.sin @@ -32,14 +35,19 @@ object ChocolateFactoryScreenFlash { fun onTick(event: SecondPassedEvent) { if (!ChocolateFactoryAPI.inChocolateFactory) return flashScreen = InventoryUtils.getItemsInOpenChest().any { - when (config.rabbitWarning.flashScreenType) { - FlashScreenTypeEntry.SPECIAL -> isSpecial(it) + when (config.rabbitWarning.flashScreenLevel) { + StrayTypeEntry.SPECIAL -> isSpecial(it) - FlashScreenTypeEntry.ALL -> { + StrayTypeEntry.LEGENDARY_P -> isRarityOrHigher(it, LorenzRarity.LEGENDARY) + StrayTypeEntry.EPIC_P -> isRarityOrHigher(it, LorenzRarity.EPIC) + StrayTypeEntry.RARE_P -> isRarityOrHigher(it, LorenzRarity.RARE) + StrayTypeEntry.UNCOMMON_P -> isRarityOrHigher(it, LorenzRarity.UNCOMMON) + + StrayTypeEntry.ALL -> { clickMeRabbitPattern.matches(it.stack.name) || isSpecial(it) } - FlashScreenTypeEntry.NONE -> false + StrayTypeEntry.NONE -> false } } } @@ -50,8 +58,20 @@ object ChocolateFactoryScreenFlash { flashScreen = false } + fun isRarityOrHigher(stack: ItemStack, rarity: LorenzRarity) = + stack.getSkullTexture()?.let { skullTexture -> + HoppityTextureHandler.getRarityBySkullId(skullTexture)?.let { skullRarity -> + skullRarity.ordinal >= rarity.ordinal + } ?: false + } ?: false + + private fun isRarityOrHigher(slot: Slot, rarity: LorenzRarity) = + slot.stack?.let { isRarityOrHigher(it, rarity) } ?: false + + fun isSpecial(stack: ItemStack) = + clickMeGoldenRabbitPattern.matches(stack.name) || stack.getSkullTexture() in specialRabbitTextures private fun isSpecial(slot: Slot) = - clickMeGoldenRabbitPattern.matches(slot.stack.name) || slot.stack.getSkullTexture() in specialRabbitTextures + slot.stack?.let { isSpecial(it) } ?: false @SubscribeEvent fun onRender(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) { @@ -59,13 +79,8 @@ object ChocolateFactoryScreenFlash { if (!flashScreen) return val minecraft = Minecraft.getMinecraft() val alpha = ((2 + sin(System.currentTimeMillis().toDouble() / 1000)) * 255 / 4).toInt().coerceIn(0..255) - Gui.drawRect( - 0, - 0, - minecraft.displayWidth, - minecraft.displayHeight, - (alpha shl 24) or (config.rabbitWarning.flashColor.toSpecialColorInt() and 0xFFFFFF), - ) + val color = (alpha shl 24) or (config.rabbitWarning.flashColor.toSpecialColorInt() and 0xFFFFFF) + Gui.drawRect(0, 0, minecraft.displayWidth, minecraft.displayHeight, color) GlStateManager.color(1F, 1F, 1F, 1F) } }