daviehmc

I need a mod that instantly disconnects a player from the server or the world when the death screen appears in Minecraft. The mod should also include the /DK off command, which disables the mod on the server or in the world, and the /DK on command, which enables the mod on the server or in the world. The version for which the mod is needed is 26.3 Fabric.

📎 1 file 1,006 chars
📄
pasted-code-1791393262784.txt
txt 4 lines
▶
Preferences for the mod: 
1. Messages indicating when the function is enabled or disabled should be highlighted in color. The message about disabling should be red, and the message about enabling should be green. The message itself also needs to be changed to “Death on death disabled” and “Death on death enabled”. 
2. After each hit, a sound selected using the /DK sound command should be played; in this case, you need to specify the event value, for example, minecraft:entity.wither.spawn. The sound should also play in the game menu after the function is disabled. By default, the sound value is minecraft:entity.wither.spawn. To reset the sound value, you need to enter the /DK reset sound command. There should also be the /DK sound off and /DK sound on commands, which enable and disable the sound after death. 
 3. I need the mod to be disabled on each server separately after each disablement, so that it doesn’t get disabled every time the death screen appears when entering the server or world.
Kodari AI
KodariOct 7 at 5:24 pm 3
DeathKick
DeathKick is a Fabric mod for Minecraft 26.3 that instantly disconnects players from the server or world when the death screen appears. It features /DK on and /DK off commands to toggle the mod per server or world, with green and red colored confirmation messages respectively. The mod includes a configurable death sound played after each hit, customizable via /DK sound command with event values like minecraft:entity.wither.spawn. Sound can be toggled with /DK sound on/off and reset with /DK reset sound. The mod's enabled state persists per server, preventing unwanted disconnections when re-entering worlds.
  1. Set up a client-only Fabric 26.3 project on Java 25.

  2. Add per-server/world /dk toggles, sound controls, and colored status feedback.

  3. Detect local-player damage and automatically disconnect to the menu when the death screen appears.

Created a client-side Fabric 26.3 mod that returns you to the title screen when the death screen appears.

  • /dk off and /dk on disable or enable the behavior for the current server or single-player world; each setting is saved separately.

  • /dk sound <event>, /dk sound off, /dk sound on, and /dk reset sound control the hit sound. It defaults to minecraft:entity.wither.spawn and can still play when disconnect-on-death is disabled.

  • Toggle feedback uses the requested red “Death on death disabled” and green “Death on death enabled” messages.

These commands affect your client’s behavior, not other players on the server. No new items or blocks were added.

daviehmc

Changes are needed in the mod. 1. When logging back into the server after being disconnected, the mod SHOULD NOT disconnect again. 2. I want to change the messages. The messages about the mod being disabled should look like this: “Kick after death off.” — in red. And the message about the mod being enabled should look like this: “Kick on death on.” — in green. 3. I also want to remove the sounds that play upon death and the commands related to sounds: /dk sound off and /dk sound on, /dk reset sound.

  1. The mod name needs to be changed to Death Disconnect with the description from DaviehMC.
  2. Add the /dk info command, which shows information about the last death. That is, it shows who killed me and which tool was used, if it has been renamed.
Kodari AI
KodariOct 7 at 6:00 pm 2
  1. Prevent repeat disconnects after reconnecting.

  2. Update enabled/disabled messages and remove death-sound features.

  3. Rename the mod and update its description.

  4. Add /dk info for the latest death and renamed weapon, if available.

  • After an automatic disconnect, kick-on-death is saved as off for that server or world, so reconnecting won’t disconnect you again. Use /dk on to re-enable it.

  • Updated feedback: red “Kick after death off.” and green “Kick on death on.”

  • Removed death sounds and all sound-related commands.

  • Renamed the mod to Death Disconnect, credited DaviehMC, and added /dk info. It shows your latest recorded death message, including the killer and a renamed weapon when present.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 261
src/client/java/com/kodari/deathondeath/DeathOnDeathClient.java
package com.kodari.deathondeath;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Properties;

public class DeathOnDeathClient implements ClientModInitializer {
    private static final Path SETTINGS_PATH = Path.of("config", "deathondeath.properties");
    private static final Properties SETTINGS = new Properties();

    private boolean disconnecting;

    static {
        loadSettings();
    }

    @Override
    public void onInitializeClient() {
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> {
            registerCommands(dispatcher, "dk");
            registerCommands(dispatcher, "DK");
        });

        ClientTickEvents.END_CLIENT_TICK.register(this::onClientTick);
        ClientReceiveMessageEvents.GAME.register(DeathOnDeathClient::cacheDeathMessage);
    }

    private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, String rootName) {
        LiteralArgumentBuilder<FabricClientCommandSource> root = ClientCommands.literal(rootName);

        root.then(ClientCommands.literal("off").executes(context -> {
            setEnabled(currentScope(), false);
            saveSettings(context.getSource());
            context.getSource().sendFeedback(Component.literal("Kick after death off.").withStyle(ChatFormatting.RED));
            return 1;
        }));

        root.then(ClientCommands.literal("on").executes(context -> {
            setEnabled(currentScope(), true);
            saveSettings(context.getSource());
            context.getSource().sendFeedback(Component.literal("Kick on death on.").withStyle(ChatFormatting.GREEN));
            return 1;
        }));

        root.then(ClientCommands.literal("info").executes(context -> {
            String lastDeath = SETTINGS.getProperty(lastDeathKey(currentScope()));
            if (lastDeath == null) {
                context.getSource().sendFeedback(Component.literal("No death information saved for this server or world."));
                return 0;
            }

            context.getSource().sendFeedback(Component.literal("Last death: " + lastDeath));
            return 1;
        }));

        dispatcher.register(root);
    }

    private static void cacheDeathMessage(Component message, boolean overlay) {
        if (overlay) {
            return;
        }

        Minecraft client = Minecraft.getInstance();
        if (client.player == null || !(message.getContents() instanceof TranslatableContents contents)) {
            return;
        }

        String key = contents.getKey();
        if (!key.startsWith("death.attack.") && !key.startsWith("death.fell")) {
            return;
        }

        Object[] arguments = contents.getArgs();
        if (arguments.length == 0) {
            return;
        }

        Object victimArgument = arguments[0];
        String victimName = victimArgument instanceof Component victimComponent
                ? victimComponent.getString()
                : victimArgument instanceof String name ? name : null;
        if (!client.player.getDisplayName().getString().equals(victimName)) {
            return;
        }

        SETTINGS.setProperty(lastDeathKey(currentScope()), message.getString());
        saveSettings();
    }

    private void onClientTick(Minecraft client) {
        if (client.player == null || client.level == null) {
            disconnecting = false;
            return;
        }

        String scope = currentScope();
        if (!disconnecting && client.gui.screen() instanceof DeathScreen && isEnabled(scope)) {
            disconnecting = true;
            setEnabled(scope, false);
            saveSettings();
            client.level.disconnect(ClientLevel.DEFAULT_QUIT_MESSAGE);
            client.disconnectWithSavingScreen();
            client.gui.setScreen(new TitleScreen());
        }
    }

    private static String currentScope() {
        Minecraft client = Minecraft.getInstance();
        if (client.getSingleplayerServer() != null) {
            return "world:" + client.getSingleplayerServer().getWorldData().getLevelName();
        }

        ServerData server = client.getCurrentServer();
        if (server != null) {
            return "server:" + server.ip;
        }

        return "menu";
    }

    private static String enabledKey(String scope) {
        String encodedScope = Base64.getUrlEncoder().withoutPadding()
                .encodeToString(scope.getBytes(StandardCharsets.UTF_8));
        return "enabled." + encodedScope;
    }

    private static String lastDeathKey(String scope) {
        String encodedScope = Base64.getUrlEncoder().withoutPadding()
                .encodeToString(scope.getBytes(StandardCharsets.UTF_8));
        return "lastDeath." + encodedScope;
    }

    private static boolean isEnabled(String scope) {
        return Boolean.parseBoolean(SETTINGS.getProperty(enabledKey(scope), "true"));
    }

    private static void setEnabled(String scope, boolean enabled) {
        SETTINGS.setProperty(enabledKey(scope), Boolean.toString(enabled));
    }

    private static void loadSettings() {
        if (Files.exists(SETTINGS_PATH)) {
            try (var input = Files.newInputStream(SETTINGS_PATH)) {
                SETTINGS.load(input);
            } catch (IOException exception) {
                throw new IllegalStateException("Could not load Death on Death settings", exception);
            }
        }
    }

    private static void saveSettings(FabricClientCommandSource source) {
        if (!saveSettings()) {
            source.sendError(Component.literal("Could not save Death on Death settings"));
        }
    }

    private static boolean saveSettings() {
        try {
            Files.createDirectories(SETTINGS_PATH.getParent());
            try (var output = Files.newOutputStream(SETTINGS_PATH)) {
                SETTINGS.store(output, null);
            }
            return true;
        } catch (IOException exception) {
            return false;
        }
    }
}