Skip to content

Instantly share code, notes, and snippets.

@KingAlterIV
Last active August 2, 2024 01:55
Show Gist options
  • Save KingAlterIV/168eb0670151476c144b2cd70373e71e to your computer and use it in GitHub Desktop.
Save KingAlterIV/168eb0670151476c144b2cd70373e71e to your computer and use it in GitHub Desktop.
The proper way of adding a prefix and suffix to nametags of a player using Scoreboard Teams made with packets and is possibly horribly made by me!

Feel free to update and expand upon this.

Requirements

  • ProtocolLib

Code

package your.package.here;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;

public class Nametag {

    PacketContainer packet;

    public Nametag(Player player) {
        this.packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);
        String name = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
        this.packet.getIntegers().write(1, 0);
        this.packet.getStrings().write(0, name);
        this.packet.getChatComponents().write(0, WrappedChatComponent.fromText(player.toString()));
        this.packet.getSpecificModifier(Collection.class).write(0, Collections.singletonList(player.getName()));
    }

    public Nametag setPrefix(String prefix) {
        this.packet.getChatComponents().write(1, WrappedChatComponent.fromText(ChatColor.translateAlternateColorCodes('&', prefix) + " "));
        return this;
    }

    public Nametag setSuffix(String suffix) {
        this.packet.getChatComponents().write(2, WrappedChatComponent.fromText(" " + ChatColor.translateAlternateColorCodes('&', suffix)));
        return this;
    }

    public void build() {
        for (Player p : Bukkit.getOnlinePlayers()) {
            try {
                ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
            } catch (InvocationTargetException e) {
                throw new RuntimeException("Cannot send packet " + packet, e);
            }
        }
    }
}

Usage

new Nametag(player).setPrefix("&6Test").setSuffix("&aTest").build();

// 1.13+
new Nametag(player).setPrefix("&6Testttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt").setSuffix("&aTestttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt").build();

Image

Image

1.13+ Image2

@heytek
Copy link

heytek commented Dec 29, 2022

this is very helpful, thank you for it, I have 1 issue the name is permanently colored black, I add colors to prefix and suffix and it works, but I can't figure out how to change the player name color, can you help with this?

I also have this problem

@imDMK
Copy link

imDMK commented Mar 16, 2023

Not work on 1.19.2.
Will you improve the code to work with the latest versions?
Thanks!

@Ayouuuu
Copy link

Ayouuuu commented Aug 4, 2023

this is very helpful, thank you for it, I have 1 issue the name is permanently colored black, I add colors to prefix and suffix and it works, but I can't figure out how to change the player name color, can you help with this?

minecraft 1.18.2

InternalStructure structure = this.packet.getOptionalStructures().readSafely(0).get();
structure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0, color);

@Ayouuuu
Copy link

Ayouuuu commented Aug 4, 2023

1.18.2

/**
 * Minecraft version 1.18.2
 */
public class Nametag {

    PacketContainer packet;
    private final InternalStructure structure;
    private final Player player;

    public Nametag(Player player) {
        this.player = player;
        this.packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);
        String name = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
        this.packet.getIntegers().write(0, 0);
        this.packet.getStrings().write(0, name);
        this.packet.getSpecificModifier(Collection.class).write(0, Collections.singletonList(player.getName()));
        this.structure = this.packet.getOptionalStructures().readSafely(0).get();
    }

    public Nametag setPrefix(String prefix) {
        this.structure.getChatComponents().write(1, WrappedChatComponent.fromText(ChatColor.translateAlternateColorCodes('&', prefix)));
        return this;
    }

    public Nametag color(ChatColor color) {
        this.structure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat"))
                .write(0, color);
        return this;
    }

    public Nametag setSuffix(String suffix) {
        this.structure.getChatComponents().write(2, WrappedChatComponent.fromText(ChatColor.translateAlternateColorCodes('&', suffix)));
        return this;
    }

    public void build(Collection<? extends Player> players) {
        if (players.isEmpty()) return;
        this.structure.getIntegers().write(0, 3);
        this.packet.getOptionalStructures().write(0, Optional.of(structure));
        for (Player p : players) {
            try {
                ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
            } catch (Exception e) {
                throw new RuntimeException("Cannot send packet " + packet, e);
            }
        }
    }

    public void build() {
        this.build(Bukkit.getOnlinePlayers());
    }
}

@Redblock6YT
Copy link

You're an angel ily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment