Jump to content

[1.11] Capability Synchronization.


Lambda

Recommended Posts

Hey there, so when trying to 'add experience' to my custom capability, it just.. doesn't add. Either my math is off, or my packets are not working correctly.

 

So here is some code, I will highlight some parts so you don't have to go digging for it. And also, I'll post the github respective.

 

Here some quick answers to some possible questions:

- Registered - Yes.

- Packets Registered - Yes

 

So firstly, we have the 'CapabilityMagicData':

public class CapabilityMagicData implements IManaData
{
    protected int mana;
    protected int level;
    protected int corruption;
    protected int experience;

    protected int capacity;
    protected int xpIncrement;

    private int checkTick;

    protected int maxReceive;
    protected int maxExtract;

    private EntityLivingBase entity;
    private EntityPlayer entityPlayer;

    public CapabilityMagicData(int capacity)
    {
        this(capacity, capacity / 2, capacity, capacity);
    }

    public CapabilityMagicData(int capacity, int maxTransfer)
    {
        this(capacity, capacity / 2, maxTransfer, maxTransfer);
    }

    public CapabilityMagicData(int capacity, int xpIncrement, int maxReceive, int maxExtract)
    {
        this.capacity = capacity;
        this.xpIncrement = xpIncrement;
        this.maxReceive = maxReceive;
        this.maxExtract = maxExtract;
    }

    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 checkExp() {
        int currentExp = this.getExperienceStored();
        int expToLevelup = this.xpIncrement * this.getLevelStored();

        checkTick++;

        System.out.println("updating");
        if(currentExp >= expToLevelup) {
            System.out.println("leval up");
            this.setLevel(this.getLevelStored() + 1);
            this.setExperience(0);
        }

        if(checkTick >= 40) {
            System.out.println(this.getManaStored());
            System.out.println(this.getCorruptionStored());
            System.out.println(this.getLevelStored());
            System.out.println(this.getExperienceStored());
            checkTick=0;
        }

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

    public int extractManaInternal(int maxExtract, boolean simulate){
        int before = this.maxExtract;
        this.maxExtract = Integer.MAX_VALUE;

        int toReturn = this.extractMana(maxExtract, simulate);

        this.maxExtract = before;
        return toReturn;
    }

    public int receiveManaInternal(int maxReceive, boolean simulate){
        int before = this.maxReceive;
        this.maxReceive = Integer.MAX_VALUE;

        int toReturn = this.receiveMana(maxReceive, simulate);

        this.maxReceive = before;
        return toReturn;
    }

    public int receiveExperienceInternal(int receive) {
        receiveExperience(receive);
        int toReturn = receive+=experience;
        return toReturn;
    }

    public int receiveCorruptionInternal(int receive) {
        int before = this.maxReceive;
        this.maxReceive = Integer.MAX_VALUE;

        int toReturn = this.receiveCorruption(receive);

        this.maxReceive = before;
        return toReturn;
    }

    public int extractCorruptionInternal(int extract) {
        int before = this.maxExtract;
        this.maxExtract = Integer.MAX_VALUE;

        int toReturn = this.extractCorruption(extract);

        this.maxExtract = before;
        return toReturn;
    }

    @Override
    public int receiveMana(int maxReceive, boolean simulate)
    {
        if (!canReceive())
            return 0;

        int manaReceived = Math.min(capacity - mana, Math.min(this.maxReceive, maxReceive));
        if (!simulate)
            mana += manaReceived;
        return manaReceived;
    }

    @Override
    public int extractMana(int maxExtract, boolean simulate)
    {
        if (!canExtract())
            return 0;

        int manaExtracted = Math.min(mana, Math.min(this.maxExtract, maxExtract));
        if (!simulate)
            mana -= manaExtracted;
        return manaExtracted;
    }

    @Override
    public int receiveExperience(int receive) {
        int currentExperience = this.getExperienceStored();
        int experienceReceived = receive+=currentExperience;

        experience += currentExperience;

        return experienceReceived;
    }

    @Override
    public int receiveCorruption(int receive) {
        int currentCorruption = this.getCorruptionStored();
        int corruptionReceived = receive+=currentCorruption;

        corruption += corruptionReceived;

        return corruptionReceived;
    }


    @Override
    public int extractCorruption(int extract) {
        int corruptionExtracted = Math.min(corruption, Math.min(this.maxExtract, extract));

        corruption -= corruptionExtracted;

        return corruptionExtracted;
    }


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

    @Override
    public int getLevelStored()
    {
        return level;
    }

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

    @Override
    public int getExperienceStored()
    {
        return experience;
    }

    @Override
    public int getMaxManaStored()
    {
        return capacity;
    }

    @Override
    public boolean canExtract()
    {
        return this.maxExtract > 0;
    }

    @Override
    public boolean canReceive()
    {
        return this.maxReceive > 0;
    }


    public NBTBase writeData()
    {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setInteger("Mana", getManaStored());
        tag.setInteger("Level", getLevelStored());
        tag.setInteger("Corruption", getCorruptionStored());
        tag.setInteger("Experience", getExperienceStored());

        return tag;
    }

    public void readData(NBTBase nbt)
    {
        NBTTagCompound tag = (NBTTagCompound) nbt;
        tag.setInteger("Mana", this.getManaStored());
        tag.setInteger("Level", this.getLevelStored());
        tag.setInteger("Corruption", this.getCorruptionStored());
        tag.setInteger("Experience", this.getExperienceStored());
    }

    public void readFromNBT(NBTTagCompound compound){
        this.setManaStored(compound.getInteger("Mana"));
        this.setLevel(compound.getInteger("Level"));
        this.setCorruption(compound.getInteger("Corruption"));
        this.setExperience(compound.getInteger("Experience"));
    }

    public void writeToNBT(NBTTagCompound compound){
        compound.setInteger("Mana", this.getManaStored());
        compound.setInteger("Level", this.getLevelStored());
        compound.setInteger("Corruption", this.getCorruptionStored());
        compound.setInteger("Experience", this.getExperienceStored());
    }

    public void setManaStored(int manaToSet){
        this.mana = manaToSet;
    }

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

    public void setLevel(int level){
        this.level = level;
    }

    public void setExperience(int experience){
        this.experience = experience;
    }


}

 

Here are some highlight points at which I think the problem is happening:

    public int receiveExperienceInternal(int receive) {
        receiveExperience(receive);
        int toReturn = receive+=experience;
        return toReturn;
    }

@Override
    public int receiveExperience(int receive) {
        int currentExperience = this.getExperienceStored();
        int experienceReceived = receive+=currentExperience;

        experience += currentExperience;

        return experienceReceived;
    }

    public void checkExp() {
        int currentExp = this.getExperienceStored();
        int expToLevelup = this.xpIncrement * this.getLevelStored();

        checkTick++;

        System.out.println("updating");
        if(currentExp >= expToLevelup) {
            System.out.println("leval up");
            this.setLevel(this.getLevelStored() + 1);
            this.setExperience(0);
        }

        if(checkTick >= 40) {
            System.out.println(this.getManaStored());
            System.out.println(this.getCorruptionStored());
            System.out.println(this.getLevelStored());
            System.out.println(this.getExperienceStored());
            checkTick=0;
        }

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

 

Next, the PacketHandler, which as it names, handles all the packets:

public final class PacketHandler{

    public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<IDataHandler>();
    public static final IDataHandler PARTICLE_HANDLER = new IDataHandler(){
        @Override
        @SideOnly(Side.CLIENT)
        public void handleData(NBTTagCompound compound){
            AssetUtil.renderParticlesFromAToB(compound.getDouble("StartX"), compound.getDouble("StartY"), compound.getDouble("StartZ"), compound.getDouble("EndX"), compound.getDouble("EndY"), compound.getDouble("EndZ"), compound.getInteger("ParticleAmount"), compound.getFloat("ParticleSize"), new float[]{compound.getFloat("Color1"), compound.getFloat("Color2"), compound.getFloat("Color3")}, compound.getFloat("AgeMultiplier"));
        }
    };
    public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler(){
        @Override
        @SideOnly(Side.CLIENT)
        public void handleData(NBTTagCompound compound){
            World world = Minecraft.getMinecraft().theWorld;
            if(world != null){
                TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
                if(tile instanceof TileEntityBase){
                    ((TileEntityBase)tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
                }
            }
        }
    };
    public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = new IDataHandler(){
        @Override
        public void handleData(NBTTagCompound compound){
            World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
            TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));

            if(tile instanceof IButtonReactor){
                IButtonReactor reactor = (IButtonReactor)tile;
                Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
                if(entity instanceof EntityPlayer){
                    reactor.onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer)entity);
                }
            }
        }
    };
    public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = new IDataHandler(){
        @Override
        public void handleData(NBTTagCompound compound){
            World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
            Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
            if(entity instanceof EntityPlayer){
                Container container = ((EntityPlayer)entity).openContainer;
                if(container instanceof IButtonReactor){
                    ((IButtonReactor)container).onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer)entity);
                }
            }
        }
    };
    public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = new IDataHandler(){
        @Override
        public void handleData(NBTTagCompound compound){
            World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
            TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));

            if(tile instanceof INumberSender){
                INumberSender reactor = (INumberSender)tile;
                reactor.onNumberReceived(compound.getInteger("Number"), compound.getInteger("NumberID"), (EntityPlayer)world.getEntityByID(compound.getInteger("PlayerID")));
            }
        }
    };
    public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = new IDataHandler(){
        @Override
        public void handleData(NBTTagCompound compound){
            World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
            TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));

            if(tile instanceof IStringSender){
                IStringSender reactor = (IStringSender)tile;
                reactor.onTextReceived(compound.getString("Text"), compound.getInteger("TextID"), (EntityPlayer)world.getEntityByID(compound.getInteger("PlayerID")));
            }
        }
    };
    public static final IDataHandler CHANGE_PLAYER_DATA_HANDLER = new IDataHandler(){
        @Override
        public void handleData(NBTTagCompound compound){
            NBTTagCompound data = compound.getCompoundTag("Data");
            UUID id = compound.getUniqueId("UUID");
            PlayerData.getDataFromPlayer(id).readFromNBT(data, false);
            if(compound.getBoolean("Log")){
                ModUtil.LOGGER.info("Receiving (new or changed) Player Data for player with UUID "+id+".");
            }
        }
    };
    public static final IDataHandler MANA_CAPABILITY = new IDataHandler() {
        @Override
        public void handleData(NBTTagCompound compound) {
            Minecraft mc = Minecraft.getMinecraft();
            if(mc.thePlayer.hasCapability(CapabilityMagic.MANA, null)) {
                CapabilityMagicData cap = mc.thePlayer.getCapability(CapabilityMagic.MANA, null);
                if(mc.thePlayer != null && mc.thePlayer.getCapability(CapabilityMagic.MANA, null) != null) {
                    cap.readData(compound);
                }
            }
        }
    };
    public static SimpleNetworkWrapper theNetwork;

    public static void init(){
        theNetwork = NetworkRegistry.INSTANCE.newSimpleChannel(ModUtil.MOD_ID);
        theNetwork.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
        theNetwork.registerMessage(PacketClientToServer.Handler.class, PacketClientToServer.class, 1, Side.SERVER);

        DATA_HANDLERS.add(PARTICLE_HANDLER);
        DATA_HANDLERS.add(TILE_ENTITY_HANDLER);
        DATA_HANDLERS.add(GUI_BUTTON_TO_TILE_HANDLER);
        DATA_HANDLERS.add(GUI_STRING_TO_TILE_HANDLER);
        DATA_HANDLERS.add(GUI_NUMBER_TO_TILE_HANDLER);
        DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER);
        DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
        DATA_HANDLERS.add(MANA_CAPABILITY);
    }
}

Here are the points that I've added with this Capability:

    public static final IDataHandler MANA_CAPABILITY = new IDataHandler() {
        @Override
        public void handleData(NBTTagCompound compound) {
            Minecraft mc = Minecraft.getMinecraft();
            if(mc.thePlayer.hasCapability(CapabilityMagic.MANA, null)) {
                CapabilityMagicData cap = mc.thePlayer.getCapability(CapabilityMagic.MANA, null);
                if(mc.thePlayer != null && mc.thePlayer.getCapability(CapabilityMagic.MANA, null) != null) {
                    cap.readData(compound);
                }
            }
        }
    };

        DATA_HANDLERS.add(MANA_CAPABILITY); // Inside the init() function

 

Here is the helper, which is so I can easily access the packets, and send them when needed:

    public static void sendCapabilityPacket(EntityPlayer player, boolean toClient) {
        NBTTagCompound compound = new NBTTagCompound();

        if(player != null && player.hasCapability(CapabilityMagic.MANA, null)) {
            CapabilityMagicData cap = player.getCapability(CapabilityMagic.MANA, null);
            int tempMana = cap.getManaStored();
            int tempCorruption = cap.getCorruptionStored();
            int tempLevel = cap.getLevelStored();
            int tempExperience = cap.getExperienceStored();
            compound.setInteger("Mana", tempMana);
            compound.setInteger("Corruption", tempCorruption);
            compound.setInteger("Level", tempLevel);
            compound.setInteger("Experience", tempExperience);

            if(toClient) {
                if(player instanceof EntityPlayerMP) {
                    PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.MANA_CAPABILITY), (EntityPlayerMP)player);
                }
            }
            else {
                PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.MANA_CAPABILITY));
            }
        }
    }

 

Lastly, my PacketServerToClient:

public class PacketServerToClient implements IMessage{

    private NBTTagCompound data;
    private IDataHandler handler;

    public PacketServerToClient(){

    }

    public PacketServerToClient(NBTTagCompound data, IDataHandler handler){
        this.data = data;
        this.handler = handler;
    }

    @Override
    public void fromBytes(ByteBuf buf){
        PacketBuffer buffer = new PacketBuffer(buf);
        try{
            this.data = buffer.readNBTTagCompoundFromBuffer();

            int handlerId = buffer.readInt();
            if(handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()){
                this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
            }
        }
        catch(Exception e){
            ModUtil.LOGGER.error("Cannot receive a client packet!", e);
        }
    }

    @Override
    public void toBytes(ByteBuf buf){
        PacketBuffer buffer = new PacketBuffer(buf);

        buffer.writeNBTTagCompoundToBuffer(this.data);
        buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
    }

    public static class Handler implements IMessageHandler<PacketServerToClient, IMessage> {

        @Override
        @SideOnly(Side.CLIENT)
        public IMessage onMessage(PacketServerToClient aMessage, MessageContext ctx){
            final PacketServerToClient message = aMessage;
            Minecraft.getMinecraft().addScheduledTask(new Runnable(){
                @Override
                public void run(){
                    if(message.data != null && message.handler != null){
                        message.handler.handleData(message.data);
                    }
                }
            });
            return null;
        }
    }
}

 

Also, the github: https://github.com/LambdaXV/PlentifulMisc/tree/master/src/main/java/com/lambda/plentifulmisc

 

Thanks for your time.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

here,

        PacketHandlerHelper.sendCapabilityPacket(getPlayerEntity(), true);

 

in the check function, which currently runs everytick

Couple things to check to make sure that everything is actually running.

  • Put a println in your packets toBytes() and fromBytes(), aswell as the onMessage().
  • Put a println in checkExp() to check if the player is null or not.
  • And specifically in your capabilities handleData() section.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Okay, my debugging is showing that everything is up to date:

[15:08:23] [Client thread/INFO]: [sTDOUT]: TO Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [Netty Server IO #1/INFO]: [sTDOUT]: From Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [Netty Server IO #1/INFO]: [sTDOUT]: From Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [server thread/INFO]: [sTDOUT]: HANDLE DATA DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [server thread/INFO]: [sTDOUT]: Message Data: {Experience:0,Mana:0,Corruption:0,Level:1}

 

I'm starting to think it has to do with how I'm adding the exp.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

Okay, my debugging is showing that everything is up to date:

[15:08:23] [Client thread/INFO]: [sTDOUT]: TO Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [Netty Server IO #1/INFO]: [sTDOUT]: From Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [Netty Server IO #1/INFO]: [sTDOUT]: From Bytes DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [server thread/INFO]: [sTDOUT]: HANDLE DATA DATA: {Experience:0,Mana:0,Corruption:0,Level:1}
[15:08:23] [server thread/INFO]: [sTDOUT]: Message Data: {Experience:0,Mana:0,Corruption:0,Level:1}

 

I'm starting to think it has to do with how I'm adding the exp.

Show where you add it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

   public int receiveExperienceInternal(int receive) {
        receiveExperience(receive);
        int toReturn = receive+=experience;
        return toReturn;
    }

@Override
    public int receiveExperience(int receive) {
        int currentExperience = this.getExperienceStored();
        int experienceReceived = receive+=currentExperience;

        experience += currentExperience;

        return experienceReceived;
    }

, is how, I temporarily add it here:

    @SubscribeEvent
    public static void LivingUpdateEvent(LivingEvent.LivingUpdateEvent event) {
        if(event.getEntity() instanceof EntityPlayer) {
            if(event.getEntity().isSneaking() && event.getEntity().hasCapability(CapabilityMagic.MANA, null)) {
                CapabilityMagicData cap = event.getEntity().getCapability(CapabilityMagic.MANA, null);
                cap.receiveExperienceInternal(200);
            }
        }
    }

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

Two things you have seriously over complicated these methods to the point where receiveExperienceInternal() doesn't even work. And second does your LivingUpdateEvent even get called? Does the player have the Capability? And why are you using LivingUpdateEvent and not PlayerTickEvent?

 

Too go into more detail on your methods.

// Your internal method could could literally be this.
   public int receiveExperienceInternal(int receive) {
        return receiveExperience(receive);
    }

//  And the non internal could be this
@Override
    public int receiveExperience(int receive) {
        setExperience(receive + getCurrentExperience())
        return getCurrentExperience;
    }

But I don't see a reason for you to return a value in the first place...

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Also, why do you need an internal version of the method anyway? Especially one that is public (i.e. externally visible)?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Didn't know the PlayerTickEvent existed, will be using that and i've simplied all of my methods and its working now.. Thanks.

 

However, my capability isnt reading from NBT, so it restores upon restart.

 

Here is the updated capabilityData:

public class CapabilityMagicData implements IManaData
{
    protected int mana;
    protected int level;
    protected int corruption;
    protected int experience;

    protected int capacity;
    protected int xpIncrement;

    private int checkTick;

    protected int maxReceive;
    protected int maxExtract;

    private EntityLivingBase entity;
    private EntityPlayer entityPlayer;

    public CapabilityMagicData(int capacity)
    {
        this(capacity, capacity / 2, capacity, capacity);
    }

    public CapabilityMagicData(int capacity, int maxTransfer)
    {
        this(capacity, capacity / 2, maxTransfer, maxTransfer);
    }

    public CapabilityMagicData(int capacity, int xpIncrement, int maxReceive, int maxExtract)
    {
        this.capacity = capacity;
        this.xpIncrement = xpIncrement;
        this.maxReceive = maxReceive;
        this.maxExtract = maxExtract;
    }

    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 checkExp() {
        Minecraft mc = Minecraft.getMinecraft();
        setEntityPlayer(mc.thePlayer);
        int currentExp = this.getExperienceStored();
        int expToLevelup = this.xpIncrement * this.getLevelStored();

        checkTick++;

        if (currentExp >= expToLevelup) {
            System.out.println("leval up");
            this.setLevel(this.getLevelStored() + 1);
            this.setExperience(0);
        }

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

    @Override
    public int receiveMana(int receive)
    {
        setManaStored(receive + this.getManaStored());
        return this.getManaStored();
    }

    @Override
    public int extractMana(int extract)
    {
        setManaStored(this.getMaxManaStored() - extract);
        return this.getManaStored();
    }

    @Override
    public int receiveExperience(int receive) {
        setExperience(receive + this.getExperienceStored());
        return this.getExperienceStored();
    }

    @Override
    public int receiveCorruption(int receive)
    {
        setCorruption(receive + this.getCorruptionStored());
        return this.getCorruptionStored();
    }


    @Override
    public int extractCorruption(int extract)
    {
        setCorruption(this.getCorruptionStored() - extract);
        return this.getCorruptionStored();
    }

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

    @Override
    public int getLevelStored()
    {
        return level;
    }

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

    @Override
    public int getExperienceStored()
    {
        return experience;
    }

    @Override
    public int getMaxManaStored()
    {
        return capacity;
    }

    @Override
    public boolean canExtract()
    {
        return this.maxExtract > 0;
    }

    @Override
    public boolean canReceive()
    {
        return this.maxReceive > 0;
    }

    public NBTBase writeData()
    {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setInteger("Mana", getManaStored());
        tag.setInteger("Level", getLevelStored());
        tag.setInteger("Corruption", getCorruptionStored());
        tag.setInteger("Experience", getExperienceStored());

        return tag;
    }

    public void readData(NBTBase nbt)
    {
        NBTTagCompound tag = (NBTTagCompound) nbt;
        this.setManaStored(tag.getInteger("Mana"));
        this.setLevel(tag.getInteger("Level"));
        this.setCorruption(tag.getInteger("Corruption"));
        this.setExperience(tag.getInteger("Experience"));
    }


    public void setManaStored(int manaToSet){
        this.mana = manaToSet;
    }

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

    public void setLevel(int level){
        this.level = level;
    }

    public void setExperience(int experience){
        this.experience = experience;
    }


}

 

the 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);

    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);
    }

}

Thanks.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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