I'm now coding since some time, but im newer to java and modding, so excuse me, if I'm making some weird mistakes.
Hello, some time ago, I started making a mod where the player has a new "awesome charge"- value that can affect him in different ways. I've created a capability for that charge which cost me a lot of time, because I didn't find a tutorial for such a thing in 1.15.2. There are some code fragments that I don't understand.
Now I'm having a big problem. As long as I'm in the world, the value can be edited via get, set, add and subtract very well, but when I quit and restart the world, The value is reset to default.
I think it's most likely, that the values are just not being saved when I'm quittig the world, but I don't know, how to make it save the value.
Here's the code:
//The java interface
package com.FenrisFox86.syringe_mod.world.entityData;
public interface IAwesomeCharge {
public void subtract(float points);
public void add(float points);
public void set(float points);
public float get();
}
//The implementation
package com.FenrisFox86.syringe_mod.world.entityData;
public class AwesomeCharge implements IAwesomeCharge {
private float points = 20.0F;
public void subtract(float points) {
this.points -= points;
if (this.points < 0.0F) {
this.points = 0.0F;
}
}
public void add(float points) {
this.points += points;
if (this.points > 20.0F) {
this.points = 20.0F;
}
}
public void set(float points) {
this.points = points;
if (this.points < 0.0F) {
this.points = 0.0F;
}
if (this.points > 20.0F) {
this.points = 20.0F;
}
}
public float get() {
return this.points;
}
}
the capability
package com.FenrisFox86.syringe_mod.world.entityData;
import net.minecraft.nbt.FloatNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class ChargeCapability implements ICapabilitySerializable<FloatNBT> {
@CapabilityInject(IAwesomeCharge.class)
public static final Capability<IAwesomeCharge> CHARGE_CAP = null;
private final LazyOptional<IAwesomeCharge> instance = LazyOptional.of(CHARGE_CAP::getDefaultInstance);
//method to register the capability in main class without having too much text.
public static void register() {
CapabilityManager.INSTANCE.register(IAwesomeCharge.class, new ChargeStorage(), AwesomeCharge::new);
}
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return CHARGE_CAP.orEmpty(cap, instance);
}
@Override
public FloatNBT serializeNBT() {
return (FloatNBT) CHARGE_CAP.getStorage().writeNBT(
CHARGE_CAP,
instance.orElseThrow(() ->
new IllegalArgumentException("LazyOptional cannot be empty!")),
null);
}
@Override
public void deserializeNBT(FloatNBT nbt) {
CHARGE_CAP.getStorage().readNBT(
CHARGE_CAP,
instance.orElseThrow(() ->
new IllegalArgumentException("LazyOptional cannot be empty!")),
null, nbt);
}
}
//the storage handler
package com.FenrisFox86.syringe_mod.world.entityData;
import net.minecraft.nbt.FloatNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
public class ChargeStorage implements Capability.IStorage<IAwesomeCharge> {
@Override
public INBT writeNBT(Capability<IAwesomeCharge> capability, IAwesomeCharge instance, Direction side) {
return FloatNBT.valueOf(instance.get());
}
@Override
public void readNBT(Capability<IAwesomeCharge> capability, IAwesomeCharge instance, Direction side, INBT nbt) {
instance.set(((FloatNBT) nbt).getFloat());
}
}
where I'm attaching the capability
public static final ResourceLocation CHARGE_CAP = new ResourceLocation(SyringeMod.MOD_ID, "charge");
@SubscribeEvent
public void attachCapabilitiesEntity(final AttachCapabilitiesEvent<Entity> event) {
if(event.getObject() instanceof PlayerEntity)
event.addCapability(CHARGE_CAP, new ChargeCapability());
}