Jump to content

Casting ItemStack to TileEntity


TcFox

Recommended Posts

The question doesn't make any sense, you can't cast between unrelated types.

 

An

Item

is a type of item, an

ItemStack

is a stack of an item. A

TileEntity

is linked to a block in the world.

 

A

TileEntity

can store

ItemStack

s in it, but these

ItemStack

s don't know anything about where they're being stored.

 

What is it you're trying to achieve?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Tile Entities are only associated with Blocks?? thats my problem then because im trying to follow bedrock miners tutorial for making inventories but with an item instead of a block. Do you know of any tutorials for making Item inventories such as a backpack?

Link to comment
Share on other sites

coolAlias has a tutorial on a backpack-like item here.

 

If you're using 1.8.9, you may want to check out the capability system and

IItemHandler

/

CapabilityItemHandler

. You should be able to use these to store an inventory in an item without having to read from and write to NBT every time you want to access it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Yah I saw his tutorial which is in 1.7 and I'm in 1.8 so I guess I'll have to research the capability system, Thanks!

The tutorial works in 1.8, too... hardly anything related to inventories changed, and there are plenty of notes explaining what you need to update.

 

The capability system is like the IExtendedEntityProperties of Items - if you've got your own class, you're better off just coding it into that directly, but if you want your stuff to apply more generally (i.e. to all items or a group of items, such as tools), then that's where it shines. That's just the opinion I formed of it after glancing at it briefly, and I reserve the right to change my opinion at any time, especially after actually using it :P

Link to comment
Share on other sites

  • 2 weeks later...

Thanks. So I followed the tutorial and modified very little, but for some reason every time I reopen the inventory my Items are deleted. I posted areas I thought would be possible culprits but I don't know whats wrong. Any suggestions for disappearing items?

 


public class ModGuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z){

	BlockPos pos = new BlockPos(x, y, z);
	switch(ID){
	case Constants.ITEM_GAUNTLET:
		return new ContainerGauntlet(player, player.inventory, new InventoryGauntlet(player.getHeldItem()));
	}		
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z){

	BlockPos pos = new BlockPos(x, y, z);

	switch (ID){
	case Constants.ITEM_GAUNTLET:
		return new GuiItemGauntlet((ContainerGauntlet) new ContainerGauntlet(player, player.inventory, new InventoryGauntlet(player.getHeldItem())));
	}
	return null;
}
}

 

thats my guiHandler^^

 

 

public void writeToNBT(NBTTagCompound tagcompound){
	NBTTagList nbttaglist = new NBTTagList();
	for (int i = 0; i < this.getSizeInventory(); ++i)
	{
		if (this.getStackInSlot(i) != null)
		{
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.getStackInSlot(i).writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}
	tagcompound.setTag(tagName, nbttaglist);
}

public void readFromNBT(NBTTagCompound compound) {
	NBTTagList items = compound.getTagList(tagName, compound.getId());
	for (int i = 0; i < items.tagCount(); ++i) {
		NBTTagCompound item = items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot,
					ItemStack.loadItemStackFromNBT(item));
		}
	}
}

 

 

read and write nbt^^

 

 

Thanks for any help

Link to comment
Share on other sites

In what way are "items deleted"? What did you see happening when you stepped through these NBT methods in the debugger? Was data mishandled, or was there no data at all?

 

Review a vanilla analog to see if there's a critical step your surrounding code might be missing (like flushing a cache to file, marking a TE dirty so it will be written, that sort of thing).

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.