bl4z35083

make a minecraft mod for 1.21.11 in which it shows player name on xp bar

Kodari AI
KodariOct 14 at 3:58 am 2
XpBarNametag
A Minecraft Fabric mod for version 1.21.11 that displays the player's name directly on the experience bar. The mod replaces the default XP bar text with the player's username, making it easy to identify yourself or others in multiplayer. The name is rendered in a clear, readable format and updates dynamically when the player changes dimension or respawns. Compatible with both survival and creative modes, the mod requires no configuration and works seamlessly with other Fabric mods.

I’ll look up the Fabric HUD and vanilla experience-bar APIs first, then write the mod.

I’ll build this as a client-side Fabric mod. It replaces the level number drawn on the XP bar with the player’s name, using a mixin into vanilla’s Bar.drawExperienceLevel.

* Begin Patch * Add File: build.gradle +plugins {

  • id ‘fabric-loom’ version ‘1.14’

  • id ‘maven-publish’ +}

+version = project.mod_version +group = project.maven_group + +base {

  • archivesName = project.archives_base_name +}

+repositories { +} + +dependencies {

  • minecraft “com.mojang:minecraft:${project.minecraft_version}”

  • mappings “net.fabricmc:yarn:${project.yarn_mappings}:v2”

  • modImplementation “net.fabricmc:fabric-loader:${project.loader_version}” +}

+loom {

  • splitEnvironmentSourceSets()

  • mods {

  • “xpnamebar” {

  • Plain Text
           sourceSet sourceSets.main
    
  • Plain Text
           sourceSet sourceSets.client
    
  • }

  • } +}

+processResources {

  • inputs.property “version”, project.version

  • filesMatching(“fabric.mod.json”) {

  • Plain Text
       expand "version": project.version
    
  • } +}

+tasks.withType(JavaCompile).configureEach {

  • it.options.release = 21 +}

+java {

  • withSourcesJar()

  • sourceCompatibility = JavaVersion.VERSION_21

  • targetCompatibility = JavaVersion.VERSION_21 +} * Add File: settings.gradle +pluginManagement {

  • repositories {

  • maven {

  • Plain Text
           name = 'Fabric'
    
  • Plain Text
           url = 'https://maven.fabricmc.net/'
    
  • }

  • Plain Text
       mavenCentral()
    
  • Plain Text
       gradlePluginPortal()
    
  • } +}

+rootProject.name = ‘xpnamebar’ * Add File: gradle.properties +org.gradle.jvmargs=-Xmx2G +org.gradle.parallel=false + +minecraft_version=1.21.11 +yarn_mappings=1.21.11+build.5 +loader_version=0.19.2 +mod_version=1.0.0 +maven_group=com.example.xpnamebar +archives_base_name=xpnamebar * Add File: src/main/resources/fabric.mod.json +{

  • “schemaVersion”: 1,

  • “id”: “xpnamebar”,

  • “version”: “${version}”,

  • “name”: “XP Bar Name”,

  • “description”: “Shows the player’s name on the XP bar in place of the level number.”,

  • “environment”: “client”,

  • “mixins”: [

  • “xpnamebar.mixins.json”

  • ],

  • “depends”: {

  • “fabricloader”: “>=0.19.2”,

  • “minecraft”: “~1.21.11”,

  • “java”: “>=21”

  • } +} * Add File: src/main/resources/xpnamebar.mixins.json +{

  • “required”: true,

  • “minVersion”: “0.8”,

  • “package”: “com.example.xpnamebar.mixin”,

  • “compatibilityLevel”: “JAVA_21”,

  • “client”: [

  • “BarMixin”

  • ],

  • “injectors”: {

  • “defaultRequire”: 1

  • } +} * Add File: src/client/java/com/example/xpnamebar/mixin/BarMixin.java +package com.example.xpnamebar.mixin;

+import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.hud.bar.Bar; +import net.minecraft.client.network.ClientPlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(Bar.class) +public class BarMixin {

  • @Inject(method = “drawExperienceLevel”, at = @At(“HEAD”), cancellable = true)

  • private static void xpnamebar$drawPlayerName(DrawContext context, TextRenderer textRenderer, int level, CallbackInfo ci) {

  • Plain Text
       ClientPlayerEntity player = MinecraftClient.getInstance().player;
    
  • Plain Text
       if (player == null) {
    
  • Plain Text
           return;
    
  • }

  • Plain Text
       int centerX = context.getScaledWindowWidth() / 2;
    
  • Plain Text
       int y = context.getScaledWindowHeight() - 24 - 9 - 2;
    
  • Plain Text
       context.drawCenteredTextWithShadow(textRenderer, player.getName(), centerX, y, 0xFFFFFF);
    
  • Plain Text
       ci.cancel();
    
  • } +} * End Patch EOF

bl4z35083

where is the mod

Kodari AI
KodariOct 14 at 4:19 am 1

The mod isn’t in the project yet. My previous reply printed the file contents as text instead of creating them, so nothing was written. I’ll check the vanilla signatures and then create the files properly.

Vanilla’s Bar is an interface, so the mixin target is still valid. I’ll create the files now, using the verified signatures.

I made a mistake in that last file: I wrote public interface BarMixin where it must be a class. Fixing that now.

The mod is now in the project root. My previous reply only printed the files as text, so nothing was saved until now.

Files:

  • build.gradle, settings.gradle, gradle.properties: Fabric Loom 1.14 build for 1.21.11 with Yarn mappings and split client sources.

  • src/main/resources/fabric.mod.json: client-only mod descriptor.

  • src/main/resources/xpnamebar.mixins.json: registers the client mixin.

  • src/client/java/com/example/xpnamebar/mixin/BarMixin.java: the mixin that replaces the XP level text with the player’s name.

How it works: vanilla draws the level number through Bar.drawExperienceLevel, a static method on the Bar interface. The mixin cancels that call and draws the player’s name at the same centered spot above the hotbar.

Limitation: vanilla only draws that text when the player’s level is above 0, so at level 0 the name won’t appear. Showing it at level 0 would need a different hook.

To get the jar: click Compile in the top bar, then Compile in the window that opens. When it succeeds, click Download. Players then drop the jar into their mods folder with Fabric Loader 0.19.2 and Fabric API installed. This mod doesn’t use Fabric API directly, but most Fabric setups include it anyway.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 125
src/client/java/com/example/xpnamebar/mixin/BarMixin.java
package com.example.xpnamebar.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.bar.Bar;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Bar.class)
public class BarMixin {
    @Inject(method = "drawExperienceLevel", at = @At("HEAD"), cancellable = true)
    private static void xpnamebar$drawPlayerName(DrawContext context, TextRenderer textRenderer, int level, CallbackInfo ci) {
        ClientPlayerEntity player = MinecraftClient.getInstance().player;
        if (player == null) {
            return;
        }

        int centerX = context.getScaledWindowWidth() / 2;
        int y = context.getScaledWindowHeight() - 24 - 9 - 2;
        context.drawCenteredTextWithShadow(textRenderer, player.getName(), centerX, y, 0xFFFFFF);
        ci.cancel();
    }
}