2014-06-16 07:48:43 +02:00
|
|
|
package appeng.crafting;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
2014-06-17 05:36:35 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.world.World;
|
2014-06-22 09:00:38 +02:00
|
|
|
import net.minecraft.world.WorldServer;
|
2014-06-17 05:36:35 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-07-03 06:48:18 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingGrid;
|
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-28 22:18:36 +02:00
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-06-22 09:00:38 +02:00
|
|
|
import appeng.container.ContainerNull;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
2014-06-22 09:00:38 +02:00
|
|
|
import appeng.util.Platform;
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
public class CraftingTreeProcess
|
|
|
|
{
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
World world;
|
2014-06-16 07:48:43 +02:00
|
|
|
CraftingTreeNode parent;
|
|
|
|
ICraftingPatternDetails details;
|
|
|
|
CraftingJob job;
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
long crafts = 0;
|
|
|
|
boolean damageable;
|
2014-06-22 09:00:38 +02:00
|
|
|
boolean fullsimulation;
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
private long bytes = 0;
|
2014-06-16 07:48:43 +02:00
|
|
|
final private int depth;
|
|
|
|
|
|
|
|
Map<CraftingTreeNode, Long> nodes = new HashMap();
|
|
|
|
public boolean possible = true;
|
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
public CraftingTreeProcess(ICraftingGrid cc, CraftingJob job, ICraftingPatternDetails details, CraftingTreeNode craftingTreeNode, int depth, World world) {
|
2014-06-16 07:48:43 +02:00
|
|
|
parent = craftingTreeNode;
|
|
|
|
this.details = details;
|
|
|
|
this.job = job;
|
|
|
|
this.depth = depth;
|
2014-06-22 09:00:38 +02:00
|
|
|
world = job.getWorld();
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
if ( details.isCraftable() )
|
|
|
|
{
|
|
|
|
IAEItemStack list[] = details.getInputs();
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
|
|
|
IAEItemStack[] is = details.getInputs();
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
ic.setInventorySlotContents( x, is[x] == null ? null : is[x].getItemStack() );
|
|
|
|
|
|
|
|
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) world ), details.getOutput( ic, world ), ic );
|
|
|
|
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack g = ic.getStackInSlot( x );
|
|
|
|
if ( g != null && g.stackSize > 1 )
|
|
|
|
fullsimulation = true;
|
|
|
|
}
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
for (int x = 0; x < list.length; x++)
|
|
|
|
{
|
|
|
|
IAEItemStack part = list[x];
|
|
|
|
if ( part != null )
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
ItemStack g = part.getItemStack();
|
|
|
|
|
|
|
|
if ( g.getItem().hasContainerItem( g ) )
|
2014-06-17 05:36:35 +02:00
|
|
|
damageable = true;
|
2014-06-22 09:00:38 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (IAEItemStack part : details.getCondencedInputs())
|
|
|
|
{
|
|
|
|
nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, -1, depth + 1 ), part.getStackSize() );
|
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean notRecurive(ICraftingPatternDetails details)
|
|
|
|
{
|
|
|
|
return parent.notRecurive( details );
|
|
|
|
}
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
long getTimes(long remaining, long stackSize)
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
if ( damageable || fullsimulation )
|
2014-06-17 05:36:35 +02:00
|
|
|
return 1;
|
|
|
|
return (remaining / stackSize) + (remaining % stackSize != 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
IAEItemStack getAmountCrafted(IAEItemStack what2)
|
|
|
|
{
|
|
|
|
for (IAEItemStack is : details.getCondencedOutputs())
|
|
|
|
{
|
|
|
|
if ( is.equals( what2 ) )
|
|
|
|
{
|
|
|
|
what2 = what2.copy();
|
|
|
|
what2.setStackSize( is.getStackSize() );
|
|
|
|
return what2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException( "Crafting Tree construction failed." );
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
public void request(MECraftingInventory inv, long i, BaseActionSource src) throws CraftBranchFailure, InterruptedException
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-06-23 06:50:56 +02:00
|
|
|
job.handlepausing();
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
if ( fullsimulation )
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
for (Entry<CraftingTreeNode, Long> entry : nodes.entrySet())
|
2014-06-17 05:36:35 +02:00
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
IAEItemStack item = entry.getKey().getStack( entry.getValue() );
|
|
|
|
IAEItemStack stack = entry.getKey().request( inv, item.getStackSize(), src );
|
|
|
|
|
|
|
|
ic.setInventorySlotContents( entry.getKey().slot, stack.getItemStack() );
|
|
|
|
}
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) world ), details.getOutput( ic, world ), ic );
|
|
|
|
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack is = ic.getStackInSlot( x );
|
|
|
|
is = Platform.getContainerItem( is );
|
|
|
|
|
|
|
|
IAEItemStack o = AEApi.instance().storage().createItemStack( is );
|
|
|
|
if ( o != null )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
|
|
|
bytes++;
|
2014-06-22 09:00:38 +02:00
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
2014-06-22 09:00:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// request and remove inputs...
|
|
|
|
for (Entry<CraftingTreeNode, Long> entry : nodes.entrySet())
|
|
|
|
{
|
|
|
|
IAEItemStack item = entry.getKey().getStack( entry.getValue() );
|
|
|
|
IAEItemStack stack = entry.getKey().request( inv, item.getStackSize() * i, src );
|
|
|
|
|
|
|
|
if ( damageable )
|
|
|
|
{
|
|
|
|
ItemStack is = Platform.getContainerItem( stack.getItemStack() );
|
2014-06-17 05:36:35 +02:00
|
|
|
IAEItemStack o = AEApi.instance().storage().createItemStack( is );
|
|
|
|
if ( o != null )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
|
|
|
bytes++;
|
2014-06-17 05:36:35 +02:00
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
2014-06-17 05:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// assume its possible.
|
|
|
|
|
|
|
|
// add crafting results..
|
|
|
|
for (IAEItemStack out : details.getCondencedOutputs())
|
|
|
|
{
|
|
|
|
IAEItemStack o = out.copy();
|
|
|
|
o.setStackSize( o.getStackSize() * i );
|
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
|
|
|
}
|
|
|
|
|
|
|
|
crafts += i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dive(CraftingJob job)
|
|
|
|
{
|
|
|
|
job.addTask( getAmountCrafted( parent.getStack( 1 ) ), crafts, details, depth );
|
|
|
|
for (CraftingTreeNode pro : nodes.keySet())
|
|
|
|
pro.dive( job );
|
2014-06-23 06:50:56 +02:00
|
|
|
|
|
|
|
job.addBytes( 8 + crafts + bytes );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
2014-06-18 08:23:37 +02:00
|
|
|
|
|
|
|
public void setSimulate()
|
|
|
|
{
|
|
|
|
crafts = 0;
|
2014-06-23 06:50:56 +02:00
|
|
|
bytes = 0;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
|
|
|
for (CraftingTreeNode pro : nodes.keySet())
|
|
|
|
pro.setSimulate();
|
|
|
|
}
|
2014-06-19 08:28:45 +02:00
|
|
|
|
|
|
|
public void setJob(MECraftingInventory storage, CraftingCPUCluster craftingCPUCluster, BaseActionSource src) throws CraftBranchFailure
|
|
|
|
{
|
|
|
|
craftingCPUCluster.addCrafting( details, crafts );
|
|
|
|
|
|
|
|
for (CraftingTreeNode pro : nodes.keySet())
|
|
|
|
pro.setJob( storage, craftingCPUCluster, src );
|
|
|
|
}
|
2014-06-28 22:18:36 +02:00
|
|
|
|
|
|
|
public void getPlan(IItemList<IAEItemStack> plan)
|
|
|
|
{
|
|
|
|
for (IAEItemStack i : details.getOutputs())
|
|
|
|
{
|
|
|
|
i = i.copy();
|
|
|
|
i.setCountRequestable( i.getStackSize() * crafts );
|
|
|
|
plan.addRequestable( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CraftingTreeNode pro : nodes.keySet())
|
|
|
|
pro.getPlan( plan );
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|