- 1,471
- 19
- 189
Сделал капу, но при перезаходе в мир, она не сохраняется.
ThirstProvider
DefaultThirstHandler
IThirstHandler
ThirstStorage
Получаю
Attach
Регистрация
Как исправить?
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);
Как исправить?