diff --git a/container/implementations/ContainerUpgradeable.java b/container/implementations/ContainerUpgradeable.java index 6e5712900..4b57902c9 100644 --- a/container/implementations/ContainerUpgradeable.java +++ b/container/implementations/ContainerUpgradeable.java @@ -143,7 +143,7 @@ public class ContainerUpgradeable extends AEBaseContainer implements IOptionalSl @GuiSync(1) public FuzzyMode fzMode = FuzzyMode.IGNORE_ALL; - @GuiSync(2) + @GuiSync(5) public YesNo cMode = YesNo.NO; public void checkToolbox() diff --git a/crafting/CraftingLink.java b/crafting/CraftingLink.java index 7729655d0..70cc9af89 100644 --- a/crafting/CraftingLink.java +++ b/crafting/CraftingLink.java @@ -1,6 +1,7 @@ package appeng.crafting; import net.minecraft.nbt.NBTTagCompound; +import appeng.api.config.Actionable; import appeng.api.networking.crafting.ICraftingCPU; import appeng.api.networking.crafting.ICraftingLink; import appeng.api.networking.crafting.ICraftingRequester; @@ -130,12 +131,12 @@ public class CraftingLink implements ICraftingLink return standalone; } - public IAEItemStack injectItems(IAEItemStack input) + public IAEItemStack injectItems(IAEItemStack input, Actionable mode) { if ( tie == null || tie.req == null || tie.req.req == null ) return input; - return tie.req.req.injectCratedItems( tie.req, input ); + return tie.req.req.injectCratedItems( tie.req, input, mode ); } public void markDone() diff --git a/helpers/DualityInterface.java b/helpers/DualityInterface.java index 3a9841d55..5e1e0135e 100644 --- a/helpers/DualityInterface.java +++ b/helpers/DualityInterface.java @@ -576,6 +576,9 @@ public class DualityInterface implements IGridTickable, ISegmentedInventory, ISt @Override public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) { + if ( !gridProxy.isActive() ) + return TickRateModulation.SLEEP; + if ( hasItemsToSend() ) pushItemsOut( EnumSet.allOf( ForgeDirection.class ) ); diff --git a/helpers/MultiCraftingTracker.java b/helpers/MultiCraftingTracker.java new file mode 100644 index 000000000..ce03980b8 --- /dev/null +++ b/helpers/MultiCraftingTracker.java @@ -0,0 +1,187 @@ +package appeng.helpers; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import appeng.api.AEApi; +import appeng.api.networking.IGrid; +import appeng.api.networking.crafting.ICraftingGrid; +import appeng.api.networking.crafting.ICraftingJob; +import appeng.api.networking.crafting.ICraftingLink; +import appeng.api.networking.crafting.ICraftingRequester; +import appeng.api.networking.security.BaseActionSource; +import appeng.api.storage.data.IAEItemStack; +import appeng.me.GridAccessException; +import appeng.parts.automation.NonNullArrayIterator; +import appeng.util.InventoryAdaptor; + +import com.google.common.collect.ImmutableSet; + +public class MultiCraftingTracker +{ + + final int size; + final ICraftingRequester owner; + + Future[] jobs = null; + ICraftingLink[] links = null; + + public MultiCraftingTracker(ICraftingRequester o, int size) { + owner = o; + this.size = size; + } + + public void readFromNBT(NBTTagCompound extra) + { + for (int x = 0; x < size; x++) + { + NBTTagCompound link = extra.getCompoundTag( "links-" + x ); + if ( link != null && !link.hasNoTags() ) + setLink( x, AEApi.instance().storage().loadCraftingLink( link, owner ) ); + } + } + + public void writeToNBT(NBTTagCompound extra) + { + for (int x = 0; x < size; x++) + { + ICraftingLink link = getLink( x ); + if ( link != null ) + { + NBTTagCompound ln = new NBTTagCompound(); + link.writeToNBT( ln ); + extra.setTag( "links-" + x, ln ); + } + } + } + + public boolean handleCrafting(int x, long itemToCraft, IAEItemStack ais, InventoryAdaptor d, World w, IGrid g, ICraftingGrid cg, BaseActionSource mySrc) + throws GridAccessException + { + if ( ais != null && d.simulateAdd( ais.getItemStack() ) == null ) + { + Future cjob = getJob( x ); + if ( getLink( x ) != null ) + { + return false; + } + else if ( cjob != null ) + { + ICraftingJob job = null; + try + { + if ( cjob.isDone() ) + job = cjob.get(); + else if ( cjob.isCancelled() ) + job = null; + + if ( job != null ) + { + setJob( x, null ); + setLink( x, cg.submitJob( job, owner, null, mySrc ) ); + return true; + } + } + catch (InterruptedException e) + { + // :P + } + catch (ExecutionException e) + { + // :P + } + } + else + { + if ( getLink( x ) == null ) + { + IAEItemStack aisC = ais.copy(); + aisC.setStackSize( itemToCraft ); + setJob( x, cg.beginCraftingJob( w, g, mySrc, aisC, null ) ); + } + } + } + return false; + } + + ICraftingLink getLink(int slot) + { + if ( links == null ) + return null; + + return links[slot]; + } + + void setLink(int slot, ICraftingLink l) + { + if ( links == null ) + links = new ICraftingLink[size]; + + links[slot] = l; + + boolean hasStuff = false; + for (int x = 0; x < links.length; x++) + { + ICraftingLink g = links[x]; + + if ( g == null || g.isCanceled() || g.isDone() ) + links[x] = null; + else + hasStuff = true; + } + + if ( hasStuff == false ) + links = null; + } + + Future getJob(int slot) + { + if ( jobs == null ) + return null; + + return jobs[slot]; + } + + void setJob(int slot, Future l) + { + if ( jobs == null ) + jobs = new Future[size]; + + jobs[slot] = l; + + boolean hasStuff = false; + for (int x = 0; x < jobs.length; x++) + { + if ( jobs[x] != null ) + hasStuff = true; + } + + if ( hasStuff == false ) + jobs = null; + } + + public ImmutableSet getRequestedJobs() + { + if ( links == null ) + return ImmutableSet.of(); + + return ImmutableSet.copyOf( new NonNullArrayIterator( links ) ); + } + + public void jobStateChange(ICraftingLink link) + { + if ( links != null ) + { + for (int x = 0; x < links.length; x++) + { + if ( links[x] == link ) + { + setLink( x, null ); + return; + } + } + } + } +} diff --git a/me/cache/CraftingGridCache.java b/me/cache/CraftingGridCache.java index 9039c8e59..1cb49bede 100644 --- a/me/cache/CraftingGridCache.java +++ b/me/cache/CraftingGridCache.java @@ -289,7 +289,6 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper public IItemList getAvailableItems(IItemList out) { // add craftable items! - // for (ICraftingPatternDetails details : craftingMethods.keySet()) for (IAEItemStack st : craftableItems.keySet()) out.addCrafting( st ); diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index d4be11f62..ddd34e78f 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -269,7 +269,41 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src) { - if ( input instanceof IAEItemStack && type == Actionable.MODULATE ) + if ( input instanceof IAEItemStack && type == Actionable.SIMULATE && false )// causes crafting to lock up? + { + IAEItemStack what = (IAEItemStack) input.copy(); + IAEItemStack is = waitingFor.findPrecise( what ); + + if ( is != null && is.getStackSize() > 0 ) + { + if ( is.getStackSize() >= what.getStackSize() ) + { + if ( finalOutput.equals( what ) ) + { + if ( myLastLink != null ) + return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) what, type ); + + return what; // ignore it. + } + + return null; + } + + if ( finalOutput.equals( what ) ) + { + if ( myLastLink != null ) + return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) what, type ); + + return what; // ignore it. + } + + IAEItemStack leftOver = what.copy(); + leftOver.decStackSize( is.getStackSize() ); + + return leftOver; + } + } + else if ( input instanceof IAEItemStack && type == Actionable.MODULATE ) { IAEItemStack what = (IAEItemStack) input; IAEItemStack is = waitingFor.findPrecise( what ); @@ -296,7 +330,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU completeJob(); if ( myLastLink != null ) - return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) input ); + return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) input, type ); return input; // ignore it. } @@ -309,10 +343,6 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU insert.setStackSize( is.getStackSize() ); what.decStackSize( is.getStackSize() ); - // AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : " + - // (is.getStackSize() + getRemainingTasks()) - // + " total left : waiting: " + (waiting ? "yes" : "no") ); - is.setStackSize( 0 ); if ( finalOutput.equals( input ) ) @@ -322,7 +352,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU completeJob(); if ( myLastLink != null ) - return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) input ); + return ((CraftingLink) myLastLink).injectItems( (IAEItemStack) input, type ); return input; // ignore it. } @@ -358,7 +388,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU if ( myLastLink != null ) ((CraftingLink) myLastLink).markDone(); - AELog.info( "marking job as complete" ); + AELog.crafting( "marking job as complete" ); isComplete = true; } @@ -497,7 +527,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU if ( is != null && details.isValidItemForSlot( x, is, getWorld() ) ) { - postChange( input[x], machineSrc ); + postChange( AEItemStack.create( is ), machineSrc ); ic.setInventorySlotContents( x, is ); found = true; break; @@ -561,7 +591,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU { IAEItemStack cItem = AEItemStack.create( output ); postChange( cItem, machineSrc ); - waitingFor.add( AEItemStack.create( cItem ) ); + waitingFor.add( cItem ); } } } diff --git a/parts/automation/PartExportBus.java b/parts/automation/PartExportBus.java index d88f633af..e2c8e8555 100644 --- a/parts/automation/PartExportBus.java +++ b/parts/automation/PartExportBus.java @@ -1,14 +1,10 @@ package appeng.parts.automation; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; - import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.Vec3; -import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.config.FuzzyMode; import appeng.api.config.PowerMultiplier; @@ -18,7 +14,6 @@ import appeng.api.config.Upgrades; import appeng.api.config.YesNo; import appeng.api.networking.IGridNode; import appeng.api.networking.crafting.ICraftingGrid; -import appeng.api.networking.crafting.ICraftingJob; import appeng.api.networking.crafting.ICraftingLink; import appeng.api.networking.crafting.ICraftingRequester; import appeng.api.networking.energy.IEnergyGrid; @@ -36,6 +31,7 @@ import appeng.client.texture.CableBusTextures; import appeng.core.AELog; import appeng.core.settings.TickRates; import appeng.core.sync.GuiBridge; +import appeng.helpers.MultiCraftingTracker; import appeng.me.GridAccessException; import appeng.util.InventoryAdaptor; import appeng.util.Platform; @@ -50,11 +46,9 @@ import cpw.mods.fml.relauncher.SideOnly; public class PartExportBus extends PartSharedItemBus implements IGridTickable, ICraftingRequester { + MultiCraftingTracker cratingTracker = new MultiCraftingTracker( this, 9 ); BaseActionSource mySrc; - Future calculatingJob = null; - ICraftingLink[] links = null; - public PartExportBus(ItemStack is) { super( PartExportBus.class, is ); settings.registerSetting( Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE ); @@ -67,30 +61,14 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I public void readFromNBT(NBTTagCompound extra) { super.readFromNBT( extra ); - - for (int x = 0; x < 9; x++) - { - NBTTagCompound link = extra.getCompoundTag( "links-" + x ); - if ( link != null && !link.hasNoTags() ) - setLink( x, AEApi.instance().storage().loadCraftingLink( link, this ) ); - } + cratingTracker.readFromNBT( extra ); } @Override public void writeToNBT(NBTTagCompound extra) { super.writeToNBT( extra ); - - for (int x = 0; x < 9; x++) - { - ICraftingLink link = getLink( x ); - if ( link != null ) - { - NBTTagCompound ln = new NBTTagCompound(); - link.writeToNBT( ln ); - extra.setTag( "links-" + x, ln ); - } - } + cratingTracker.writeToNBT( extra ); } @Override @@ -205,6 +183,7 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I InventoryAdaptor d = getHandler(); IMEMonitor inv = proxy.getStorage().getItemInventory(); IEnergyGrid energy = proxy.getEnergy(); + ICraftingGrid cg = proxy.getCrafting(); FuzzyMode fzMode = (FuzzyMode) getConfigManager().getSetting( Settings.FUZZY_MODE ); if ( d != null ) @@ -214,7 +193,9 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I IAEItemStack ais = config.getAEStackInSlot( x ); if ( ais == null || itemToSend <= 0 || craftOnly() ) { - handleCrafting( x, ais, d ); + if ( isCraftingEnabled() ) + didSomething = cratingTracker.handleCrafting( x, itemToSend, ais, d, getTile().getWorldObj(), proxy.getGrid(), cg, mySrc ) + || didSomething; continue; } @@ -232,8 +213,9 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I else pushItemIntoTarget( d, energy, inv, ais ); - if ( itemToSend == before ) - handleCrafting( x, ais, d ); + if ( itemToSend == before && isCraftingEnabled() ) + didSomething = cratingTracker.handleCrafting( x, itemToSend, ais, d, getTile().getWorldObj(), proxy.getGrid(), cg, mySrc ) + || didSomething; } } @@ -246,54 +228,6 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I return didSomething ? TickRateModulation.FASTER : TickRateModulation.SLOWER; } - private void handleCrafting(int x, IAEItemStack ais, InventoryAdaptor d) throws GridAccessException - { - if ( isCraftingEnabled() && ais != null && d.simulateAdd( ais.getItemStack() ) == null ) - { - ICraftingGrid cg = proxy.getCrafting(); - - if ( getLink( x ) != null ) - { - return; - } - else if ( calculatingJob != null ) - { - ICraftingJob job = null; - try - { - if ( calculatingJob.isDone() ) - job = calculatingJob.get(); - else if ( calculatingJob.isCancelled() ) - calculatingJob = null; - - if ( job != null ) - { - calculatingJob = null; - setLink( x, cg.submitJob( job, this, null, mySrc ) ); - didSomething = true; - } - } - catch (InterruptedException e) - { - // :P - } - catch (ExecutionException e) - { - // :P - } - } - else - { - if ( getLink( x ) == null ) - { - IAEItemStack aisC = ais.copy(); - aisC.setStackSize( itemToSend ); - calculatingJob = cg.beginCraftingJob( getTile().getWorldObj(), proxy.getGrid(), mySrc, aisC, null ); - } - } - } - } - private boolean craftOnly() { return getConfigManager().getSetting( Settings.CRAFT_ONLY ) == YesNo.YES; @@ -335,6 +269,34 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I } } + @Override + public IAEItemStack injectCratedItems(ICraftingLink link, IAEItemStack items, Actionable mode) + { + InventoryAdaptor d = getHandler(); + + try + { + if ( proxy.isActive() ) + { + IEnergyGrid energy = proxy.getEnergy(); + + double power = items.getStackSize(); + if ( energy.extractAEPower( power, mode, PowerMultiplier.CONFIG ) > power - 0.01 ) + { + if ( mode == Actionable.MODULATE ) + return AEItemStack.create( d.addItems( items.getItemStack() ) ); + return AEItemStack.create( d.simulateAdd( items.getItemStack() ) ); + } + } + } + catch (GridAccessException e) + { + AELog.error( e ); + } + + return items; + } + @Override public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) { @@ -358,84 +320,16 @@ public class PartExportBus extends PartSharedItemBus implements IGridTickable, I return new TickingRequest( TickRates.ExportBus.min, TickRates.ExportBus.max, isSleeping(), false ); } - ICraftingLink getLink(int slot) - { - if ( links == null ) - return null; - - return links[slot]; - } - - void setLink(int slot, ICraftingLink l) - { - if ( links == null ) - links = new ICraftingLink[9]; - - links[slot] = l; - - boolean hasStuff = false; - for (int x = 0; x < links.length; x++) - { - ICraftingLink g = links[x]; - - if ( g == null || g.isCanceled() || g.isDone() ) - links[x] = null; - else - hasStuff = true; - } - - if ( hasStuff == false ) - links = null; - } - @Override public ImmutableSet getRequestedJobs() { - if ( links == null ) - return ImmutableSet.of(); - - return ImmutableSet.copyOf( new NonNullArrayIterator( links ) ); - } - - @Override - public IAEItemStack injectCratedItems(ICraftingLink link, IAEItemStack items) - { - InventoryAdaptor d = getHandler(); - - try - { - if ( proxy.isActive() ) - { - IEnergyGrid energy = proxy.getEnergy(); - - double power = items.getStackSize(); - if ( energy.extractAEPower( power, Actionable.MODULATE, PowerMultiplier.CONFIG ) > power - 0.01 ) - { - return AEItemStack.create( d.addItems( items.getItemStack() ) ); - } - } - } - catch (GridAccessException e) - { - AELog.error( e ); - } - - return items; + return cratingTracker.getRequestedJobs(); } @Override public void jobStateChange(ICraftingLink link) { - if ( links != null ) - { - for (int x = 0; x < links.length; x++) - { - if ( links[x] == link ) - { - setLink( x, null ); - return; - } - } - } + cratingTracker.jobStateChange( link ); } + }