Jump to content

Recommended Posts

Posted

Hey there!

 

I'm working on a mod that requires a backpack. I've created a tileentity, specialrenderer, model etc so it's not a regular block.

 

What I'm trying to do is be able to place objects in the backpack (like a chest), be able to destroy it and keep the inventory, then when I place the pack again, it'll have the inventory it had when destroyed.

 

It's proving to be a tremendous task. I've looked into hundreds of tut's and I've got absolutely no clue how to go about this. I don't even have the slightest hint as to where to start.

 

Right now, I have just the tile I want to use.

 

I know, this isn't much to go off of, but I'm literally clueless. I've never done anything with GUI's or Inventories, so this is all alien knowledge. Help would be much appreciated!

 

I'll send any info/files needed.

Posted

Why a Block and not an Item?

 

If you must use a Block, then you will need to make it like the Ender Chest and store the inventory not in the block, but in the player's IExtendedEntityProperties. You won't need a TileEntity, then, either, as you can create the GUI / Container directly from the IEEP when clicking on the Block.

 

If you want to make it so the player can have multiple backpacks, however, you need to store the inventory in the ItemStack NBT.

Posted

I have seen this done before, but rather than destroying the block, the player right clicked it instead. and then the backpack "block" would disappear and appear in the players inventory.

Oh yeah, you could do it that way, too. ItemBlock calls a method to effectively recreate the TileEntity from the ItemStack NBT when placed, so as long as you got it in there correctly, then that would obviate the need for IEEP to store the inventory and you'd be able to store as many as you wanted. So many options! ;)

Posted

It is possible to do the stack nbt when the block is destroyed as well.

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.

Posted

Why a Block and not an Item?

 

If you must use a Block, then you will need to make it like the Ender Chest and store the inventory not in the block, but in the player's IExtendedEntityProperties. You won't need a TileEntity, then, either, as you can create the GUI / Container directly from the IEEP when clicking on the Block.

 

If you want to make it so the player can have multiple backpacks, however, you need to store the inventory in the ItemStack NBT.

 

Just a matter of preference really, and thanks! I'll go try it out!

Posted

I have seen this done before, but rather than destroying the block, the player right clicked it instead. and then the backpack "block" would disappear and appear in the players inventory.

Oh yeah, you could do it that way, too. ItemBlock calls a method to effectively recreate the TileEntity from the ItemStack NBT when placed, so as long as you got it in there correctly, then that would obviate the need for IEEP to store the inventory and you'd be able to store as many as you wanted. So many options! ;)

 

How would I go about doing that? And which method would that happen to be? :D Man, this seems to be a lot easier than I thought.

Posted

Hm, actually, that method only exists in 1.8, but pre-1.8, the Block method #onBlockPlacedBy will still be called when the block is placed by a player and has an ItemStack parameter, so if you write the TileEntity to NBT when the block is broken by a player and store that tag in the ItemStack, you can restore the TileEntity to its previous state using that tag.

 

In pseudo-code with possibly made-up method names (they should be close to real ones, but I didn't type this in Eclipse or anything, so...):

// if #breakBlock is called before this, the TileEntity will have been removed, so you'll have to spawn the EntityItem from there instead
// and then don't drop anything from here
#dropBlockAsItem {
TileEntity te = world.getTileEntity(x, y, z);
NBTTagCompound nbt = new NBTTagCompound();
te.writeToNBT(nbt);
parItemStack.setTagCompound(nbt); // this will store the TileEntity data in the ItemStack tag compound
}

#onBlockPlacedBy {
TileEntity te = world.getTileEntity(x, y, z); // TileEntity should already exist since the block has already been placed
te.readFromNBT(parItemStack.getTagCompound()); // now just read the data back in and you're done
}

Posted

Hm, actually, that method only exists in 1.8, but pre-1.8, the Block method #onBlockPlacedBy will still be called when the block is placed by a player and has an ItemStack parameter, so if you write the TileEntity to NBT when the block is broken by a player and store that tag in the ItemStack, you can restore the TileEntity to its previous state using that tag.

 

In pseudo-code with possibly made-up method names (they should be close to real ones, but I didn't type this in Eclipse or anything, so...):

// if #breakBlock is called before this, the TileEntity will have been removed, so you'll have to spawn the EntityItem from there instead
// and then don't drop anything from here
#dropBlockAsItem {
TileEntity te = world.getTileEntity(x, y, z);
NBTTagCompound nbt = new NBTTagCompound();
te.writeToNBT(nbt);
parItemStack.setTagCompound(nbt); // this will store the TileEntity data in the ItemStack tag compound
}

#onBlockPlacedBy {
TileEntity te = world.getTileEntity(x, y, z); // TileEntity should already exist since the block has already been placed
te.readFromNBT(parItemStack.getTagCompound()); // now just read the data back in and you're done
}

 

OH MY GOD IT WAS SO OBVIOUS! Thanks m80! I'll go get to work.

 

P.S. Merry Christmas, if you celebrate it

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.

×
×
  • Create New...

Important Information

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