Jump to content

[1.8] How do i change a tileEntity's nbt data when i right click it?


Recommended Posts

Posted

I want to make a block that when right clicked with my item, which already has a nbt tag with coordinates, will store the coordinate, and do something with it. How do I change/add the block's nbt data tag? Also, do I need the block to be a tile entity? I'm not sure..

Posted

What is the Item going to do with the Block coordinates? Is the Block going to be responsible for doing anything? The answers to those two questions will determine whether you need a TileEntity or not, at least with respect to this particular interaction.

 

Note that Blocks by themselves are incapable of storing NBT data - additional data is typically stored in a TileEntity, though NBT data such as vanilla lockable containers are probably stored somewhere else.

Posted

What is the Item going to do with the Block coordinates? Is the Block going to be responsible for doing anything? The answers to those two questions will determine whether you need a TileEntity or not, at least with respect to this particular interaction.

 

Note that Blocks by themselves are incapable of storing NBT data - additional data is typically stored in a TileEntity, though NBT data such as vanilla lockable containers are probably stored somewhere else.

 

im making the block teleport me to a coordinate when an entity is touching it. the item gets the coordinate of the block i shift right click on, and stores it on a nbt tag. if it is right clicked on the teleporter block, i want the block to take the nbt data from the item and store it. based on what you said that blocks cant store nbt, i guess the block has to be a tile entity.

Posted

If you want to be teleported only as you right-click the block, you don't need a TileEntity. If you want the block to remember the coordinates, so that it teleports anything at any time AFTER the right-click, you do need a TileEntity.

 

Just make your block class extend BlockContainer and create a TileEntity class for it, with a field for the stored coordinates. In the Block class (now BlockContainer), you're interested in the onBlockActivated method. That supplies you with the player, so you can get the ItemStack you're holding and get the NBT data from that, and it gives you World and BlockPos, so you can access your TileEntity via world.getTileEntity(blockpos) or whatever. And there you just change the TileEntity's stored coordinates.

 

TileEntities don't store data purely in NBT. Because they have individual classes, they just use normal fields in them, and only use NBT when saving the world/chunk/whatever. Look at basic TileEntity tutorials for this.

 

If you want any entity to be teleported whenever the block is touched, you want a method along the lines of onLanded, isEntityInsideMaterial, or onEntityCollidedWithBlock, depending on the conditions to teleport you want.

Posted

Implementing ITileEntityProvider instead of extending BlockContainer is the recommended method of adding a TileEntity to your block.

 

Having the block teleport any entity that walks on it sounds like a very griefer-friendly mechanic... click block above lava, destroy block, set teleporter block coordinates, and sit back and watch everyone and everything to fall into the lava... You may want to consider requiring players at least to activate the teleporter themselves, rather than automatically when touching it.

Posted

Implementing ITileEntityProvider instead of extending BlockContainer is the recommended method of adding a TileEntity to your block.

No.
ITileEntityProvider

sucks, maybe not as much as

BlockContainer

, but it still sucks. Simply override

hasTileEntity

and

createTileEntity

from the

Block

class.

Ah, my mistake - for some reason I thought ITileEntityProvider was responsible for those two methods. What's the point of that interface, then?

Posted

Implementing ITileEntityProvider instead of extending BlockContainer is the recommended method of adding a TileEntity to your block.

No.
ITileEntityProvider

sucks, maybe not as much as

BlockContainer

, but it still sucks. Simply override

hasTileEntity

and

createTileEntity

from the

Block

class.

Ah, my mistake - for some reason I thought ITileEntityProvider was responsible for those two methods. What's the point of that interface, then?

 

The interface is added by vanilla and used by

BlockContainer

. Forge patches the

Block

class to add the methods diesieben07 mentioned, which use

IBlockState

instead of metadata.

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.

Posted

If you want to be teleported only as you right-click the block, you don't need a TileEntity. If you want the block to remember the coordinates, so that it teleports anything at any time AFTER the right-click, you do need a TileEntity.

 

Just make your block class extend BlockContainer and create a TileEntity class for it, with a field for the stored coordinates. In the Block class (now BlockContainer), you're interested in the onBlockActivated method. That supplies you with the player, so you can get the ItemStack you're holding and get the NBT data from that, and it gives you World and BlockPos, so you can access your TileEntity via world.getTileEntity(blockpos) or whatever. And there you just change the TileEntity's stored coordinates.

 

TileEntities don't store data purely in NBT. Because they have individual classes, they just use normal fields in them, and only use NBT when saving the world/chunk/whatever. Look at basic TileEntity tutorials for this.

 

If you want any entity to be teleported whenever the block is touched, you want a method along the lines of onLanded, isEntityInsideMaterial, or onEntityCollidedWithBlock, depending on the conditions to teleport you want.

i want the block to keep the data after world save. so yea i need it to save it in a nbt tag. also im using onentitycollidedwithblock.

Posted

almost makes me think we need to rename those methods to

readFromNBTModdersDoNotCallManually

and

writeToNBTModdersDoNotCallManually

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

You store the data as normal fields in your TileEntity class, there is no need for NBT here at all.

NBT is only for saving to disk, which happens through the methods writeToNbt and readFromNbt, these are called by Minecraft when it loads/saves your TileEntity.

ok... so if i store them in normal fields how do i make it store when saving to disk?
Posted

You override the read/write methods and make them save the fields to NBT and read them back again.

 

You don't invoke these methods, you implement them.

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.

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.