Applied-Energistics-2-tiler.../tile/storage/TileIOPort.java

414 lines
10 KiB
Java
Raw Normal View History

package appeng.tile.storage;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2014-01-20 17:41:37 +01:00
import net.minecraft.nbt.NBTTagCompound;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
2014-01-20 17:41:37 +01:00
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FullnessMode;
import appeng.api.config.OperationMode;
import appeng.api.config.RedstoneMode;
import appeng.api.config.Settings;
import appeng.api.config.Upgrades;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.IUpgradeableHost;
2014-01-20 17:41:37 +01:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
2014-01-20 17:41:37 +01:00
import appeng.api.util.IConfigManager;
2014-03-17 20:59:50 +01:00
import appeng.core.settings.TickRates;
2014-01-20 17:41:37 +01:00
import appeng.me.GridAccessException;
import appeng.parts.automation.UpgradeInventory;
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;
2014-01-20 17:41:37 +01:00
import appeng.util.ConfigManager;
import appeng.util.IConfigManagerHost;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.WrapperInventoryRange;
import appeng.util.item.ItemList;
2014-01-23 20:02:48 +01:00
public class TileIOPort extends AENetworkInvTile implements IUpgradeableHost, IConfigManagerHost, IGridTickable
{
2014-01-20 17:41:37 +01:00
ConfigManager cm = new ConfigManager( this );
final int sides[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
2014-01-20 17:41:37 +01:00
final int outputSlots[] = { 6, 7, 8, 9, 10, 11 };
AppEngInternalInventory cells = new AppEngInternalInventory( this, 12 );
UpgradeInventory upgrades = new UpgradeInventory( AEApi.instance().blocks().blockIOPort.block(), this, 3 );
BaseActionSource mySrc = new MachineSource( this );
boolean lastRedstoneState = false;
class TileIOPortHandler extends AETileEventHandler
{
public TileIOPortHandler() {
2014-01-26 07:46:16 +01:00
super( TileEventType.WORLD_NBT );
2014-01-20 17:41:37 +01:00
}
@Override
public void writeToNBT(NBTTagCompound data)
{
cm.writeToNBT( data );
cells.writeToNBT( data, "cells" );
upgrades.writeToNBT( data, "upgrades" );
}
@Override
public void readFromNBT(NBTTagCompound data)
{
cm.readFromNBT( data );
cells.readFromNBT( data, "cells" );
upgrades.readFromNBT( data, "upgrades" );
}
};
public TileIOPort() {
addNewHandler( new TileIOPortHandler() );
cm.registerSetting( Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE );
cm.registerSetting( Settings.FULLNESS_MODE, FullnessMode.EMPTY );
cm.registerSetting( Settings.OPERATION_MODE, OperationMode.EMPTY );
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.SMART;
}
@Override
public DimensionalCoord getLocation()
{
return new DimensionalCoord( this );
}
@Override
public IInventory getInternalInventory()
{
2014-01-20 17:41:37 +01:00
return cells;
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
{
2014-01-20 17:41:37 +01:00
if ( cells == inv )
{
updateTask();
}
}
private void updateTask()
{
try
{
if ( hasWork() )
gridProxy.getTick().wakeDevice( gridProxy.getNode() );
else
gridProxy.getTick().sleepDevice( gridProxy.getNode() );
}
catch (GridAccessException e)
{
// :P
}
}
public void updateRedstoneState()
{
boolean currentState = worldObj.isBlockIndirectlyGettingPowered( xCoord, yCoord, zCoord );
if ( lastRedstoneState != currentState )
{
lastRedstoneState = currentState;
updateTask();
}
}
private boolean isEnabled()
{
if ( getInstalledUpgrades( Upgrades.REDSTONE ) == 0 )
return true;
RedstoneMode rs = (RedstoneMode) cm.getSetting( Settings.REDSTONE_CONTROLLED );
if ( rs == RedstoneMode.HIGH_SIGNAL )
return lastRedstoneState;
return !lastRedstoneState;
}
@Override
public int[] getAccessibleSlotsBySide(ForgeDirection side)
{
return sides;
}
2014-01-20 17:41:37 +01:00
@Override
public IConfigManager getConfigManager()
{
return cm;
}
@Override
public IInventory getInventoryByName(String name)
{
if ( name.equals( "upgrades" ) )
return upgrades;
if ( name.equals( "cells" ) )
return cells;
return null;
}
@Override
public int getInstalledUpgrades(Upgrades u)
{
return upgrades.getInstalledUpgrades( u );
}
@Override
2014-02-17 01:50:25 +01:00
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
2014-01-20 17:41:37 +01:00
{
updateTask();
}
boolean hasWork()
{
if ( isEnabled() )
{
for (int x = 0; x < 6; x++)
if ( cells.getStackInSlot( x ) != null )
return true;
}
return false;
}
@Override
public TickingRequest getTickingRequest(IGridNode node)
{
2014-03-17 20:59:50 +01:00
return new TickingRequest( TickRates.IOPort.min, TickRates.IOPort.max, hasWork(), false );
2014-01-20 17:41:37 +01:00
}
@Override
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
{
if ( !gridProxy.isActive() )
return TickRateModulation.IDLE;
long ItemsToMove = 256;
switch (getInstalledUpgrades( Upgrades.SPEED ))
{
case 1:
ItemsToMove *= 2;
break;
case 2:
ItemsToMove *= 4;
break;
case 3:
ItemsToMove *= 8;
break;
}
try
{
IMEInventory<IAEItemStack> itemNet = gridProxy.getStorage().getItemInventory();
IMEInventory<IAEFluidStack> fluidNet = gridProxy.getStorage().getFluidInventory();
IEnergySource energy = gridProxy.getEnergy();
for (int x = 0; x < 6; x++)
{
ItemStack is = cells.getStackInSlot( x );
if ( is != null )
{
if ( ItemsToMove > 0 )
{
IMEInventory<IAEItemStack> itemInv = getInv( is, StorageChannel.ITEMS );
IMEInventory<IAEFluidStack> fluidInv = getInv( is, StorageChannel.FLUIDS );
if ( cm.getSetting( Settings.OPERATION_MODE ) == OperationMode.EMPTY )
{
if ( itemInv != null )
ItemsToMove = transferContents( energy, itemInv, itemNet, ItemsToMove, StorageChannel.ITEMS );
if ( fluidInv != null )
ItemsToMove = transferContents( energy, fluidInv, fluidNet, ItemsToMove, StorageChannel.FLUIDS );
}
else
{
if ( itemInv != null )
ItemsToMove = transferContents( energy, itemNet, itemInv, ItemsToMove, StorageChannel.ITEMS );
if ( fluidInv != null )
ItemsToMove = transferContents( energy, fluidNet, fluidInv, ItemsToMove, StorageChannel.FLUIDS );
}
if ( ItemsToMove > 0 && shouldMove( itemInv, fluidInv ) && !moveSlot( x ) )
return TickRateModulation.IDLE;
return TickRateModulation.URGENT;
}
else
return TickRateModulation.URGENT;
}
}
}
catch (GridAccessException e)
{
return TickRateModulation.IDLE;
}
// nothing left to do...
return TickRateModulation.SLEEP;
}
private boolean shouldMove(IMEInventory<IAEItemStack> itemInv, IMEInventory<IAEFluidStack> fluidInv)
{
FullnessMode fm = (FullnessMode) cm.getSetting( Settings.FULLNESS_MODE );
if ( itemInv != null && fluidInv != null )
return matches( fm, itemInv ) && matches( fm, fluidInv );
else if ( itemInv != null )
return matches( fm, itemInv );
else if ( fluidInv != null )
return matches( fm, fluidInv );
return true;
}
private boolean matches(FullnessMode fm, IMEInventory src)
{
if ( fm == FullnessMode.HALF )
return true;
IItemList<? extends IAEStack> myList;
if ( src instanceof IMEMonitor )
myList = ((IMEMonitor) src).getStorageList();
else
2014-02-21 21:36:40 +01:00
myList = src.getAvailableItems( new ItemList( src.getChannel().type ) );
2014-01-20 17:41:37 +01:00
if ( fm == FullnessMode.EMPTY )
return myList.isEmpty();
IAEStack test = myList.getFirstItem();
if ( test != null )
{
test.setStackSize( 1 );
return src.injectItems( test, Actionable.SIMULATE, mySrc ) != null;
}
return false;
}
ItemStack currentCell;
IMEInventory<IAEFluidStack> cachedFluid;
IMEInventory<IAEItemStack> cachedItem;
private IMEInventory getInv(ItemStack is, StorageChannel chan)
{
if ( currentCell != is )
{
currentCell = is;
cachedFluid = AEApi.instance().registries().cell().getCellInventory( is, StorageChannel.FLUIDS );
cachedItem = AEApi.instance().registries().cell().getCellInventory( is, StorageChannel.ITEMS );
}
if ( StorageChannel.ITEMS == chan )
return cachedItem;
return cachedFluid;
}
private long transferContents(IEnergySource energy, IMEInventory src, IMEInventory dest, long itemsToMove, StorageChannel chan)
{
IItemList<? extends IAEStack> myList;
if ( src instanceof IMEMonitor )
myList = ((IMEMonitor) src).getStorageList();
else
2014-02-21 21:36:40 +01:00
myList = src.getAvailableItems( new ItemList( src.getChannel().type ) );
2014-01-20 17:41:37 +01:00
boolean didStuff;
do
{
didStuff = false;
for (IAEStack s : myList)
{
long totalStackSize = s.getStackSize();
if ( totalStackSize > 0 )
{
IAEStack stack = dest.injectItems( s, Actionable.SIMULATE, mySrc );
long possible = 0;
if ( stack == null )
possible = totalStackSize;
else
possible = totalStackSize - stack.getStackSize();
if ( possible > 0 )
{
possible = Math.min( possible, itemsToMove );
s.setStackSize( possible );
IAEStack extracted = src.extractItems( s, Actionable.MODULATE, mySrc );
if ( extracted != null )
{
possible = extracted.getStackSize();
IAEStack failed = Platform.poweredInsert( energy, dest, extracted, mySrc );
if ( failed != null )
{
possible -= failed.getStackSize();
src.injectItems( failed, Actionable.MODULATE, mySrc );
}
2014-06-17 23:45:26 +02:00
if ( possible > 0 )
{
itemsToMove -= possible;
didStuff = true;
}
2014-04-10 02:52:13 +02:00
break;
2014-01-20 17:41:37 +01:00
}
}
}
}
}
while (itemsToMove > 0 && didStuff);
return itemsToMove;
}
private boolean moveSlot(int x)
{
WrapperInventoryRange wir = new WrapperInventoryRange( this, outputSlots, true );
ItemStack result = InventoryAdaptor.getAdaptor( wir, ForgeDirection.UNKNOWN ).addItems( getStackInSlot( x ) );
if ( result == null )
{
setInventorySlotContents( x, null );
return true;
}
return false;
}
2014-01-23 17:28:12 +01:00
}