2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile.storage;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.io.IOException;
|
2014-02-03 03:30:52 +01:00
|
|
|
import java.util.ArrayList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
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.minecraftforge.common.util.ForgeDirection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.tiles.IChestOrDrive;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.events.MENetworkCellArrayUpdate;
|
|
|
|
import appeng.api.networking.events.MENetworkChannelsChanged;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.networking.security.MachineSource;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.storage.ICellHandler;
|
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.AECableType;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
2014-01-26 07:46:16 +01:00
|
|
|
import appeng.helpers.IPriorityHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.GridAccessException;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.storage.DriveWatcher;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.storage.MEInventoryHandler;
|
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
|
|
|
import appeng.tile.grid.AENetworkInvTile;
|
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2014-01-26 07:46:16 +01:00
|
|
|
public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPriorityHost
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final int sides[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
AppEngInternalInventory inv = new AppEngInternalInventory( this, 10 );
|
|
|
|
|
|
|
|
boolean isCached = false;
|
|
|
|
ICellHandler handlersBySlot[] = new ICellHandler[10];
|
2014-01-20 17:41:37 +01:00
|
|
|
DriveWatcher<IAEItemStack> invBySlot[] = new DriveWatcher[10];
|
2013-12-27 23:59:59 +01:00
|
|
|
List<MEInventoryHandler> items = new LinkedList();
|
|
|
|
List<MEInventoryHandler> fluids = new LinkedList();
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
BaseActionSource mySrc;
|
2013-12-27 23:59:59 +01:00
|
|
|
long lastStateChange = 0;
|
|
|
|
int state = 0;
|
|
|
|
int priority = 0;
|
2014-02-03 03:30:52 +01:00
|
|
|
boolean wasActive = false;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
private void recalculateDisplay()
|
|
|
|
{
|
|
|
|
int oldState = 0;
|
|
|
|
|
2014-02-03 03:30:52 +01:00
|
|
|
boolean currentActive;
|
|
|
|
if ( currentActive = gridProxy.isActive() )
|
2013-12-27 23:59:59 +01:00
|
|
|
state |= 0x80000000;
|
|
|
|
else
|
|
|
|
state &= ~0x80000000;
|
|
|
|
|
2014-02-03 03:30:52 +01:00
|
|
|
if ( wasActive != currentActive )
|
|
|
|
{
|
|
|
|
wasActive = currentActive;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
gridProxy.getGrid().postEvent( new MENetworkCellArrayUpdate() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
for (int x = 0; x < getCellCount(); x++)
|
|
|
|
state |= (getCellStatus( x ) << (3 * x));
|
|
|
|
|
|
|
|
if ( oldState != state )
|
|
|
|
markForUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class invManger extends AETileEventHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
public invManger() {
|
2014-01-26 07:46:16 +01:00
|
|
|
super( TileEventType.WORLD_NBT, TileEventType.NETWORK );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void writeToStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( worldObj.getTotalWorldTime() - lastStateChange > 8 )
|
|
|
|
state = 0;
|
|
|
|
else
|
|
|
|
state &= 0x24924924; // just keep the blinks...
|
|
|
|
|
|
|
|
if ( gridProxy.isActive() )
|
|
|
|
state |= 0x80000000;
|
|
|
|
else
|
|
|
|
state &= ~0x80000000;
|
|
|
|
|
|
|
|
for (int x = 0; x < getCellCount(); x++)
|
|
|
|
state |= (getCellStatus( x ) << (3 * x));
|
|
|
|
|
|
|
|
data.writeInt( state );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean readFromStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
int oldState = state;
|
|
|
|
state = data.readInt();
|
|
|
|
lastStateChange = worldObj.getTotalWorldTime();
|
|
|
|
return (state & 0xDB6DB6DB) != (oldState & 0xDB6DB6DB);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
priority = data.getInteger( "priority" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
data.setInteger( "priority", priority );
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void powerRender(MENetworkPowerStatusChange c)
|
|
|
|
{
|
|
|
|
recalculateDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void channelRender(MENetworkChannelsChanged c)
|
|
|
|
{
|
|
|
|
recalculateDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
public TileDrive() {
|
2014-01-05 09:43:49 +01:00
|
|
|
mySrc = new MachineSource( this );
|
2014-01-31 22:12:15 +01:00
|
|
|
gridProxy.setFlags( GridFlags.REQUIRE_CHANNEL );
|
2013-12-27 23:59:59 +01:00
|
|
|
addNewHandler( new invManger() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AECableType getCableConnectionType(ForgeDirection dir)
|
|
|
|
{
|
|
|
|
return AECableType.SMART;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getLocation()
|
|
|
|
{
|
|
|
|
return new DimensionalCoord( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInternalInventory()
|
|
|
|
{
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
|
|
|
|
{
|
|
|
|
isCached = false; // recalculate the storage cell.
|
|
|
|
updateState();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
IStorageGrid gs = gridProxy.getStorage();
|
2014-01-05 09:43:49 +01:00
|
|
|
Platform.postChanges( gs, removed, added, mySrc );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
gridProxy.getGrid().postEvent( new MENetworkCellArrayUpdate() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
markForUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getAccessibleSlotsFromSide(int side)
|
|
|
|
{
|
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateState()
|
|
|
|
{
|
|
|
|
if ( !isCached )
|
|
|
|
{
|
|
|
|
items = new LinkedList();
|
|
|
|
fluids = new LinkedList();
|
|
|
|
|
2014-01-24 17:32:31 +01:00
|
|
|
double power = 2.0;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
for (int x = 0; x < inv.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack is = inv.getStackInSlot( x );
|
|
|
|
invBySlot[x] = null;
|
|
|
|
handlersBySlot[x] = null;
|
|
|
|
|
|
|
|
if ( is != null )
|
|
|
|
{
|
|
|
|
handlersBySlot[x] = AEApi.instance().registries().cell().getHander( is );
|
|
|
|
|
|
|
|
if ( handlersBySlot[x] != null )
|
|
|
|
{
|
|
|
|
IMEInventoryHandler cell = handlersBySlot[x].getCellInventory( is, StorageChannel.ITEMS );
|
|
|
|
|
|
|
|
if ( cell != null )
|
|
|
|
{
|
2014-01-24 17:32:31 +01:00
|
|
|
power += handlersBySlot[x].cellIdleDrain( is, cell );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, handlersBySlot[x], this );
|
2013-12-27 23:59:59 +01:00
|
|
|
ih.myPriority = priority;
|
|
|
|
invBySlot[x] = ih;
|
|
|
|
items.add( ih );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell = handlersBySlot[x].getCellInventory( is, StorageChannel.FLUIDS );
|
|
|
|
|
|
|
|
if ( cell != null )
|
|
|
|
{
|
2014-01-24 17:32:31 +01:00
|
|
|
power += handlersBySlot[x].cellIdleDrain( is, cell );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, handlersBySlot[x], this );
|
2013-12-27 23:59:59 +01:00
|
|
|
ih.myPriority = priority;
|
|
|
|
invBySlot[x] = ih;
|
|
|
|
fluids.add( ih );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:32:31 +01:00
|
|
|
gridProxy.setIdlePowerUsage( power );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
isCached = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<IMEInventoryHandler> getCellArray(StorageChannel channel)
|
|
|
|
{
|
2014-02-03 03:30:52 +01:00
|
|
|
if ( gridProxy.isActive() )
|
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
return (List) (channel == StorageChannel.ITEMS ? items : fluids);
|
|
|
|
}
|
|
|
|
return new ArrayList();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPriority()
|
|
|
|
{
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCellCount()
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void blinkCell(int slot)
|
|
|
|
{
|
|
|
|
long now = worldObj.getTotalWorldTime();
|
|
|
|
if ( now - lastStateChange > 8 )
|
|
|
|
state = 0;
|
|
|
|
lastStateChange = now;
|
|
|
|
|
|
|
|
state |= 1 << (slot * 3 + 2);
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
recalculateDisplay();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isCellBlinking(int slot)
|
|
|
|
{
|
|
|
|
long now = worldObj.getTotalWorldTime();
|
|
|
|
if ( now - lastStateChange > 8 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ((state >> (slot * 3 + 2)) & 0x01) == 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCellStatus(int slot)
|
|
|
|
{
|
|
|
|
if ( Platform.isClient() )
|
|
|
|
return (state >> (slot * 3)) & 3;
|
|
|
|
|
|
|
|
ItemStack cell = inv.getStackInSlot( 2 );
|
|
|
|
ICellHandler ch = handlersBySlot[slot];
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
MEInventoryHandler handler = invBySlot[slot];
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( handler == null )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( handler.getChannel() == StorageChannel.ITEMS )
|
|
|
|
{
|
|
|
|
if ( ch != null )
|
2014-01-20 17:41:37 +01:00
|
|
|
return ch.getStatusForCell( cell, handler.getInternal() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( handler.getChannel() == StorageChannel.FLUIDS )
|
|
|
|
{
|
|
|
|
if ( ch != null )
|
2014-01-20 17:41:37 +01:00
|
|
|
return ch.getStatusForCell( cell, handler.getInternal() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPowered()
|
|
|
|
{
|
|
|
|
if ( Platform.isClient() )
|
|
|
|
return (state & 0x80000000) == 0x80000000;
|
|
|
|
|
|
|
|
return gridProxy.isActive();
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:46:16 +01:00
|
|
|
@Override
|
|
|
|
public void setPriority(int newValue)
|
|
|
|
{
|
|
|
|
priority = newValue;
|
2014-02-09 02:34:52 +01:00
|
|
|
markDirty();
|
2014-01-26 07:46:16 +01:00
|
|
|
|
|
|
|
isCached = false; // recalculate the storage cell.
|
|
|
|
updateState();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
gridProxy.getGrid().postEvent( new MENetworkCellArrayUpdate() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 22:06:45 +01:00
|
|
|
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
|
|
|
return itemstack != null && AEApi.instance().registries().cell().isCellHandled( itemstack );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|