Basic Tile Entity

From Minecraft Forge
Jump to: navigation, search

Looks like this is broken

For example, you need to implement ITileEntityProvider in your block class. There's probably other broken things also, so I suggest that you check another tile entity tutorial.

--Bensku 03:44, 12 October 2014 (PDT)


Learn how to make basic Tile Entity, add some params to it and read them.


Basic Blocks

What is Tile Entity

The standard block class has no field that can vary from block to block in the world, except metadata. That metadata has a very small size that is rarely enough. Tile Entities were made to workaround this. Every single Tile Entity is bound to coordinates in the world, and its fields have unique values. As examples, blocks that have an inventory, pistons, signs or other items use Tile Entity. Avoid extensive usage of Tile Entities as they are more expensive on memory.

Standard fields

Tile Entity have some self-explanatory standard fields like

  • World worldObj
  • int xcoord, int ycoord, int zcoord
  • Block blockType

Basic Tile Entity

Firstly, you have to create a Tile Entity class. <syntaxhighlight lang="java">package yourModPackage; import net.minecraft.nbt.NBTTagCompound; import; import; import; import net.minecraft.tileentity.TileEntity; import;

public class TileEntityYour extends TileEntity{

  public void writeToNBT(NBTTagCompound par1)
  public void readFromNBT(NBTTagCompound par1)

}</syntaxhighlight> That's the basic code you need to get it working.

Adding fields

To add new field and make it save&load to/from world, firstly add it inside your tileEntity class <syntaxhighlight lang="java">public int customField;</syntaxhighlight> Then, add read and write function calls corresponding to your field type. As our customField has int type, the code will be <syntaxhighlight lang="java">@Override public void writeToNBT(NBTTagCompound par1) {

  par1.setInteger("customField", customField);


@Override public void readFromNBT(NBTTagCompound par1) {

  this.customField = par1.getInteger("customField");

}</syntaxhighlight> If your field had float type, that will be par1.setFloat and so on.

Registering Tile Entity

To bind your Tile Entity to exact block, add these lines to your block class <syntaxhighlight lang="java">public TileEntity createTileEntity(World world, int metadata) {

  return new TileEntityYour();

}</syntaxhighlight> And next lines to your main class load method <syntaxhighlight lang="java">GameRegistry.registerTileEntity(yourModPackage.TileEntityYour.class, "stringID");</syntaxhighlight>

Reading Tile Entity fields

To read the field you made from any place in the code, use next lines inside of base mod class <syntaxhighlight lang="java">TileEntityYour tile = (TileEntityYour) world.getBlockTileEntity(i, j, k); if (tile != null) {

  int l = tile.customField;

}</syntaxhighlight> {{#set:Difficulty=Beginner}}

Synchronizing Tile Entity

This is beyond the scope of this article. For further information, see Syncing a TileEntity.