Jump to content

[1.11] Problem with NBT not saving on Capabilities.


Recommended Posts

Posted

Hey there!

 

So I have a capability that adds a few integers... However, I'm having an issue with saving it.. On restart it seems to reset, even though I believe I have it properly setup...

 

Here is the main Capability Class:

public class CapabilityMagic
{
    @CapabilityInject(CapabilityMagicData.class)
    public static Capability<CapabilityMagicData> MANA = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(CapabilityMagicData.class, new Capability.IStorage<CapabilityMagicData>()
                {
                    @Override
                    public NBTBase writeNBT(Capability<CapabilityMagicData> capability, CapabilityMagicData instance, EnumFacing side)
                    {
                        return instance.writeData();
                    }

                    @Override
                    public void readNBT(Capability<CapabilityMagicData> capability, CapabilityMagicData instance, EnumFacing side, NBTBase nbt)
                    {
                        instance.readData(nbt);
                    }
                },
                new Callable<CapabilityMagicData>()
                {
                    @Override
                    public CapabilityMagicData call() throws Exception
                    {
                        return new CapabilityMagicData(5000, 5);
                    }
                });
    }
}

 

The 'Capability Data:

public class CapabilityMagicData implements IManaData {


    private Random rand = new Random();

    protected float mana;
    protected float corruption;
    protected int maxManaStored;

    protected boolean malignantCorruption;
    protected int chanceToSpread=150; //higher = more rare.

    protected int regen;
    protected boolean isRegenCooldown;
    protected int regenTick;

    private EntityLivingBase entity;
    private EntityPlayer entityPlayer;


    /** IMPLEMENTATION LIST:
     * CORRUPTION ADD / SUBTRACT
     * MANA ADD / SUBTRACT
     * CORRUPTED MANA
     */

    public CapabilityMagicData(int maxManaStored, int regen) {
        this.maxManaStored = maxManaStored;
        this.regen = regen;
    }

    public EntityLivingBase getEntity()
    {
        return entity;
    }

    public EntityPlayer getPlayerEntity()
    {
        return entityPlayer;
    }

    public void setEntity(EntityLivingBase entity)
    {
        this.entity = entity;
    }

    public void setEntityPlayer(EntityPlayer entity) {
        this.entityPlayer = entity;
    }

    public void updateCapability() {
        Minecraft mc = Minecraft.getMinecraft();

        regenTick++;

        this.setEntityPlayer(mc.thePlayer); //reference Standpoint

        malignantCheck();
        manaCheck();

        PacketHandlerHelper.sendCapabilityPacket(this.getPlayerEntity(), true);
    }

    public void malignantCheck() {
        if(malignantCorruption) {
            if(chanceToSpread > 0) {
                if (rand.nextInt(chanceToSpread) == 0) {
                    this.setCorruption(this.getCorruptionStored() + rand.nextInt(5));
                }else {
                    chanceToSpread--;
                }
            }
            if(this.chanceToSpread <= 0) {
                malignantCorruption = false;
            }
        }
    }

    public void manaCheck() {
        if(this.getManaStored() > this.getMaxManaStored()) {
            this.setMana(this.getMaxManaStored());
        }
        if(isRegenCooldown) {
            if(regenTick >= 160) {
                isRegenCooldown=false;
            }
        }else {
            regenTick=0;
        }
        if(this.getManaStored() < this.getMaxManaStored() && !isRegenCooldown) {
            this.receiveMana(regen);
        }

        if(this.getManaStored() < 0) {
            this.setMana(0);
        }
        if(this.getMaxManaStored() < 0) {
            this.setMaxMana(0);
        }
    }

    @Override
    public void receiveMana(float amount) {
        if(canReceiveMana(amount)) {
            this.setMana(this.getManaStored() + amount);
        }
    }

    @Override
    public void extractMana(float amount) {
        if(canExtractMana(amount)) {
            this.setMana(this.getManaStored() - amount);
            this.isRegenCooldown = true;
        }
    }

    @Override
    public void receiveCorruption() {
        if(this.getCorruptionStored() == 0) {
            if(rand.nextInt(20) == 0) {
                this.setCorruption(1);
            }
        }else {
            this.setCorruption(this.getCorruptionStored() * rand.nextFloat() + 1);
            if(rand.nextInt(1500) == 0) {
                this.malignantCorruption = true;
            }
        }
    }

    @Override
    public void extractCorruption(float amount) {
        if(this.canExtractCorruption(amount)) {
            this.setCorruption(this.getCorruptionStored() - amount);
        }
    }

    public boolean canExtractCorruption(float amount) {
        if((this.getCorruptionStored() - amount) >= 0) {
            return true;
        }else {
            return false;
        }
    }


    public boolean canExtractMana(float amount) {
        if((this.getManaStored() - amount) >= 0) {
            return true;
        }else {
            return false;
        }
    }

    public boolean canReceiveMana(float amount) {
        if((this.getManaStored() + amount) <= this.maxManaStored) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public float getManaStored() {
        return mana;
    }

    @Override
    public int getMaxManaStored() {
        return maxManaStored - (int)this.getCorruptionStored();
    }

    @Override
    public float getCorruptionStored() {
        return corruption;
    }

    public void setCorruption(float corruptionToSet) {
        this.corruption = corruptionToSet;
    }

    public float setMana(float manaToSet) {
        return this.mana = manaToSet;
    }

    public int setMaxMana(int maxToSet) {
        return this.maxManaStored = maxToSet;
    }

    public NBTBase writeData()
    {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setFloat("Mana", getManaStored());
        tag.setFloat("Corruption", getCorruptionStored());

        return tag;
    }

    public void readData(NBTBase nbt)
    {
        NBTTagCompound tag = (NBTTagCompound) nbt;

        this.setMana(tag.getFloat("Mana"));
        this.setCorruption(tag.getInteger("Corruption"));
    }

    public float getPercentedMana() {
        return this.getManaStored() / this.getMaxManaStored();
    }

    public float getCorruptedMana() {
        if(this.getCorruptionStored() > 0) {
            return this.getCorruptionStored() / this.getManaStored();
        }else {
            return 0;
        }
    }
}

 

Lastly, the 'Capability Provider'

public class CapabilityMagicProvider implements ICapabilityProvider, ICapabilitySerializable<NBTTagCompound>
{

    public static final ResourceLocation KEY = new ResourceLocation(ModUtil.MOD_ID, "mana_atr");

    private CapabilityMagicData INSTANCE = new CapabilityMagicData(5000, 5);

    public CapabilityMagicProvider()
    {}

    public CapabilityMagicProvider(EntityLivingBase entity)
    {
        INSTANCE.setEntity(entity);
        if(entity instanceof EntityPlayerMP) INSTANCE.setEntityPlayer((EntityPlayerMP)entity);
    }

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing)
    {
        return capability == CapabilityMagic.MANA;
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing)
    {
        if (capability == CapabilityMagic.MANA) return (T) INSTANCE;
        return null;
    }

    @Override
    public NBTTagCompound serializeNBT()
    {
        return (NBTTagCompound) CapabilityMagic.MANA.writeNBT(INSTANCE, null);
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt)
    {
        CapabilityMagic.MANA.readNBT(INSTANCE, null, nbt);
    }

}

 

Also, I the github if you need to check out the packets:

https://github.com/LambdaXV/PlentifulMisc/tree/master/src/main/java/com/lambda/plentifulmisc/network

 

 

Thanks,

 

EDIT:

 

as a note, I know I have a few other bools / intergers in there that are not being saved in the read/writedata, I'll add them as soon as I get the base ones working.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

they are probably saved correctly (haven't looked at your read/write in your capability) What happens is you need to send a packet from the server to the client to keep them in sync.

 

EDIT: looked at your read and write, you're only saving and loading the mana and corruption. But that will only keep it sever side, any client reconnecting will need a packet to tell them "this is what the server has for you"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.