diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.kt b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.kt index ce3282402728..851288e80fba 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.kt @@ -28,6 +28,11 @@ class ChatConfig { @Accordion var playerMessage: PlayerMessagesConfig = PlayerMessagesConfig() + @Expose + @ConfigOption(name = "Sound Responses", desc = "") + @Accordion + var soundResponse: ChatSoundResponseConfig = ChatSoundResponseConfig() + @Expose @ConfigOption(name = "Dungeon Filters", desc = "Hide specific message types in Dungeons.") @ConfigEditorDraggableList diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatSoundResponseConfig.kt b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatSoundResponseConfig.kt new file mode 100644 index 000000000000..1396818d5656 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatSoundResponseConfig.kt @@ -0,0 +1,24 @@ +package at.hannibal2.skyhanni.config.features.chat + +import at.hannibal2.skyhanni.config.FeatureToggle +import at.hannibal2.skyhanni.features.chat.SoundResponseTypes +import com.google.gson.annotations.Expose +import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean +import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorDraggableList +import io.github.notenoughupdates.moulconfig.annotations.ConfigOption + +class ChatSoundResponseConfig { + @Expose + @ConfigOption( + name = "Enabled", + desc = "Enable sound responses which play animal sounds when they are said in chat.", + ) + @FeatureToggle + @ConfigEditorBoolean + var enabled: Boolean = false + + @Expose + @ConfigOption(name = "Sound Responses", desc = "Add animal sounds to play when certain words are said in chat.") + @ConfigEditorDraggableList + var soundResponses = SoundResponseTypes.entries.toMutableList() +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/ChatSoundResponse.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/ChatSoundResponse.kt new file mode 100644 index 000000000000..94ad2f7d4353 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/ChatSoundResponse.kt @@ -0,0 +1,60 @@ +package at.hannibal2.skyhanni.features.chat + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.RegexUtils.matches +import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.SoundUtils.playSound +import at.hannibal2.skyhanni.utils.StringUtils.firstLetterUppercase +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object ChatSoundResponse { + + private val config get() = SkyHanniMod.feature.chat.soundResponse + + init { + SoundResponseTypes.entries.forEach { it.pattern } + } + + @SubscribeEvent + fun onLorenzChat(event: LorenzChatEvent) { + if (!isEnabled()) return + + for (soundType in SoundResponseTypes.entries) { + if (!config.soundResponses.contains(soundType)) continue + if (soundType.pattern.matches(event.message)) { + soundType.sound.playSound() + return + } + } + } + + fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled +} + +private const val START_PATTERN = "(?:^|^.* )(?: |§.)*(?i)" +private const val END_PATTERN = "(?: |§.|!|\\?|\\.)*(?:\$| .*\$)" + +enum class SoundResponseTypes(soundLocation: String, triggersOn: List) { + CAT("mob.cat.meow", listOf("meow")), + DOG("mob.wolf.bark", listOf("bark", "arf", "woof")), + SHEEP("mob.sheep.say", listOf("baa+h*")), + COW("mob.cow.say", listOf("moo+")), + PIG("mob.pig.say", listOf("oink")), + CHICKEN("mob.chicken.say", listOf("cluck")), + ; + + val sound by lazy { SoundUtils.createSound(soundLocation, 1f) } + + // creates a pattern that looks for if the message contains any of the triggerOn strings but as a full word + val pattern by RepoPattern.pattern( + "chat.sound.response" + name.lowercase(), + "$START_PATTERN(?:${triggersOn.joinToString("|")})$END_PATTERN", + ) + + override fun toString(): String = name.firstLetterUppercase() +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt index c05b305499c4..2827ab41813a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt @@ -31,7 +31,11 @@ object SoundUtils { if (e.message?.startsWith("value already present:") == true) return@execute ErrorManager.logErrorWithData(e, "Failed to play a sound", "soundLocation" to this.soundLocation) } catch (e: Exception) { - ErrorManager.logErrorWithData(e, "Failed to play a sound", "soundLocation" to this.soundLocation) + + ErrorManager.logErrorWithData( + e, "Failed to play a sound", + "soundLocation" to this.soundLocation, + ) } finally { if (!SkyHanniMod.feature.misc.maintainGameVolume) { gameSettings.setSoundLevel(SoundCategory.PLAYERS, oldLevel)