2014-06-16 07:48:43 +02:00
|
|
|
package appeng.crafting;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
import net.minecraft.world.World;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-06-17 05:36:35 +02:00
|
|
|
import appeng.api.config.FuzzyMode;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.me.cache.CraftingCache;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
public class CraftingTreeNode
|
|
|
|
{
|
|
|
|
|
|
|
|
// parent node.
|
|
|
|
private CraftingTreeProcess parent;
|
2014-06-17 05:36:35 +02:00
|
|
|
private World world;
|
|
|
|
|
|
|
|
// what slot!
|
|
|
|
int slot;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
// what item is this?
|
|
|
|
private IAEItemStack what;
|
|
|
|
|
|
|
|
// what are the crafting patterns for this?
|
|
|
|
private ArrayList<CraftingTreeProcess> nodes = new ArrayList();
|
|
|
|
|
|
|
|
boolean cannotUse = false;
|
2014-06-17 05:36:35 +02:00
|
|
|
long missing = 0;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
IItemList<IAEItemStack> used = AEApi.instance().storage().createItemList();
|
|
|
|
boolean exhausted = false;
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
boolean sim;
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
public CraftingTreeNode(CraftingCache cc, CraftingJob job, IAEItemStack wat, CraftingTreeProcess par, int slot, int depth) {
|
2014-06-16 07:48:43 +02:00
|
|
|
what = wat;
|
|
|
|
parent = par;
|
2014-06-17 05:36:35 +02:00
|
|
|
this.slot = slot;
|
|
|
|
this.world = job.jobHost.getWorld();
|
2014-06-18 08:23:37 +02:00
|
|
|
sim = false;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
for (ICraftingPatternDetails details : cc.getCraftingFor( what ))// in order.
|
|
|
|
{
|
|
|
|
if ( notRecurive( details ) )
|
2014-06-17 05:36:35 +02:00
|
|
|
nodes.add( new CraftingTreeProcess( cc, job, details, this, depth + 1, world ) );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IAEItemStack getStack(long size)
|
|
|
|
{
|
|
|
|
IAEItemStack is = what.copy();
|
|
|
|
is.setStackSize( size );
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean notRecurive(ICraftingPatternDetails details)
|
|
|
|
{
|
|
|
|
IAEItemStack[] o = details.getOutputs();
|
|
|
|
for (IAEItemStack i : o)
|
|
|
|
if ( i.equals( what ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( parent == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return parent.notRecurive( details );
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
public IAEItemStack request(MECraftingInventory inv, long l, BaseActionSource src) throws CraftBranchFailure, InterruptedException
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-06-18 08:23:37 +02:00
|
|
|
if ( Thread.interrupted() )
|
|
|
|
throw new InterruptedException();
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
what.setStackSize( l );
|
2014-06-17 05:36:35 +02:00
|
|
|
if ( slot >= 0 && parent != null && parent.details.isCraftable() )
|
|
|
|
{
|
|
|
|
for (IAEItemStack fuzz : inv.getItemList().findFuzzy( what, FuzzyMode.IGNORE_ALL ))
|
|
|
|
{
|
|
|
|
if ( parent.details.isValidItemForSlot( slot, fuzz.getItemStack(), world ) )
|
|
|
|
{
|
|
|
|
fuzz = fuzz.copy();
|
|
|
|
fuzz.setStackSize( l );
|
|
|
|
IAEItemStack available = inv.extractItems( fuzz, Actionable.MODULATE, src );
|
|
|
|
|
|
|
|
if ( available != null )
|
|
|
|
{
|
2014-06-19 08:28:45 +02:00
|
|
|
if ( !exhausted )
|
|
|
|
used.add( available );
|
2014-06-17 05:36:35 +02:00
|
|
|
l -= available.getStackSize();
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
if ( l == 0 )
|
|
|
|
return available;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src );
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
if ( available != null )
|
|
|
|
{
|
2014-06-19 08:28:45 +02:00
|
|
|
if ( !exhausted )
|
|
|
|
used.add( available );
|
2014-06-17 05:36:35 +02:00
|
|
|
l -= available.getStackSize();
|
|
|
|
|
|
|
|
if ( l == 0 )
|
|
|
|
return available;
|
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
exhausted = true;
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( nodes.size() == 1 )
|
|
|
|
{
|
|
|
|
CraftingTreeProcess pro = nodes.get( 0 );
|
|
|
|
|
|
|
|
while (pro.possible && l > 0)
|
|
|
|
{
|
2014-06-17 05:36:35 +02:00
|
|
|
pro.request( inv, pro.getTimes( l, pro.getAmountCrafted( what ).getStackSize() ), src );
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
what.setStackSize( l );
|
2014-06-17 05:36:35 +02:00
|
|
|
IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src );
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
if ( available != null )
|
|
|
|
{
|
|
|
|
l -= available.getStackSize();
|
|
|
|
|
|
|
|
if ( l <= 0 )
|
2014-06-17 05:36:35 +02:00
|
|
|
return available;
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pro.possible = false; // ;P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( nodes.size() > 1 )
|
|
|
|
{
|
|
|
|
for (CraftingTreeProcess pro : nodes)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (pro.possible && l > 0)
|
|
|
|
{
|
|
|
|
MECraftingInventory subInv = new MECraftingInventory( inv );
|
|
|
|
pro.request( subInv, 1, src );
|
|
|
|
subInv.commit( src );
|
|
|
|
|
|
|
|
what.setStackSize( l );
|
2014-06-17 05:36:35 +02:00
|
|
|
IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src );
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
if ( available != null )
|
|
|
|
{
|
|
|
|
l -= available.getStackSize();
|
|
|
|
|
|
|
|
if ( l <= 0 )
|
2014-06-17 05:36:35 +02:00
|
|
|
return available;
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pro.possible = false; // ;P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (CraftBranchFailure fail)
|
|
|
|
{
|
|
|
|
pro.possible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
if ( sim )
|
|
|
|
{
|
|
|
|
missing += l;
|
|
|
|
return what;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new CraftBranchFailure( what, l );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void dive(CraftingJob job)
|
|
|
|
{
|
|
|
|
if ( missing > 0 )
|
|
|
|
job.addMissing( getStack( missing ) );
|
|
|
|
missing = 0;
|
|
|
|
|
|
|
|
for (CraftingTreeProcess pro : nodes)
|
|
|
|
pro.dive( job );
|
|
|
|
}
|
2014-06-18 08:23:37 +02:00
|
|
|
|
|
|
|
public void setSimulate()
|
|
|
|
{
|
|
|
|
sim = true;
|
|
|
|
missing = 0;
|
2014-06-19 08:28:45 +02:00
|
|
|
used.resetStatus();
|
|
|
|
exhausted = false;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
|
|
|
for (CraftingTreeProcess pro : nodes)
|
|
|
|
pro.setSimulate();
|
|
|
|
}
|
2014-06-19 08:28:45 +02:00
|
|
|
|
|
|
|
public void setJob(MECraftingInventory storage, CraftingCPUCluster craftingCPUCluster, BaseActionSource src) throws CraftBranchFailure
|
|
|
|
{
|
|
|
|
for (IAEItemStack i : used)
|
|
|
|
{
|
|
|
|
IAEItemStack ex = storage.extractItems( i, Actionable.MODULATE, src );
|
|
|
|
|
|
|
|
if ( ex == null || ex.getStackSize() != i.getStackSize() )
|
|
|
|
throw new CraftBranchFailure( i, i.getStackSize() );
|
|
|
|
|
|
|
|
craftingCPUCluster.addStorage( ex );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CraftingTreeProcess pro : nodes)
|
|
|
|
pro.setJob( storage, craftingCPUCluster, src );
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|