diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.java index 53f7aac94df8..1449bcc4c5c8 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/chat/ChatConfig.java @@ -142,4 +142,9 @@ public String toString() { @ConfigEditorBoolean @FeatureToggle public boolean petRarityDropMessage = true; + + @Expose + @ConfigOption(name = "Sound Responses", desc = "") + @Accordion + public SoundResponseConfig soundResponse = new SoundResponseConfig(); } diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/chat/SoundResponseConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/chat/SoundResponseConfig.java new file mode 100644 index 000000000000..5bec262b7be9 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/config/features/chat/SoundResponseConfig.java @@ -0,0 +1,25 @@ +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; + +import java.util.ArrayList; +import java.util.List; + +public class SoundResponseConfig { + + @Expose + @ConfigOption(name = "Enabled", desc = "Enable sound responses which play animal sounds when they are said in chat.") + @FeatureToggle + @ConfigEditorBoolean + public boolean enabled = false; + + @Expose + @ConfigOption(name = "Sound Responses", desc = "Add animal sounds to play when certain words are said in chat.") + @ConfigEditorDraggableList + public List soundResponses = new ArrayList<>(SoundResponseTypes.getEntries()); +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/SoundResponse.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/SoundResponse.kt new file mode 100644 index 000000000000..61e6981b7227 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/SoundResponse.kt @@ -0,0 +1,52 @@ +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.RegexUtils.matches +import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.SoundUtils.playSound +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object SoundResponse { + + private val config get() = SkyHanniMod.feature.chat.soundResponse + + init { + SoundResponseTypes.entries.forEach { it.pattern } + } + + @SubscribeEvent + fun onLorenzChat(event: LorenzChatEvent) { + for (soundType in SoundResponseTypes.entries) { + if (!config.soundResponses.contains(soundType)) continue + if (soundType.pattern.matches(event.message)) { + soundType.sound.playSound() + return + } + } + } +} + +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", + ) +} 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)