Separated Crafting job into a worker thread.
Crafting calculation can be canceled. Hooked up a crafting calculation launch and the confirmation GUI.
This commit is contained in:
+93
-32
@@ -8,6 +8,7 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.security.BaseActionSource;
|
||||
import appeng.api.networking.storage.IStorageGrid;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
@@ -17,7 +18,7 @@ import appeng.util.Platform;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
|
||||
public class CraftingJob
|
||||
public class CraftingJob implements Runnable
|
||||
{
|
||||
|
||||
IAEItemStack output;
|
||||
@@ -28,10 +29,17 @@ public class CraftingJob
|
||||
|
||||
ICraftingHost jobHost;
|
||||
|
||||
boolean simulate = false;
|
||||
final MECraftingInventory original;
|
||||
|
||||
public CraftingTreeNode tree;
|
||||
private BaseActionSource actionSrc;
|
||||
|
||||
public CraftingJob(ICraftingHost host, NBTTagCompound data) {
|
||||
jobHost = host;
|
||||
storage = AEApi.instance().storage().createItemList();
|
||||
prophecies = new HashSet();
|
||||
original = null;
|
||||
}
|
||||
|
||||
public CraftingJob(ICraftingHost host, IAEItemStack what, Actionable mode) {
|
||||
@@ -39,41 +47,13 @@ public class CraftingJob
|
||||
output = what.copy();
|
||||
storage = AEApi.instance().storage().createItemList();
|
||||
prophecies = new HashSet();
|
||||
actionSrc = host.getActionSrc();
|
||||
|
||||
CraftingCache cc = host.getGrid().getCache( CraftingCache.class );
|
||||
IStorageGrid sg = host.getGrid().getCache( IStorageGrid.class );
|
||||
original = new MECraftingInventory( sg.getItemInventory(), false, false, false );
|
||||
|
||||
IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
|
||||
|
||||
MECraftingInventory meci = new MECraftingInventory( sg.getItemInventory(), true, false, true );
|
||||
meci.ignore( what );
|
||||
|
||||
CraftingTreeNode tree = getCraftingTree( cc, what );
|
||||
|
||||
try
|
||||
{
|
||||
Stopwatch timer = Stopwatch.createStarted();
|
||||
tree.request( meci, what.getStackSize(), host.getActionSrc() );
|
||||
tree.dive( this );
|
||||
|
||||
for (String s : opsAndMultiplier.keySet())
|
||||
{
|
||||
twoIntegers ti = opsAndMultiplier.get( s );
|
||||
AELog.info( s + " * " + ti.times + " = " + (ti.perOp * ti.times) );
|
||||
}
|
||||
|
||||
AELog.info( "-------------" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
|
||||
// if ( mode == Actionable.MODULATE )
|
||||
// meci.moveItemsToStorage( storage );
|
||||
}
|
||||
catch (CraftBranchFailure e)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
catch (CraftingCalculationFailure f)
|
||||
{
|
||||
AELog.error( f );
|
||||
}
|
||||
tree = getCraftingTree( cc, what );
|
||||
}
|
||||
|
||||
private CraftingTreeNode getCraftingTree(CraftingCache cc, IAEItemStack what)
|
||||
@@ -86,16 +66,25 @@ public class CraftingJob
|
||||
|
||||
}
|
||||
|
||||
IItemList<IAEItemStack> crafting = AEApi.instance().storage().createItemList();
|
||||
IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
|
||||
|
||||
public void addTask(IAEItemStack what, long crafts, ICraftingPatternDetails details, int depth)
|
||||
{
|
||||
if ( crafts > 0 )
|
||||
{
|
||||
postOp( "new task: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize(), what.getStackSize(), crafts );
|
||||
what = what.copy();
|
||||
what.setStackSize( what.getStackSize() * crafts );
|
||||
crafting.add( what );
|
||||
}
|
||||
}
|
||||
|
||||
public void addMissing(IAEItemStack what)
|
||||
{
|
||||
what = what.copy();
|
||||
missing.add( what );
|
||||
|
||||
postOp( "required material: " + Platform.getItemDisplayName( what ), 1, what.getStackSize() );
|
||||
}
|
||||
|
||||
@@ -117,4 +106,76 @@ public class CraftingJob
|
||||
ti.perOp = stackSize;
|
||||
ti.times += crafts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
Stopwatch timer = Stopwatch.createStarted();
|
||||
|
||||
MECraftingInventory meci = new MECraftingInventory( original, true, false, true );
|
||||
meci.ignore( output );
|
||||
|
||||
tree.request( meci, output.getStackSize(), actionSrc );
|
||||
tree.dive( this );
|
||||
|
||||
for (String s : opsAndMultiplier.keySet())
|
||||
{
|
||||
twoIntegers ti = opsAndMultiplier.get( s );
|
||||
AELog.info( s + " * " + ti.times + " = " + (ti.perOp * ti.times) );
|
||||
}
|
||||
|
||||
AELog.info( "------------- real" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
|
||||
// if ( mode == Actionable.MODULATE )
|
||||
// meci.moveItemsToStorage( storage );
|
||||
}
|
||||
catch (CraftBranchFailure e)
|
||||
{
|
||||
simulate = true;
|
||||
|
||||
try
|
||||
{
|
||||
Stopwatch timer = Stopwatch.createStarted();
|
||||
MECraftingInventory meci = new MECraftingInventory( original, true, false, true );
|
||||
meci.ignore( output );
|
||||
|
||||
tree.setSimulate();
|
||||
tree.request( meci, output.getStackSize(), actionSrc );
|
||||
tree.dive( this );
|
||||
|
||||
for (String s : opsAndMultiplier.keySet())
|
||||
{
|
||||
twoIntegers ti = opsAndMultiplier.get( s );
|
||||
AELog.info( s + " * " + ti.times + " = " + (ti.perOp * ti.times) );
|
||||
}
|
||||
|
||||
AELog.info( "------------- simulate" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
|
||||
}
|
||||
catch (CraftBranchFailure e1)
|
||||
{
|
||||
AELog.error( e1 );
|
||||
}
|
||||
catch (CraftingCalculationFailure f)
|
||||
{
|
||||
AELog.error( f );
|
||||
}
|
||||
catch (InterruptedException e1)
|
||||
{
|
||||
AELog.info( "Crafting calculation canceled." );
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (CraftingCalculationFailure f)
|
||||
{
|
||||
AELog.error( f );
|
||||
}
|
||||
catch (InterruptedException e1)
|
||||
{
|
||||
AELog.info( "Crafting calculation canceled." );
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user