Basic Tile Entity

From Minecraft Forge
Jump to: navigation, search

Contents

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)

Goal

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

Requirements

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.

package yourModPackage;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityYour extends TileEntity{
   @Override
   public void writeToNBT(NBTTagCompound par1)
   {
      super.writeToNBT(par1);
   }

   @Override
   public void readFromNBT(NBTTagCompound par1)
   {
      super.readFromNBT(par1);
   }
}

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

public int customField;

Then, add read and write function calls corresponding to your field type. As our customField has int type, the code will be

@Override
public void writeToNBT(NBTTagCompound par1)
{
   super.writeToNBT(par1);
   par1.setInteger("customField", customField);
}

@Override
public void readFromNBT(NBTTagCompound par1)
{
   super.readFromNBT(par1);
   this.customField = par1.getInteger("customField");
}

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

public TileEntity createTileEntity(World world, int metadata)
{
   return new TileEntityYour();
}

And next lines to your main class load method

GameRegistry.registerTileEntity(yourModPackage.TileEntityYour.class, "stringID");

Reading Tile Entity fields

To read the field you made from any place in the code, use next lines inside of base mod class

TileEntityYour tile = (TileEntityYour) world.getBlockTileEntity(i, j, k);
if (tile != null)
{
   int l = tile.customField;
}


Synchronizing Tile Entity

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

Personal tools
Namespaces
Variants
Actions
Navigation
tutorials
Toolbox