PlayerCapability

Версия Minecraft
1.16.5
API
Forge
30
3
2
Написал PlayerCapability

Java:
public class PlayerCapabilities implements ICapabilitySerializable<CompoundNBT> {
    private int breath;
    private int selectKata;
    private int[] learnedKata;
    public static final Capability<PlayerCapabilities> CAPABILITY = null;
    private final LazyOptional<PlayerCapabilities> holder = LazyOptional.of(() -> this);

    public PlayerCapabilities() {
        this.breath = 0;
        this.breath = 0;
        this.learnedKata = new int[15];
    }

    public int getBreath() {
        return this.breath;
    }

    public void setBreath(int breath) {
        this.breath = breath;
    }

    public int getSelected() {
        return this.selectKata;
    }

    public void setSelectKata(int selectKata) {
        this.selectKata = selectKata;
    }

    public int[] getLearnedKata() {
        return this.learnedKata;
    }

    public void setLearnedKata(int[] field3) {
        this.learnedKata = learnedKata;
    }

    @Override
    public CompoundNBT serializeNBT() {
        CompoundNBT nbt = new CompoundNBT();
        nbt.putInt("breath", breath);
        nbt.putInt("selectedKata", selectKata);
        ListNBT listNBT = new ListNBT();
        for (int i : learnedKata) {
            listNBT.add(IntNBT.valueOf(i));
        }
        nbt.put("field3", listNBT);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        breath = nbt.getInt("breath");
        selectKata = nbt.getInt("selectKata");
        ListNBT listNBT = nbt.getList("learnedKata", Constants.NBT.TAG_INT);
        learnedKata = new int[listNBT.size()];
        for (int i = 0; i < listNBT.size(); i++) {
            learnedKata[i] = listNBT.getInt(i);
        }
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        return cap == PlayerCapabilities.CAPABILITY ? holder.cast() : LazyOptional.empty();
    }
}
Написал комманду для уст playercapability
Java:
package net.light.demonslayer.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.light.demonslayer.capabilities.PlayerCapabilities;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.*;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;

import java.util.List;


public class SetBreathCommand {
    public static void register(CommandDispatcher<CommandSource> dispatcher) {
        LiteralCommandNode<CommandSource> setBreathCommand = dispatcher.register(
                Commands.literal("setbreath")
                        .then(Commands.argument("value", IntegerArgumentType.integer())
                                .executes(context -> setBreath(context.getSource(), IntegerArgumentType.getInteger(context, "value")))
                        )
        );
    }
    private static int setBreath(CommandSource source, int value) {
        if (!(source.getEntity() instanceof PlayerEntity)) {
            source.sendFailure(new TranslationTextComponent("commands.setbreath.error"));
            return 0;
        }
        PlayerEntity player = (PlayerEntity) source.getEntity();
        PlayerCapabilities cap = player.getCapability(PlayerCapabilities.CAPABILITY, null).orElse(new PlayerCapabilities());
        cap.setBreath(value);
        source.sendSuccess(new TranslationTextComponent("commands.setbreath.success", value), true);
        return 1;
    }
}
Зарегал
Java:
public class RegisterCommandsEvent {
    @SubscribeEvent
    public static void registerCommands(net.minecraftforge.event.RegisterCommandsEvent event)
    {
        SetBreathCommand.register(event.getDispatcher());
        ConfigCommand.register(event.getDispatcher());
    }
    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event) {
        PlayerEntity original = event.getOriginal();
        PlayerEntity player = event.getPlayer();
        PlayerCapabilities originalCap = original.getCapability(PlayerCapabilities.CAPABILITY, null).orElse(new PlayerCapabilities());
        PlayerCapabilities newCap = player.getCapability(PlayerCapabilities.CAPABILITY, null).orElse(new PlayerCapabilities());
        newCap.setBreath(originalCap.getBreath());
        newCap.setSelectKata(originalCap.getSelected());
        newCap.setLearnedKata(originalCap.getLearnedKata().clone());
    }

    @SubscribeEvent
    public static void onWorldUnload(WorldEvent.Unload event) {
        if (event.getWorld() instanceof ServerWorld) {
            ServerWorld world = (ServerWorld) event.getWorld();
            for (ServerPlayerEntity player : world.getPlayers(ServerPlayerEntity::isAlive)) {
                PlayerCapabilities cap = player.getCapability(PlayerCapabilities.CAPABILITY, null).orElse(new PlayerCapabilities());
                File file = new File("saves/" + world.dimension().getRegistryName() + "/capabilities/" + player.getUUID() + ".nbt");
                CompoundNBT nbt = new CompoundNBT();
                nbt = cap.serializeNBT();
                try (FileOutputStream stream = new FileOutputStream(file)) {
                    CompressedStreamTools.writeCompressed(nbt, stream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
И зарегал капу
Java:
public static void registerCapabilities() {
        CapabilityManager.INSTANCE.register(PlayerCapabilities.class, new PlayerCapabilitiesStorage(), PlayerCapabilities::new);
    }

    public static void attachCapabilityToPlayer(PlayerEntity player) {
        player.getCapability(PlayerCapabilities.CAPABILITY, null).ifPresent(cap -> cap.setBreath(0));
    }
И сделал кейбинд чтоб дебажить
Java:
@SubscribeEvent
    public static void onKeyPress(InputEvent.KeyInputEvent event) {
        Minecraft mc = Minecraft.getInstance();
        PlayerEntity player = Minecraft.getInstance().player;
        PlayerCapabilities cap = player.getCapability(PlayerCapabilities.CAPABILITY, null).orElse(new PlayerCapabilities());

        if (mc.screen == null) {
            if (RadialMenuKeybind.isDown()) {
                System.out.println(breath);
            }
        }
}
И при нажатии пишет 0 хоть я написал комманду setbreath 1
В чём ошибка
 
Сверху