2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile.spatial;
|
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-08-23 08:25:06 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
2014-08-23 08:25:06 +02:00
|
|
|
import appeng.api.config.YesNo;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.api.implementations.TransitionResult;
|
|
|
|
import appeng.api.implementations.items.ISpatialStorageCell;
|
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
2014-07-23 20:56:00 +02:00
|
|
|
import appeng.api.networking.events.MENetworkEvent;
|
|
|
|
import appeng.api.networking.events.MENetworkSpatialEvent;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.api.networking.spatial.ISpatialCache;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.AECableType;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.hooks.TickHandler;
|
|
|
|
import appeng.items.storage.ItemSpatialStorageCell;
|
|
|
|
import appeng.me.cache.SpatialPylonCache;
|
2014-08-28 09:39:52 +02:00
|
|
|
import appeng.tile.TileEvent;
|
2014-08-23 08:25:06 +02:00
|
|
|
import appeng.tile.events.TileEventType;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.tile.grid.AENetworkInvTile;
|
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.util.Platform;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
public class TileSpatialIOPort extends AENetworkInvTile implements Callable
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final int sides[] = { 0, 1 };
|
|
|
|
AppEngInternalInventory inv = new AppEngInternalInventory( this, 2 );
|
2014-08-23 08:25:06 +02:00
|
|
|
YesNo lastRedstoneState = YesNo.UNDECIDED;
|
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.WORLD_NBT_WRITE)
|
|
|
|
public void writeToNBT_TileSpatialIOPort(NBTTagCompound data)
|
2014-08-23 08:25:06 +02:00
|
|
|
{
|
2014-08-28 09:39:52 +02:00
|
|
|
data.setInteger( "lastRedstoneState", lastRedstoneState.ordinal() );
|
|
|
|
}
|
2014-08-23 08:25:06 +02:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.WORLD_NBT_READ)
|
|
|
|
public void readFromNBT_TileSpatialIOPort(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
if ( data.hasKey( "lastRedstoneState" ) )
|
|
|
|
lastRedstoneState = YesNo.values()[data.getInteger( "lastRedstoneState" )];
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
public TileSpatialIOPort() {
|
|
|
|
gridProxy.setFlags( GridFlags.REQUIRE_CHANNEL );
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public void updateRedstoneState()
|
|
|
|
{
|
2014-08-23 08:25:06 +02:00
|
|
|
YesNo currentState = worldObj.isBlockIndirectlyGettingPowered( xCoord, yCoord, zCoord ) ? YesNo.YES : YesNo.NO;
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( lastRedstoneState != currentState )
|
|
|
|
{
|
|
|
|
lastRedstoneState = currentState;
|
2014-08-23 08:25:06 +02:00
|
|
|
if ( lastRedstoneState == YesNo.YES )
|
2013-12-27 23:59:59 +01:00
|
|
|
triggerTransition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 08:25:06 +02:00
|
|
|
public boolean getRedstoneState()
|
|
|
|
{
|
|
|
|
if ( lastRedstoneState == YesNo.UNDECIDED )
|
|
|
|
updateRedstoneState();
|
|
|
|
|
|
|
|
return lastRedstoneState == YesNo.YES;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
private void triggerTransition()
|
|
|
|
{
|
2014-03-05 08:27:42 +01:00
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
|
|
|
ItemStack cell = getStackInSlot( 0 );
|
|
|
|
if ( isSpatialCell( cell ) )
|
|
|
|
{
|
2014-07-08 06:54:28 +02:00
|
|
|
TickHandler.instance.addCallable( null, this );// this needs to be cross world sycned.
|
2014-03-05 08:27:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object call() throws Exception
|
|
|
|
{
|
|
|
|
|
|
|
|
ItemStack cell = getStackInSlot( 0 );
|
|
|
|
if ( isSpatialCell( cell ) && getStackInSlot( 1 ) == null )
|
|
|
|
{
|
|
|
|
IGrid gi = gridProxy.getGrid();
|
|
|
|
IEnergyGrid energy = gridProxy.getEnergy();
|
|
|
|
|
|
|
|
ItemSpatialStorageCell sc = (ItemSpatialStorageCell) cell.getItem();
|
|
|
|
|
|
|
|
SpatialPylonCache spc = (SpatialPylonCache) gi.getCache( ISpatialCache.class );
|
|
|
|
if ( spc.hasRegion() && spc.isValidRegion() )
|
|
|
|
{
|
|
|
|
double req = spc.requiredPower();
|
|
|
|
double pr = energy.extractAEPower( req, Actionable.SIMULATE, PowerMultiplier.CONFIG );
|
|
|
|
if ( Math.abs( pr - req ) < req * 0.001 )
|
|
|
|
{
|
2014-07-23 20:56:00 +02:00
|
|
|
MENetworkEvent res = gi.postEvent( new MENetworkSpatialEvent( this, req ) );
|
2014-08-23 08:25:06 +02:00
|
|
|
if ( !res.isCanceled() )
|
2014-03-05 08:27:42 +01:00
|
|
|
{
|
2014-07-23 20:56:00 +02:00
|
|
|
TransitionResult tr = sc.doSpatialTransition( cell, worldObj, spc.getMin(), spc.getMax(), true );
|
|
|
|
if ( tr.success )
|
|
|
|
{
|
|
|
|
energy.extractAEPower( req, Actionable.MODULATE, PowerMultiplier.CONFIG );
|
|
|
|
setInventorySlotContents( 0, null );
|
|
|
|
setInventorySlotContents( 1, cell );
|
|
|
|
}
|
2014-03-05 08:27:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
return null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AECableType getCableConnectionType(ForgeDirection dir)
|
|
|
|
{
|
|
|
|
return AECableType.SMART;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getLocation()
|
|
|
|
{
|
|
|
|
return new DimensionalCoord( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInternalInventory()
|
|
|
|
{
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
|
|
|
return (i == 0 ? isSpatialCell( itemstack ) : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isSpatialCell(ItemStack cell)
|
|
|
|
{
|
|
|
|
if ( cell != null && cell.getItem() instanceof ISpatialStorageCell )
|
|
|
|
{
|
|
|
|
ISpatialStorageCell sc = (ISpatialStorageCell) cell.getItem();
|
|
|
|
return sc != null && sc.isSpatialStorage( cell );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int i, ItemStack itemstack, int j)
|
|
|
|
{
|
|
|
|
return isItemValidForSlot( i, itemstack );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int i, ItemStack itemstack, int j)
|
|
|
|
{
|
|
|
|
return i == 1;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-14 03:59:37 +01:00
|
|
|
public int[] getAccessibleSlotsBySide(ForgeDirection side)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|