Frizolek69
📎 1 file 5,658 chars
📄
pasted-code-1792689512526.txt
txt 118 lines
▶
Update the Minecraft Fabric mod called **TPA Sounds**. I want a complete graphical settings interface (GUI), not a command-based configuration system.

## MAIN FUNCTION

The mod must play an alert sound immediately when another player sends me an incoming `/tpa` teleport request on a multiplayer Anarchy server.

Example:
1. Another player sends a teleport request to me.
2. The server sends a chat or system message notifying me about the incoming request.
3. The mod detects the request and immediately plays the selected sound.
4. I hear the alert without needing to look at the chat.

Do NOT trigger the alert when I send a teleport request, when I teleport to another player, or when another player actually teleports to me. The trigger must be an incoming TPA request.

Because different servers use different messages, implement configurable message detection and explain any limitations of client-side detection.

## 1. GRAPHICAL SETTINGS GUI

Create a polished, modern Minecraft-style settings screen for TPA Sounds.

The entire configuration must be accessible through a graphical interface. Do not require commands to change settings.

The GUI must include:

- **Enable/Disable toggle:** Turn incoming TPA alerts on or off.
- **Sound selector:** A dropdown list of available Minecraft sound effects.
- **Sound preview button:** Play the currently selected sound so I can hear it before saving.
- **Volume slider:** Adjust the alert volume from 0% to 100%.
- **Pitch slider:** Adjust the sound pitch.
- **Cooldown setting:** Choose how long to wait before playing another alert.
- **Chat detection settings:** Configure the text patterns used to recognize incoming TPA requests.
- **Language/pattern presets:** Include editable example patterns for Polish and English server messages.
- **Reset to defaults button:** Restore the default settings.
- **Save and Cancel buttons:** Save or discard changes.
- **Clear visual feedback:** Make enabled and disabled settings easy to distinguish.

The interface should be intuitive, visually attractive, and consistent with Minecraft's UI style. Use proper buttons, sliders, dropdowns, labels, tooltips, and scrolling if necessary.

## 2. OPENING THE GUI

Add a reliable way to open the settings screen:

- Register a configurable keyboard shortcut.
- Let the player change the shortcut in Minecraft's Controls settings.
- Open the TPA Sounds settings screen when the shortcut is pressed.
- Ensure the screen works while connected to a multiplayer server.
- Do not replace or break the normal Minecraft pause menu or other screens.

If practical, add a button to the pause menu that opens TPA Sounds settings.

## 3. INCOMING TPA DETECTION

Implement client-side detection of incoming teleport requests.

- Listen for incoming chat and system messages.
- Match messages against configurable patterns.
- Detect requests addressed to the local player.
- Support Polish-language and English-language message patterns.
- Avoid triggering on outgoing requests, completed teleportations, or unrelated chat messages.
- Prevent duplicate sounds when the same request appears in multiple messages.
- Keep detection lightweight and avoid unnecessary performance overhead.

Do not assume a single universal message format. Provide editable pattern settings and explain how I can configure them for my server.

A client-only mod cannot detect requests that the server never exposes to the client. Be honest about this limitation.

## 4. CONFIGURATION STORAGE

Save all settings to a local configuration file.

Remember settings after Minecraft restarts, including:
- Enabled/disabled state.
- Selected sound.
- Volume and pitch.
- Cooldown.
- Message patterns.
- Keyboard shortcut, using Minecraft's normal keybinding system where appropriate.

Validate settings and handle invalid configuration values safely.

## 5. TECHNICAL REQUIREMENTS

- Minecraft Java Edition.
- Fabric Loader and compatible Fabric API.
- Use Java and the appropriate APIs for the chosen Minecraft version.
- Ask me for my Minecraft version if needed.
- If a default is required, target Minecraft 1.21.1 and use compatible dependencies.
- Follow Fabric's recommended client initialization, keybinding, screen, sound, and configuration APIs.
- Organize the source code into maintainable classes.
- Do not leave placeholder implementations for essential features.

## 6. COMPLETE PROJECT AND BUILD

Generate the complete project, including:
- Gradle build files and wrapper files if possible.
- `fabric.mod.json`.
- All Java source files.
- GUI screen implementation.
- Sound selection and preview functionality.
- Incoming chat detection.
- Configuration saving and loading.
- Keybinding registration.
- Resources and assets, if needed.
- README with build and installation instructions.

If your environment supports creating files and running commands:
1. Create the actual project.
2. Compile it using Gradle.
3. Fix compilation errors.
4. Verify API and dependency compatibility.
5. Build the final JAR.
6. Provide a downloadable JAR and a ZIP of the source project.

If you cannot compile the project, clearly say so and provide the source files and precise build instructions instead.

Do not claim the mod works or compiles unless you have actually verified it.

**FINAL GOAL:** I want a usable Minecraft Fabric mod with a graphical settings menu where I can change the alert sound, preview it, adjust volume and pitch, configure incoming TPA message detection, and enable or disable notifications without using commands.
Kodari AI
KodariOct 22 at 5:30 pm 4
TpaAlertSounds
A Minecraft Fabric mod that plays an alert sound when another player sends an incoming /tpa teleport request. Features a complete graphical settings GUI with enable/disable toggle, sound selector with preview, volume and pitch sliders, cooldown setting, configurable chat message patterns with Polish and English presets, reset to defaults, and save/cancel buttons. Opens via a configurable keybinding registered in Minecraft's Controls settings and an optional pause menu button. Detects incoming requests client-side by matching chat and system messages against editable patterns, avoiding triggers on outgoing requests or completed teleportations. Settings persist to a local configuration file with validation. Includes Gradle build files, fabric.mod.json, all Java sources, and README with build and installation instructions.
  1. Build a Fabric 1.21.4 client-only mod skeleton and persistent settings model.

  2. Add chat/system detection, a configurable Controls keybind, and a multiplayer-safe settings screen.

  3. Include resources and usage/build documentation; verify every Minecraft/Fabric API call against the mounted source.

Created the TPA Sounds project for Minecraft 1.21.4 with a client-only Fabric entrypoint, editable GUI settings, sound picker and preview, volume/pitch/cooldown sliders, and an O keybind that can be changed in Controls. Settings persist in config/tpa_sounds.properties.

Detection checks received chat and system messages against editable English and Polish regular-expression patterns. {player} is replaced with your username. It can only detect requests the server exposes to your client; adjust the patterns to match your server’s incoming-request messages.

The Gradle project files and README are included. I haven’t run a build, so compilation is unverified; no JAR or source ZIP was generated. To build in Kodari, click Compile in the top bar (or ⋮ → Compile Project on phone), then click Compile in the window and Download when it succeeds.

Kodari can make mistakes. Review important code before using.
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;
    }
}