No chat to show

This project's files are all here to explore.

Files
.github
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
89 2,927
src/main/java/dev/vale/mod/ValeClient.java
package dev.vale.mod;

import com.mojang.blaze3d.platform.InputConstants;
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.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.Identifier;
import net.minecraft.sounds.SoundSource;
import org.lwjgl.glfw.GLFW;

/**
 * Vale SMP client:
 *  - handshake (server kicks you without it on the main server)
 *  - keybinds menu: Ability 1, Ability 2, Edit HUD (Options > Controls > Vale SMP)
 *  - cooldown HUD, Nevaeh's dialogue box, flash / shake / tint, Cielo's colour-invert + sound cut
 * NOTE (1.21.11): ResourceLocation is now Identifier; keybind categories are KeyMapping.Category.
 */
public class ValeClient implements ClientModInitializer {
    public static final String VERSION = "1.1.0";
    private static KeyMapping ABILITY1, ABILITY2, EDIT_HUD;
    private static boolean helloSent = false;
    private static int joinTicks = 0;
    private static long invertEnd = 0;
    private static double savedVolume = -1;

    @Override
    public void onInitializeClient() {
        HudConfig.load();
        PayloadTypeRegistry.playC2S().register(Payloads.Hello.TYPE, Payloads.Hello.CODEC);
        PayloadTypeRegistry.playC2S().register(Payloads.Key.TYPE, Payloads.Key.CODEC);
        PayloadTypeRegistry.playS2C().register(Payloads.Fx.TYPE, Payloads.Fx.CODEC);
        PayloadTypeRegistry.playS2C().register(Payloads.State.TYPE, Payloads.State.CODEC);
        PayloadTypeRegistry.playS2C().register(Payloads.Dialogue.TYPE, Payloads.Dialogue.CODEC);

        // ---- keybinds (shows up in Controls as its own "Vale SMP" category)
        KeyMapping.Category cat = KeyMapping.Category.register(Identifier.fromNamespaceAndPath("vale", "main"));
        ABILITY1 = KeyBindingHelper.registerKeyBinding(new KeyMapping("key.vale.ability1", InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_Z, cat));
        ABILITY2 = KeyBindingHelper.registerKeyBinding(new KeyMapping("key.vale.ability2", InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_X, cat));
        EDIT_HUD = KeyBindingHelper.registerKeyBinding(new KeyMapping("key.vale.edit_hud", InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_EQUAL, cat));

        // ---- HUD layer
        HudElementRegistry.addLast(Identifier.fromNamespaceAndPath("vale", "hud"), (g, dt) -> ValeHud.render(g, Minecraft.getInstance(), false));

        // ---- incoming
        ClientPlayConnectionEvents.JOIN.register((h, s, c) -> { helloSent = false; joinTicks = 0; });
        ClientPlayConnectionEvents.DISCONNECT.register((h, c) -> clearInvert(c));
        ClientPlayNetworking.registerGlobalReceiver(Payloads.State.TYPE, (p, ctx) -> ctx.client().execute(() -> ValeHud.setState(p)));
        ClientPlayNetworking.registerGlobalReceiver(Payloads.Dialogue.TYPE, (p, ctx) -> ctx.client().execute(() -> ValeHud.say(p)));
        ClientPlayNetworking.registerGlobalReceiver(Payloads.Fx.TYPE, (p, ctx) -> ctx.client().execute(() -> {
            if (p.effect().equals("invert")) startInvert(ctx.client(), p.ticks());
            else if (p.effect().equals("clear")) clearInvert(ctx.client());
            else ValeHud.fx(p);
        }));

        ClientTickEvents.END_CLIENT_TICK.register(mc -> {
            if (mc.player != null && !helloSent) {
                joinTicks++;
                if (joinTicks >= 10 && joinTicks % 10 == 0 && ClientPlayNetworking.canSend(Payloads.Hello.TYPE)) {
                    ClientPlayNetworking.send(new Payloads.Hello(VERSION));
                    helloSent = true;
                }
            }
            if (mc.screen == null && mc.player != null) {
                while (ABILITY1.consumeClick()) if (ClientPlayNetworking.canSend(Payloads.Key.TYPE)) ClientPlayNetworking.send(new Payloads.Key(0));
                while (ABILITY2.consumeClick()) if (ClientPlayNetworking.canSend(Payloads.Key.TYPE)) ClientPlayNetworking.send(new Payloads.Key(1));
                while (EDIT_HUD.consumeClick()) mc.setScreen(new EditHudScreen());
            }
            if (invertEnd != 0 && System.currentTimeMillis() >= invertEnd) clearInvert(mc);
        });
    }

    private static void startInvert(Minecraft mc, int ticks) {
        invertEnd = System.currentTimeMillis() + ticks * 50L;
        mc.gameRenderer.setPostEffect(Identifier.withDefaultNamespace("invert")); // vanilla invert post effect
        var master = mc.options.getSoundSourceOptionInstance(SoundSource.MASTER);
        if (savedVolume < 0) savedVolume = master.get();
        master.set(0.0);
    }

    private static void clearInvert(Minecraft mc) {
        if (invertEnd == 0 && savedVolume < 0) return;
        invertEnd = 0;
        mc.gameRenderer.clearPostEffect();
        if (savedVolume >= 0) { mc.options.getSoundSourceOptionInstance(SoundSource.MASTER).set(savedVolume); savedVolume = -1; }
    }
}