Jump to content

Recommended Posts

Posted

I'm creating a balance for an item, and everytime you add money to that balance, the balance variable goes up. But the problem is that the balance is the same for all the items. So if I added $100 to an item, it would add to everybody elses items aswell. I hope I explained it right

Posted

You need to store any data about Items in the ItemStack. You can't store it in the Item class as there is only ever one instance of it.

 

How would I do this?

Posted

Every ItemStack has an NBTTagCompound associated with it (ItemStack#stackTagCompound). You can store any data you want there.

 

Ok, so I already have it set-up with NBT, but it is the same on all the items, what am I doing wrong

package com.bugzoo.FinancialMod;

import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class ItemDebitCard extends Item {
	public ItemDebitCard(){
		setMaxStackSize(1);
		setUnlocalizedName("DebitCard");
		setCreativeTab(FinancialMod.financialTab);
	}

	int UniquePin = 1000 + (int)(Math.random()*9999); 
	public static double balance;

	    @Override
	    public void onCreated(ItemStack itemStack, World world, EntityPlayer player) {
	    	 itemStack.stackTagCompound = new NBTTagCompound();
	    	 itemStack.stackTagCompound.setString("owner", player.getDisplayName());
	    	 itemStack.stackTagCompound.setInteger("pin", UniquePin);
	    	 itemStack.stackTagCompound.setDouble("balance", balance);
	    	 if(!world.isRemote){
	    	 player.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.RED + "Your Unique Pin is " + UniquePin));
	    	 }
	    }

	    public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4) {
	    	 if (itemStack.stackTagCompound != null) {
                     String owner = itemStack.stackTagCompound.getString("owner");
                     String pin = itemStack.stackTagCompound.getString("pin");
                     list.add("Owner: " + owner);
                     list.add("Balance: " + balance);
                     
                     
                     if (!owner.equals(player.getDisplayName())) {
                    	  list.add("Pin: " + EnumChatFormatting.OBFUSCATED + pin);
    		    	 } else {
    		    		 list.add("Pin: " + pin);
    		    	 }
	    	 }
	    	 
             }
	    
	    
	    }

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.