2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.EnumSet;
|
2014-01-23 17:28:12 +01:00
|
|
|
import java.util.HashMap;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.world.World;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-02-20 06:18:49 +01:00
|
|
|
import appeng.api.implementations.tiles.ISegmentedInventory;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.ICommonTile;
|
2014-02-20 06:18:49 +01:00
|
|
|
import appeng.api.util.IConfigManager;
|
|
|
|
import appeng.api.util.IConfigureableObject;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.IOrientable;
|
|
|
|
import appeng.core.AELog;
|
2014-02-09 08:55:44 +01:00
|
|
|
import appeng.core.features.ItemStackSrc;
|
2014-03-15 07:58:21 +01:00
|
|
|
import appeng.helpers.ICustomNameObject;
|
2014-02-20 06:18:49 +01:00
|
|
|
import appeng.helpers.IPriorityHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
2014-02-20 06:18:49 +01:00
|
|
|
import appeng.tile.inventory.AppEngInternalAEInventory;
|
2014-02-09 02:34:52 +01:00
|
|
|
import appeng.util.Platform;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.util.SettingsFrom;
|
|
|
|
|
2014-03-15 07:58:21 +01:00
|
|
|
public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, ICustomNameObject
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private final EnumMap<TileEventType, List<AETileEventHandler>> handlers = new EnumMap<TileEventType, List<AETileEventHandler>>( TileEventType.class );
|
2014-02-09 08:55:44 +01:00
|
|
|
private final static HashMap<Class, ItemStackSrc> myItem = new HashMap();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
private ForgeDirection forward = ForgeDirection.UNKNOWN;
|
|
|
|
private ForgeDirection up = ForgeDirection.UNKNOWN;
|
|
|
|
|
|
|
|
public boolean dropItems = true;
|
2014-01-01 10:00:13 +01:00
|
|
|
public int renderFragment = 0;
|
2014-03-15 07:58:21 +01:00
|
|
|
public String customName;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
public TileEntity getTile()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:55:44 +01:00
|
|
|
static public void registerTileItem(Class c, ItemStackSrc wat)
|
2014-01-23 17:28:12 +01:00
|
|
|
{
|
|
|
|
myItem.put( c, wat );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ItemStack getItemFromTile(Object obj)
|
|
|
|
{
|
2014-02-09 08:55:44 +01:00
|
|
|
ItemStackSrc src = myItem.get( obj.getClass() );
|
|
|
|
if ( src == null )
|
|
|
|
return null;
|
|
|
|
return src.stack( 1 );
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
/**
|
|
|
|
* isRedstonePowerd has already changed.
|
|
|
|
*/
|
|
|
|
public void onRedstoneEvent()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean hasHandlerFor(TileEventType type)
|
|
|
|
{
|
|
|
|
List<AETileEventHandler> list = handlers.get( type );
|
|
|
|
return list != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<AETileEventHandler> getHandlerListFor(TileEventType type)
|
|
|
|
{
|
|
|
|
List<AETileEventHandler> list = handlers.get( type );
|
|
|
|
|
|
|
|
if ( list == null )
|
|
|
|
handlers.put( type, list = new LinkedList<AETileEventHandler>() );
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void addNewHandler(AETileEventHandler handler)
|
|
|
|
{
|
|
|
|
EnumSet<TileEventType> types = handler.getSubscribedEvents();
|
|
|
|
|
|
|
|
for (TileEventType type : types)
|
|
|
|
getHandlerListFor( type ).add( handler );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
final public boolean canUpdate()
|
|
|
|
{
|
|
|
|
return hasHandlerFor( TileEventType.TICK );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
final public void updateEntity()
|
|
|
|
{
|
|
|
|
for (AETileEventHandler h : getHandlerListFor( TileEventType.TICK ))
|
|
|
|
h.Tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-22 22:51:37 +01:00
|
|
|
// NOTE: WAS FINAL, changed for Immibis
|
|
|
|
public void writeToNBT(NBTTagCompound data)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
super.writeToNBT( data );
|
|
|
|
|
|
|
|
if ( canBeRotated() )
|
|
|
|
{
|
|
|
|
data.setString( "orientation_forward", forward.name() );
|
|
|
|
data.setString( "orientation_up", up.name() );
|
|
|
|
}
|
|
|
|
|
2014-03-15 07:58:21 +01:00
|
|
|
if ( customName != null )
|
|
|
|
data.setString( "customName", customName );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
for (AETileEventHandler h : getHandlerListFor( TileEventType.WORLD_NBT ))
|
|
|
|
h.writeToNBT( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-22 22:51:37 +01:00
|
|
|
// NOTE: WAS FINAL, changed for Immibis
|
|
|
|
public void readFromNBT(NBTTagCompound data)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
super.readFromNBT( data );
|
|
|
|
|
2014-03-15 07:58:21 +01:00
|
|
|
if ( data.hasKey( "customName" ) )
|
|
|
|
customName = data.getString( "customName" );
|
|
|
|
else
|
|
|
|
customName = null;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( canBeRotated() )
|
|
|
|
{
|
|
|
|
forward = ForgeDirection.valueOf( data.getString( "orientation_forward" ) );
|
|
|
|
up = ForgeDirection.valueOf( data.getString( "orientation_up" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException iae)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
for (AETileEventHandler h : getHandlerListFor( TileEventType.WORLD_NBT ))
|
|
|
|
{
|
|
|
|
h.readFromNBT( data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
final public void writeToStream(ByteBuf data)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( canBeRotated() )
|
|
|
|
{
|
|
|
|
byte orientation = (byte) ((up.ordinal() << 3) | forward.ordinal());
|
|
|
|
data.writeByte( orientation );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (AETileEventHandler h : getHandlerListFor( TileEventType.NETWORK ))
|
|
|
|
h.writeToStream( data );
|
|
|
|
}
|
|
|
|
catch (Throwable t)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( t );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
final public boolean readfromStream(ByteBuf data)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
boolean output = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
if ( canBeRotated() )
|
|
|
|
{
|
|
|
|
ForgeDirection old_Forward = forward;
|
|
|
|
ForgeDirection old_Up = up;
|
|
|
|
|
|
|
|
byte orientation = data.readByte();
|
|
|
|
forward = ForgeDirection.getOrientation( orientation & 0x7 );
|
|
|
|
up = ForgeDirection.getOrientation( orientation >> 3 );
|
|
|
|
|
|
|
|
output = !forward.equals( old_Forward ) || !up.equals( old_Up );
|
|
|
|
}
|
|
|
|
|
2014-01-01 10:00:13 +01:00
|
|
|
renderFragment = 100;
|
2013-12-27 23:59:59 +01:00
|
|
|
for (AETileEventHandler h : getHandlerListFor( TileEventType.NETWORK ))
|
|
|
|
if ( h.readFromStream( data ) )
|
|
|
|
output = true;
|
2014-01-01 10:00:13 +01:00
|
|
|
|
|
|
|
if ( (renderFragment & 1) == 1 )
|
|
|
|
output = true;
|
|
|
|
renderFragment = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
catch (Throwable t)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( t );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-09 02:34:52 +01:00
|
|
|
* By default all blocks can have orientation, this handles saving, and loading, as well as synchronization.
|
2013-12-27 23:59:59 +01:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean canBeRotated()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ForgeDirection getForward()
|
|
|
|
{
|
|
|
|
return forward;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ForgeDirection getUp()
|
|
|
|
{
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOrientation(ForgeDirection inForward, ForgeDirection inUp)
|
|
|
|
{
|
|
|
|
forward = inForward;
|
|
|
|
up = inUp;
|
|
|
|
markForUpdate();
|
2014-02-09 02:34:52 +01:00
|
|
|
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord, Platform.air );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onPlacement(ItemStack stack, EntityPlayer player, int side)
|
|
|
|
{
|
|
|
|
if ( stack.hasTagCompound() )
|
|
|
|
{
|
|
|
|
uploadSettings( SettingsFrom.DISMANTLE_ITEM, stack.getTagCompound() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet getDescriptionPacket()
|
|
|
|
{
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
ByteBuf stream = Unpooled.buffer();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
writeToStream( stream );
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( stream.readableBytes() == 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch (Throwable t)
|
|
|
|
{
|
|
|
|
AELog.error( t );
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
data.setByteArray( "X", stream.array() );
|
|
|
|
return new S35PacketUpdateTileEntity( xCoord, yCoord, zCoord, 64, data );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
// / pkt.actionType
|
|
|
|
if ( pkt.func_148853_f() == 64 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
ByteBuf stream = Unpooled.copiedBuffer( pkt.func_148857_g().getByteArray( "X" ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( readfromStream( stream ) )
|
|
|
|
markForUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void markForUpdate()
|
|
|
|
{
|
2014-01-01 10:00:13 +01:00
|
|
|
if ( renderFragment > 0 )
|
|
|
|
renderFragment = renderFragment | 1;
|
|
|
|
else
|
|
|
|
{
|
2014-03-09 04:35:53 +01:00
|
|
|
// TODO: Optimize Network Load
|
2014-01-01 10:00:13 +01:00
|
|
|
if ( worldObj != null )
|
2014-03-13 04:14:55 +01:00
|
|
|
{
|
|
|
|
AELog.blockUpdate( xCoord, yCoord, zCoord, this );
|
2014-01-01 10:00:13 +01:00
|
|
|
worldObj.markBlockForUpdate( xCoord, yCoord, zCoord );
|
2014-03-13 04:14:55 +01:00
|
|
|
}
|
2014-01-01 10:00:13 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-09 02:34:52 +01:00
|
|
|
* returns the contents of the tile entity, into the world, defaults to dropping everything in the inventory.
|
2013-12-27 23:59:59 +01:00
|
|
|
*
|
|
|
|
* @param w
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
|
|
|
* @param drops
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void getDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops)
|
|
|
|
{
|
|
|
|
if ( this instanceof IInventory )
|
|
|
|
{
|
|
|
|
IInventory inv = (IInventory) this;
|
|
|
|
|
|
|
|
for (int l = 0; l < inv.getSizeInventory(); l++)
|
|
|
|
{
|
|
|
|
ItemStack is = inv.getStackInSlot( l );
|
|
|
|
|
|
|
|
if ( is != null )
|
|
|
|
{
|
|
|
|
drops.add( is );
|
|
|
|
inv.setInventorySlotContents( l, (ItemStack) null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReady()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-09 02:34:52 +01:00
|
|
|
* depending on the from, diffrent settings will be accepted, don't call this with null
|
2013-12-27 23:59:59 +01:00
|
|
|
*
|
|
|
|
* @param from
|
|
|
|
* @param compound
|
|
|
|
*/
|
|
|
|
public void uploadSettings(SettingsFrom from, NBTTagCompound compound)
|
|
|
|
{
|
2014-02-20 06:18:49 +01:00
|
|
|
if ( compound != null && this instanceof IConfigureableObject )
|
|
|
|
{
|
|
|
|
IConfigManager cm = ((IConfigureableObject) this).getConfigManager();
|
|
|
|
if ( cm != null )
|
|
|
|
cm.readFromNBT( compound );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this instanceof IPriorityHost )
|
|
|
|
{
|
|
|
|
IPriorityHost pHost = (IPriorityHost) this;
|
|
|
|
pHost.setPriority( compound.getInteger( "priority" ) );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-20 06:18:49 +01:00
|
|
|
if ( this instanceof ISegmentedInventory )
|
|
|
|
{
|
|
|
|
IInventory inv = ((ISegmentedInventory) this).getInventoryByName( "config" );
|
|
|
|
if ( inv != null && inv instanceof AppEngInternalAEInventory )
|
|
|
|
{
|
|
|
|
AppEngInternalAEInventory target = (AppEngInternalAEInventory) inv;
|
|
|
|
AppEngInternalAEInventory tmp = new AppEngInternalAEInventory( null, target.getSizeInventory() );
|
|
|
|
tmp.readFromNBT( compound, "config" );
|
|
|
|
for (int x = 0; x < tmp.getSizeInventory(); x++)
|
|
|
|
target.setInventorySlotContents( x, tmp.getStackInSlot( x ) );
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* null means nothing to store...
|
|
|
|
*
|
|
|
|
* @param from
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public NBTTagCompound downloadSettings(SettingsFrom from)
|
|
|
|
{
|
2014-02-20 06:18:49 +01:00
|
|
|
NBTTagCompound output = new NBTTagCompound();
|
|
|
|
|
|
|
|
if ( this instanceof IConfigureableObject )
|
|
|
|
{
|
|
|
|
IConfigManager cm = ((IConfigureableObject) this).getConfigManager();
|
|
|
|
if ( cm != null )
|
|
|
|
cm.writeToNBT( output );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this instanceof IPriorityHost )
|
|
|
|
{
|
|
|
|
IPriorityHost pHost = (IPriorityHost) this;
|
|
|
|
output.setInteger( "priority", pHost.getPriority() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this instanceof ISegmentedInventory )
|
|
|
|
{
|
|
|
|
IInventory inv = ((ISegmentedInventory) this).getInventoryByName( "config" );
|
|
|
|
if ( inv != null && inv instanceof AppEngInternalAEInventory )
|
|
|
|
{
|
|
|
|
((AppEngInternalAEInventory) inv).writeToNBT( output, "config" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.hasNoTags() ? null : output;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public void securityBreak()
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
worldObj.func_147480_a( xCoord, yCoord, zCoord, true ); // worldObj.destroyBlock( xCoord, yCoord, zCoord, true
|
|
|
|
// );
|
2014-01-27 05:00:36 +01:00
|
|
|
dropItems = false;
|
|
|
|
}
|
2014-01-31 01:50:11 +01:00
|
|
|
|
|
|
|
public void saveChanges()
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
super.markDirty();
|
2014-01-31 01:50:11 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 05:36:10 +01:00
|
|
|
public boolean requiresTESR()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 07:58:21 +01:00
|
|
|
public void setName(String name)
|
|
|
|
{
|
|
|
|
this.customName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getCustomName()
|
|
|
|
{
|
|
|
|
return hasCustomName() ? customName : getClass().getSimpleName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomName()
|
|
|
|
{
|
|
|
|
return customName != null && customName.length() > 0;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|