socolis
📎 1 file 2,663 chars
📄
pasted-code-1795313848038.txt
txt 28 lines
▶
Create a purely client-side Fabric 26.3 mod called "ScopedPrefixes" that adds visual prefix nicknames before players' real usernames on my screen only. Do not include or register any chat commands at all—the mod must be controlled exclusively through a Mod Menu config screen.

Core Features:
1. Prefix-Only Visual Display:
   - Whenever an enabled entry matches a player's real username, display the formatted nickname strictly before their real username with a space in between (for example, if the real username is "Socolis" and the nickname is "&lVIP", render it as "VIP Socolis" with VIP in bold).
   - Apply this prefix visually in chat messages, the player tab list, and standard overhead nametags.
   - IMPORTANT: Only modify the text string of nametags that Minecraft is already rendering. Do NOT alter nametag render distance, visibility rules, or render nametags through walls.

2. Colors, Formatting & Rainbow Support (& Codes):
   - Support all standard Minecraft & color codes (&0-&9, &a-&f) and formatting codes (&l bold, &o italic, &n underline, &m strikethrough, &r reset).
   - Add a custom &z code that renders the text following it with a smooth animated rainbow color cycle.

3. Optional Server Address / IP Scoping:
   - Each nickname entry has an optional Server Address / IP field (e.g., "donutsmp.net" or "192.168.1.1:25565").
   - If a server address is entered, the prefix only shows when connected to that matching server IP or base domain (e.g., "play.donutsmp.net" matches "donutsmp.net") and nowhere else.
   - If the Server Address field is left blank, the prefix applies globally across all servers and singleplayer (with server-specific entries taking priority over global ones).

4. Mod Menu GUI Screen (No Commands):
   - Integrate with Mod Menu (ModMenuApi) so clicking the mod's config button opens a clean, scrollable custom GUI screen.
   - Create / Edit Form: Include three text input boxes:
     1. Target Username (required)
     2. Nickname / Prefix (required, with a live styled preview showing how " " will look in-game)
     3. Server Address / IP (optional — leave blank for Global)
   - Saved Entries List: Display all created entries in a scrollable list showing the live-styled prefix preview, target username, server scope (or "Global"), an Enable / Disable toggle button for each individual nickname, an Edit button, and a Delete button.
   - Include a master Mod Enabled / Disabled toggle at the top of the screen and a Save & Close button.

5. Local JSON Persistence:
   - Automatically save all entries, their enabled/disabled states, and the master toggle in config/scopedprefixes.json.
Kodari AI
KodariToday at 2:22 am 5
ScopedPrefixes
A client-side Fabric mod that displays visual prefix nicknames before players' real usernames on the user's screen only. Supports all standard Minecraft color and formatting codes plus a custom animated rainbow code. Each entry can be scoped to a specific server IP or domain, with global fallback. Controlled exclusively through a Mod Menu config screen featuring a scrollable entries list with live-styled previews, per-entry enable/disable toggles, edit and delete buttons, and a master mod toggle. All data persists locally in JSON format.
  • Add client-only prefix matching, formatting, and local JSON persistence.

  • Integrate the three vanilla display paths without changing nametag visibility.

  • Build the scrollable Mod Menu editor with live previews.

Created ScopedPrefixes with a Mod Menu editor, local JSON persistence, server scoping, and client-only name decoration. No commands were added.

Rainbow prefixes animate in the tab list, nametags, and previews. Chat prefixes keep the rainbow colors from when each message arrived; existing chat lines do not animate. The chat hook applies to standard player chat, not server-generated system messages.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
13 472
src/client/java/com/scopedprefixes/client/PrefixScreen.java
package com.scopedprefixes.client;

import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.TextBoxComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.HorizontalAlignment;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.OwoUIAdapter;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.core.VerticalAlignment;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.ArrayList;
import java.util.List;

public class PrefixScreen extends BaseOwoScreen<FlowLayout> {
    private final Screen parent;
    private final PrefixConfig config = PrefixConfig.INSTANCE;
    private final List<Runnable> previews = new ArrayList<>();
    private FlowLayout list;
    private TextBoxComponent username;
    private TextBoxComponent prefix;
    private TextBoxComponent server;
    private LabelComponent preview;
    private LabelComponent status;
    private PrefixConfig.Entry editing;

    public PrefixScreen(Screen parent) {
        super(Component.literal("ScopedPrefixes"));
        this.parent = parent;
    }

    @Override
    protected OwoUIAdapter<FlowLayout> createAdapter() {
        return OwoUIAdapter.create(this, UIContainers::verticalFlow);
    }

    @Override
    protected void build(FlowLayout root) {
        root.surface(Surface.VANILLA_TRANSLUCENT);
        root.alignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
        FlowLayout panel = UIContainers.verticalFlow(Sizing.fixed(330), Sizing.fill(94));
        panel.padding(Insets.of(8));
        panel.gap(4);
        panel.surface(Surface.DARK_PANEL);
        panel.child(UIComponents.label(Component.literal("ScopedPrefixes")));
        panel.child(UIComponents.checkbox(Component.literal("Mod enabled")).checked(config.enabled).onChanged(value -> {
            config.enabled = value;
            config.save();
        }));
        username = UIComponents.textBox(Sizing.fill(100));
        username.setHint(Component.literal("Target username (required)"));
        username.setMaxLength(16);
        prefix = UIComponents.textBox(Sizing.fill(100));
        prefix.setHint(Component.literal("Nickname / prefix (required)"));
        prefix.setMaxLength(128);
        server = UIComponents.textBox(Sizing.fill(100));
        server.setHint(Component.literal("Server address (optional; blank = global)"));
        server.setMaxLength(255);
        panel.child(username);
        panel.child(prefix);
        panel.child(server);
        preview = UIComponents.label(Component.literal("Preview"));
        panel.child(preview);
        status = UIComponents.label(Component.literal("Create an entry"));
        panel.child(status);
        FlowLayout actions = UIContainers.horizontalFlow(Sizing.fill(100), Sizing.content());
        actions.gap(4);
        actions.child(UIComponents.button(Component.literal("Save entry"), button -> saveEntry()));
        actions.child(UIComponents.button(Component.literal("Clear form"), button -> clearForm()));
        panel.child(actions);
        panel.child(UIComponents.label(Component.literal("Saved entries")));
        list = UIContainers.verticalFlow(Sizing.fill(100), Sizing.content());
        list.gap(5);
        list.margins(Insets.right(5));
        panel.child(UIContainers.verticalScroll(Sizing.fill(100), Sizing.expand(), list));
        panel.child(UIComponents.button(Component.literal("Save & Close"), button -> onClose()));
        root.child(panel);
        refreshList();
        refreshPreview();
    }

    private void saveEntry() {
        String name = username.getValue().trim();
        String text = prefix.getValue().trim();
        if (name.isEmpty() || text.isEmpty() || !name.matches("[A-Za-z0-9_]{1,16}")) {
            status.text(Component.literal("Enter a valid username and a non-empty prefix."));
            return;
        }
        if (editing == null) {
            editing = new PrefixConfig.Entry();
            config.entries.add(editing);
        }
        editing.username = name;
        editing.prefix = text;
        editing.server = server.getValue().trim();
        config.save();
        clearForm();
        refreshList();
    }

    private void clearForm() {
        editing = null;
        username.text("");
        prefix.text("");
        server.text("");
        status.text(Component.literal("Create an entry"));
    }

    private void refreshList() {
        list.clearChildren();
        previews.clear();
        for (PrefixConfig.Entry entry : config.entries) {
            FlowLayout row = UIContainers.verticalFlow(Sizing.fill(100), Sizing.content());
            row.gap(2);
            LabelComponent label = UIComponents.label(Component.literal(""));
            row.child(label);
            previews.add(() -> label.text(Prefixes.format(entry.prefix).append(" ").append(Component.literal(entry.username))));
            row.child(UIComponents.label(Component.literal(entry.server == null || entry.server.isBlank() ? "Global" : entry.server)));
            FlowLayout buttons = UIContainers.horizontalFlow(Sizing.fill(100), Sizing.content());
            buttons.gap(4);
            buttons.child(UIComponents.button(Component.literal(entry.enabled ? "Enabled" : "Disabled"), button -> {
                entry.enabled = !entry.enabled;
                button.setMessage(Component.literal(entry.enabled ? "Enabled" : "Disabled"));
                config.save();
            }));
            buttons.child(UIComponents.button(Component.literal("Edit"), button -> {
                editing = entry;
                username.text(entry.username);
                prefix.text(entry.prefix);
                server.text(entry.server == null ? "" : entry.server);
                status.text(Component.literal("Editing entry"));
            }));
            buttons.child(UIComponents.button(Component.literal("Delete"), button -> {
                config.entries.remove(entry);
                if (editing == entry) clearForm();
                config.save();
                refreshList();
            }));
            row.child(buttons);
            list.child(row);
        }
        for (Runnable update : previews) update.run();
    }

    private void refreshPreview() {
        preview.text(Prefixes.format(prefix.getValue()).append(" ").append(Component.literal(username.getValue().isBlank() ? "Username" : username.getValue())));
    }

    @Override
    public void tick() {
        super.tick();
        refreshPreview();
        for (Runnable update : previews) update.run();
    }

    @Override
    public void onClose() {
        config.save();
        this.minecraft.gui.setScreen(parent);
    }
}