diff --git a/src/main/java/appeng/helpers/MultiCraftingTracker.java b/src/main/java/appeng/helpers/MultiCraftingTracker.java index 7483246a..c3ad5d3c 100644 --- a/src/main/java/appeng/helpers/MultiCraftingTracker.java +++ b/src/main/java/appeng/helpers/MultiCraftingTracker.java @@ -35,18 +35,18 @@ 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.parts.automation.NonNullArrayIterator; import appeng.util.InventoryAdaptor; public class MultiCraftingTracker { - final int size; - final ICraftingRequester owner; + private final int size; + private final ICraftingRequester owner; - Future[] jobs = null; - ICraftingLink[] links = null; + private Future[] jobs = null; + private ICraftingLink[] links = null; + private int failedCraftingAttempts = 0; public MultiCraftingTracker( ICraftingRequester o, int size ) { @@ -54,11 +54,17 @@ public class MultiCraftingTracker this.size = size; } + public int getFailedCraftingAttempts() + { + return failedCraftingAttempts; + } + public void readFromNBT( NBTTagCompound extra ) { for( int x = 0; x < this.size; x++ ) { - NBTTagCompound link = extra.getCompoundTag( "links-" + x ); + final NBTTagCompound link = extra.getCompoundTag( "links-" + x ); + if( link != null && !link.hasNoTags() ) { this.setLink( x, AEApi.instance().storage().loadCraftingLink( link, this.owner ) ); @@ -66,65 +72,27 @@ public class MultiCraftingTracker } } - void setLink( int slot, ICraftingLink l ) - { - if( this.links == null ) - { - this.links = new ICraftingLink[this.size]; - } - - this.links[slot] = l; - - boolean hasStuff = false; - for( int x = 0; x < this.links.length; x++ ) - { - ICraftingLink g = this.links[x]; - - if( g == null || g.isCanceled() || g.isDone() ) - { - this.links[x] = null; - } - else - { - hasStuff = true; - } - } - - if( !hasStuff ) - { - this.links = null; - } - } - public void writeToNBT( NBTTagCompound extra ) { for( int x = 0; x < this.size; x++ ) { - ICraftingLink link = this.getLink( x ); + final ICraftingLink link = this.getLink( x ); + if( link != null ) { - NBTTagCompound ln = new NBTTagCompound(); + final NBTTagCompound ln = new NBTTagCompound(); link.writeToNBT( ln ); extra.setTag( "links-" + x, ln ); } } } - ICraftingLink getLink( int slot ) - { - if( this.links == null ) - { - return null; - } - - return this.links[slot]; - } - public boolean handleCrafting( int x, long itemToCraft, IAEItemStack ais, InventoryAdaptor d, World w, IGrid g, ICraftingGrid cg, BaseActionSource mySrc ) { if( ais != null && d.simulateAdd( ais.getItemStack() ) == null ) { - Future craftingJob = this.getJob( x ); + final Future craftingJob = this.getJob( x ); + if( this.getLink( x ) != null ) { return false; @@ -132,6 +100,7 @@ public class MultiCraftingTracker else if( craftingJob != null ) { ICraftingJob job = null; + try { if( craftingJob.isDone() ) @@ -141,8 +110,20 @@ public class MultiCraftingTracker if( job != null ) { + final ICraftingLink link = cg.submitJob( job, this.owner, null, false, mySrc ); + this.setJob( x, null ); - this.setLink( x, cg.submitJob( job, this.owner, null, false, mySrc ) ); + + if( link != null ) + { + this.failedCraftingAttempts = Math.max( 0, this.failedCraftingAttempts - 1 ); + + this.setLink( x, link ); + } + else + { + this.failedCraftingAttempts = Math.min( 10, this.failedCraftingAttempts + 1 ); + } return true; } } @@ -159,8 +140,9 @@ public class MultiCraftingTracker { if( this.getLink( x ) == null ) { - IAEItemStack aisC = ais.copy(); + final IAEItemStack aisC = ais.copy(); aisC.setStackSize( itemToCraft ); + this.setJob( x, cg.beginCraftingJob( w, g, mySrc, aisC, null ) ); } } @@ -168,40 +150,6 @@ public class MultiCraftingTracker return false; } - Future getJob( int slot ) - { - if( this.jobs == null ) - { - return null; - } - - return this.jobs[slot]; - } - - void setJob( int slot, Future l ) - { - if( this.jobs == null ) - { - this.jobs = new Future[this.size]; - } - - this.jobs[slot] = l; - - boolean hasStuff = false; - for( Future job : this.jobs ) - { - if( job != null ) - { - hasStuff = true; - } - } - - if( !hasStuff ) - { - this.jobs = null; - } - } - public ImmutableSet getRequestedJobs() { if( this.links == null ) @@ -209,7 +157,7 @@ public class MultiCraftingTracker return ImmutableSet.of(); } - return ImmutableSet.copyOf( new NonNullArrayIterator( this.links ) ); + return ImmutableSet.copyOf( new NonNullArrayIterator( this.links ) ); } public void jobStateChange( ICraftingLink link ) @@ -276,4 +224,79 @@ public class MultiCraftingTracker { return this.getLink( slot ) != null || this.getJob( slot ) != null; } + + private ICraftingLink getLink( int slot ) + { + if( this.links == null ) + { + return null; + } + + return this.links[slot]; + } + + private void setLink( int slot, ICraftingLink l ) + { + if( this.links == null ) + { + this.links = new ICraftingLink[this.size]; + } + + this.links[slot] = l; + + boolean hasStuff = false; + for( int x = 0; x < this.links.length; x++ ) + { + final ICraftingLink g = this.links[x]; + + if( g == null || g.isCanceled() || g.isDone() ) + { + this.links[x] = null; + } + else + { + hasStuff = true; + } + } + + if( !hasStuff ) + { + this.links = null; + } + } + + private Future getJob( int slot ) + { + if( this.jobs == null ) + { + return null; + } + + return this.jobs[slot]; + } + + private void setJob( int slot, Future l ) + { + if( this.jobs == null ) + { + this.jobs = new Future[this.size]; + } + + this.jobs[slot] = l; + + boolean hasStuff = false; + + for( Future job : this.jobs ) + { + if( job != null ) + { + hasStuff = true; + } + } + + if( !hasStuff ) + { + this.jobs = null; + } + } } diff --git a/src/main/java/appeng/parts/automation/NonNullArrayIterator.java b/src/main/java/appeng/helpers/NonNullArrayIterator.java similarity index 92% rename from src/main/java/appeng/parts/automation/NonNullArrayIterator.java rename to src/main/java/appeng/helpers/NonNullArrayIterator.java index 975c1312..3eb05b53 100644 --- a/src/main/java/appeng/parts/automation/NonNullArrayIterator.java +++ b/src/main/java/appeng/helpers/NonNullArrayIterator.java @@ -16,7 +16,7 @@ * along with Applied Energistics 2. If not, see . */ -package appeng.parts.automation; +package appeng.helpers; import java.util.Iterator; @@ -27,8 +27,8 @@ import scala.NotImplementedError; public class NonNullArrayIterator implements Iterator { - final E[] g; - int offset = 0; + private final E[] g; + private int offset = 0; public NonNullArrayIterator( E[] o ) { @@ -49,8 +49,9 @@ public class NonNullArrayIterator implements Iterator @Override public E next() { - E result = this.g[this.offset]; + final E result = this.g[this.offset]; this.offset++; + return result; } diff --git a/src/main/java/appeng/parts/automation/PartExportBus.java b/src/main/java/appeng/parts/automation/PartExportBus.java index e6ee4976..b4285364 100644 --- a/src/main/java/appeng/parts/automation/PartExportBus.java +++ b/src/main/java/appeng/parts/automation/PartExportBus.java @@ -176,6 +176,13 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest // :P } + final int failedAttempts = this.craftingTracker.getFailedCraftingAttempts(); + + if( this.isCraftingEnabled() && failedAttempts > 0 ) + { + return this.getFailedCraftingPenalty( failedAttempts ); + } + return this.didSomething ? TickRateModulation.FASTER : TickRateModulation.SLOWER; } @@ -381,4 +388,18 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest this.nextSlot = ( this.nextSlot + x ) % this.availableSlots(); } } + + private TickRateModulation getFailedCraftingPenalty( int failedAttempts ) + { + if( failedAttempts > 5 ) + { + return TickRateModulation.SLOWER; + } + else if( failedAttempts > 1 ) + { + return TickRateModulation.SAME; + } + + return TickRateModulation.FASTER; + } }