Иконка ресурса

Руководство Создать scoreboard через NMS для 1.21/1.21.1

Поддерживаемые версии
  1. 1.21
Java:
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.numbers.BlankFormat;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundSetObjectivePacket;
import net.minecraft.world.scores.*;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import net.minecraft.network.protocol.game.*;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;

import static net.minecraft.world.scores.DisplaySlot.SIDEBAR;

public class CustomScoreBoard {

    private String id;
    private String title;
    private List<String> line;

    public CustomScoreBoard(String id, String title, String... line) {
        this.id = id;
        this.title = title;
        this.line = List.of(line);
    }

    public static void remove(Player player){
        CraftPlayer craftPlayer = (CraftPlayer) player;
        sendPacket(new ClientboundSetDisplayObjectivePacket(SIDEBAR,null),craftPlayer);
    }

    public void create(Player player){
        CraftPlayer craftPlayer = (CraftPlayer) player;
        Scoreboard scoreboard = new Scoreboard();
        Objective obj = scoreboard.addObjective(id,ObjectiveCriteria.AIR,Component.nullToEmpty(title),ObjectiveCriteria.RenderType.INTEGER,false, BlankFormat.INSTANCE);
        sendPacket(new ClientboundSetObjectivePacket(obj,0),craftPlayer);
        sendPacket(new ClientboundSetDisplayObjectivePacket(SIDEBAR,obj),craftPlayer);
        int i = 100;
        for(String lines : getLine()){
            sendPacket(new ClientboundSetScorePacket(lines,id,i, Optional.ofNullable(Component.nullToEmpty(lines)), Optional.ofNullable(obj.numberFormat())),craftPlayer);
            i--;
        }

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getLine() {
        return line;
    }

    public void setLine(List<String> line) {
        this.line = line;
    }

    private static void sendPacket(Packet<?> packet, Player player) {
        ((CraftPlayer) player).getHandle().connection.send(packet);
    }

}

Java:
    @EventHandler
    public void onJoin(PlayerJoinEvent e){
        new CustomScoreBoard("test","CustomScoreBoard","by Hahatyn","for spigotmc.ru").create(e.getPlayer());
    }

Итог:
Снимок экрана (273).png
  • Мне нравится (+1)
Реакции: xentany
Автор
Hahatyn
Просмотры
869
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок

Другие ресурсы пользователя Hahatyn

Поделиться ресурсом

Назад
Сверху Снизу