make this mod as working properly
๐
pasted-code-1789122089842.txt
txt
142 lines
โถ
Create a professional Minecraft Fabric mod feature called "Ping Display".
PURPOSE:
Add a clean, customizable ping display that shows the player's current network latency (ping) in-game. The display should be lightweight, accurate, modern, and optimized for PvP/SMP gameplay.
SETTINGS LOCATION:
Add the feature to:
Video Settings โ Extras โ Ping Display
The player must be able to easily toggle the feature ON/OFF from the Extras section.
CONFIGURATION:
Add these settings under:
Video Settings โ Extras โ Ping Display
1. Enable Ping Display
- Boolean toggle
- ON/OFF
- Default: ON
2. Show Ping Unit
- Display the ping with "ms"
- Example: "42 ms"
3. Color Mode
- Dynamic color based on ping
- Static color option
- Allow the player to choose a custom static color if supported.
PING COLOR SYSTEM:
Use the following latency ranges and colors:
โค 50 ms
Name: Pure Green
Hex: #26FF0A
51โ100 ms
Name: Yellow-Green
Hex: #80FF0A
101โ150 ms
Name: Yellow
Hex: #D9FF0A
151โ175 ms
Name: Orange-Yellow
Hex: #FFE60A
176โ200 ms
Name: Orange
Hex: #FF8C0A
201โ225 ms
Name: Red-Orange
Hex: #FF330A
โฅ 250 ms
Name: Pure Red
Hex: #FF0A0A
For the missing range between 226โ249 ms, smoothly transition/interpolate between Red-Orange and Pure Red so there is no sudden visual jump.
DISPLAY:
Create a clean Minecraft-style HUD element.
Default display:
Ping: 42 ms
The numerical ping value should dynamically update according to the player's actual server latency.
The color of the ping value should automatically change according to the configured latency range.
Example:
Ping: 35 ms โ green
Ping: 87 ms โ yellow-green
Ping: 132 ms โ yellow
Ping: 168 ms โ orange-yellow
Ping: 194 ms โ orange
Ping: 214 ms โ red-orange
Ping: 260 ms โ red
UI/UX:
- Keep the design minimal and professional.
- Do not make the HUD oversized.
- Use clean Minecraft-compatible text rendering.
- Avoid unnecessary animations or visual clutter.
- Make sure the display remains readable during PvP.
- Support Minecraft GUI scaling correctly.
- Make the feature compatible with different screen resolutions and GUI scales.
- Prevent the ping text from overlapping important HUD elements.
- Allow the display position to be configurable if the mod's configuration system supports HUD positioning.
PERFORMANCE:
- Do not constantly create unnecessary objects every frame.
- Avoid expensive calculations.
- Update the displayed ping efficiently.
- Do not cause FPS drops.
- Do not interfere with Minecraft's networking system.
- Do not modify or replace vanilla networking behavior.
COMPATIBILITY:
- Fabric Loader
- Fabric API
- The target Minecraft version of the project
- Compatible with common client-side optimization and PvP mods where possible.
- The feature must work on multiplayer servers.
- Do not require server-side installation.
IMPORTANT:
Use the Minecraft client's available player/server latency information instead of attempting to calculate ping incorrectly from frame time or other unrelated values.
CONFIGURATION REQUIREMENTS:
The setting must actually work in-game.
The user should be able to:
Video Settings
โ Extras
โ Ping Display
โ Enable/Disable
When disabled, the ping HUD must completely disappear and consume minimal resources.
When enabled, it must immediately appear without requiring a game restart whenever possible.
CODE QUALITY:
- Use clean, modular, maintainable Java code.
- Follow Fabric/Minecraft client-side coding conventions.
- Keep the feature separated from unrelated mod functionality.
- Add comments only where useful.
- Avoid deprecated APIs when an appropriate modern API exists.
- Handle null player/server states safely.
- Prevent crashes when the player is in the main menu, singleplayer, disconnected, or joining/leaving a server.
VISUAL REFERENCE:
Use the provided reference image as inspiration for the ping color system and professional visual presentation.
Do NOT copy the UI exactly. Create an original, polished Minecraft-compatible implementation inspired by the reference.
FINAL REQUIREMENT:
Deliver a fully functional implementation, not a mockup or placeholder. All settings must be connected to the actual rendering logic and the ping value must update correctly in real gameplay.
Set up a Fabric 1.21.11 client-side mod project with Yarn mappings.
Add persisted ping-display settings and an Extras โ Ping Display menu path from Video Settings.
Render cached multiplayer latency through the modern HUD registry with dynamic/static colors and configurable placement.
Implemented the client-side Fabric 1.21.11 Ping Display mod.
Adds Video Settings โ Extras โ Ping Display.
Includes enable toggle,
msunit toggle, dynamic/static color mode, editable static hex color, and four HUD positions.Reads actual multiplayer latency from the client player-list entry and refreshes it efficiently every 10 ticks.
Uses modern
HudElementRegistry, supports GUI scaling, disappears completely when disabled, and safely avoids rendering outside connected multiplayer states.
package com.kodari.pingdisplay.client;
import com.kodari.pingdisplay.PingDisplay;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.util.Identifier;
public final class PingDisplayClient implements ClientModInitializer {
private int ticksUntilRefresh;
@Override
public void onInitializeClient() {
PingDisplayConfig.load();
HudElementRegistry.addLast(Identifier.of(PingDisplay.MOD_ID, "ping_display"), PingHudRenderer::render);
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (--ticksUntilRefresh <= 0) {
PingHudRenderer.refresh(client);
ticksUntilRefresh = 10;
}
});
}
}