Applied-Energistics-2-tiler.../src/main/java/appeng/tile/AEBaseTile.java

522 lines
13 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.tile;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
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;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.implementations.tiles.ISegmentedInventory;
import appeng.api.util.ICommonTile;
import appeng.api.util.IConfigManager;
import appeng.api.util.IConfigurableObject;
import appeng.api.util.IOrientable;
import appeng.core.AELog;
import appeng.core.features.ItemStackSrc;
import appeng.helpers.ICustomNameObject;
import appeng.helpers.IPriorityHost;
import appeng.tile.events.AETileEventHandler;
import appeng.tile.events.TileEventType;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.util.Platform;
import appeng.util.SettingsFrom;
public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, ICustomNameObject
{
static private final HashMap<Class, EnumMap<TileEventType, List<AETileEventHandler>>> handlers = new HashMap<Class, EnumMap<TileEventType, List<AETileEventHandler>>>();
2014-09-28 22:20:14 +02:00
static private final HashMap<Class, ItemStackSrc> myItem = new HashMap<Class, ItemStackSrc>();
2014-09-24 02:26:27 +02:00
private ForgeDirection forward = ForgeDirection.UNKNOWN;
private ForgeDirection up = ForgeDirection.UNKNOWN;
public static final ThreadLocal<WeakReference<AEBaseTile>> dropNoItems = new ThreadLocal<WeakReference<AEBaseTile>>();
2014-09-24 02:26:27 +02:00
public void disableDrops()
{
dropNoItems.set( new WeakReference<AEBaseTile>( this ) );
}
public boolean dropItems()
{
WeakReference<AEBaseTile> what = dropNoItems.get();
return what == null || what.get() != this;
}
public int renderFragment = 0;
public String customName;
public boolean notLoaded()
{
2014-12-29 15:13:47 +01:00
return !this.worldObj.blockExists( this.xCoord, this.yCoord, this.zCoord );
2014-09-24 02:26:27 +02:00
}
public TileEntity getTile()
{
return this;
}
static public void registerTileItem(Class c, ItemStackSrc wat)
{
myItem.put( c, wat );
}
protected ItemStack getItemFromTile(Object obj)
{
ItemStackSrc src = myItem.get( obj.getClass() );
if ( src == null )
return null;
return src.stack( 1 );
}
protected boolean hasHandlerFor(TileEventType type)
{
2014-12-29 15:13:47 +01:00
List<AETileEventHandler> list = this.getHandlerListFor( type );
2014-09-24 02:26:27 +02:00
return list != null && !list.isEmpty();
}
protected List<AETileEventHandler> getHandlerListFor(TileEventType type)
{
2014-12-29 15:13:47 +01:00
Class clz = this.getClass();
2014-09-24 02:26:27 +02:00
EnumMap<TileEventType, List<AETileEventHandler>> handlerSet = handlers.get( clz );
if ( handlerSet == null )
{
handlers.put( clz, handlerSet = new EnumMap<TileEventType, List<AETileEventHandler>>( TileEventType.class ) );
for (Method m : clz.getMethods())
{
TileEvent te = m.getAnnotation( TileEvent.class );
if ( te != null )
{
2014-12-29 15:13:47 +01:00
this.addHandler( handlerSet, te.value(), m );
2014-09-24 02:26:27 +02:00
}
}
}
List<AETileEventHandler> list = handlerSet.get( type );
if ( list == null )
handlerSet.put( type, list = new LinkedList<AETileEventHandler>() );
return list;
}
private void addHandler(EnumMap<TileEventType, List<AETileEventHandler>> handlerSet, TileEventType value, Method m)
{
List<AETileEventHandler> list = handlerSet.get( value );
if ( list == null )
2014-09-28 22:20:14 +02:00
handlerSet.put( value, list = new ArrayList<AETileEventHandler>() );
2014-09-24 02:26:27 +02:00
list.add( new AETileEventHandler( m, value ) );
}
@Override
final public boolean canUpdate()
{
2014-12-29 15:13:47 +01:00
return this.hasHandlerFor( TileEventType.TICK );
2014-09-24 02:26:27 +02:00
}
final public void Tick()
{
}
@Override
final public void updateEntity()
{
2014-12-29 15:13:47 +01:00
for (AETileEventHandler h : this.getHandlerListFor( TileEventType.TICK ))
2014-09-24 02:26:27 +02:00
h.Tick( this );
}
@Override
public void onChunkUnload()
{
2014-12-29 15:13:47 +01:00
if ( !this.isInvalid() )
this.invalidate();
2014-09-24 02:26:27 +02:00
}
/**
* for dormant chunk cache.
*/
public void onChunkLoad()
{
2014-12-29 15:13:47 +01:00
if ( this.isInvalid() )
this.validate();
2014-09-24 02:26:27 +02:00
}
@Override
// NOTE: WAS FINAL, changed for Immibis
final public void writeToNBT(NBTTagCompound data)
{
super.writeToNBT( data );
2014-12-29 15:13:47 +01:00
if ( this.canBeRotated() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
data.setString( "orientation_forward", this.forward.name() );
data.setString( "orientation_up", this.up.name() );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
if ( this.customName != null )
data.setString( "customName", this.customName );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
for (AETileEventHandler h : this.getHandlerListFor( TileEventType.WORLD_NBT_WRITE ))
2014-09-24 02:26:27 +02:00
h.writeToNBT( this, data );
}
@Override
// NOTE: WAS FINAL, changed for Immibis
final public void readFromNBT(NBTTagCompound data)
{
super.readFromNBT( data );
if ( data.hasKey( "customName" ) )
2014-12-29 15:13:47 +01:00
this.customName = data.getString( "customName" );
2014-09-24 02:26:27 +02:00
else
2014-12-29 15:13:47 +01:00
this.customName = null;
2014-09-24 02:26:27 +02:00
try
{
2014-12-29 15:13:47 +01:00
if ( this.canBeRotated() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.forward = ForgeDirection.valueOf( data.getString( "orientation_forward" ) );
this.up = ForgeDirection.valueOf( data.getString( "orientation_up" ) );
2014-09-24 02:26:27 +02:00
}
}
catch (IllegalArgumentException ignored)
2014-09-24 02:26:27 +02:00
{
}
2014-12-29 15:13:47 +01:00
for (AETileEventHandler h : this.getHandlerListFor( TileEventType.WORLD_NBT_READ ))
2014-09-24 02:26:27 +02:00
{
h.readFromNBT( this, data );
}
}
final public void writeToStream(ByteBuf data)
{
try
{
2014-12-29 15:13:47 +01:00
if ( this.canBeRotated() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
byte orientation = (byte) ((this.up.ordinal() << 3) | this.forward.ordinal());
2014-09-24 02:26:27 +02:00
data.writeByte( orientation );
}
2014-12-29 15:13:47 +01:00
for (AETileEventHandler h : this.getHandlerListFor( TileEventType.NETWORK_WRITE ))
2014-09-24 02:26:27 +02:00
h.writeToStream( this, data );
}
catch (Throwable t)
{
AELog.error( t );
}
}
final public boolean readFromStream(ByteBuf data)
{
boolean output = false;
try
{
2014-12-29 15:13:47 +01:00
if ( this.canBeRotated() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
ForgeDirection old_Forward = this.forward;
ForgeDirection old_Up = this.up;
2014-09-24 02:26:27 +02:00
byte orientation = data.readByte();
2014-12-29 15:13:47 +01:00
this.forward = ForgeDirection.getOrientation( orientation & 0x7 );
this.up = ForgeDirection.getOrientation( orientation >> 3 );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
output = !this.forward.equals( old_Forward ) || !this.up.equals( old_Up );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.renderFragment = 100;
for (AETileEventHandler h : this.getHandlerListFor( TileEventType.NETWORK_READ ))
2014-09-24 02:26:27 +02:00
if ( h.readFromStream( this, data ) )
output = true;
2014-12-29 15:13:47 +01:00
if ( (this.renderFragment & 1) == 1 )
2014-09-24 02:26:27 +02:00
output = true;
2014-12-29 15:13:47 +01:00
this.renderFragment = 0;
2014-09-24 02:26:27 +02:00
}
catch (Throwable t)
{
AELog.error( t );
}
return output;
}
/**
* By default all blocks can have orientation, this handles saving, and loading, as well as synchronization.
*
2014-09-27 23:17:47 +02:00
* @return true if tile can be rotated
2014-09-24 02:26:27 +02:00
*/
@Override
public boolean canBeRotated()
{
return true;
}
@Override
public ForgeDirection getForward()
{
2014-12-29 15:13:47 +01:00
return this.forward;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getUp()
{
2014-12-29 15:13:47 +01:00
return this.up;
2014-09-24 02:26:27 +02:00
}
@Override
public void setOrientation(ForgeDirection inForward, ForgeDirection inUp)
{
2014-12-29 15:13:47 +01:00
this.forward = inForward;
this.up = inUp;
this.markForUpdate();
Platform.notifyBlocksOfNeighbors( this.worldObj, this.xCoord, this.yCoord, this.zCoord );
2014-09-24 02:26:27 +02:00
}
public void onPlacement(ItemStack stack, EntityPlayer player, int side)
{
if ( stack.hasTagCompound() )
{
2014-12-29 15:13:47 +01:00
this.uploadSettings( SettingsFrom.DISMANTLE_ITEM, stack.getTagCompound() );
2014-09-24 02:26:27 +02:00
}
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound data = new NBTTagCompound();
ByteBuf stream = Unpooled.buffer();
try
{
2014-12-29 15:13:47 +01:00
this.writeToStream( stream );
2014-09-24 02:26:27 +02:00
if ( stream.readableBytes() == 0 )
return null;
}
catch (Throwable t)
{
AELog.error( t );
}
stream.capacity( stream.readableBytes() );
data.setByteArray( "X", stream.array() );
2014-12-29 15:13:47 +01:00
return new S35PacketUpdateTileEntity( this.xCoord, this.yCoord, this.zCoord, 64, data );
2014-09-24 02:26:27 +02:00
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
{
// / pkt.actionType
if ( pkt.func_148853_f() == 64 )
{
ByteBuf stream = Unpooled.copiedBuffer( pkt.func_148857_g().getByteArray( "X" ) );
2014-12-29 15:13:47 +01:00
if ( this.readFromStream( stream ) )
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
}
public void markForUpdate()
{
2014-12-29 15:13:47 +01:00
if ( this.renderFragment > 0 )
this.renderFragment = this.renderFragment | 1;
2014-09-24 02:26:27 +02:00
else
{
// TODO: Optimize Network Load
2014-12-29 15:13:47 +01:00
if ( this.worldObj != null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
AELog.blockUpdate( this.xCoord, this.yCoord, this.zCoord, this );
this.worldObj.markBlockForUpdate( this.xCoord, this.yCoord, this.zCoord );
2014-09-24 02:26:27 +02:00
}
}
}
/**
* returns the contents of the tile entity, into the world, defaults to dropping everything in the inventory.
*
2014-09-27 23:17:47 +02:00
* @param w world
* @param x x pos of tile entity
* @param y y pos of tile entity
* @param z z pos of tile entity
* @param drops drops of tile entity
2014-09-24 02:26:27 +02:00
*/
@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 );
}
}
}
public void getNoDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops)
{
}
public void onReady()
{
}
/**
* depending on the from, different settings will be accepted, don't call this with null
*
2014-09-27 23:17:47 +02:00
* @param from source of settings
* @param compound compound of source
2014-09-24 02:26:27 +02:00
*/
public void uploadSettings(SettingsFrom from, NBTTagCompound compound)
{
if ( compound != null && this instanceof IConfigurableObject )
{
IConfigManager cm = ((IConfigurableObject) this).getConfigManager();
if ( cm != null )
cm.readFromNBT( compound );
}
if ( this instanceof IPriorityHost )
{
IPriorityHost pHost = (IPriorityHost) this;
pHost.setPriority( compound.getInteger( "priority" ) );
}
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 ) );
}
}
}
/**
* null means nothing to store...
*
2014-09-27 23:17:47 +02:00
* @param from source of settings
* @return compound of source
2014-09-24 02:26:27 +02:00
*/
public NBTTagCompound downloadSettings(SettingsFrom from)
{
NBTTagCompound output = new NBTTagCompound();
2014-12-29 15:13:47 +01:00
if ( this.hasCustomName() )
2014-09-24 02:26:27 +02:00
{
NBTTagCompound dsp = new NBTTagCompound();
2014-12-29 15:13:47 +01:00
dsp.setString( "Name", this.getCustomName() );
2014-09-24 02:26:27 +02:00
output.setTag( "display", dsp );
}
if ( this instanceof IConfigurableObject )
{
IConfigManager cm = ((IConfigurableObject) 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;
}
public void securityBreak()
{
2014-12-29 15:13:47 +01:00
this.worldObj.func_147480_a( this.xCoord, this.yCoord, this.zCoord, true );
this.disableDrops();
2014-09-24 02:26:27 +02:00
}
public void saveChanges()
{
super.markDirty();
}
public boolean requiresTESR()
{
return false;
}
public void setName(String name)
{
this.customName = name;
}
@Override
public String getCustomName()
{
2014-12-29 15:13:47 +01:00
return this.hasCustomName() ? this.customName : this.getClass().getSimpleName();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean hasCustomName()
{
2014-12-29 15:13:47 +01:00
return this.customName != null && this.customName.length() > 0;
2014-09-24 02:26:27 +02:00
}
}