Jump to content

Recommended Posts

Posted

Here is the code ( I've deleted most of the stuff just to make it more clear)

	public int maxAmmo;
	public int ammo;
	
	public GunBase(String name, CreativeTabs creativetab, int maxAmmo) 
	{
		super(name, creativetab);
		setMaxStackSize(1);
		this.maxAmmo = maxAmmo;
		this.ammo = maxAmmo;
	}

	@Override
	public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) 
	{
		EntityPlayer player = (EntityPlayer) entityIn;
		ItemStack itemstack = player.getHeldItemMainhand();
		if(itemstack.areItemsEqual(itemstack, stack))
		{
			NBTTagCompound compound;
		    if (itemstack.hasTagCompound()) 
		    {
		    	compound = itemstack.getTagCompound();
		    }
		    else 
		    {
		    	compound = new NBTTagCompound();
		    }
		    compound.setInteger("MaxAmmo", this.maxAmmo);
		    compound.setInteger("Ammo", this.ammo);
		    stack.setTagCompound(compound);
		}
	}

I've set maxammo and ammo in the constructor, aren't they going to have their own instance? There aren't any static method as well, but the result is same kind of item shares the nbt tag. How do I fix it?

Posted (edited)
21 hours ago, diesieben07 said:

There is only one instance of your item class. This is why ItemStack is a thing, which represents an actual item in an inventory. The Item class represents the type of item as a whole.

You need to store your data only in the ItemStack. If you are going to use the raw stack NBT you should also prefix your entries with your mod ID to avoid conflicts.

 

Edited by poopoodice
Posted (edited)
27 minutes ago, diesieben07 said:

This is in your item class.

I am going to create another class for the ammo thing, am I correct?

Should I use events such as playertick or something like that?

Edited by poopoodice

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.