Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Sound Responses (Meow) #2222

Merged
merged 15 commits into from
Jan 11, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<SoundResponseTypes> soundResponses = new ArrayList<>(SoundResponseTypes.getEntries());
}
52 changes: 52 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/features/chat/SoundResponse.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
CAT("mob.cat.meow", listOf("meow")),
DOG("mob.wolf.bark", listOf("bark", "arf", "woof")),
SHEEP("mob.sheep.say", listOf("baa", "baah", "baaa", "baaaa", "baaaaa")),
COW("mob.cow.say", listOf("moo", "mooo", "moooo")),
Thunderblade73 marked this conversation as resolved.
Show resolved Hide resolved
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",
)
}
6 changes: 5 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading