Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
10 613
src/main/java/com/kodari/tpasounds/TpaSoundsClient.java
package com.kodari.tpasounds;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.InputUtil;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Locale;
import java.util.regex.Pattern;
public final class TpaSoundsClient implements ClientModInitializer {
static final String MOD_ID = "tpa_sounds";
static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final long DUPLICATE_WINDOW_MILLIS = 2500L;
private static final Pattern NON_INCOMING_TERMS = Pattern.compile(
"(?i).*(?:you (?:have )?(?:sent|requested)|you teleported|teleport completed|teleportation complete|request accepted|request denied|wysłałeś|wysłałaś|zaakceptował|zaakceptowała|odrzucił|odrzuciła|teleportacja zakończona).*"
);
private static TpaConfig config;
private static KeyBinding openSettingsKey;
private static String lastAlertMessage = "";
private static long lastAlertMessageAt;
private static long lastPlayedAt;
@Override
public void onInitializeClient() {
config = TpaConfig.load();
openSettingsKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.tpa_sounds.open_settings",
InputUtil.GLFW_KEY_O,
"key.categories.tpa_sounds"
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (openSettingsKey.wasPressed()) {
if (client.currentScreen == null) {
client.setScreen(new TpaSettingsScreen(null, config.copy()));
}
}
});
ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) -> handleMessage(message.getString()));
ClientReceiveMessageEvents.GAME.register((message, overlay) -> handleMessage(message.getString()));
}
static TpaConfig getConfig() {
return config;
}
static void applyConfig(TpaConfig updated) {
updated.sanitize();
updated.save();
config = updated;
}
private static void handleMessage(String message) {
if (config == null || !config.enabled || message == null || message.isBlank()) {
return;
}
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null) {
return;
}
String normalized = message.replaceAll("\\s+", " ").trim();
String lowerCase = normalized.toLowerCase(Locale.ROOT);
if (NON_INCOMING_TERMS.matcher(lowerCase).matches()) {
return;
}
String username = client.getSession().getUsername();
boolean matches = config.patternsFor(username).stream().anyMatch(pattern -> pattern.matcher(normalized).find());
if (!matches) {
return;
}
long now = System.currentTimeMillis();
if (normalized.equals(lastAlertMessage) && now - lastAlertMessageAt <= DUPLICATE_WINDOW_MILLIS) {
return;
}
lastAlertMessage = normalized;
lastAlertMessageAt = now;
long cooldownMillis = (long) (config.cooldownSeconds * 1000.0);
if (lastPlayedAt != 0L && now - lastPlayedAt < cooldownMillis) {
return;
}
Identifier soundId = Identifier.tryParse(config.soundId);
SoundEvent sound = soundId == null ? null : Registries.SOUND_EVENT.get(soundId);
if (sound == null) {
sound = Registries.SOUND_EVENT.get(Identifier.of("minecraft:block.note_block.pling"));
}
client.getSoundManager().play(PositionedSoundInstance.master(sound, config.pitch, config.volume));
lastPlayedAt = now;
}
}