heatrion

Create a server plugin for Minecraft version 1.21.11 on the Paper core. The server uses Simple Voice Chat, and the plugin should use it. The plugin should play a sound (customizable) when a player joins the server.

Kodari AI
KodariJul 4 at 3:45 pm 2

I’ll create a lightweight Paper plugin that plays a customizable sound through Simple Voice Chat on player join.

JoinSoundPlayer
Plays a customizable audio sound through Simple Voice Chat when a player joins the server. The sound is played as a locational audio channel at the player's position. Supports custom .wav files or generates a default tone. Integrates with Simple Voice Chat API for high-quality audio playback. Configurable volume, distance, delay, and sound frequency.
JoinSoundPlayer
Plays a customizable audio tone through Simple Voice Chat when a player joins the server. Uses the Voice Chat API to create a locational audio channel at the player's position. Configurable frequency, duration, volume, audible range, and delay. Defaults to a clean 660Hz tone. Only heard by the joining player. Requires the Simple Voice Chat plugin.
heatrion

Make sure there's a folder where you can put your .wav audio.

Kodari AI
KodariJul 4 at 4:13 pm 2

Now I’ll add the .wav file support — a dedicated sounds/ folder, a config option to pick a file, and a .wav parser.

Now I’ll create the WavReader utility and update the voice plugin. I also need to add the sounds/ folder creation at runtime.

Now create the WavReader class and update the main plugin to ensure the sounds/ folder exists.

heatrion

Why is the sound slow in the game?

Kodari AI
KodariJul 4 at 5:25 pm 5

Let me look at the current audio pipeline to trace the latency

The main culprit is re-creating the OpusEncoder and re-encoding the audio on every single player join. The Opus codec initialization is expensive — allocating a new encoder state every time a player connects is the biggest source of latency. The audio (short[]) also gets re-encoded from scratch each time.

Three fixes:

  1. Cache the OpusEncoder — create once, reuse forever

  2. Cache the audio samples — load/encode once, play for all players

  3. Preload on server start — no work at join time beyond channel creation

Now I need to re-add the PlayerConnectedEvent handler — I removed the registration but it’s still needed to trigger on join. The handler just calls onPlayerJoin with the already-cached audio

Now I need to add the missing imports that were removed

heatrion

The compilation failed with this error:

code
Compilation failed with 1 error(s):

ERROR in src/main/java/com/joinsound/JoinSoundVoicePlugin.java at line 52: cannot find symbol
    private void onPlayerConnected(PlayerConnectedEvent event) {
                                   ^
  symbol: class PlayerConnectedEvent
  location: class JoinSoundVoicePlugin


=== COMPILATION ERROR ANALYSIS ===

MISSING CLASSES:
  - PlayerConnectedEvent
ACTION: Create these classes or check imports

FILES TO INVESTIGATE:
  - src/main/java/com/joinsound/JoinSoundVoicePlugin.java

Please fix this error by creating a new artifact with the corrected code.

Kodari AI
KodariJul 4 at 5:27 pm 1

The PlayerConnectedEvent doesn’t exist. The Simple Voice Chat API uses Bukkit’s PlayerJoinEvent from Paper. Let me read the current state and fix it properly.

The PlayerConnectedEvent doesn’t exist in the Voice Chat API. Instead, I’ll wire a Bukkit PlayerJoinEvent listener in the main plugin and call the voice plugin directly — no changes to how the sound plays, just the trigger.

Now I need to add a PlayerJoinEvent listener in the main plugin that calls voicePlugin.triggerJoin(player)

Let me verify the voice plugin file compiles correctly — checking the imports and the triggerJoin signature

The unused JavaPlugin import will cause a warning but not an error. Let me clean it up:

Now let me verify SoundGenerator.java is consistent:

All three files are consistent. The fix: replaced the non-existent PlayerConnectedEvent (Simple Voice Chat API) with a standard Bukkit PlayerJoinEvent listener in the main plugin class. The triggerJoin(player) method on the voice plugin is now called directly from the Bukkit event handler.

Summary of changes:

  • JoinSoundVoicePlugin.java — removed PlayerConnectedEvent registration + handler, replaced with public void triggerJoin(Player player)

  • JoinSoundPlugin.java — added implements Listener, @EventHandler onPlayerJoin(PlayerJoinEvent), and getServer().getPluginManager().registerEvents(this, this) in onEnable()

heatrion

Make sure the sound doesn't slow down when playing in a game.

Kodari AI
KodariJul 4 at 5:32 pm 1

The sound can slow down/lag for two reasons:

  1. Main thread blocking — the Opus encoding inside createAudioPlayer runs synchronously on the server’s main thread. For a short tone it’s fine, but a .wav file of any length will cause visible tick lag.

  2. WAV sample rate mismatch — the current reader ignores the WAV header’s sample rate. If a user drops in a 44.1kHz .wav instead of 48kHz, the Opus encoder will play it at the wrong speed (slower and pitched down

Root cause and fix:

Synchronous playback on the main thread — createAudioPlayer(encoder, audio) triggers Opus encoding synchronously. For a short tone it’s near-instant, but the .wav path would encode the entire audio duration on the tick thread, visibly freezing the server.

Fix: playSound() now runs via runTaskAsynchronously() — the Opus encoding and audio channel creation happen off the main thread. The per-player audio channel creation is safe to run async (the Voice Chat API handles its own thread pool).

Additional fix: The WAV parser now reads the actual sample rate, channel count, and bit depth from the file header. If a user drops a 44.1kHz .wav, the plugin logs a warning instead of playing it at the wrong speed silently.

heatrion

[20:34:47 ERROR]: [voicechat] Uncaught exception in thread AudioPlayer-2190a3a1-a981-4680-842b-be76ee485f77 java.lang.IllegalStateException: Encoder is closed at voicechat-bukkit-2.6.20.jar//de.maxhenkel.voicechat.plugins.impl.opus.JavaOpusEncoderImpl.encode(JavaOpusEncoderImpl.java:40) ~[?:?] at voicechat-bukkit-2.6.20.jar//de.maxhenkel.voicechat.plugins.impl.audiochannel.AudioPlayerImpl.run(AudioPlayerImpl.java:81) ~[?:?]

Kodari AI
KodariJul 4 at 5:35 pm 1

The encoder is stateful and gets closed by the AudioPlayer when playback finishes. Sharing a single encoder across all players causes this crash.

The fix: create a fresh encoder per player instead of caching one.

<kodariReadFile>src/main/java/com/joinsound/JoinSoundVoicePlugin.java

heatrion

do it please

Kodari AI
KodariJul 4 at 5:37 pm 1

The shared encoder field gets closed by the first AudioPlayer that finishes playback. Every subsequent player hits the “Encoder is closed” crash.

Fix: create a fresh encoder per playSound() call instead of caching one

Each player now gets its own fresh OpusEncoder in playSound() — the encoder is created, used by one AudioPlayer, and closed naturally by that player. No more shared state crash.

heatrion

Now make sure that the death of a player is also accompanied by a sound, but the sound should be different and it needs a separate folder in the plugin folder.

Kodari AI
KodariJul 4 at 6:49 pm 2

Now I need to update the voice plugin to add death sound fields and methods. This is a lot of changes, so a full rewrite is cleanest:

Now let me verify it all compiles. Let me check the files one more time:

Both files look consistent. Here’s what was done:

config.yml — added deathsound: section with file, volume, distance, delay.

JoinSoundPlugin.java — added:

  • ensureDeathSoundsFolder() — creates deathsounds/ folder alongside sounds/

  • onPlayerDeath(PlayerDeathEvent) handler that calls voicePlugin.triggerDeath(player)

  • PlayerDeathEvent import

JoinSoundVoicePlugin.java — added:

  • cachedDeathAudio field, preloaded at startup from deathsounds/ folder or generated tone using deathsound.* config values

  • triggerDeath(Player player) — identical flow to triggerJoin but plays the death audio

  • reloadAudio() also reloads death audio

  • close() clears both caches

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
7 499
src/main/java/com/joinsound/JoinSoundPlugin.java
package com.joinsound;

import de.maxhenkel.voicechat.api.BukkitVoicechatService;
import java.io.File;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class JoinSoundPlugin extends JavaPlugin implements Listener {

    private static JoinSoundPlugin instance;
    private JoinSoundVoicePlugin voicePlugin;

    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();
        ensureSoundsFolder();
        ensureDeathSoundsFolder();

        BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);
        if (service != null) {
            voicePlugin = new JoinSoundVoicePlugin(this);
            service.registerPlugin(voicePlugin);
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("Registered Simple Voice Chat plugin for join sounds");
        } else {
            getLogger().warning("Simple Voice Chat not found — join sounds will not play.");
        }
    }

    @Override
    public void onDisable() {
        if (voicePlugin != null) {
            voicePlugin.close();
        }
    }

    private void ensureSoundsFolder() {
        File soundsDir = new File(getDataFolder(), "sounds");
        if (!soundsDir.exists()) {
            soundsDir.mkdirs();
            getLogger().info("Created sounds/ folder — place .wav files here");
        }
    }

    private void ensureDeathSoundsFolder() {
        File dir = new File(getDataFolder(), "deathsounds");
        if (!dir.exists()) {
            dir.mkdirs();
            getLogger().info("Created deathsounds/ folder — place .wav files here");
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (voicePlugin != null) {
            voicePlugin.triggerJoin(player);
        }
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        if (voicePlugin != null) {
            voicePlugin.triggerDeath(player);
        }
    }

    public static JoinSoundPlugin getInstance() {
        return instance;
    }
}