2013-12-30 06:55:02 +01:00
|
|
|
package appeng.parts.automation;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-01-02 06:51:41 +01:00
|
|
|
import appeng.api.config.RedstoneMode;
|
2014-01-01 10:05:27 +01:00
|
|
|
import appeng.api.config.Upgrades;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.ticking.IGridTickable;
|
2014-01-02 06:51:41 +01:00
|
|
|
import appeng.api.networking.ticking.TickRateModulation;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.api.networking.ticking.TickingRequest;
|
|
|
|
import appeng.me.GridAccessException;
|
|
|
|
import appeng.tile.inventory.AppEngInternalAEInventory;
|
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
|
|
|
public abstract class PartSharedItemBus extends PartUpgradeable implements IGridTickable
|
|
|
|
{
|
|
|
|
|
|
|
|
public PartSharedItemBus(Class c, ItemStack is) {
|
|
|
|
super( c, is );
|
|
|
|
}
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
@Override
|
|
|
|
public void upgradesChanged()
|
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2014-01-01 10:05:27 +01:00
|
|
|
protected int availableSlots()
|
|
|
|
{
|
|
|
|
return Math.min( 1 + getInstalledUpgrades( Upgrades.CAPACITY ) * 4, config.getSizeInventory() );
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
public void writeToNBT(net.minecraft.nbt.NBTTagCompound extra)
|
|
|
|
{
|
|
|
|
super.writeToNBT( extra );
|
|
|
|
config.writeToNBT( extra, "config" );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(net.minecraft.nbt.NBTTagCompound extra)
|
|
|
|
{
|
|
|
|
super.readFromNBT( extra );
|
|
|
|
config.readFromNBT( extra, "config" );
|
|
|
|
}
|
|
|
|
|
|
|
|
AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, 9 );
|
|
|
|
|
|
|
|
boolean cached = false;
|
|
|
|
int adaptorHash = 0;
|
|
|
|
InventoryAdaptor adaptor;
|
|
|
|
|
|
|
|
InventoryAdaptor getHandler()
|
|
|
|
{
|
|
|
|
if ( cached )
|
|
|
|
return adaptor;
|
|
|
|
|
|
|
|
cached = true;
|
|
|
|
TileEntity self = getHost().getTile();
|
|
|
|
TileEntity target = self.worldObj.getBlockTileEntity( self.xCoord + side.offsetX, self.yCoord + side.offsetY, self.zCoord + side.offsetZ );
|
|
|
|
|
|
|
|
int newAdaptorHash = Platform.generateTileHash( target );
|
|
|
|
|
2014-01-03 19:28:52 +01:00
|
|
|
if ( adaptorHash == newAdaptorHash && newAdaptorHash != 0 )
|
2013-12-30 06:55:02 +01:00
|
|
|
return adaptor;
|
|
|
|
|
|
|
|
adaptorHash = newAdaptorHash;
|
|
|
|
adaptor = InventoryAdaptor.getAdaptor( target, side.getOpposite() );
|
|
|
|
cached = true;
|
|
|
|
|
|
|
|
return adaptor;
|
|
|
|
}
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
boolean lastRedstone = false;
|
|
|
|
|
|
|
|
abstract TickRateModulation doBusWork();
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
@Override
|
|
|
|
public void onNeighborChanged()
|
2014-01-02 06:51:41 +01:00
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
if ( lastRedstone != host.hasRedstone( side ) )
|
|
|
|
{
|
|
|
|
lastRedstone = !lastRedstone;
|
|
|
|
if ( lastRedstone && getRSMode() == RedstoneMode.SIGNAL_PULSE )
|
|
|
|
doBusWork();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState()
|
2013-12-30 06:55:02 +01:00
|
|
|
{
|
|
|
|
cached = false;
|
2014-01-02 06:51:41 +01:00
|
|
|
try
|
2013-12-30 06:55:02 +01:00
|
|
|
{
|
2014-01-02 06:51:41 +01:00
|
|
|
if ( !isSleeping() )
|
2013-12-30 06:55:02 +01:00
|
|
|
proxy.getTick().wakeDevice( proxy.getNode() );
|
2014-01-02 06:51:41 +01:00
|
|
|
else
|
|
|
|
proxy.getTick().sleepDevice( proxy.getNode() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInventoryByName(String name)
|
|
|
|
{
|
|
|
|
if ( name.equals( "config" ) )
|
|
|
|
return config;
|
|
|
|
|
|
|
|
return super.getInventoryByName( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TickingRequest getTickingRequest(IGridNode node)
|
|
|
|
{
|
|
|
|
return new TickingRequest( 5, 60, isSleeping(), false );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|