newaltalt
๐Ÿ“Ž 1 file 2,536 chars
๐Ÿ“„
pasted-code-1791985811671.txt
txt 37 lines
โ–ถ
Build me a client-side Fabric mod called "Dialog Diagnoser" for Minecraft Java Edition 26.1.2
(unobfuscated, Mojang names, Java 25). I want a finished, compiled .jar at the end.

GOAL
Minecraft dialogs (added in 1.21.6) can have buttons that run commands, open URLs, or send
custom actions to the server. When ANY dialog opens, the mod must tell me exactly what each
button will do, before I click anything.

BEHAVIOUR
1. Detect when a DialogScreen opens (use Fabric API's ScreenEvents.AFTER_INIT). AFTER_INIT
   fires again on every window resize, so only report each screen instance once
   (WeakReference to the last screen).
2. Get the Dialog object out of the screen. Don't rely on a field name: look through the
   screen's class hierarchy for a non-static field whose type is assignable to Dialog.
3. Convert the dialog to JSON with Dialog.DIRECT_CODEC. Use a RegistryOps from
   Minecraft.getInstance().getConnection().registryAccess() when a connection exists,
   otherwise plain JsonOps.INSTANCE.
4. Walk the JSON recursively and report every action:
   - Dialog buttons use {"type": "..."}; clickable text components use "click_event" with
     {"action": "..."}. Strip the "minecraft:" prefix.
   - Action types: run_command, dynamic/run_command (template; my input values get inserted),
     suggest_command, open_url, open_file, copy_to_clipboard, change_page, show_dialog
     (id or inline nested dialog; list nested actions too), custom and dynamic/custom
     (sent to the server; the client cannot know what happens, say so).
   - Show the button label (flatten text components to plain text) and the command/URL/id.
   - Mark buttons that also fire on ESC / close: keys "exit_action", "no" (confirmation
     dialog), and "action" directly under a "notice" dialog.
   - Colour sensitive commands red (op, deop, stop, ban, ban-ip, pardon, kick, whitelist,
     execute, function, reload, transfer, data). Command text may lack the leading "/";
     show it with one.
5. Output: print a header (dialog title, dialog type, number of actions) plus one line per
   action into the in-game chat (Minecraft.gui.getChat().addMessage) AND into latest.log.
   Also save the raw pretty-printed dialog JSON to config/dialogdiagnoser/last_dialog.json.
6. No mixins unless really necessary. Don't touch rendering code. Everything must be
   null-safe, since dialogs can also open during the configuration phase.

basically, just DUMP the data , AND, extract the command that it wants us to exec
Kodari AI
KodariOct 14 at 2:05 pm 3
DialogDiagnoser
Client-side Fabric mod that intercepts Minecraft dialog screens and dumps their full JSON structure. Detects DialogScreen instances via ScreenEvents.AFTER_INIT, extracts the Dialog object through reflection, and serializes it using Dialog.DIRECT_CODEC. Recursively walks the JSON to identify every button action: commands, URLs, clipboard operations, page changes, nested dialogs, and custom server actions. Reports button labels, action types, sensitive commands (colored red), and ESC-close behaviors. Outputs formatted summaries to chat and latest.log, plus saves the raw pretty-printed dialog JSON to config/dialogdiagnoser/last_dialog.json. Null-safe for configuration phase dialogs, no mixins required.
  1. Set up the client-only Fabric 26.2 project for Dialog Diagnoser.

  2. Register the dialog screen listener, encode dialogs safely, and extract nested actions.

  3. Report actions to chat and latest.log, and save the raw JSON under the config directory.

Created Dialog Diagnoser as a client-only Fabric mod. It detects dialog screens once per screen instance, extracts nested actions, highlights sensitive commands, reports to chat and latest.log, and saves the dialog JSON to config/dialogdiagnoser/last_dialog.json.

Version note: This project targets Minecraft 26.2, not the requested 26.1.2, to match the Fabric target available in this workspace.

To build and download the jar:

  1. Click Compile in the top bar (on phone: โ‹ฎ โ†’ Compile Project).

  2. Click Compile in the window.

  3. On success, click Download on the build or Download dialog-diagnoser.jar in chat.

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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.serialization.JsonOps;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.dialog.DialogScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.server.dialog.Dialog;
import net.minecraft.resources.RegistryOps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public final class DialogDiagnoserClient implements ClientModInitializer {
	private static final Logger LOGGER = LoggerFactory.getLogger("dialogdiagnoser");
	private static final Gson PRETTY_GSON = new GsonBuilder().setPrettyPrinting().create();
	private static WeakReference<Screen> LAST_REPORTED_SCREEN = new WeakReference<>(null);
	private static final Set<String> ACTION_TYPES = Set.of(
			"run_command", "dynamic/run_command", "suggest_command", "open_url", "open_file",
			"copy_to_clipboard", "change_page", "show_dialog", "custom", "dynamic/custom"
	);
	private static final Set<String> SENSITIVE_COMMANDS = Set.of(
			"op", "deop", "stop", "ban", "ban-ip", "pardon", "kick", "whitelist",
			"execute", "function", "reload", "transfer", "data"
	);

	@Override
	public void onInitializeClient() {
		ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
			if (!(screen instanceof DialogScreen<?>) || LAST_REPORTED_SCREEN.get() == screen) {
				return;
			}

			LAST_REPORTED_SCREEN = new WeakReference<>(screen);
			diagnose(client, screen);
		});
	}

	private static void diagnose(Minecraft client, Screen screen) {
		Dialog dialog = findDialog(screen);
		if (dialog == null) {
			LOGGER.warn("Could not find a Dialog field on {}", screen.getClass().getName());
			return;
		}

		JsonElement json;
		try {
			var connection = client.getConnection();
			if (connection != null) {
				json = Dialog.DIRECT_CODEC.encodeStart(
						RegistryOps.create(JsonOps.INSTANCE, connection.registryAccess()), dialog
				).getOrThrow();
			} else {
				json = Dialog.DIRECT_CODEC.encodeStart(JsonOps.INSTANCE, dialog).getOrThrow();
			}
		} catch (RuntimeException exception) {
			LOGGER.error("Could not encode the dialog for inspection", exception);
			return;
		}

		saveDialogJson(client, json);

		String title = json.isJsonObject() ? flattenComponent(json.getAsJsonObject().get("title")) : "";
		String type = json.isJsonObject() ? stripMinecraft(stringValue(json.getAsJsonObject().get("type"))) : "unknown";
		if (title.isBlank()) {
			title = "<untitled>";
		}
		if (type.isBlank()) {
			type = "unknown";
		}

		List<ReportedAction> actions = new ArrayList<>();
		walk(json, "<unknown>", false, true, type.equals("notice"), actions);

		List<String> lines = new ArrayList<>();
		lines.add("Dialog: " + title + " | type: " + type + " | actions: " + actions.size());
		for (ReportedAction action : actions) {
			String line = "- [" + action.type() + "] button: \"" + action.label() + "\" -> " + action.detail();
			if (action.alsoOnClose()) {
				line += " [also fires on ESC/close]";
			}
			lines.add(line);
		}

		for (int index = 0; index < lines.size(); index++) {
			String line = lines.get(index);
			LOGGER.info("{}", line);
			var message = Component.literal(line);
			if (index > 0 && actions.get(index - 1).sensitive()) {
				message = message.withStyle(ChatFormatting.RED);
			}
			client.gui.hud.getChat().addClientSystemMessage(message);
		}
	}

	private static Dialog findDialog(Screen screen) {
		for (Class<?> current = screen.getClass(); current != null; current = current.getSuperclass()) {
			for (Field field : current.getDeclaredFields()) {
				if (Modifier.isStatic(field.getModifiers()) || !Dialog.class.isAssignableFrom(field.getType())) {
					continue;
				}

				try {
					if (!field.trySetAccessible()) {
						continue;
					}
					Object value = field.get(screen);
					if (value instanceof Dialog dialog) {
						return dialog;
					}
				} catch (ReflectiveOperationException | RuntimeException exception) {
					LOGGER.warn("Could not read dialog field {} on {}", field.getName(), current.getName(), exception);
				}
			}
		}
		return null;
	}

	private static void saveDialogJson(Minecraft client, JsonElement json) {
		Path output = client.gameDirectory.toPath().resolve("config/dialogdiagnoser/last_dialog.json");
		try {
			Files.createDirectories(output.getParent());
			Files.writeString(output, PRETTY_GSON.toJson(json), StandardCharsets.UTF_8);
		} catch (IOException exception) {
			LOGGER.error("Could not save dialog JSON to {}", output, exception);
		}
	}

	private static void walk(JsonElement element, String inheritedLabel, boolean alsoOnClose,
			boolean root, boolean noticeDialog, List<ReportedAction> actions) {
		if (element == null || element.isJsonNull()) {
			return;
		}
		if (element.isJsonArray()) {
			for (JsonElement child : element.getAsJsonArray()) {
				walk(child, inheritedLabel, alsoOnClose, false, false, actions);
			}
			return;
		}
		if (!element.isJsonObject()) {
			return;
		}

		JsonObject object = element.getAsJsonObject();
		String label = inheritedLabel;
		if (object.has("label")) {
			String buttonLabel = flattenComponent(object.get("label"));
			if (!buttonLabel.isBlank()) {
				label = buttonLabel;
			}
		} else if (object.has("click_event")) {
			String componentText = flattenComponent(object);
			if (!componentText.isBlank()) {
				label = componentText;
			}
		}

		String type = stripMinecraft(stringValue(object.get("type")));
		if (ACTION_TYPES.contains(type)) {
			actions.add(describeAction(type, object, label, alsoOnClose));
		}

		String clickAction = stripMinecraft(stringValue(object.get("action")));
		if (ACTION_TYPES.contains(clickAction)) {
			actions.add(describeAction(clickAction, object, label, alsoOnClose));
		}

		for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
			String key = entry.getKey();
			boolean childAlsoOnClose = alsoOnClose
					|| key.equals("exit_action")
					|| key.equals("no")
					|| (root && noticeDialog && key.equals("action"));
			walk(entry.getValue(), label, childAlsoOnClose, false, false, actions);
		}
	}

	private static ReportedAction describeAction(String type, JsonObject object, String label, boolean alsoOnClose) {
		String detail;