package gregtech.api.metatileentity;

import static gregtech.api.enums.GT_Values.NW;
import gregtech.api.GregTech_API;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.interfaces.tileentity.IPipeRenderedTileEntity;
import gregtech.api.net.GT_Packet_TileEntity;
import gregtech.api.objects.GT_ItemStack;
import gregtech.api.util.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.Packet;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;

/**
 * NEVER INCLUDE THIS FILE IN YOUR MOD!!!
 * 
 * This is the main TileEntity for EVERYTHING.
 */
public class BaseMetaPipeEntity extends BaseTileEntity implements IGregTechTileEntity, IPipeRenderedTileEntity {
	public byte mConnections = 0;
	
	protected MetaPipeEntity mMetaTileEntity;
	
	private byte[] mSidedRedstone = new byte[] {0,0,0,0,0,0};
	private int[] mCoverSides = new int[] {0,0,0,0,0,0}, mCoverData = new int[] {0,0,0,0,0,0}, mTimeStatistics = new int[GregTech_API.TICKS_FOR_LAG_AVERAGING];
	private boolean mInventoryChanged = false, mWorkUpdate = false, mWorks = true, mNeedsUpdate = true, mNeedsBlockUpdate = true, mSendClientData = false;
	private byte mColor = 0, oColor = 0, mStrongRedstone = 0, oRedstoneData = 63, oTextureData = 0, oUpdateData = 0, mLagWarningCount = 0;
	private int oX = 0, oY = 0, oZ = 0, mTimeStatisticsIndex = 0;
	private short mID = 0;
	private long mTickTimer = 0;
	
	public BaseMetaPipeEntity() {}
	
	@Override
    public void writeToNBT(NBTTagCompound aNBT) {
		try {
			super.writeToNBT(aNBT);
    	} catch(Throwable e) {
			GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
			e.printStackTrace(GT_Log.err);
		}
		try {
	        aNBT.setInteger		("mID"				, mID);
	        aNBT.setIntArray	("mCoverData"		, mCoverData);
	        aNBT.setIntArray	("mCoverSides"		, mCoverSides);
	    	aNBT.setByteArray	("mRedstoneSided"	, mSidedRedstone);
	        aNBT.setByte		("mConnections"		, mConnections);
	        aNBT.setByte		("mColor"			, mColor);
	        aNBT.setByte		("mStrongRedstone"	, mStrongRedstone);
	    	aNBT.setBoolean		("mWorks"			, !mWorks);
		} catch(Throwable e) {
			GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
			e.printStackTrace(GT_Log.err);
		}
		try {
	    	if (hasValidMetaTileEntity()) {
	            NBTTagList tItemList = new NBTTagList();
	            for (int i = 0; i < mMetaTileEntity.getRealInventory().length; i++) {
	                ItemStack tStack = mMetaTileEntity.getRealInventory()[i];
	                if (tStack != null) {
	                    NBTTagCompound tTag = new NBTTagCompound();
	                    tTag.setInteger("IntSlot", i);
	                    tStack.writeToNBT(tTag);
	                    tItemList.appendTag(tTag);
	                }
	            }
	            aNBT.setTag("Inventory", tItemList);
	            
	    		try {
	    			mMetaTileEntity.saveNBTData(aNBT);
	    		} catch(Throwable e) {
	    			GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
	    			e.printStackTrace(GT_Log.err);
	    		}
	    	}
    	} catch(Throwable e) {
			GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
			e.printStackTrace(GT_Log.err);
		}
    }
	
	@Override
	public void readFromNBT(NBTTagCompound aNBT) {
		super.readFromNBT(aNBT);
		setInitialValuesAsNBT(aNBT, (short)0);
    }
	
	@Override
	public void setInitialValuesAsNBT(NBTTagCompound aNBT, short aID) {
		if (aNBT == null) {
			if (aID>0) mID=aID; else mID=mID>0?mID:0;
			if (mID!=0) createNewMetatileEntity(mID);
		} else {
	        if (aID<=0) 	mID	= (short)aNBT.getInteger	("mID"); else mID = aID;
	        mCoverSides 		= aNBT.getIntArray	("mCoverSides");
	        mCoverData 			= aNBT.getIntArray	("mCoverData");
	        mSidedRedstone		= aNBT.getByteArray ("mRedstoneSided");
	        mConnections		= aNBT.getByte		("mConnections");
	        mColor				= aNBT.getByte		("mColor");
	        mStrongRedstone		= aNBT.getByte		("mStrongRedstone");
	    	mWorks				=!aNBT.getBoolean	("mWorks");
			
	    	if (mCoverData.length != 6)    	mCoverData	   = new int[]  { 0, 0, 0, 0, 0, 0};
	    	if (mCoverSides.length != 6)    mCoverSides    = new int[]  { 0, 0, 0, 0, 0, 0};
	        if (mSidedRedstone.length != 6) mSidedRedstone = new byte[] { 0, 0, 0, 0, 0, 0};
	        
    		for (byte i = 0; i < 6; i++) mCoverBehaviors[i] = GregTech_API.getCoverBehavior(mCoverSides[i]);
	        
	    	if (mID!=0 && createNewMetatileEntity(mID)) {
	            NBTTagList tItemList = aNBT.getTagList("Inventory", 10);
	            for (int i = 0; i < tItemList.tagCount(); i++) {
	                NBTTagCompound tTag = tItemList.getCompoundTagAt(i);
	                int tSlot = tTag.getInteger("IntSlot");
	                if (tSlot >= 0 && tSlot < mMetaTileEntity.getRealInventory().length) {
	                	mMetaTileEntity.getRealInventory()[tSlot] = GT_Utility.loadItem(tTag);
	                }
	            }
	            
	    		try {
	    			mMetaTileEntity.loadNBTData(aNBT);
	        	} catch(Throwable e) {
	        		GT_Log.err.println("Encountered Exception while loading MetaTileEntity, the Server should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
	        		e.printStackTrace(GT_Log.err);
	        	}
			}
		}
		
    	if (mCoverData.length != 6)    	mCoverData	   = new int[]  { 0, 0, 0, 0, 0, 0};
    	if (mCoverSides.length != 6)    mCoverSides    = new int[]  { 0, 0, 0, 0, 0, 0};
        if (mSidedRedstone.length != 6) mSidedRedstone = new byte[] { 0, 0, 0, 0, 0, 0};
        
    	for (byte i = 0; i < 6; i++) mCoverBehaviors[i] = GregTech_API.getCoverBehavior(mCoverSides[i]);
	}
	
	private boolean createNewMetatileEntity(short aID) {
		if (aID <= 0 || aID >= GregTech_API.METATILEENTITIES.length || GregTech_API.METATILEENTITIES[aID] == null) {
			GT_Log.err.println("MetaID " + aID + " not loadable => locking TileEntity!");
		} else {
			if (aID != 0) {
				if (hasValidMetaTileEntity()) mMetaTileEntity.setBaseMetaTileEntity(null);
				GregTech_API.METATILEENTITIES[aID].newMetaEntity(this).setBaseMetaTileEntity(this);
	    		mTickTimer = 0;
				mID = aID;
				return true;
			}
		}
		return false;
	}
	
	@Override
    public void updateEntity() {
		super.updateEntity();
		
    	if (!hasValidMetaTileEntity()) {
    		if (mMetaTileEntity == null) return;
	    	mMetaTileEntity.setBaseMetaTileEntity(this);
    	}
    	
    	long tTime = System.currentTimeMillis();
    	
    	for (int tCode = 0; hasValidMetaTileEntity() && tCode >= 0;) {try {switch (tCode) {
    	case 0:
    		tCode++;
    	    if (mTickTimer++==0) {
	    		oX = xCoord;
	    		oY = yCoord;
	    		oZ = zCoord;
	    		if (isServerSide()) for (byte i = 0; i < 6; i++) if (getCoverIDAtSide(i)!=0) if (!mMetaTileEntity.allowCoverOnSide(i, new GT_ItemStack(getCoverIDAtSide(i)))) dropCover(i, i, true);
	    		worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this);
	    	    mMetaTileEntity.onFirstTick(this);
	    		if (!hasValidMetaTileEntity()) return;
    	    }
    	case 1:
    		tCode++;
    	    if (isClientSide()) {
    	    	if (mColor != oColor) {
    	    		mMetaTileEntity.onColorChangeClient(oColor = mColor);
    		    	issueTextureUpdate();
    	    	}
    	    	
    	    	if (mNeedsUpdate) {
    			    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    			    //worldObj.func_147479_m(xCoord, yCoord, zCoord);
    			    mNeedsUpdate = false;
    	    	}
    	    }
    	case 2: case 3: case 4: case 5: case 6: case 7:
    	    if (isServerSide() && mTickTimer > 10) {
    	    	for (byte i = (byte)(tCode-2); i < 6; i++) if (getCoverIDAtSide(i) != 0) {
    	    		tCode++;
    	    		GT_CoverBehavior tCover = getCoverBehaviorAtSide(i);
    	    		int tCoverTickRate = tCover.getTickRate(i, getCoverIDAtSide(i), mCoverData[i], this);
    	    		if (tCoverTickRate > 0 && mTickTimer % tCoverTickRate == 0) {
    	    			mCoverData[i] = tCover.doCoverThings(i, getInputRedstoneSignal(i), getCoverIDAtSide(i), mCoverData[i], this, mTickTimer);
    	        		if (!hasValidMetaTileEntity()) return;
    	    		}
    	    	}
    	    	mConnections = (byte)(mMetaTileEntity.mConnections | (mConnections & ~63));
    	    	if ((mConnections & -64) == 64 && getRandomNumber(1000) == 0) {
    	    		mConnections = (byte)((mConnections & ~64) | -128);
    	    	}
    	    }
    	case 8:
    		tCode=9;
	    	mMetaTileEntity.onPreTick(this, mTickTimer);
    		if (!hasValidMetaTileEntity()) return;
    	case 9:
    		tCode++;
    	    if (isServerSide()) {
    	    	if (mTickTimer == 10) {
    	    		for (byte i = 0; i < 6; i++) mCoverBehaviors[i] = GregTech_API.getCoverBehavior(mCoverSides[i]);
    		    	issueBlockUpdate();
    	    	}
    	    	
    	    	if (xCoord != oX || yCoord != oY || zCoord != oZ) {
    	    		oX = xCoord;
    	    		oY = yCoord;
    	    		oZ = zCoord;
    		    	issueClientUpdate();
    		    	clearTileEntityBuffer();
    	    	}
   	        }
    	case 10:
    		tCode++;
	    	mMetaTileEntity.onPostTick(this, mTickTimer);
    		if (!hasValidMetaTileEntity()) return;
    	case 11:
    		tCode++;
        	if (isServerSide()) {
        		if (mTickTimer % 10 == 0) {
        		    if (mSendClientData) {
        		    	NW.sendPacketToAllPlayersInRange(worldObj, new GT_Packet_TileEntity(xCoord, (short)yCoord, zCoord, mID, mCoverSides[0], mCoverSides[1], mCoverSides[2], mCoverSides[3], mCoverSides[4], mCoverSides[5], oTextureData = mConnections, oUpdateData = hasValidMetaTileEntity()?mMetaTileEntity.getUpdateData():0, oRedstoneData = (byte)(((mSidedRedstone[0]>0)?1:0)|((mSidedRedstone[1]>0)?2:0)|((mSidedRedstone[2]>0)?4:0)|((mSidedRedstone[3]>0)?8:0)|((mSidedRedstone[4]>0)?16:0)|((mSidedRedstone[5]>0)?32:0)), oColor = mColor), xCoord, zCoord);
	    		    	mSendClientData = false;
        	    	}
        		}
        		
        		if (mTickTimer > 10) {
	    		    if (mConnections != oTextureData)	sendBlockEvent((byte)0, oTextureData = mConnections);
	    		    byte tData = mMetaTileEntity.getUpdateData();
	    		    if (tData != oUpdateData)			sendBlockEvent((byte)1, oUpdateData = tData);
	    		    if (mColor != oColor)				sendBlockEvent((byte)2, oColor = mColor);
	    		    tData = (byte)(((mSidedRedstone[0]>0)?1:0)|((mSidedRedstone[1]>0)?2:0)|((mSidedRedstone[2]>0)?4:0)|((mSidedRedstone[3]>0)?8:0)|((mSidedRedstone[4]>0)?16:0)|((mSidedRedstone[5]>0)?32:0));
	    		    if (tData != oRedstoneData)			sendBlockEvent((byte)3, oRedstoneData = tData);
        		}
        		
    	    	if (mNeedsBlockUpdate) {
    		    	worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockOffset(0, 0, 0));
    	    		mNeedsBlockUpdate = false;
    	    	}
    	    }
    	default:
    		tCode = -1;
    		break;
    	}} catch (Throwable e) {
    		GT_Log.err.println("Encountered Exception while ticking MetaTileEntity in Step "+(tCode-1)+". The Game should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
	    	e.printStackTrace(GT_Log.err);
    	}}
    	
    	if (isServerSide() && hasValidMetaTileEntity()) {
        	tTime = System.currentTimeMillis() - tTime;
	    	if (mTimeStatistics.length > 0) mTimeStatistics[mTimeStatisticsIndex = (mTimeStatisticsIndex + 1) % mTimeStatistics.length] = (int)tTime;
	    	if (tTime > 0 && tTime > GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING && mTickTimer > 1000 && getMetaTileEntity().doTickProfilingMessageDuringThisTick() && mLagWarningCount++<10) System.out.println("WARNING: Possible Lag Source at [" + xCoord + ", " + yCoord + ", " + zCoord + "] in Dimension " + worldObj.provider.dimensionId + " with " + tTime + "ms caused by an instance of " + getMetaTileEntity().getClass());
    	}
    	
    	mWorkUpdate = mInventoryChanged = false;
    }
	
	@Override
    public Packet getDescriptionPacket() {
		issueClientUpdate();
        return null;
    }
	
	public final void receiveMetaTileEntityData(short aID, int aCover0, int aCover1, int aCover2, int aCover3, int aCover4, int aCover5, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) {
    	issueTextureUpdate();
		if (mID != aID && aID > 0) {
	    	mID = aID;
	    	createNewMetatileEntity(mID);
		}
		
		mCoverSides[0] = aCover0;
		mCoverSides[1] = aCover1;
		mCoverSides[2] = aCover2;
		mCoverSides[3] = aCover3;
		mCoverSides[4] = aCover4;
		mCoverSides[5] = aCover5;
		
		for (byte i = 0; i < 6; i++) mCoverBehaviors[i] = GregTech_API.getCoverBehavior(mCoverSides[i]);
		
		receiveClientEvent(0, aTextureData);
		receiveClientEvent(1, aUpdateData);
		receiveClientEvent(2, aColorData);
		receiveClientEvent(3, aRedstoneData);
	}
	
    @Override
    public boolean receiveClientEvent(int aEventID, int aValue) {
		super.receiveClientEvent(aEventID, aValue);
		
		if (hasValidMetaTileEntity()) {
			try {
				mMetaTileEntity.receiveClientEvent((byte)aEventID, (byte)aValue);
			} catch(Throwable e) {
				GT_Log.err.println("Encountered Exception while receiving Data from the Server, the Client should've been crashed by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
				e.printStackTrace(GT_Log.err);
			}
		}
		
		if (isClientSide()) {
	    	issueTextureUpdate();
			switch(aEventID) {
			case  0:
				mConnections = (byte)aValue;
				break;
			case  1:
				if (hasValidMetaTileEntity()) mMetaTileEntity.onValueUpdate((byte)aValue);
				break;
			case  2:
				if (aValue > 16 || aValue < 0) aValue = 0;
				mColor = (byte)aValue;
				break;
			case  3:
				mSidedRedstone[0] = (byte)((aValue& 1)>0?15:0);
				mSidedRedstone[1] = (byte)((aValue& 2)>0?15:0);
				mSidedRedstone[2] = (byte)((aValue& 4)>0?15:0);
				mSidedRedstone[3] = (byte)((aValue& 8)>0?15:0);
				mSidedRedstone[4] = (byte)((aValue&16)>0?15:0);
				mSidedRedstone[5] = (byte)((aValue&32)>0?15:0);
				break;
			case  4:
		    	if (hasValidMetaTileEntity() && mTickTimer > 20) mMetaTileEntity.doSound((byte)aValue, xCoord+0.5, yCoord+0.5, zCoord+0.5);
		    	break;
			case  5:
				if (hasValidMetaTileEntity() && mTickTimer > 20) mMetaTileEntity.startSoundLoop((byte)aValue, xCoord+0.5, yCoord+0.5, zCoord+0.5);
		    	break;
			case  6:
				if (hasValidMetaTileEntity() && mTickTimer > 20) mMetaTileEntity.stopSoundLoop((byte)aValue, xCoord+0.5, yCoord+0.5, zCoord+0.5);
	    		break;
			}
		}
		return true;
	}
	
	public ArrayList<String> getDebugInfo(EntityPlayer aPlayer, int aLogLevel) {
		ArrayList<String> tList = new ArrayList<String>();
		if (aLogLevel > 2) {
			tList.add("Meta-ID: " + mID + (hasValidMetaTileEntity()?" valid":" invalid") + (mMetaTileEntity==null?" MetaTileEntity == null!":" "));
		}
		if (aLogLevel > 1) {
			if (mTimeStatistics.length > 0) {
				double tAverageTime = 0; for (int tTime : mTimeStatistics) tAverageTime += tTime;
				tList.add("This particular TileEntity has caused an average CPU-load of ~" + (tAverageTime/mTimeStatistics.length) + "ms over the last " + mTimeStatistics.length + " ticks.");
			}
			if (mLagWarningCount > 0) {
				tList.add("This TileEntity has also caused " + (mLagWarningCount>=10?"more than 10":mLagWarningCount) + " Lag Spike Warnings (anything taking longer than " + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + "ms) on the Server.");
			}
			tList.add("Is" + (mMetaTileEntity.isAccessAllowed(aPlayer)?" ":" not ") + "accessible for you");
		}
		return mMetaTileEntity.getSpecialDebugInfo(this, aPlayer, aLogLevel, tList);
	}
	
	@Override public void issueTextureUpdate() {mNeedsUpdate = true;}
	@Override public void issueBlockUpdate() {mNeedsBlockUpdate = true;}
	@Override public void issueClientUpdate() {mSendClientData = true;}
	@Override public void issueCoverUpdate(byte aSide) {issueClientUpdate();}
	
	@Override public byte getStrongestRedstone() {return (byte)Math.max(getInternalInputRedstoneSignal((byte)0), Math.max(getInternalInputRedstoneSignal((byte)1), Math.max(getInternalInputRedstoneSignal((byte)2), Math.max(getInternalInputRedstoneSignal((byte)3), Math.max(getInternalInputRedstoneSignal((byte)4), getInternalInputRedstoneSignal((byte)5))))));}
	
	@Override public boolean getRedstone() {return getRedstone((byte)0)||getRedstone((byte)1)||getRedstone((byte)2)||getRedstone((byte)3)||getRedstone((byte)4)||getRedstone((byte)5);}
	@Override public boolean getRedstone(byte aSide) {return getInternalInputRedstoneSignal(aSide) > 0;}
    
    public ITexture getCoverTexture(byte aSide) {return GregTech_API.sCovers.get(new GT_ItemStack(getCoverIDAtSide(aSide)));}
	
	@Override public boolean isGivingInformation() {if (canAccessData()) return mMetaTileEntity.isGivingInformation(); return false;}
	@Override public boolean isValidFacing(byte aSide) {if (canAccessData()) return mMetaTileEntity.isFacingValid(aSide); return false;}
	@Override public byte getBackFacing() {return GT_Utility.getOppositeSide(getFrontFacing());}
	@Override public byte getFrontFacing() {return 6;}
	@Override public void setFrontFacing(byte aFacing) {/*Do nothing*/}
	@Override public int getSizeInventory() {if (canAccessData()) return mMetaTileEntity.getSizeInventory(); return 0;}
	@Override public ItemStack getStackInSlot(int aIndex) {if (canAccessData()) return mMetaTileEntity.getStackInSlot(aIndex); return null;}
	@Override public void setInventorySlotContents(int aIndex, ItemStack aStack) {mInventoryChanged = true; if (canAccessData()) mMetaTileEntity.setInventorySlotContents(aIndex, worldObj.isRemote?aStack:GT_OreDictUnificator.setStack(true, aStack));}
	@Override public String getInventoryName() {if (canAccessData()) return mMetaTileEntity.getInventoryName(); if (GregTech_API.METATILEENTITIES[mID] != null) return GregTech_API.METATILEENTITIES[mID].getInventoryName(); return "";}
	@Override public int getInventoryStackLimit() {if (canAccessData()) return mMetaTileEntity.getInventoryStackLimit(); return 64;}
	@Override public void openInventory()  {/*Do nothing*/}
	@Override public void closeInventory() {/*Do nothing*/}
	@Override public boolean isUseableByPlayer(EntityPlayer aPlayer) {return hasValidMetaTileEntity() && mTickTimer>40 && getTileEntityOffset(0, 0, 0) == this && aPlayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64 && mMetaTileEntity.isAccessAllowed(aPlayer);}
	@Override public void validate() {super.validate(); mTickTimer = 0;}
    @Override public void invalidate() {tileEntityInvalid = false; if (hasValidMetaTileEntity()) {mMetaTileEntity.onRemoval(); mMetaTileEntity.setBaseMetaTileEntity(null);} super.invalidate();}
    @Override public void onChunkUnload() {super.onChunkUnload();}
    @Override public boolean hasCustomInventoryName() {return false;}
    @Override public ItemStack getStackInSlotOnClosing(int slot) {ItemStack stack = getStackInSlot(slot); if (stack != null) setInventorySlotContents(slot, null); return stack;}
    @Override public void onMachineBlockUpdate() {if (canAccessData()) mMetaTileEntity.onMachineBlockUpdate();}
    @Override public int getProgress() {return canAccessData()?mMetaTileEntity.getProgresstime():0;}
	@Override public int getMaxProgress() {return canAccessData()?mMetaTileEntity.maxProgresstime():0;}
	@Override public boolean increaseProgress(int aProgressAmountInTicks) {return canAccessData()?mMetaTileEntity.increaseProgress(aProgressAmountInTicks)!=aProgressAmountInTicks:false;}
	@Override public boolean hasThingsToDo() {return getMaxProgress()>0;}
	@Override public void enableWorking() {if (!mWorks) mWorkUpdate = true; mWorks = true;}
	@Override public void disableWorking() {mWorks = false;}
	@Override public boolean isAllowedToWork() {return mWorks;}
	@Override public boolean hasWorkJustBeenEnabled() {return mWorkUpdate;}
	@Override public void setWorkDataValue(byte aValue) {/*Do nothing*/}
	@Override public byte getWorkDataValue() {return 0;}
    @Override public int getMetaTileID() {return mID;}
    @Override public int setMetaTileID(short aID) {return mID = aID;}
	@Override public boolean isActive() {return false;}
    @Override public void setActive(boolean aActive) {/*Do nothing*/}
	@Override public long getTimer() {return mTickTimer;}
	@Override public boolean decreaseStoredEnergyUnits(long aEnergy, boolean aIgnoreTooLessEnergy) {return false;}
	@Override public boolean increaseStoredEnergyUnits(long aEnergy, boolean aIgnoreTooMuchEnergy) {return false;}
	@Override public boolean inputEnergyFrom(byte aSide) {return false;}
	@Override public boolean outputsEnergyTo(byte aSide) {return false;}
	@Override public long getOutputAmperage() {return 0;}
	@Override public long getOutputVoltage() {return 0;}
	@Override public long getInputAmperage() {return 0;}
	@Override public long getInputVoltage() {return 0;}
	@Override public boolean increaseStoredSteam(long aEnergy, boolean aIgnoreTooMuchEnergy) {return false;}
	@Override public String[] getDescription() {if (canAccessData()) return mMetaTileEntity.getDescription(); return new String[0];}
    @Override public boolean isValidSlot(int aIndex) {if (canAccessData()) return mMetaTileEntity.isValidSlot(aIndex); return false;}
    @Override public long getUniversalEnergyStored() {return Math.max(getStoredEU(), getStoredSteam());}
	@Override public long getUniversalEnergyCapacity() {return Math.max(getEUCapacity(), getSteamCapacity());}
    @Override public long getStoredEU() {return 0;}
    @Override public long getEUCapacity() {return 0;}
    @Override public long getStoredSteam() {return 0;}
    @Override public long getSteamCapacity() {return 0;}
    @Override public ITexture[] getTexture(byte aSide) {ITexture rIcon = getCoverTexture(aSide); if (rIcon != null) return new ITexture[] {rIcon}; return getTextureUncovered(aSide);}
    @Override public ITexture[] getTextureUncovered(byte aSide) {if ((mConnections & 64) != 0) return Textures.BlockIcons.FRESHFOAM; if ((mConnections & -128) != 0) return Textures.BlockIcons.HARDENEDFOAMS[mColor]; if ((mConnections & -64) != 0) return Textures.BlockIcons.ERROR_RENDERING; byte tConnections = mConnections; if (tConnections ==  1 || tConnections ==  2) tConnections = 3; else if (tConnections ==  4 || tConnections ==  8) tConnections = 12; else if (tConnections == 16 || tConnections == 32) tConnections = 48; if (hasValidMetaTileEntity()) return mMetaTileEntity.getTexture(this, aSide, tConnections, (byte)(mColor-1), tConnections == 0 || (tConnections & (1<<aSide)) != 0, getOutputRedstoneSignal(aSide)>0); return Textures.BlockIcons.ERROR_RENDERING;}
    
    protected boolean hasValidMetaTileEntity()	{return mMetaTileEntity != null && mMetaTileEntity.getBaseMetaTileEntity() == this;}
    protected boolean canAccessData()			{return !isDead && hasValidMetaTileEntity();}
    
    @Override
    public void doExplosion(long aAmount) {
    	if (canAccessData()) {
    		mMetaTileEntity.onExplosion();
    		mMetaTileEntity.doExplosion(aAmount);
    	}
    }
	
	@Override
	public ArrayList<ItemStack> getDrops() {
		ItemStack rStack = new ItemStack(GregTech_API.sBlockMachines, 1, mID);
		NBTTagCompound tNBT = new NBTTagCompound();
    	if (mStrongRedstone		> 0) tNBT.setByte	("mStrongRedstone"	, mStrongRedstone);
        for (byte i = 0; i < mCoverSides.length; i++) {
        	if (mCoverSides[i] != 0) {
        		tNBT.setIntArray("mCoverData"	, mCoverData);
        		tNBT.setIntArray("mCoverSides"	, mCoverSides);
        		break;
        	}
        }
		if (hasValidMetaTileEntity()) mMetaTileEntity.setItemNBT(tNBT);
		if (!tNBT.hasNoTags()) rStack.setTagCompound(tNBT);
		return new ArrayList<ItemStack>(Arrays.asList(rStack));
	}
	
	@Override
	public boolean onRightclick(EntityPlayer aPlayer, byte aSide, float aX, float aY, float aZ) {
		if (isClientSide()) {
			if (getCoverBehaviorAtSide(aSide).onCoverRightclickClient(aSide, this, aPlayer, aX, aY, aZ)) return true;
		}
		if (isServerSide()) {
			ItemStack tCurrentItem = aPlayer.inventory.getCurrentItem();
			if (tCurrentItem != null) {
				if (getColorization() >= 0 && GT_Utility.areStacksEqual(new ItemStack(Items.water_bucket, 1), tCurrentItem)) {
					tCurrentItem.func_150996_a(Items.bucket);
					setColorization((byte)-1);
					return true;
				}
				byte tSide = GT_Utility.determineWrenchingSide(aSide, aX, aY, aZ);
				if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sWrenchList)) {
					if (mMetaTileEntity.onWrenchRightClick(aSide, tSide, aPlayer, aX, aY, aZ)) {
						GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer);
						GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(100), 1.0F, -1, xCoord, yCoord, zCoord);
					}
					return true;
				}
				if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sScrewdriverList)) {
					if (getCoverIDAtSide(aSide) == 0 && getCoverIDAtSide(tSide) != 0) {
						if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 200, aPlayer)) {
							setCoverDataAtSide(tSide, getCoverBehaviorAtSide(tSide).onCoverScrewdriverclick(tSide, getCoverIDAtSide(tSide), getCoverDataAtSide(tSide), this, aPlayer, 0.5F, 0.5F, 0.5F));
							mMetaTileEntity.onScrewdriverRightClick(tSide, aPlayer, aX, aY, aZ);
							GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(100), 1.0F, -1, xCoord, yCoord, zCoord);
						}
					} else {
						if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) {
							setCoverDataAtSide(aSide, getCoverBehaviorAtSide(aSide).onCoverScrewdriverclick(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this, aPlayer, aX, aY, aZ));
							mMetaTileEntity.onScrewdriverRightClick(aSide, aPlayer, aX, aY, aZ);
							GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(100), 1.0F, -1, xCoord, yCoord, zCoord);
						}
					}
					return true;
				}
				
				if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sHardHammerList)) {
					//if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) {
					//	GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(1), 1.0F, -1, xCoord, yCoord, zCoord);
					//}
					return true;
				}
				
				if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sSoftHammerList)) {
					if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) {
						if (mWorks) disableWorking(); else enableWorking();
						GT_Utility.sendChatToPlayer(aPlayer, "Machine Processing: " + (isAllowedToWork()?"Enabled":"Disabled"));
						GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(101), 1.0F, -1, xCoord, yCoord, zCoord);
					}
					return true;
				}
				
				if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sSolderingToolList)) {
					if (GT_ModHandler.useSolderingIron(tCurrentItem, aPlayer)) {
						mStrongRedstone ^= (1 << tSide);
						GT_Utility.sendChatToPlayer(aPlayer, "Redstone Output at Side " + tSide + " set to: " + ((mStrongRedstone & (1 << tSide))!=0?"Strong":"Weak"));
						GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(103), 3.0F, -1, xCoord, yCoord, zCoord);
					}
					return true;
				}
				
				byte cSide = tSide;
				if (getCoverIDAtSide(aSide) != 0) cSide = aSide;
				
				if (getCoverIDAtSide(cSide) == 0) {
					if (GregTech_API.sCovers.containsKey(new GT_ItemStack(tCurrentItem))) {
						if (GregTech_API.getCoverBehavior(tCurrentItem).isCoverPlaceable(cSide, new GT_ItemStack(tCurrentItem), this) && mMetaTileEntity.allowCoverOnSide(cSide, new GT_ItemStack(tCurrentItem))) {
							setCoverItemAtSide(cSide, tCurrentItem);
							if (!aPlayer.capabilities.isCreativeMode) tCurrentItem.stackSize--;
							GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(100), 1.0F, -1, xCoord, yCoord, zCoord);
						}
						return true;
					}
				} else {
					if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sCrowbarList)) {
						if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) {
							GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(0), 1.0F, -1, xCoord, yCoord, zCoord);
							dropCover(cSide, aSide, false);
						}
						return true;
					}
				}
			}
			
			if (getCoverBehaviorAtSide(aSide).onCoverRightclick(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this, aPlayer, aX, aY, aZ)) return true;
		}
		
		if (!getCoverBehaviorAtSide(aSide).isGUIClickable(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this)) return false;
		
		try {
			if (hasValidMetaTileEntity()) return mMetaTileEntity.onRightclick(this, aPlayer, aSide, aX, aY, aZ);
    	} catch(Throwable e) {
    		GT_Log.err.println("Encountered Exception while rightclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
    		e.printStackTrace(GT_Log.err);
    	}
		
		return false;
	}
	
	@Override
	public void onLeftclick(EntityPlayer aPlayer) {
		try {
			if (aPlayer != null && hasValidMetaTileEntity()) mMetaTileEntity.onLeftclick(this, aPlayer);
    	} catch(Throwable e) {
    		GT_Log.err.println("Encountered Exception while leftclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!");
    		e.printStackTrace(GT_Log.err);
    	}
	}
	
	@Override
	public boolean isDigitalChest() {
		return false;
	}
	
	@Override
	public ItemStack[] getStoredItemData() {
		return null;
	}
	
	@Override
	public void setItemCount(int aCount) {
		//
	}
	
	@Override
	public int getMaxItemCount() {
		return 0;
	}
	
	/**
	 * Can put aStack into Slot
	 */
	@Override
	public boolean isItemValidForSlot(int aIndex, ItemStack aStack) {
		return canAccessData() && mMetaTileEntity.isItemValidForSlot(aIndex, aStack);
	}
	
	/**
	 * returns all valid Inventory Slots, no matter which Side (Unless it's covered).
	 * The Side Stuff is done in the following two Functions.
	 */
	@Override
	public int[] getAccessibleSlotsFromSide(int aSide) {
		if (canAccessData() && (getCoverBehaviorAtSide((byte)aSide).letsItemsOut((byte)aSide, getCoverIDAtSide((byte)aSide), getCoverDataAtSide((byte)aSide), -1, this) || getCoverBehaviorAtSide((byte)aSide).letsItemsIn((byte)aSide, getCoverIDAtSide((byte)aSide), getCoverDataAtSide((byte)aSide), -1, this))) return mMetaTileEntity.getAccessibleSlotsFromSide(aSide);
		return new int[0];
	}
	
	/**
	 * Can put aStack into Slot at Side
	 */
	@Override
	public boolean canInsertItem(int aIndex, ItemStack aStack, int aSide) {
		return canAccessData() && getCoverBehaviorAtSide((byte)aSide).letsItemsIn ((byte)aSide, getCoverIDAtSide((byte)aSide), getCoverDataAtSide((byte)aSide), aIndex, this) && mMetaTileEntity.canInsertItem(aIndex, aStack, aSide);
	}
	
	/**
	 * Can pull aStack out of Slot from Side
	 */
	@Override
	public boolean canExtractItem(int aIndex, ItemStack aStack, int aSide) {
		return canAccessData() && getCoverBehaviorAtSide((byte)aSide).letsItemsOut((byte)aSide, getCoverIDAtSide((byte)aSide), getCoverDataAtSide((byte)aSide), aIndex, this) && mMetaTileEntity.canExtractItem(aIndex, aStack, aSide);
	}
	
	@Override
	public boolean isUpgradable() {
		return false;
	}
	
	@Override
	public byte getInternalInputRedstoneSignal(byte aSide) {
		return (byte)(getCoverBehaviorAtSide(aSide).getRedstoneInput(aSide, getInputRedstoneSignal(aSide), getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this)&15);
	}
	
	@Override
	public byte getInputRedstoneSignal(byte aSide) {
		return (byte)(worldObj.getIndirectPowerLevelTo(getOffsetX(aSide, 1), getOffsetY(aSide, 1), getOffsetZ(aSide, 1), aSide)&15);
	}
	
	@Override
	public byte getOutputRedstoneSignal(byte aSide) {
		return (byte)(getCoverBehaviorAtSide(aSide).manipulatesSidedRedstoneOutput(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this) || (getCoverBehaviorAtSide(aSide).letsRedstoneGoOut(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this))?mSidedRedstone[aSide]&15:0);
	}
	
	@Override
	public void setInternalOutputRedstoneSignal(byte aSide, byte aStrength) {
		if (!getCoverBehaviorAtSide(aSide).manipulatesSidedRedstoneOutput(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this)) setOutputRedstoneSignal(aSide, aStrength);
	}
	
	@Override
	public void setOutputRedstoneSignal(byte aSide, byte aStrength) {
		aStrength = (byte)Math.min(Math.max(0, aStrength), 15);
		if (aSide >= 0 && aSide < 6 && mSidedRedstone[aSide] != aStrength) {
			mSidedRedstone[aSide] = aStrength;
    		issueBlockUpdate();
		}
	}
	
	@Override
	public boolean isSteamEngineUpgradable() {
		return isUpgradable()&&!hasSteamEngineUpgrade()&&getSteamCapacity()>0;
	}
	
	@Override
	public boolean addSteamEngineUpgrade() {
		if (isSteamEngineUpgradable()) {
			issueBlockUpdate();
			return true;
		}
		return false;
	}
	
	@Override
	public boolean hasSteamEngineUpgrade() {
		return false;
	}
	
	@Override
	public boolean hasInventoryBeenModified() {
		return mInventoryChanged;
	}
	
	@Override
	public void setGenericRedstoneOutput(boolean aOnOff) {
		//
	}
	
	@Override
	public int getErrorDisplayID() {
		return 0;
	}
	
	@Override
	public void setErrorDisplayID(int aErrorID) {
		//
	}
	
	@Override
	public IMetaTileEntity getMetaTileEntity() {
		return hasValidMetaTileEntity()?mMetaTileEntity:null;
	}
	
	private final GT_CoverBehavior[] mCoverBehaviors = new GT_CoverBehavior[] {GregTech_API.sNoBehavior, GregTech_API.sNoBehavior, GregTech_API.sNoBehavior, GregTech_API.sNoBehavior, GregTech_API.sNoBehavior, GregTech_API.sNoBehavior};
	
	@Override
	public GT_CoverBehavior getCoverBehaviorAtSide(byte aSide) {
		return aSide >= 0 && aSide < mCoverBehaviors.length ? mCoverBehaviors[aSide] : GregTech_API.sNoBehavior;
	}
	
	@Override
	public void setCoverIDAtSide(byte aSide, int aID) {
		if (aSide >= 0 && aSide < 6) {
			mCoverSides[aSide] = aID;
			mCoverData[aSide] = 0;
			mCoverBehaviors[aSide] = GregTech_API.getCoverBehavior(aID);
			issueCoverUpdate(aSide);
			issueBlockUpdate();
		}
	}
	
	@Override
	public void setCoverItemAtSide(byte aSide, ItemStack aCover) {
		GregTech_API.getCoverBehavior(aCover).placeCover(aSide, aCover, this);
	}
	
	@Override
	public int getCoverIDAtSide(byte aSide) {
		if (aSide >= 0 && aSide < 6) return mCoverSides[aSide]; return 0;
	}
	
	@Override
	public ItemStack getCoverItemAtSide(byte aSide) {
		return GT_Utility.intToStack(getCoverIDAtSide(aSide));
	}
	
	@Override
	public boolean canPlaceCoverIDAtSide(byte aSide, int aID) {
		return getCoverIDAtSide(aSide) == 0;
	}
	
	@Override
	public boolean canPlaceCoverItemAtSide(byte aSide, ItemStack aCover) {
		return getCoverIDAtSide(aSide) == 0;
	}
	
	@Override
	public void setCoverDataAtSide(byte aSide, int aData) {
		if (aSide >= 0 && aSide < 6) mCoverData[aSide] = aData;
	}
	
	@Override
	public int getCoverDataAtSide(byte aSide) {
		if (aSide >= 0 && aSide < 6) return mCoverData[aSide];
		return 0;
	}
	
	@Override
	public void setLightValue(byte aLightValue) {
		//
	}
	
	@Override
	public long getAverageElectricInput() {
		return 0;
	}
	
	@Override
	public long getAverageElectricOutput() {
		return 0;
	}

	@Override
	public boolean dropCover(byte aSide, byte aDroppedSide, boolean aForced) {
		if (getCoverBehaviorAtSide(aSide).onCoverRemoval(aSide, getCoverIDAtSide(aSide), mCoverData[aSide], this, aForced) || aForced) {
			ItemStack tStack = getCoverBehaviorAtSide(aSide).getDrop(aSide, getCoverIDAtSide(aSide), getCoverDataAtSide(aSide), this);
			if (tStack != null) {
				tStack.setTagCompound(null);
				EntityItem tEntity = new EntityItem(worldObj, getOffsetX(aDroppedSide, 1) + 0.5, getOffsetY(aDroppedSide, 1) + 0.5, getOffsetZ(aDroppedSide, 1) + 0.5, tStack);
				tEntity.motionX = 0;
				tEntity.motionY = 0;
				tEntity.motionZ = 0;
				worldObj.spawnEntityInWorld(tEntity);
			}
			
			setCoverIDAtSide(aSide, 0);
			setOutputRedstoneSignal(aSide, (byte) 0);
			return true;
		}
		return false;
	}
	
	@Override
	public String getOwnerName() {
		return "Player";
	}
	
	@Override
	public String setOwnerName(String aName) {
		return "Player";
	}
	
	@Override
	public byte getComparatorValue(byte aSide) {
		return canAccessData()?mMetaTileEntity.getComparatorValue(aSide):0;
	}
	
	@Override
	public byte getStrongOutputRedstoneSignal(byte aSide) {
		return aSide>=0&&aSide<6&&(mStrongRedstone & (1 << aSide))!=0?(byte)(mSidedRedstone[aSide]&15):0;
	}
	
	@Override
	public void setStrongOutputRedstoneSignal(byte aSide, byte aStrength) {
		mStrongRedstone |= (1 << aSide);
		setOutputRedstoneSignal(aSide, aStrength);
	}
	
	@Override
	public ItemStack decrStackSize(int aIndex, int aAmount) {
		if (canAccessData()) {
			mInventoryChanged = true;
			return mMetaTileEntity.decrStackSize(aIndex, aAmount);
		}
		return null;
	}
	
	@Override
	public long injectEnergyUnits(byte aSide, long aVoltage, long aAmperage) {
		if (canAccessData()) return mMetaTileEntity.injectEnergyUnits(aSide, aVoltage, aAmperage);
		return 0;
	}
	
	@Override
	public boolean drainEnergyUnits(byte aSide, long aVoltage, long aAmperage) {
		return false;
	}
	
	@Override
	public boolean acceptsRotationalEnergy(byte aSide) {
		if (!canAccessData() || getCoverIDAtSide(aSide) != 0) return false;
		return mMetaTileEntity.acceptsRotationalEnergy(aSide);
	}
	
	@Override
	public boolean injectRotationalEnergy(byte aSide, long aSpeed, long aEnergy) {
		if (!canAccessData() || getCoverIDAtSide(aSide) != 0) return false;
		return mMetaTileEntity.injectRotationalEnergy(aSide, aSpeed, aEnergy);
	}
	
    @Override
    public int fill(ForgeDirection aSide, FluidStack aFluid, boolean doFill) {
    	if (mTickTimer > 5 && canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidInput ((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidIn ((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), aFluid==null?null:aFluid.getFluid(), this)))) return mMetaTileEntity.fill(aSide, aFluid, doFill);
		return 0;
    }
    
	@Override
	public FluidStack drain(ForgeDirection aSide, int maxDrain, boolean doDrain) {
		if (mTickTimer > 5 && canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidOutput((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidOut((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), mMetaTileEntity.getFluid()==null?null:mMetaTileEntity.getFluid().getFluid(), this)))) return mMetaTileEntity.drain(aSide, maxDrain, doDrain);
		return null;
	}
	
	@Override
	public FluidStack drain(ForgeDirection aSide, FluidStack aFluid, boolean doDrain) {
		if (mTickTimer > 5 && canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidOutput((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidOut((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), aFluid==null?null:aFluid.getFluid(), this)))) return mMetaTileEntity.drain(aSide, aFluid, doDrain);
		return null;
	}
	
	@Override
	public boolean canFill(ForgeDirection aSide, Fluid aFluid) {
		if (mTickTimer > 5 && canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidInput ((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidIn ((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), aFluid, this)))) return mMetaTileEntity.canFill(aSide, aFluid);
		return false;
	}
	
	@Override
	public boolean canDrain(ForgeDirection aSide, Fluid aFluid) {
		if (mTickTimer > 5 && canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidOutput((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidOut((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), aFluid, this)))) return mMetaTileEntity.canDrain(aSide, aFluid);
		return false;
	}
	
	@Override
	public FluidTankInfo[] getTankInfo(ForgeDirection aSide) {
		if (canAccessData() && (aSide == ForgeDirection.UNKNOWN || (mMetaTileEntity.isLiquidInput((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidIn ((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), null, this)) || (mMetaTileEntity.isLiquidOutput((byte)aSide.ordinal()) && getCoverBehaviorAtSide((byte)aSide.ordinal()).letsFluidOut((byte)aSide.ordinal(), getCoverIDAtSide((byte)aSide.ordinal()), getCoverDataAtSide((byte)aSide.ordinal()), null, this)))) return mMetaTileEntity.getTankInfo(aSide);
		return new FluidTankInfo[] {};
	}
	
	@Override
	public boolean isInvalidTileEntity() {
		return isInvalid();
	}
	
	@Override
	public boolean addStackToSlot(int aIndex, ItemStack aStack) {
		if (GT_Utility.isStackInvalid(aStack)) return true;
		if (aIndex < 0 || aIndex >= getSizeInventory()) return false;
		ItemStack tStack = getStackInSlot(aIndex);
		if (GT_Utility.isStackInvalid(tStack)) {
			setInventorySlotContents(aIndex, aStack);
			return true;
		}
		aStack = GT_OreDictUnificator.get(aStack);
		if (GT_Utility.areStacksEqual(tStack, aStack) && tStack.stackSize + aStack.stackSize <= Math.min(aStack.getMaxStackSize(), getInventoryStackLimit())) {
			tStack.stackSize+=aStack.stackSize;
			return true;
		}
		return false;
	}
	
	@Override
	public boolean addStackToSlot(int aIndex, ItemStack aStack, int aAmount) {
		return addStackToSlot(aIndex, GT_Utility.copyAmount(aAmount, aStack));
	}
	
	@Override
	public void setMetaTileEntity(IMetaTileEntity aMetaTileEntity) {
		mMetaTileEntity = (MetaPipeEntity)aMetaTileEntity;
	}
	
	@Override
	public byte getColorization() {
		return (byte)(mColor-1);
	}
	
	@Override
	public byte setColorization(byte aColor) {
		if (aColor > 15 || aColor < -1) aColor = -1;
		if (canAccessData()) mMetaTileEntity.onColorChangeServer(aColor);
		return mColor = (byte)(aColor+1);
	}
	
	@Override
	public float getThickNess() {
		if (canAccessData()) return mMetaTileEntity.getThickNess();
		return 1.0F;
	}
	
	public boolean renderInside(byte aSide) {
		if (canAccessData()) return mMetaTileEntity.renderInside(aSide);
		return false;
	}
	
	@Override
	public float getBlastResistance(byte aSide) {
		return (mConnections & 192) != 0 ? 50.0F : 5.0F;
	}

	@Override
	public boolean isMufflerUpgradable() {
		return false;
	}

	@Override
	public boolean addMufflerUpgrade() {
		return false;
	}

	@Override
	public boolean hasMufflerUpgrade() {
		return false;
	}
	
	@Override
	public boolean isUniversalEnergyStored(long aEnergyAmount) {
		return getUniversalEnergyStored() >= aEnergyAmount;
	}
	
	@Override
	public String[] getInfoData() {
		{if (canAccessData()) return getMetaTileEntity().getInfoData(); return new String[]{};}
	}

	@Override
	public byte getConnections() {
		return mConnections;
	}
	
	@Override
    public void markDirty() {
    	super.markDirty();
    	mInventoryChanged = true;
    }
	
	@Override
	public int getLightOpacity() {
		return mMetaTileEntity == null ? 0 : mMetaTileEntity.getLightOpacity();
	}
	
	@Override
	public void addCollisionBoxesToList(World aWorld, int aX, int aY, int aZ, AxisAlignedBB inputAABB, List<AxisAlignedBB> outputAABB, Entity collider) {
		mMetaTileEntity.addCollisionBoxesToList(aWorld, aX, aY, aZ, inputAABB, outputAABB, collider);
	}
	
	@Override
	public AxisAlignedBB getCollisionBoundingBoxFromPool(World aWorld, int aX, int aY, int aZ) {
		return mMetaTileEntity.getCollisionBoundingBoxFromPool(aWorld, aX, aY, aZ);
	}
	
	@Override
	public void onEntityCollidedWithBlock(World aWorld, int aX, int aY, int aZ, Entity collider) {
		mMetaTileEntity.onEntityCollidedWithBlock(aWorld, aX, aY, aZ, collider);
	}
}