Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

  • Author

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?

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.