Jump to content

Item NBT


SaoModder

Recommended Posts

i have a problem with my nbt data inside an item, this is for 1.7.10

 

the nbt data works perfect... the only issue is whatever one items nbt data is all other instances of that item in your inventory shares the same nbt data, example if one item has an int set to nbt data with a value of 23 and you have 3 of said item in your inventory all 3 copies are having that same int (23) if the int gets raised they all raise etc

 

how do i get each instance of the item have its own independent nbt?

 

item code below

package com.souleater.Items;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;

public class ModItemWorldEater extends ItemPickaxe{

private int soulefficiency = 0;
private String Owner = null;
private boolean created = false;
protected ModItemWorldEater(ToolMaterial WorldEater) {
	super(WorldEater);
	this.setMaxDamage(soulefficiency);
}

@Override
    public float getDigSpeed(ItemStack stack, Block block, int meta)
    {
        if (soulefficiency > 0)
        {
            return (soulefficiency/2);
        }else if(soulefficiency == 0){return 0;}
        return super.getDigSpeed(stack, block, meta);
    }

@Override
public void onCreated(ItemStack itemStack, World world, EntityPlayer player) {
	if(!world.isRemote){
    itemStack.stackTagCompound = new NBTTagCompound();
    itemStack.stackTagCompound.setInteger("SoulPower", soulefficiency);
    itemStack.stackTagCompound.setString("Owner", player.getDisplayName());
	}
    
    created = true;
}

/**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
@Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {
	System.out.println(soulefficiency);
}

@Override
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4) {

	if(Owner!= null){
	String owner = Owner;
	list.add(EnumChatFormatting.BLUE + "Owner: " + Owner);}
	else {list.add(EnumChatFormatting.BLUE + "Owner: null");}

    	int Soul = soulefficiency;
    	list.add(EnumChatFormatting.GREEN + "Souls: " + Soul);
            

} 

public void AddSouls(int soul){
	soulefficiency += soul;
}

/**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
    {
    	soulefficiency = (soulefficiency +1);
    	Owner = player.getDisplayName();
        return stack;
    }

}

Link to comment
Share on other sites

oh thanks! :) done and works a charm ive never really used nbt before outside of tile entities and they seem to be different off memory (may be wrong)

 

ItemStack NBT is just like TE NBT.  They just store different things.

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

oh thanks! :) done and works a charm ive never really used nbt before outside of tile entities and they seem to be different off memory (may be wrong)

 

ItemStack NBT is just like TE NBT.  They just store different things.

 

I think he was referring to the way tile nbt is read and saved to fields inside the tile entity class.

 

The difference is with tile entities every single block that has a tile entity has its own unique instance of the tile entity class not unlike regular entities. But with items all items share one instance of the item class so if you change something in the item class it affects all items. It can be a little confusing at first but there aren't actually any items in game. Every item is an items stack that contains that item and it is the item stack that you can store data in using nbt.

I am the author of Draconic Evolution

Link to comment
Share on other sites

oh thanks! :) done and works a charm ive never really used nbt before outside of tile entities and they seem to be different off memory (may be wrong)

 

ItemStack NBT is just like TE NBT.  They just store different things.

 

I think he was referring to the way tile nbt is read and saved to fields inside the tile entity class.

 

The difference is with tile entities every single block that has a tile entity has its own unique instance of the tile entity class not unlike regular entities. But with items all items share one instance of the item class so if you change something in the item class it affects all items. It can be a little confusing at first but there aren't actually any items in game. Every item is an items stack that contains that item and it is the item stack that you can store data in using nbt.

 

yeah that was what i was getting at lol :)

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.

Announcements



×
×
  • Create New...

Important Information

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