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;
}
}
}