Jump to content

Recommended Posts

Posted

I'm trying to add mana values for players, but I can't seem to change them. I have an item that should change the max value for mana when I right click it, but it doesn't seem to work; I have println's for debugging and they keep displaying that my max mana is 0.

 

ExtendedPlayer code:

Spoiler

package com.fvg.blackmagic.entities;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public class ExtendedPlayer implements IExtendedEntityProperties {
    public final static String MANA = "mana";
    protected EntityPlayer player;
    private String manaDebug = "Mana: " + this.currentMana + "/" + this.maxMana;

    private boolean isMagicUser = true;
    private int currentMana;
    private int maxMana;

    public boolean isMagicUser() {
        return isMagicUser;
    }

    public void setMagicUser(boolean magicUser) {
        isMagicUser = magicUser;
        if(magicUser){this.currentMana = this.maxMana = 50;}
    }

    public int getCurrentMana() {
        return currentMana;
    }

    public void setCurrentMana(int currentMana) {
        this.currentMana = currentMana;
    }

    public int getMaxMana() {
        return maxMana;
    }

    public void setMaxMana(int maxMana) {
        this.maxMana = maxMana;
    }

    public ExtendedPlayer(EntityPlayer player){
        this.player = player;
        this.currentMana = this.maxMana = (this.isMagicUser ? 50 : 0);
    }

    public static final void register(EntityPlayer player){
        player.registerExtendedProperties(ExtendedPlayer.MANA, new ExtendedPlayer(player));
    }

    public static final ExtendedPlayer get(EntityPlayer player){
        return (ExtendedPlayer) player.getExtendedProperties(MANA);
    }

    @Override
    public void saveNBTData(NBTTagCompound compound) {
        NBTTagCompound properties = new NBTTagCompound();

        //Var tags
        properties.setBoolean("IsMagicUser", this.isMagicUser);
        properties.setInteger("CurrentMana", this.currentMana);
        properties.setInteger("MaxMana", this.maxMana);

        compound.setTag(MANA, properties);
    }

    @Override
    public void loadNBTData(NBTTagCompound compound) {
        NBTTagCompound properties = (NBTTagCompound) compound.getTag(MANA);

        //get var tags
        this.isMagicUser = properties.getBoolean("IsMagicUser");
        this.currentMana = properties.getInteger("CurrentMana");
        this.maxMana = properties.getInteger("MaxMana");

        System.out.println(manaDebug);
    }

    @Override
    public void init(Entity entity, World world) {

    }

    public boolean consumeMana(int amount){
        boolean sufficient = amount <= this.currentMana;
        this.currentMana -= (amount < this.currentMana ? amount : this.currentMana);

        System.out.println(manaDebug);

        return !sufficient;
    }

    public void regainMana(int amount) {
        int sum = this.currentMana + amount;
        this.currentMana = (sum < this.maxMana ? sum : this.maxMana);

        System.out.println(manaDebug);
    }
}

 

 

ItemModSword(contains the code for the item mentioned above):

Spoiler

package com.fvg.blackmagic.items.gear;

import com.fvg.blackmagic.core.BlackMagic;
import com.fvg.blackmagic.entities.ExtendedPlayer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

public class ItemModSword extends ItemSword{
    public ItemModSword(ToolMaterial material, String unlocalizedName){
        super(material);
        this.setUnlocalizedName(unlocalizedName);
        this.setCreativeTab(BlackMagic.TabBlackMagicCore);
    }



    @Override
    public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity){

        if(!player.getEntityWorld().isRemote) {
            ExtendedPlayer props = ExtendedPlayer.get(player);
            if(props.consumeMana(15)) {
                entity.getEntityWorld().addWeatherEffect(new EntityLightningBolt(entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ));
            }

        }

        return false;

    }

    @Override
    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) {

        if(!worldIn.isRemote) {
            ExtendedPlayer props = ExtendedPlayer.get(playerIn);
            if(props.consumeMana(10)){
                worldIn.addWeatherEffect(new EntityLightningBolt(worldIn, pos.getX(), pos.getY(), pos.getZ()));
            }
        }
        return false;

    }

    @Override
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn) {
        ExtendedPlayer props = ExtendedPlayer.get(playerIn);
        if(!props.isMagicUser()){
            props.setMagicUser(true);
            System.out.println(props.isMagicUser());
        }
        else if(props.getMaxMana()==0) {
            props.setMaxMana(50);
            System.out.println(props.isMagicUser());
        }
        else {
            props.regainMana(25);
            System.out.println(props.isMagicUser());
        }

        return itemStackIn;
    }
}

 

 

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.