tcr/java/src/game/tileentity/TileEntity.java

227 lines
5.4 KiB
Java
Executable file

package game.tileentity;
import game.block.Block;
import game.init.Blocks;
import game.init.TileRegistry;
import game.log.Log;
import game.nbt.NBTTagCompound;
import game.network.Packet;
import game.world.BlockPos;
import game.world.State;
import game.world.World;
public abstract class TileEntity
{
public static enum EnumCreateEntityType
{
IMMEDIATE,
QUEUED,
CHECK;
}
/** the instance of the world the tile entity is in. */
protected World worldObj;
protected BlockPos pos = BlockPos.ORIGIN;
protected boolean tileEntityInvalid;
private int blockMetadata = -1;
/** the Block type that this TileEntity is contained within */
protected Block blockType;
/**
* Returns the worldObj for this tileEntity.
*/
public World getWorld()
{
return this.worldObj;
}
/**
* Sets the worldObj for this tileEntity.
*/
public void setWorldObj(World worldIn)
{
this.worldObj = worldIn;
}
/**
* Returns true if the worldObj isn't null.
*/
public boolean hasWorldObj()
{
return this.worldObj != null;
}
public void readFromNBT(NBTTagCompound compound)
{
this.pos = new BlockPos(compound.getInteger("x"), compound.getInteger("y"), compound.getInteger("z"));
}
public void writeToNBT(NBTTagCompound compound)
{
String s = (String)TileRegistry.classToNameMap.get(this.getClass());
if (s == null)
{
throw new RuntimeException(this.getClass() + " is missing a mapping! This is a bug!");
}
else
{
compound.setString("id", s);
compound.setInteger("x", this.pos.getX());
compound.setInteger("y", this.pos.getY());
compound.setInteger("z", this.pos.getZ());
}
}
/**
* Creates a new entity and loads its data from the specified NBT.
*/
public static TileEntity createAndLoadEntity(NBTTagCompound nbt)
{
TileEntity tileentity = null;
try
{
Class <? extends TileEntity > oclass = (Class)TileRegistry.nameToClassMap.get(nbt.getString("id"));
if (oclass != null)
{
tileentity = (TileEntity)oclass.newInstance();
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
if (tileentity != null)
{
tileentity.readFromNBT(nbt);
}
else
{
Log.JNI.warn("Ignoriere Block-Objekt mit ID " + nbt.getString("id"));
}
return tileentity;
}
public int getBlockMetadata()
{
if (this.blockMetadata == -1)
{
State iblockstate = this.worldObj.getState(this.pos);
this.blockMetadata = iblockstate.getBlock().getMetaFromState(iblockstate);
}
return this.blockMetadata;
}
/**
* For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
* hasn't changed and skip it.
*/
public void markDirty()
{
if (this.worldObj != null)
{
State iblockstate = this.worldObj.getState(this.pos);
this.blockMetadata = iblockstate.getBlock().getMetaFromState(iblockstate);
this.worldObj.markChunkDirty(this.pos, this);
if (this.getBlockType() != Blocks.air)
{
this.worldObj.updateComparatorOutputLevel(this.pos, this.getBlockType());
}
}
}
/**
* Returns the square of the distance between this entity and the passed in coordinates.
*/
public double getDistanceSq(double x, double y, double z)
{
double d0 = (double)this.pos.getX() + 0.5D - x;
double d1 = (double)this.pos.getY() + 0.5D - y;
double d2 = (double)this.pos.getZ() + 0.5D - z;
return d0 * d0 + d1 * d1 + d2 * d2;
}
public double getMaxRenderDistanceSquared()
{
return 4096.0D;
}
public BlockPos getPos()
{
return this.pos;
}
/**
* Gets the block type at the location of this entity (client-only).
*/
public Block getBlockType()
{
if (this.blockType == null)
{
this.blockType = this.worldObj.getState(this.pos).getBlock();
}
return this.blockType;
}
/**
* Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
* server to the client easily. For example this is used by signs to synchronise the text to be displayed.
*/
public Packet getDescriptionPacket()
{
return null;
}
public boolean isInvalid()
{
return this.tileEntityInvalid;
}
/**
* invalidates a tile entity
*/
public void invalidate()
{
this.tileEntityInvalid = true;
}
/**
* validates a tile entity
*/
public boolean validate()
{
this.tileEntityInvalid = false;
return false;
}
public boolean receiveClientEvent(int id, int type)
{
return false;
}
public void updateContainingBlockInfo()
{
this.blockType = null;
this.blockMetadata = -1;
}
public void setPos(BlockPos posIn)
{
this.pos = posIn;
}
// public boolean hasSpecialNBT()
// {
// return false;
// }
public abstract int getColor();
}