Не сохраняется капа

Версия Minecraft
1.12.2
1,471
19
189
Сделал капу, но при перезаходе в мир, она не сохраняется.
ThirstProvider
Java:
public class ThirstProvider implements ICapabilitySerializable<NBTTagCompound> {
    @CapabilityInject(IThirstHandler.class)
    public static final Capability<IThirstHandler> CAPABILITY_THIRST = null;

    IThirstHandler instance = CAPABILITY_THIRST.getDefaultInstance();

    @Override
    public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CAPABILITY_THIRST;
    }

    @Nullable
    @Override
    public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
        return hasCapability(capability, facing) ? CAPABILITY_THIRST.<T>cast(instance) : null;
    }

    @Override
    public NBTTagCompound serializeNBT() {
        Logger.debug("Thirst serialized!");
        return (NBTTagCompound) CAPABILITY_THIRST.getStorage().writeNBT(CAPABILITY_THIRST, instance, null);
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt) {
        Logger.debug("Thirst deserialized!");
        CAPABILITY_THIRST.getStorage().readNBT(CAPABILITY_THIRST, instance, null, nbt);
    }
}

DefaultThirstHandler
Java:
public class DefaultThirstHandler implements IThirstHandler {
    private int thirst = 100;
    @Override
    public int getThirst() {
        return thirst;
    }

    @Override
    public void setThirst(int thirst) {
        this.thirst = thirst;
    }

    @Override
    public void consumeThirst(int thirst) {
        this.thirst -= thirst;

        if(this.thirst < 0){
            this.thirst = 0;
        }
    }

    @Override
    public void addThirst(int thirst) {
        this.thirst += thirst;
    }
}

IThirstHandler
Java:
public interface IThirstHandler {
    int getThirst();
    void setThirst(int thirst);
    void consumeThirst(int thirst);
    void addThirst(int thirst);
}

ThirstStorage
Java:
public class ThirstStorage implements Capability.IStorage<IThirstHandler> {
    @Nullable
    @Override
    public NBTBase writeNBT(Capability<IThirstHandler> capability, IThirstHandler instance, EnumFacing side) {
        final NBTTagCompound tag = new NBTTagCompound();
        tag.setInteger("thirst", instance.getThirst());
        return tag;
    }

    @Override
    public void readNBT(Capability<IThirstHandler> capability, IThirstHandler instance, EnumFacing side, NBTBase nbt) {
        final NBTTagCompound tag = new NBTTagCompound();
        instance.setThirst(tag.getInteger("thirst"));
    }
}

Получаю
Java:
@SubscribeEvent
public void onPlayerLog(PlayerEvent.PlayerLoggedInEvent event) {
    EntityPlayer player = event.player;

    DefaultThirstHandler thirstHandler = new DefaultThirstHandler();

    player.sendMessage(new TextComponentString(ChatFormatting.AQUA + String.valueOf(thirstHandler.getThirst())));
}

Attach
Java:
@SubscribeEvent
public void attachCapabilities(AttachCapabilitiesEvent<Entity> event) {
    if (event.getObject() instanceof EntityPlayer) {
        event.addCapability(new ResourceLocation("rcc", "thirst"), new ThirstProvider());
    }
}

Регистрация
Java:
CapabilityManager.INSTANCE.register(IThirstHandler.class, new ThirstStorage(), DefaultThirstHandler.class);

Как исправить?
 
3,005
192
594
Java:
    @Override
    public void readNBT(Capability<IThirstHandler> capability, IThirstHandler instance, EnumFacing side, NBTBase nbt) {
        final NBTTagCompound tag = new NBTTagCompound();
        instance.setThirst(tag.getInteger("thirst"));
    }
}
Я создаю новый тег и беру от туда инт! (c)
По теме:
Бери инт с nbt.
 
3,005
192
594
serializeNBT добавь вывод instance.getThirst(), в deserializeNBT добавь ((NBTTagCompound) nbt).getInteger("thirst")
 

Eifel

Модератор
1,624
79
611
К капабилити игрока нужно доступаться следующим образом:

Java:
IThirstHandler capa = player.getCapability(ThirstProvider.CAPABILITY_THIRST, null);

А ты просто создаешь новый обьект, так делать не надо:

Java:
DefaultThirstHandler thirstHandler = new DefaultThirstHandler();
 

Eifel

Модератор
1,624
79
611
И еще в readNbt замени это:
Java:
@Override
    public void readNBT(Capability<IThirstHandler> capability, IThirstHandler instance, EnumFacing side, NBTBase nbt) {
        final NBTTagCompound tag = new NBTTagCompound();
        instance.setThirst(tag.getInteger("thirst"));
    }
На это:
Java:
@Override
    public void readNBT(Capability<IThirstHandler> capability, IThirstHandler instance, EnumFacing side, NBTBase nbt) {
        instance.setThirst(((NBTTagCompound) nbt).getInteger("thirst"));
    }
 
Сверху