Jump to content

Tool Item to ItemStack method help


TuxCraft

Recommended Posts

How do I make an item reference itself as an itemstack? I've tried this

 

itemToDrop = par3EntityPlayer.inventory.getCurrentItem();

 

But when I add it back into the inventory (par3EntityPlayer.inventory.addItemStackToInventory(itemToDrop);) in another method the item disappears whenever I try to use it (onItemRightClick). I think it might be a server/client error but I'm very inexperienced with packets and such. I tried following the tutorial on the wiki but I really didn't understand it.

Link to comment
Share on other sites

To reference an item as an ItemStack, as simply use

new ItemStack(Item.bucketEmpty);

Replace Item.bucketEmpty with your item.

 

 

To reference it in terms of a player's current item, simply use

new ItemStack(par3EntityPlayer.inventory.getCurrentItem());

 

Hope this helps!

Link to comment
Share on other sites

Thanks for responding but I should probably add that I want to convert it to an itemstack so that it will keep its name, damage etc. So that means your first method wouldn't work. As for your second one par3EntityPlayer.inventory.getCurrentItem() returns an itemstack so you wouldn't be able to put it in the new itemstack annotation. If you wanted to do that you'd have to add .getItem() to the end. However that returns the item not the itemstack.

 

Would I have to just get all the features I want the item to keep in different variables and store it in nbt or something? The main ones I'd like it to keep are the name, damage, and enchantment. For the name I found this method...

 

itemToDropName = par3EntityPlayer.inventory.getCurrentItem().getDisplayName()

 

For the rest I'm not sure what to do, please help.

Link to comment
Share on other sites

You can't "convert" to an ItemStack. Item and ItemStack are 2 different things. The Item class basically stores how the Item behaves. Only one instance of it exists for every Item. Then whenever it comes to inventory management, etc. the ItemStack comes in. It stores:

- the Item it contains

- the stack size

- the damage value

- possibly an additional NBTTagCompound (used e.g. for enchantments)

You can access that NBTTagCompound by using stack.stackTagCompound (iirc). Just be sure to initialize it, because it is null by default.

 

So for example to get damage would I do something like this?

 

short damage = itemstack.stackTagCompound.getShort("Damage");

 

If it is, that isn't working for me.

Link to comment
Share on other sites

You can't "convert" to an ItemStack. Item and ItemStack are 2 different things. The Item class basically stores how the Item behaves. Only one instance of it exists for every Item. Then whenever it comes to inventory management, etc. the ItemStack comes in. It stores:

- the Item it contains

- the stack size

- the damage value

- possibly an additional NBTTagCompound (used e.g. for enchantments)

You can access that NBTTagCompound by using stack.stackTagCompound (iirc). Just be sure to initialize it, because it is null by default.

 

So for example to get damage would I do something like this?

 

short damage = itemstack.stackTagCompound.getShort("Damage");

 

If it is, that isn't working for me.

 

No, look at the ItemStack class itself, it has methods which names are self-descriptive:

damage = stack.getItemDamage()

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

No, look at the ItemStack class itself, it has methods which names are self-descriptive:

damage = stack.getItemDamage()

 

Thank you, I did that and so far so good, the only thing I'm not sure about enchantments. There is one function that in a comment says that it holds enchantments, but it returns an nbttagcompound. Here's what I have so far.

public static ItemStack getItemStack(Item item, int i, String s) {

	ItemStack par1itemstack = new ItemStack (item, 1);
	par1itemstack.setItemDamage(i);
	if(s != null)
	{
		par1itemstack.setItemName(s);
	}

	return par1itemstack;
}

 

And this to add the item

par1EntityPlayer.inventory.addItemStackToInventory(itemSpear.getItemStack(itemTool.item, damage, null));

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.