Daniel Guest
📎 1 file 3,764 chars
📄
pasted-code-1794690609263.txt
txt 32 lines
▶
Create a client-side Minecraft Fabric mod called WorldLock that protects access to singleplayer worlds, multiplayer servers, and Realms with a password screen styled to look exactly like the native vanilla Minecraft UI, controlled through an in-game Config GUI instead of chat commands.
Core Requirements:
1. Trigger Events: Intercept connections before loading whenever the player joins a Singleplayer world, joins a Multiplayer server, uses Direct Connect, or joins a Realm. Also allow a configurable keybind (unbound by default) in Controls to manually lock the screen at any time.
2. Native Minecraft UI Styling:
	• Use Minecraft's built-in screen background rendering (the standard dirt/menu background texture or blurred menu panorama depending on the Minecraft version, with the dark translucent gradient overlay).
	• Use standard vanilla TextFieldWidget (176px wide, 20px tall) for the password input box (with the classic black fill, grey 1px border, and white/yellow focus border) and mask typed characters as * by default.
	• Use standard vanilla ButtonWidget components (200px wide, 20px tall) for "Done" and "Cancel" with the classic grey stone button texture, hover highlight, and white Minecraft pixel font with drop shadows.
	• Center a white screen title at the top (e.g., "Enter Password" or "Create Password") matching the layout and spacing of vanilla menus like the Direct Connect screen.
3. Show/Hide Eye Toggle Button:
	• Place a small square vanilla ButtonWidget (20x20 pixels) directly to the right of the password text box (with a 4px gap so the text box + eye button together equal 200px total width, aligning neatly with the buttons below).
	• Render a crisp pixel-art eye icon inside the 20x20 button: an open eye when the password is hidden (*), and a closed eye or eye with a slash through it when the password is visible in plain text.
	• Clicking the button toggles password masking on and off without clearing the typed text, and adds a vanilla tooltip ("Show Password" / "Hide Password") when hovered.
4. Vanilla Sound Effects:
	• Play UI_BUTTON_CLICK when clicking the eye toggle or menu buttons.
	• Play BLOCK_IRON_DOOR_OPEN when the correct password is entered or a new password is created.
	• Play ENTITY_VILLAGER_NO when an incorrect password is submitted.
5. Security & Verification:
	• On first launch, prompt the player to "Create a New Password" and store it as a SHA-256 hash in config/worldlock.json.
	• Add a 15-second cooldown timer displayed in red text (0xFF5555) on the screen if the player enters the wrong password 3 times in a row.
	• Do NOT add any client chat commands.
6. In-Game Config Screen & File (config/worldlock.json):
	• Build a native vanilla-styled Config Screen (using ButtonWidget toggles and TextFieldWidget inputs) that opens via Mod Menu integration (optional dependency) and via a configurable keybind (default unbound).
	• Require the user to enter their current password before opening the Config Screen so nobody can change settings without unlocking it first.
	• Include the following toggleable/editable options in the Config GUI and save them to config/worldlock.json:
		• Mod Enabled: ON / OFF toggle button.
		• Lock Scope: Cycles between All, Servers Only, Worlds Only, and Custom List.
		• Remember Until Quit: ON / OFF toggle button (when ON, only asks for the password once per game launch).
		• Sound Effects: ON / OFF toggle button.
		• Cooldown Seconds: Editable numeric field (default 15).
		• Custom List (Whitelist/Blacklist): Text box or scrollable list to enter specific server IPs or world folder names.
		• Change Password Button: Opens a vanilla screen with "Old Password" and "New Password" fields (with eye toggle buttons) to update the saved SHA-256 password hash.
Kodari AI
KodariNov 14 at 9:17 pm 13
WorldLock
WorldLock is a client-side Fabric mod that secures singleplayer worlds, multiplayer servers, and Realms behind a password screen styled to match vanilla Minecraft UI. It intercepts connection events before world loading, requiring authentication via a native-styled TextFieldWidget with masked input and an eye toggle button. The mod features SHA-256 password hashing stored in config/worldlock.json, a 15-second cooldown after three failed attempts, vanilla sound effects, and a configurable keybind for manual locking. An in-game Config GUI, protected by the current password, allows toggling mod enabled state, lock scope (All, Servers Only, Worlds Only, Custom List), remember-until-quit, sound effects, cooldown duration, custom whitelist/blacklist entries, and password changes. The mod integrates with Mod Menu and uses only vanilla UI components for a seamless native appearance.
  • Add client-side password gates for worlds, servers, Realms, and manual locking.

  • Add password-protected settings, cooldowns, and local JSON persistence.

  • Use vanilla-style controls; client-side locking is a convenience barrier, not protection against someone who can edit local files or remove the mod.

Created WorldLock with password prompts, connection hooks, configurable scopes, cooldowns, keybinds, and Mod Menu settings.

The UI uses vanilla-style components rather than an exact vanilla layout. The connection hooks, particularly Realms and cancelled world launches, still need in-game testing.

Security limitation: This is a client-side barrier, not encryption. Someone with access to the files can remove the mod or alter its configuration.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
15 461
src/client/java/com/worldlock/client/WorldLock.java
package com.worldlock.client;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import io.wispforest.owo.ui.util.UISounds;

public class WorldLock implements ClientModInitializer {
    private static boolean remembered;
    private static boolean startupChecked;
    private static int failures;
    private static long cooldownUntil;
    private static KeyBinding lockKey;
    private static KeyBinding settingsKey;

    @Override
    public void onInitializeClient() {
        WorldLockConfig.load();
        KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("worldlock", "general"));
        lockKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.worldlock.lock", GLFW.GLFW_KEY_UNKNOWN, category));
        settingsKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.worldlock.settings", GLFW.GLFW_KEY_UNKNOWN, category));
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            if (!startupChecked && client.currentScreen != null) {
                startupChecked = true;
                Screen parent = client.currentScreen;
                if (WorldLockConfig.instance.passwordHash.isEmpty()) open(parent, () -> client.setScreen(parent), true, false);
            }
            while (lockKey.wasPressed()) {
                Screen parent = client.currentScreen;
                open(parent, () -> client.setScreen(parent), true, false);
            }
            while (settingsKey.wasPressed()) {
                Screen parent = client.currentScreen;
                client.setScreen(configGate(parent));
            }
        });
    }

    public static boolean shouldGate(boolean world, String identity) {
        WorldLockConfig config = WorldLockConfig.instance;
        return config.protects(world, identity) && !(config.rememberUntilQuit && remembered);
    }

    public static void open(Screen parent, Runnable success, boolean forced, boolean cancelAllowed) {
        MinecraftClient.getInstance().setScreen(new PasswordScreen(parent, success, forced, cancelAllowed, false));
    }

    public static Screen configGate(Screen parent) {
        return new PasswordScreen(parent, () -> MinecraftClient.getInstance().setScreen(WorldLockSettings.create(parent)), true, true, false);
    }

    public static int secondsLeft() {
        return (int) Math.max(0, (cooldownUntil - System.currentTimeMillis() + 999) / 1000);
    }

    public static boolean verify(String password, boolean create) {
        WorldLockConfig config = WorldLockConfig.instance;
        if (secondsLeft() > 0 || password.isEmpty()) return false;
        if (create || config.matches(password)) {
            if (create) { config.passwordHash = WorldLockConfig.hash(password); config.save(); }
            failures = 0;
            remembered = true;
            return true;
        }
        if (++failures >= 3) {
            failures = 0;
            cooldownUntil = System.currentTimeMillis() + Math.max(0, config.cooldownSeconds) * 1000L;
        }
        return false;
    }

    public static void play(SoundEvent sound) {
        if (WorldLockConfig.instance.soundEffects) UISounds.play(sound);
    }
}