diff --git a/client/gui/implementations/GuiCraftingCPU.java b/client/gui/implementations/GuiCraftingCPU.java index 1ceafe0a..17c7b177 100644 --- a/client/gui/implementations/GuiCraftingCPU.java +++ b/client/gui/implementations/GuiCraftingCPU.java @@ -116,7 +116,8 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource if ( l.getStackSize() <= 0 ) { - a.reset(); + if ( a != null ) + a.reset(); } else { @@ -126,6 +127,8 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource a = s.findPrecise( l ); } + if ( a != null ) + a.setStackSize( l.getStackSize() ); } } diff --git a/container/implementations/ContainerCraftingCPU.java b/container/implementations/ContainerCraftingCPU.java index 3d8b8943..4a8db72b 100644 --- a/container/implementations/ContainerCraftingCPU.java +++ b/container/implementations/ContainerCraftingCPU.java @@ -5,27 +5,34 @@ import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.item.ItemStack; +import net.minecraft.inventory.ICrafting; import net.minecraftforge.common.util.ForgeDirection; import appeng.api.AEApi; import appeng.api.networking.IGrid; -import appeng.api.networking.IGridBlock; import appeng.api.networking.IGridHost; import appeng.api.networking.IGridNode; +import appeng.api.networking.crafting.CraftingItemList; +import appeng.api.networking.security.BaseActionSource; +import appeng.api.networking.storage.IBaseMonitor; +import appeng.api.storage.IMEMonitorHandlerReceiver; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; import appeng.container.AEBaseContainer; import appeng.core.sync.network.NetworkHandler; import appeng.core.sync.packets.PacketMEInventoryUpdate; +import appeng.me.cluster.IAECluster; +import appeng.me.cluster.implementations.CraftingCPUCluster; import appeng.tile.crafting.TileCraftingTile; import appeng.util.Platform; -import appeng.util.item.AEItemStack; -public class ContainerCraftingCPU extends AEBaseContainer +public class ContainerCraftingCPU extends AEBaseContainer implements IMEMonitorHandlerReceiver { + CraftingCPUCluster monitor = null; IGrid network; + IItemList list = AEApi.instance().storage().createItemList(); + public ContainerCraftingCPU(InventoryPlayer ip, TileCraftingTile te) { super( ip, null, null ); IGridHost host = te;// .getGridHost(); @@ -37,6 +44,20 @@ public class ContainerCraftingCPU extends AEBaseContainer findNode( host, d ); } + if ( te instanceof TileCraftingTile ) + { + IAECluster c = ((TileCraftingTile) te).getCluster(); + if ( c instanceof CraftingCPUCluster ) + { + monitor = (CraftingCPUCluster) c; + if ( monitor != null ) + { + monitor.getListOfItem( list, CraftingItemList.ALL ); + monitor.addListener( this, null ); + } + } + } + if ( network == null && Platform.isServer() ) isContainerValid = false; } @@ -53,43 +74,49 @@ public class ContainerCraftingCPU extends AEBaseContainer int delay = 40; + @Override + public void onContainerClosed(EntityPlayer player) + { + super.onContainerClosed( player ); + if ( monitor != null ) + monitor.removeListener( this ); + } + + @Override + public void removeCraftingFromCrafters(ICrafting c) + { + super.removeCraftingFromCrafters( c ); + + if ( this.crafters.isEmpty() && monitor != null ) + monitor.removeListener( this ); + } + @Override public void detectAndSendChanges() { - delay++; - if ( Platform.isServer() && delay > 15 && network != null ) + if ( Platform.isServer() ) { - delay = 0; - - PacketMEInventoryUpdate piu; try { - piu = new PacketMEInventoryUpdate(); + PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 ); + PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 ); + PacketMEInventoryUpdate c = new PacketMEInventoryUpdate( (byte) 2 ); - for (Class machineClass : network.getMachinesClasses()) + for (IAEItemStack out : list) { - IItemList list = AEApi.instance().storage().createItemList(); - for (IGridNode machine : network.getMachines( machineClass )) - { - IGridBlock blk = machine.getGridBlock(); - ItemStack is = blk.getMachineRepresentation(); - if ( is != null && is.getItem() != null ) - { - IAEItemStack ais = AEItemStack.create( is ); - ais.setStackSize( 1 ); - ais.setCountRequestable( (long) (blk.getIdlePowerUsage() * 100.0) ); - list.add( ais ); - } - } - - for (IAEItemStack ais : list) - piu.appendItem( ais ); + a.appendItem( monitor.getItemStack( out, CraftingItemList.STORAGE ) ); + b.appendItem( monitor.getItemStack( out, CraftingItemList.ACTIVE ) ); + c.appendItem( monitor.getItemStack( out, CraftingItemList.PENDING ) ); } - for (Object c : this.crafters) + for (Object g : this.crafters) { - if ( c instanceof EntityPlayer ) - NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c ); + if ( g instanceof EntityPlayer ) + { + NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g ); + NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g ); + NetworkHandler.instance.sendTo( c, (EntityPlayerMP) g ); + } } } catch (IOException e) @@ -100,4 +127,24 @@ public class ContainerCraftingCPU extends AEBaseContainer } super.detectAndSendChanges(); } + + @Override + public boolean isValid(Object verificationToken) + { + return true; + } + + @Override + public void postChange(IBaseMonitor monitor, IAEItemStack change, BaseActionSource actionSource) + { + change = change.copy(); + change.setStackSize( 1 ); + list.add( change ); + } + + @Override + public void onListUpdate() + { + + } } diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index 591bd348..3adf1fec 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -16,12 +16,15 @@ import appeng.api.config.FuzzyMode; import appeng.api.networking.IGrid; import appeng.api.networking.IGridHost; import appeng.api.networking.IGridNode; +import appeng.api.networking.crafting.CraftingItemList; import appeng.api.networking.crafting.ICraftingMedium; import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.networking.events.MENetworkCraftingCpuChange; import appeng.api.networking.security.BaseActionSource; +import appeng.api.networking.storage.IBaseMonitor; import appeng.api.networking.storage.IStorageGrid; import appeng.api.storage.IMEInventory; +import appeng.api.storage.IMEMonitorHandlerReceiver; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; @@ -38,9 +41,48 @@ import appeng.util.Platform; import appeng.util.item.AEItemStack; import cpw.mods.fml.common.FMLCommonHandler; -public class CraftingCPUCluster implements IAECluster +public class CraftingCPUCluster implements IAECluster, IBaseMonitor { + private final HashMap, Object> listeners = new HashMap, Object>(); + + protected Iterator, Object>> getListeners() + { + return listeners.entrySet().iterator(); + } + + protected void postChange(IAEItemStack diff, BaseActionSource src) + { + Iterator, Object>> i = getListeners(); + while (i.hasNext()) + { + Entry, Object> o = i.next(); + IMEMonitorHandlerReceiver recv = o.getKey(); + if ( recv.isValid( o.getValue() ) ) + recv.postChange( null, diff, src ); + else + i.remove(); + } + } + + /** + * add a new Listener to the monitor, be sure to properly remove yourself when your done. + */ + @Override + public void addListener(IMEMonitorHandlerReceiver l, Object verificationToken) + { + listeners.put( l, verificationToken ); + } + + /** + * remove a Listener to the monitor. + */ + @Override + public void removeListener(IMEMonitorHandlerReceiver l) + { + listeners.remove( l ); + } + public WorldCoord min; public WorldCoord max; public boolean isDestroyed = false; @@ -58,6 +100,49 @@ public class CraftingCPUCluster implements IAECluster private LinkedList storage = new LinkedList(); private LinkedList status = new LinkedList(); + public void getListOfItem(IItemList list, CraftingItemList whichList) + { + switch (whichList) + { + case ACTIVE: + for (IAEItemStack ais : waitingFor) + list.add( ais ); + break; + case PENDING: + for (Entry t : tasks.entrySet()) + { + for (IAEItemStack ais : t.getKey().getCondencedOutputs()) + { + ais = ais.copy(); + ais.setStackSize( ais.getStackSize() * t.getValue().value ); + list.add( ais ); + } + } + break; + case STORAGE: + inventory.getAvailableItems( list ); + break; + default: + case ALL: + inventory.getAvailableItems( list ); + + for (IAEItemStack ais : waitingFor) + list.add( ais ); + + for (Entry t : tasks.entrySet()) + { + for (IAEItemStack ais : t.getKey().getCondencedOutputs()) + { + ais = ais.copy(); + ais.setStackSize( ais.getStackSize() * t.getValue().value ); + list.add( ais ); + } + } + break; + + } + } + /** * crafting job info */ @@ -485,4 +570,43 @@ public class CraftingCPUCluster implements IAECluster return !tasks.isEmpty() || !waitingFor.isEmpty(); } + + public IAEItemStack getItemStack(IAEItemStack what, CraftingItemList storage2) + { + IAEItemStack is = null; + switch (storage2) + { + case STORAGE: + is = inventory.getItemList().findPrecise( what ); + break; + case ACTIVE: + is = waitingFor.findPrecise( what ); + break; + case PENDING: + + is = what.copy(); + is.setStackSize( 0 ); + + for (Entry t : tasks.entrySet()) + { + for (IAEItemStack ais : t.getKey().getCondencedOutputs()) + { + if ( ais.equals( is ) ) + is.setStackSize( is.getStackSize() + ais.getStackSize() * t.getValue().value ); + } + } + + break; + default: + case ALL: + throw new RuntimeException( "Invalid Operation" ); + } + + if ( is != null ) + return is.copy(); + + is = what.copy(); + is.setStackSize( 0 ); + return is; + } }