Skip to content

Commit

Permalink
Improvement: CF Stray Filter (#2907)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <[email protected]>
  • Loading branch information
DavidArthurCole and hannibal002 authored Dec 5, 2024
1 parent 88bb2a0 commit 0da7414
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<HoppityRabbitTextureEntry>>,
)

data class HoppityRabbitTextureEntry(
@Expose @SerializedName("rabbits") val rabbits: List<String>,
@Expose @SerializedName("texture_value_b64") val textureValueB64: String,
@Expose @SerializedName("skull_id") val skullId: String,
@Expose @SerializedName("texture_id") val textureId: String,
)
Original file line number Diff line number Diff line change
@@ -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<LorenzRarity, List<HoppityRabbitTextureEntry>>()

@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
val data = event.getConstant<HoppityRabbitTexturesJson>("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

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<ChocolateFactoryUpgrade>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
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
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

Expand All @@ -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
}
}
}
Expand All @@ -50,22 +58,29 @@ 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) {
if (!ChocolateFactoryAPI.inChocolateFactory) return
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)
}
}

0 comments on commit 0da7414

Please sign in to comment.