diff --git a/helpers/DualityInterface.java b/helpers/DualityInterface.java new file mode 100644 index 00000000..32359686 --- /dev/null +++ b/helpers/DualityInterface.java @@ -0,0 +1,447 @@ +package appeng.helpers; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; +import appeng.api.AEApi; +import appeng.api.config.Actionable; +import appeng.api.implementations.ISegmentedInventory; +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.parts.IPart; +import appeng.api.storage.IMEInventory; +import appeng.api.storage.IMEMonitor; +import appeng.api.storage.IStorageMonitorable; +import appeng.api.storage.data.IAEFluidStack; +import appeng.api.storage.data.IAEItemStack; +import appeng.api.util.AECableType; +import appeng.api.util.DimensionalCoord; +import appeng.me.GridAccessException; +import appeng.me.helpers.AENetworkProxy; +import appeng.me.storage.MEMonitorIInventory; +import appeng.me.storage.MEMonitorPassthu; +import appeng.me.storage.NullInventory; +import appeng.tile.inventory.AppEngInternalAEInventory; +import appeng.tile.inventory.AppEngInternalInventory; +import appeng.tile.inventory.IAEAppEngInventory; +import appeng.tile.inventory.InvOperation; +import appeng.util.InventoryAdaptor; +import appeng.util.Platform; +import appeng.util.inv.AdaptorIInventory; +import appeng.util.inv.IInventoryDestination; +import appeng.util.inv.WrapperInvSlot; + +public class DualityInterface implements IGridTickable, ISegmentedInventory, IStorageMonitorable, IInventoryDestination, IAEAppEngInventory +{ + + final int sides[] = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; + final IAEItemStack requireWork[] = new IAEItemStack[] { null, null, null, null, null, null, null, null }; + + boolean hasConfig = false; + AENetworkProxy gridProxy; + IInterfaceHost iHost; + BaseActionSource mySrc; + + public DualityInterface(AENetworkProxy prox, IInterfaceHost ih) { + gridProxy = prox; + iHost = ih; + mySrc = fluids.changeSource = items.changeSource = new MachineSource( iHost ); + } + + private void readConfig() + { + boolean hadConfig = hasConfig; + + hasConfig = false; + + for (ItemStack p : config) + { + if ( p != null ) + { + hasConfig = true; + break; + } + } + + boolean had = hasWorkToDo(); + + for (int x = 0; x < 8; x++) + updatePlan( x ); + + boolean has = hasWorkToDo(); + + if ( had != has ) + { + try + { + if ( has ) + gridProxy.getTick().wakeDevice( gridProxy.getNode() ); + else + gridProxy.getTick().sleepDevice( gridProxy.getNode() ); + } + catch (GridAccessException e) + { + // :P + } + } + + TileEntity te = iHost.getTileEntity(); + if ( hadConfig != hasConfig && te != null && te.worldObj != null ) + { + te.worldObj.notifyBlocksOfNeighborChange( te.xCoord, te.yCoord, te.zCoord, 0 ); + } + } + + public void writeToNBT(NBTTagCompound data) + { + config.writeToNBT( data, "config" ); + patterns.writeToNBT( data, "patterns" ); + storage.writeToNBT( data, "storage" ); + } + + public void readFromNBT(NBTTagCompound data) + { + config.readFromNBT( data, "config" ); + patterns.readFromNBT( data, "patterns" ); + storage.readFromNBT( data, "storage" ); + readConfig(); + } + + AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, 8 ); + AppEngInternalInventory storage = new AppEngInternalInventory( this, 8 ); + AppEngInternalInventory patterns = new AppEngInternalInventory( this, 9 ); + + WrapperInvSlot slotInv = new WrapperInvSlot( storage ); + InventoryAdaptor adaptor = new AdaptorIInventory( slotInv ); + + IMEInventory destination; + private boolean isWorking = false; + + @Override + public boolean canInsert(ItemStack stack) + { + IAEItemStack out = destination.injectItems( AEApi.instance().storage().createItemStack( stack ), Actionable.SIMULATE ); + if ( out == null ) + return true; + return out.getStackSize() != stack.stackSize; + // ItemStack after = adaptor.simulateAdd( stack ); + // if ( after == null ) + // return true; + // return after.stackSize != stack.stackSize; + } + + private void updatePlan(int slot) + { + IAEItemStack req = config.getAEStackInSlot( slot ); + ItemStack Stored = storage.getStackInSlot( slot ); + + if ( req == null && Stored != null ) + { + IAEItemStack work = AEApi.instance().storage().createItemStack( Stored ); + requireWork[slot] = work.setStackSize( -work.getStackSize() ); + return; + } + else if ( req != null ) + { + if ( Stored == null ) // need to add stuff! + { + requireWork[slot] = req.copy(); + return; + } + else if ( req.isSameType( Stored ) ) // same type ( qty diffrent? )! + { + if ( req.getStackSize() != Stored.stackSize ) + { + requireWork[slot] = req.copy(); + requireWork[slot].setStackSize( req.getStackSize() - Stored.stackSize ); + return; + } + } + else if ( Stored != null ) // dispose! + { + IAEItemStack work = AEApi.instance().storage().createItemStack( Stored ); + requireWork[slot] = work.setStackSize( -work.getStackSize() ); + return; + } + } + + // else + + requireWork[slot] = null; + } + + static private boolean interfaceRequest = false; + + class InterfaceInventory extends MEMonitorIInventory + { + + public InterfaceInventory(DualityInterface tileInterface) { + super( tileInterface.storage, ForgeDirection.UP ); + mySource = new MachineSource( iHost ); + } + + @Override + public IAEItemStack injectItems(IAEItemStack input, Actionable type) + { + if ( interfaceRequest ) + return input; + + return super.injectItems( input, type ); + } + + @Override + public IAEItemStack extractItems(IAEItemStack request, Actionable type) + { + if ( interfaceRequest ) + return null; + + return super.extractItems( request, type ); + } + + }; + + private boolean usePlan(int x, IAEItemStack itemStack) + { + boolean changed = false; + slotInv.setSlot( x ); + interfaceRequest = isWorking = true; + + try + { + destination = gridProxy.getStorage().getItemInventory(); + IEnergySource src = gridProxy.getEnergy(); + + if ( itemStack.getStackSize() > 0 ) + { + IAEItemStack aquired = Platform.poweredExtraction( src, destination, itemStack, mySrc ); + if ( aquired != null ) + { + changed = true; + ItemStack issue = adaptor.addItems( aquired.getItemStack() ); + if ( issue != null ) + throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); + } + } + else if ( itemStack.getStackSize() < 0 ) + { + IAEItemStack toStore = itemStack.copy(); + toStore.setStackSize( -toStore.getStackSize() ); + + long diff = toStore.getStackSize(); + + toStore = Platform.poweredInsert( src, destination, toStore, mySrc ); + + if ( toStore != null ) + diff -= toStore.getStackSize(); + + if ( diff != 0 ) + { + // extract items! + changed = true; + ItemStack removed = adaptor.removeItems( (int) diff, null, null ); + if ( removed == null ) + throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); + else if ( removed.stackSize != diff ) + throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); + } + } + // else wtf? + } + catch (GridAccessException e) + { + // :P + } + + if ( changed ) + updatePlan( x ); + + interfaceRequest = isWorking = false; + return changed; + } + + public IInventory getConfig() + { + return config; + } + + public IInventory getPatterns() + { + return patterns; + } + + MEMonitorPassthu items = new MEMonitorPassthu( new NullInventory() ); + MEMonitorPassthu fluids = new MEMonitorPassthu( new NullInventory() ); + + public void gridChanged() + { + try + { + items.setInternal( gridProxy.getStorage().getItemInventory() ); + fluids.setInternal( gridProxy.getStorage().getFluidInventory() ); + } + catch (GridAccessException gae) + { + items.setInternal( new NullInventory() ); + fluids.setInternal( new NullInventory() ); + } + + TileEntity te = iHost.getTileEntity(); + te.worldObj.notifyBlocksOfNeighborChange( te.xCoord, te.yCoord, te.zCoord, 0 ); + } + + public AECableType getCableConnectionType(ForgeDirection dir) + { + return AECableType.SMART; + } + + public DimensionalCoord getLocation() + { + return new DimensionalCoord( iHost.getTileEntity() ); + } + + public IInventory getInternalInventory() + { + return storage; + } + + public void onInventoryChanged() + { + } + + @Override + public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added) + { + if ( isWorking ) + return; + + if ( inv == config ) + readConfig(); + else if ( inv == patterns ) + { + + } + else if ( inv == storage && slot >= 0 ) + { + boolean had = hasWorkToDo(); + + updatePlan( slot ); + + boolean now = hasWorkToDo(); + + if ( had != now ) + { + try + { + if ( now ) + gridProxy.getTick().wakeDevice( gridProxy.getNode() ); + else + gridProxy.getTick().sleepDevice( gridProxy.getNode() ); + } + catch (GridAccessException e) + { + // :P + } + } + } + } + + public boolean hasWorkToDo() + { + return requireWork[0] != null || requireWork[1] != null || requireWork[2] != null || requireWork[3] != null || requireWork[4] != null + || requireWork[5] != null || requireWork[6] != null || requireWork[7] != null; + } + + private boolean updateStorage() + { + boolean didSomething = false; + + for (int x = 0; x < 8; x++) + { + if ( requireWork[x] != null ) + { + didSomething = usePlan( x, requireWork[x] ) || didSomething; + } + } + + return didSomething; + } + + public boolean hasConfig() + { + return hasConfig; + } + + public int[] getAccessibleSlotsFromSide(int side) + { + return sides; + } + + @Override + public TickingRequest getTickingRequest(IGridNode node) + { + return new TickingRequest( 5, 120, !hasWorkToDo(), false ); + } + + @Override + public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) + { + boolean couldDoWork = updateStorage(); + return hasWorkToDo() ? (couldDoWork ? TickRateModulation.URGENT : TickRateModulation.SLOWER) : TickRateModulation.SLEEP; + } + + @Override + public IMEMonitor getItemInventory() + { + if ( hasConfig() ) + return new InterfaceInventory( this ); + + return items; + } + + @Override + public IMEMonitor getFluidInventory() + { + if ( hasConfig() ) + return null; + + return fluids; + } + + @Override + public IInventory getInventoryByName(String name) + { + if ( name.equals( "storage" ) ) + return storage; + + if ( name.equals( "patterns" ) ) + return patterns; + + if ( name.equals( "config" ) ) + return config; + + return null; + } + + public IInventory getStorage() + { + return storage; + } + + public TileEntity getTile() + { + return (TileEntity) (iHost instanceof TileEntity ? iHost : null); + } + + public IPart getPart() + { + return (IPart) (iHost instanceof IPart ? iHost : null); + } + +} diff --git a/helpers/IInterfaceHost.java b/helpers/IInterfaceHost.java new file mode 100644 index 00000000..78492f5f --- /dev/null +++ b/helpers/IInterfaceHost.java @@ -0,0 +1,12 @@ +package appeng.helpers; + +import net.minecraft.tileentity.TileEntity; +import appeng.api.networking.IGridHost; + +public interface IInterfaceHost extends IGridHost +{ + + DualityInterface getInterfaceDuality(); + + TileEntity getTileEntity(); +} diff --git a/parts/misc/PartInterface.java b/parts/misc/PartInterface.java new file mode 100644 index 00000000..9d75428f --- /dev/null +++ b/parts/misc/PartInterface.java @@ -0,0 +1,282 @@ +package appeng.parts.misc; + +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.Vec3; +import net.minecraftforge.common.ForgeDirection; +import appeng.api.implementations.ISegmentedInventory; +import appeng.api.implementations.ITileStorageMonitorable; +import appeng.api.networking.IGridNode; +import appeng.api.networking.ticking.IGridTickable; +import appeng.api.networking.ticking.TickRateModulation; +import appeng.api.networking.ticking.TickingRequest; +import appeng.api.parts.IPartCollsionHelper; +import appeng.api.parts.IPartRenderHelper; +import appeng.api.storage.IMEMonitor; +import appeng.api.storage.IStorageMonitorable; +import appeng.api.storage.data.IAEFluidStack; +import appeng.api.storage.data.IAEItemStack; +import appeng.client.texture.CableBusTextures; +import appeng.core.sync.GuiBridge; +import appeng.helpers.DualityInterface; +import appeng.helpers.IInterfaceHost; +import appeng.parts.PartBasicState; +import appeng.tile.inventory.IAEAppEngInventory; +import appeng.tile.inventory.InvOperation; +import appeng.util.Platform; +import appeng.util.inv.IInventoryDestination; + +public class PartInterface extends PartBasicState implements IGridTickable, ISegmentedInventory, IStorageMonitorable, IInventoryDestination, IInterfaceHost, + ISidedInventory, IAEAppEngInventory, ITileStorageMonitorable +{ + + DualityInterface duality = new DualityInterface( proxy, this ); + + public PartInterface(ItemStack is) { + super( PartInterface.class, is ); + } + + @Override + public void gridChanged() + { + duality.gridChanged(); + } + + @Override + public void writeToNBT(NBTTagCompound data) + { + super.writeToNBT( data ); + duality.writeToNBT( data ); + } + + @Override + public void readFromNBT(NBTTagCompound data) + { + super.readFromNBT( data ); + duality.readFromNBT( data ); + } + + @Override + public void renderInventory(IPartRenderHelper rh, RenderBlocks renderer) + { + rh.setTexture( CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), + is.getIconIndex(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon() ); + + rh.setBounds( 3, 3, 15, 13, 13, 16 ); + rh.renderInventoryBox( renderer ); + + rh.setBounds( 2, 2, 14, 14, 14, 15 ); + rh.renderInventoryBox( renderer ); + + rh.setBounds( 5, 5, 12, 11, 11, 14 ); + rh.renderInventoryBox( renderer ); + } + + @Override + public void renderStatic(int x, int y, int z, IPartRenderHelper rh, RenderBlocks renderer) + { + rh.useSimpliedRendering( x, y, z, this ); + rh.setTexture( CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), + is.getIconIndex(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon() ); + + rh.setBounds( 2, 2, 14, 14, 14, 16 ); + rh.renderBlock( x, y, z, renderer ); + + rh.setTexture( CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), + is.getIconIndex(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon() ); + + rh.setBounds( 5, 5, 12, 11, 11, 13 ); + rh.renderBlock( x, y, z, renderer ); + + rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon(), + CableBusTextures.PartMonitorBack.getIcon(), is.getIconIndex(), CableBusTextures.PartMonitorSidesStatus.getIcon(), + CableBusTextures.PartMonitorSidesStatus.getIcon() ); + + rh.setBounds( 5, 5, 13, 11, 11, 14 ); + rh.renderBlock( x, y, z, renderer ); + + renderLights( x, y, z, rh, renderer ); + } + + @Override + public void getBoxes(IPartCollsionHelper bch) + { + bch.addBox( 3, 3, 15, 13, 13, 16 ); + bch.addBox( 2, 2, 14, 14, 14, 15 ); + bch.addBox( 5, 5, 12, 11, 11, 13 ); + bch.addBox( 5, 5, 13, 11, 11, 14 ); + } + + @Override + public int cableConnectionRenderTo() + { + return 4; + } + + @Override + public TileEntity getTileEntity() + { + return (TileEntity) super.getHost().getTile(); + } + + @Override + public boolean canInsert(ItemStack stack) + { + return duality.canInsert( stack ); + } + + @Override + public IMEMonitor getItemInventory() + { + return duality.getItemInventory(); + } + + @Override + public IMEMonitor getFluidInventory() + { + return duality.getFluidInventory(); + } + + @Override + public TickingRequest getTickingRequest(IGridNode node) + { + return duality.getTickingRequest( node ); + } + + @Override + public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) + { + return duality.tickingRequest( node, TicksSinceLastCall ); + } + + @Override + public int getSizeInventory() + { + return duality.getStorage().getSizeInventory(); + } + + @Override + public ItemStack getStackInSlot(int i) + { + return duality.getStorage().getStackInSlot( i ); + } + + @Override + public ItemStack decrStackSize(int i, int j) + { + return duality.getStorage().decrStackSize( i, j ); + } + + @Override + public ItemStack getStackInSlotOnClosing(int i) + { + return duality.getStorage().getStackInSlotOnClosing( i ); + } + + @Override + public void setInventorySlotContents(int i, ItemStack itemstack) + { + duality.getStorage().setInventorySlotContents( i, itemstack ); + } + + @Override + public String getInvName() + { + return duality.getStorage().getInvName(); + } + + @Override + public boolean isInvNameLocalized() + { + return duality.getStorage().isInvNameLocalized(); + } + + @Override + public int getInventoryStackLimit() + { + return duality.getStorage().getInventoryStackLimit(); + } + + @Override + public void onInventoryChanged() + { + duality.getStorage().onInventoryChanged(); + } + + @Override + public boolean isUseableByPlayer(EntityPlayer entityplayer) + { + return duality.getStorage().isUseableByPlayer( entityplayer ); + } + + @Override + public void openChest() + { + duality.getStorage().openChest(); + } + + @Override + public void closeChest() + { + duality.getStorage().closeChest(); + } + + @Override + public boolean isItemValidForSlot(int i, ItemStack itemstack) + { + return duality.getStorage().isItemValidForSlot( i, itemstack ); + } + + @Override + public int[] getAccessibleSlotsFromSide(int s) + { + return duality.getAccessibleSlotsFromSide( s ); + } + + @Override + public boolean canInsertItem(int i, ItemStack itemstack, int j) + { + return true; + } + + @Override + public boolean canExtractItem(int i, ItemStack itemstack, int j) + { + return true; + } + + @Override + public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack) + { + duality.onChangeInventory( inv, slot, mc, removedStack, newStack ); + } + + @Override + public DualityInterface getInterfaceDuality() + { + return duality; + } + + @Override + public boolean onActivate(EntityPlayer p, Vec3 pos) + { + if ( p.isSneaking() ) + return false; + + if ( Platform.isServer() ) + Platform.openGUI( p, getTileEntity(), side, GuiBridge.GUI_INTERFACE ); + + return true; + } + + @Override + public IStorageMonitorable getMonitorable(ForgeDirection side) + { + return this; + } +} diff --git a/tile/misc/TileInterface.java b/tile/misc/TileInterface.java index d6c1e313..53944b8e 100644 --- a/tile/misc/TileInterface.java +++ b/tile/misc/TileInterface.java @@ -5,238 +5,40 @@ import java.util.EnumSet; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; -import appeng.api.AEApi; -import appeng.api.config.Actionable; import appeng.api.implementations.ISegmentedInventory; -import appeng.api.implementations.IStorageMonitorable; +import appeng.api.implementations.ITileStorageMonitorable; import appeng.api.networking.IGridNode; -import appeng.api.networking.energy.IEnergySource; 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.IStorageMonitorable; import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEItemStack; import appeng.api.util.AECableType; import appeng.api.util.DimensionalCoord; -import appeng.me.GridAccessException; -import appeng.me.storage.MEMonitorIInventory; -import appeng.me.storage.MEMonitorPassthu; -import appeng.me.storage.NullInventory; +import appeng.api.util.IConfigManager; +import appeng.api.util.IConfigureableObject; +import appeng.helpers.DualityInterface; +import appeng.helpers.IInterfaceHost; import appeng.tile.events.AETileEventHandler; import appeng.tile.events.TileEventType; import appeng.tile.grid.AENetworkInvTile; -import appeng.tile.inventory.AppEngInternalAEInventory; -import appeng.tile.inventory.AppEngInternalInventory; import appeng.tile.inventory.InvOperation; -import appeng.util.InventoryAdaptor; -import appeng.util.Platform; -import appeng.util.inv.AdaptorIInventory; import appeng.util.inv.IInventoryDestination; -import appeng.util.inv.WrapperInvSlot; -public class TileInterface extends AENetworkInvTile implements IGridTickable, ISegmentedInventory, IStorageMonitorable, IInventoryDestination +public class TileInterface extends AENetworkInvTile implements IGridTickable, ISegmentedInventory, ITileStorageMonitorable, IStorageMonitorable, + IInventoryDestination, IInterfaceHost, IConfigureableObject { - final int sides[] = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; - final IAEItemStack requireWork[] = new IAEItemStack[] { null, null, null, null, null, null, null, null }; - - boolean hasConfig = false; - - private void readConfig() - { - boolean hadConfig = hasConfig; - - hasConfig = false; - - for (ItemStack p : config) - { - if ( p != null ) - { - hasConfig = true; - break; - } - } - - boolean had = hasWorkToDo(); - - for (int x = 0; x < 8; x++) - updatePlan( x ); - - boolean has = hasWorkToDo(); - - if ( had != has ) - { - try - { - if ( has ) - gridProxy.getTick().wakeDevice( gridProxy.getNode() ); - else - gridProxy.getTick().sleepDevice( gridProxy.getNode() ); - } - catch (GridAccessException e) - { - // :P - } - } - - if ( hadConfig != hasConfig && worldObj != null ) - { - worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord, 0 ); - } - } - - AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, 8 ); - AppEngInternalInventory storage = new AppEngInternalInventory( this, 8 ); - AppEngInternalInventory patterns = new AppEngInternalInventory( this, 9 ); - - WrapperInvSlot slotInv = new WrapperInvSlot( storage ); - InventoryAdaptor adaptor = new AdaptorIInventory( slotInv ); - - IMEInventory destination; - private boolean isWorking = false; + DualityInterface duality = new DualityInterface( gridProxy, this ); @Override - public boolean canInsert(ItemStack stack) + public void gridChanged() { - IAEItemStack out = destination.injectItems( AEApi.instance().storage().createItemStack( stack ), Actionable.SIMULATE ); - if ( out == null ) - return true; - return out.getStackSize() != stack.stackSize; - // ItemStack after = adaptor.simulateAdd( stack ); - // if ( after == null ) - // return true; - // return after.stackSize != stack.stackSize; - } - - private void updatePlan(int slot) - { - IAEItemStack req = config.getAEStackInSlot( slot ); - ItemStack Stored = storage.getStackInSlot( slot ); - - if ( req == null && Stored != null ) - { - IAEItemStack work = AEApi.instance().storage().createItemStack( Stored ); - requireWork[slot] = work.setStackSize( -work.getStackSize() ); - return; - } - else if ( req != null ) - { - if ( Stored == null ) // need to add stuff! - { - requireWork[slot] = req.copy(); - return; - } - else if ( req.isSameType( Stored ) ) // same type ( qty diffrent? )! - { - if ( req.getStackSize() != Stored.stackSize ) - { - requireWork[slot] = req.copy(); - requireWork[slot].setStackSize( req.getStackSize() - Stored.stackSize ); - return; - } - } - else if ( Stored != null ) // dispose! - { - IAEItemStack work = AEApi.instance().storage().createItemStack( Stored ); - requireWork[slot] = work.setStackSize( -work.getStackSize() ); - return; - } - } - - // else - - requireWork[slot] = null; - } - - static private boolean interfaceRequest = false; - - class InterfaceInventory extends MEMonitorIInventory - { - - public InterfaceInventory(TileInterface tileInterface) { - super( tileInterface, ForgeDirection.UP ); - } - - @Override - public IAEItemStack injectItems(IAEItemStack input, Actionable type) - { - if ( interfaceRequest ) - return input; - - return super.injectItems( input, type ); - } - - @Override - public IAEItemStack extractItems(IAEItemStack request, Actionable type) - { - if ( interfaceRequest ) - return null; - - return super.extractItems( request, type ); - } - - }; - - private boolean usePlan(int x, IAEItemStack itemStack) - { - boolean changed = false; - slotInv.setSlot( x ); - interfaceRequest = isWorking = true; - - try - { - destination = gridProxy.getStorage().getItemInventory(); - IEnergySource src = gridProxy.getEnergy(); - - if ( itemStack.getStackSize() > 0 ) - { - IAEItemStack aquired = Platform.poweredExtraction( src, destination, itemStack ); - if ( aquired != null ) - { - changed = true; - ItemStack issue = adaptor.addItems( aquired.getItemStack() ); - if ( issue != null ) - throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); - } - } - else if ( itemStack.getStackSize() < 0 ) - { - IAEItemStack toStore = itemStack.copy(); - toStore.setStackSize( -toStore.getStackSize() ); - - long diff = toStore.getStackSize(); - - toStore = Platform.poweredInsert( src, destination, toStore ); - - if ( toStore != null ) - diff -= toStore.getStackSize(); - - if ( diff != 0 ) - { - // extract items! - changed = true; - ItemStack removed = adaptor.removeItems( (int) diff, null, null ); - if ( removed == null ) - throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); - else if ( removed.stackSize != diff ) - throw new RuntimeException( "bad attempt at managining inventory. ( addItems )" ); - } - } - // else wtf? - } - catch (GridAccessException e) - { - // :P - } - - if ( changed ) - updatePlan( x ); - - interfaceRequest = isWorking = false; - return changed; + duality.gridChanged(); } class TileInterfaceHandler extends AETileEventHandler @@ -249,16 +51,13 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, IS @Override public void writeToNBT(NBTTagCompound data) { - config.writeToNBT( data, "config" ); - patterns.writeToNBT( data, "patterns" ); + duality.writeToNBT( data ); } @Override public void readFromNBT(NBTTagCompound data) { - config.readFromNBT( data, "config" ); - patterns.readFromNBT( data, "patterns" ); - readConfig(); + duality.readFromNBT( data ); } }; @@ -267,172 +66,94 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, IS addNewHandler( new TileInterfaceHandler() ); } - public IInventory getConfig() - { - return config; - } - - public IInventory getPatterns() - { - return patterns; - } - - MEMonitorPassthu items = new MEMonitorPassthu( new NullInventory() ); - MEMonitorPassthu fluids = new MEMonitorPassthu( new NullInventory() ); - - @Override - public void gridChanged() - { - try - { - items.setInternal( gridProxy.getStorage().getItemInventory() ); - fluids.setInternal( gridProxy.getStorage().getFluidInventory() ); - } - catch (GridAccessException gae) - { - items.setInternal( new NullInventory() ); - fluids.setInternal( new NullInventory() ); - } - - worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord, 0 ); - } - @Override public AECableType getCableConnectionType(ForgeDirection dir) { - return AECableType.SMART; + return duality.getCableConnectionType( dir ); } @Override public DimensionalCoord getLocation() { - return new DimensionalCoord( this ); + return duality.getLocation(); } @Override - public IInventory getInternalInventory() + public TileEntity getTileEntity() { - return storage; + return this; } @Override - public void onInventoryChanged() + public boolean canInsert(ItemStack stack) { - readConfig(); - } - - @Override - public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added) - { - if ( isWorking ) - return; - - if ( inv == config ) - readConfig(); - else if ( inv == patterns ) - { - - } - else if ( inv == storage && slot >= 0 ) - { - boolean had = hasWorkToDo(); - - updatePlan( slot ); - - boolean now = hasWorkToDo(); - - if ( had != now ) - { - try - { - if ( now ) - gridProxy.getTick().wakeDevice( gridProxy.getNode() ); - else - gridProxy.getTick().sleepDevice( gridProxy.getNode() ); - } - catch (GridAccessException e) - { - // :P - } - } - } - } - - public boolean hasWorkToDo() - { - return requireWork[0] != null || requireWork[1] != null || requireWork[2] != null || requireWork[3] != null || requireWork[4] != null - || requireWork[5] != null || requireWork[6] != null || requireWork[7] != null; - } - - private boolean updateStorage() - { - boolean didSomething = false; - - for (int x = 0; x < 8; x++) - { - if ( requireWork[x] != null ) - { - didSomething = usePlan( x, requireWork[x] ) || didSomething; - } - } - - return didSomething; - } - - public boolean hasConfig() - { - return hasConfig; - } - - @Override - public int[] getAccessibleSlotsFromSide(int side) - { - return sides; - } - - @Override - public TickingRequest getTickingRequest(IGridNode node) - { - return new TickingRequest( 5, 120, !hasWorkToDo(), false ); - } - - @Override - public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) - { - boolean couldDoWork = updateStorage(); - return hasWorkToDo() ? (couldDoWork ? TickRateModulation.URGENT : TickRateModulation.SLOWER) : TickRateModulation.SLEEP; + return duality.canInsert( stack ); } @Override public IMEMonitor getItemInventory() { - if ( hasConfig() ) - return new InterfaceInventory( this ); - - return items; + return duality.getItemInventory(); } @Override public IMEMonitor getFluidInventory() { - if ( hasConfig() ) - return null; - - return fluids; + return duality.getFluidInventory(); } @Override public IInventory getInventoryByName(String name) { - if ( name.equals( "storage" ) ) - return storage; + return duality.getInventoryByName( name ); + } - if ( name.equals( "patterns" ) ) - return patterns; + @Override + public TickingRequest getTickingRequest(IGridNode node) + { + return duality.getTickingRequest( node ); + } - if ( name.equals( "config" ) ) - return config; + @Override + public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) + { + return duality.tickingRequest( node, TicksSinceLastCall ); + } + @Override + public IInventory getInternalInventory() + { + return duality.getInternalInventory(); + } + + @Override + public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added) + { + duality.onChangeInventory( inv, slot, mc, removed, added ); + } + + @Override + public int[] getAccessibleSlotsFromSide(int side) + { + return duality.getAccessibleSlotsFromSide( side ); + } + + @Override + public DualityInterface getInterfaceDuality() + { + return duality; + } + + @Override + public IStorageMonitorable getMonitorable(ForgeDirection side) + { + return this; + } + + @Override + public IConfigManager getConfigManager() + { + // TODO Auto-generated method stub return null; }