Crafting is virtually working 100%
This commit is contained in:
@@ -9,6 +9,7 @@ import java.util.Map.Entry;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
@@ -33,7 +34,9 @@ import appeng.crafting.MECraftingInventory;
|
||||
import appeng.me.cache.CraftingCache;
|
||||
import appeng.me.cluster.IAECluster;
|
||||
import appeng.tile.crafting.TileCraftingTile;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
|
||||
public class CraftingCPUCluster implements IAECluster
|
||||
{
|
||||
@@ -41,6 +44,7 @@ public class CraftingCPUCluster implements IAECluster
|
||||
public WorldCoord min;
|
||||
public WorldCoord max;
|
||||
public boolean isDestroyed = false;
|
||||
private boolean isComplete = true;
|
||||
|
||||
class TaskProgress
|
||||
{
|
||||
@@ -60,6 +64,7 @@ public class CraftingCPUCluster implements IAECluster
|
||||
MECraftingInventory inventory = new MECraftingInventory();
|
||||
|
||||
IAEItemStack finalOutput;
|
||||
BaseActionSource mySrc;
|
||||
|
||||
boolean waiting = false;
|
||||
Map<ICraftingPatternDetails, TaskProgress> tasks = new HashMap<ICraftingPatternDetails, TaskProgress>();
|
||||
@@ -149,11 +154,19 @@ public class CraftingCPUCluster implements IAECluster
|
||||
{
|
||||
is.decStackSize( input.getStackSize() );
|
||||
|
||||
AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : "
|
||||
+ (is.getStackSize() + getRemainingTasks()) + " total left : waiting: " + (waiting ? "yes" : "no") );
|
||||
// AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() +
|
||||
// " remaining : "
|
||||
// + (is.getStackSize() + getRemainingTasks()) + " total left : waiting: " + (waiting ? "yes" :
|
||||
// "no") );
|
||||
|
||||
if ( finalOutput.equals( input ) )
|
||||
{
|
||||
finalOutput.decStackSize( input.getStackSize() );
|
||||
if ( finalOutput.getStackSize() <= 0 )
|
||||
completeJob();
|
||||
|
||||
return input; // ignore it.
|
||||
}
|
||||
|
||||
// 2000
|
||||
return inventory.injectItems( what, type, src );
|
||||
@@ -163,13 +176,20 @@ public class CraftingCPUCluster implements IAECluster
|
||||
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") );
|
||||
// AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : " +
|
||||
// (is.getStackSize() + getRemainingTasks())
|
||||
// + " total left : waiting: " + (waiting ? "yes" : "no") );
|
||||
|
||||
is.setStackSize( 0 );
|
||||
|
||||
if ( finalOutput.equals( input ) )
|
||||
{
|
||||
finalOutput.decStackSize( input.getStackSize() );
|
||||
if ( finalOutput.getStackSize() <= 0 )
|
||||
completeJob();
|
||||
|
||||
return input; // ignore it.
|
||||
}
|
||||
|
||||
inventory.injectItems( insert, type, src );
|
||||
|
||||
@@ -180,6 +200,20 @@ public class CraftingCPUCluster implements IAECluster
|
||||
return input;
|
||||
}
|
||||
|
||||
public IGrid getGrid()
|
||||
{
|
||||
for (TileCraftingTile r : tiles)
|
||||
return r.getActionableNode().getGrid();
|
||||
|
||||
throw new RuntimeException( "No tiles inside a cpu cluster." );
|
||||
}
|
||||
|
||||
private void completeJob()
|
||||
{
|
||||
AELog.info( "marking job as complete" );
|
||||
isComplete = true;
|
||||
}
|
||||
|
||||
private int getRemainingTasks()
|
||||
{
|
||||
int o = 0;
|
||||
@@ -188,19 +222,72 @@ public class CraftingCPUCluster implements IAECluster
|
||||
return o;
|
||||
}
|
||||
|
||||
private boolean canCraft(IAEItemStack[] condencedInputs)
|
||||
private boolean canCraft(ICraftingPatternDetails details, IAEItemStack[] condencedInputs)
|
||||
{
|
||||
for (IAEItemStack is : condencedInputs)
|
||||
for (IAEItemStack g : condencedInputs)
|
||||
{
|
||||
IAEItemStack avail = inventory.extractItems( is, Actionable.SIMULATE, null );
|
||||
if ( avail == null || avail.getStackSize() < is.getStackSize() )
|
||||
return false;
|
||||
|
||||
if ( details.isCraftable() )
|
||||
{
|
||||
boolean found = false;
|
||||
|
||||
for (IAEItemStack fuzz : inventory.getItemList().findFuzzy( g, FuzzyMode.IGNORE_ALL ))
|
||||
{
|
||||
fuzz = fuzz.copy();
|
||||
fuzz.setStackSize( g.getStackSize() );
|
||||
IAEItemStack ais = inventory.extractItems( fuzz, Actionable.SIMULATE, null );
|
||||
ItemStack is = ais == null ? null : ais.getItemStack();
|
||||
|
||||
if ( is != null && is.stackSize == g.getStackSize() )
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !found )
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
IAEItemStack ais = inventory.extractItems( g.copy(), Actionable.SIMULATE, null );
|
||||
ItemStack is = ais == null ? null : ais.getItemStack();
|
||||
|
||||
if ( is == null || is.stackSize < g.getStackSize() )
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void updateCraftingLogic(IGrid grid, CraftingCache cc)
|
||||
{
|
||||
if ( isComplete )
|
||||
{
|
||||
if ( inventory.getItemList().isEmpty() )
|
||||
return;
|
||||
|
||||
IStorageGrid sg = grid.getCache( IStorageGrid.class );
|
||||
IMEInventory<IAEItemStack> ii = sg.getItemInventory();
|
||||
|
||||
for (IAEItemStack is : inventory.getItemList())
|
||||
{
|
||||
is = inventory.extractItems( is.copy(), Actionable.MODULATE, mySrc );
|
||||
|
||||
if ( is != null )
|
||||
is = ii.injectItems( is, Actionable.MODULATE, mySrc );
|
||||
|
||||
if ( is != null )
|
||||
inventory.injectItems( is, Actionable.MODULATE, mySrc );
|
||||
}
|
||||
|
||||
if ( inventory.getItemList().isEmpty() )
|
||||
inventory = new MECraftingInventory();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
waiting = false;
|
||||
if ( waiting || tasks.isEmpty() ) // nothing to do here...
|
||||
return;
|
||||
@@ -214,7 +301,7 @@ public class CraftingCPUCluster implements IAECluster
|
||||
continue;
|
||||
|
||||
ICraftingPatternDetails details = e.getKey();
|
||||
if ( canCraft( details.getCondencedInputs() ) )
|
||||
if ( canCraft( details, details.getCondencedInputs() ) )
|
||||
{
|
||||
InventoryCrafting ic = null;
|
||||
|
||||
@@ -228,21 +315,22 @@ public class CraftingCPUCluster implements IAECluster
|
||||
if ( ic == null )
|
||||
{
|
||||
ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
||||
boolean found = false;
|
||||
|
||||
IAEItemStack[] input = details.getInputs();
|
||||
for (int x = 0; x < input.length; x++)
|
||||
{
|
||||
|
||||
if ( input[x] != null )
|
||||
{
|
||||
boolean found = true;
|
||||
|
||||
found = false;
|
||||
if ( details.isCraftable() )
|
||||
{
|
||||
for (IAEItemStack fuzz : inventory.getItemList().findFuzzy( input[x], FuzzyMode.IGNORE_ALL ))
|
||||
{
|
||||
fuzz = fuzz.copy();
|
||||
fuzz.setStackSize( input[x].getStackSize() );
|
||||
IAEItemStack ais = inventory.extractItems( fuzz, Actionable.MODULATE, null );
|
||||
IAEItemStack ais = inventory.extractItems( fuzz, Actionable.MODULATE, mySrc );
|
||||
ItemStack is = ais == null ? null : ais.getItemStack();
|
||||
|
||||
if ( is != null && details.isValidItemForSlot( x, is, getWorld() ) )
|
||||
@@ -255,7 +343,7 @@ public class CraftingCPUCluster implements IAECluster
|
||||
}
|
||||
else
|
||||
{
|
||||
IAEItemStack ais = inventory.extractItems( input[x].copy(), Actionable.MODULATE, null );
|
||||
IAEItemStack ais = inventory.extractItems( input[x].copy(), Actionable.MODULATE, mySrc );
|
||||
ItemStack is = ais == null ? null : ais.getItemStack();
|
||||
|
||||
if ( is != null )
|
||||
@@ -269,6 +357,20 @@ public class CraftingCPUCluster implements IAECluster
|
||||
if ( !found )
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( !found )
|
||||
{
|
||||
// put stuff back..
|
||||
for (int x = 0; x < ic.getSizeInventory(); x++)
|
||||
{
|
||||
ItemStack is = ic.getStackInSlot( x );
|
||||
if ( is != null )
|
||||
inventory.injectItems( AEItemStack.create( is ), Actionable.MODULATE, mySrc );
|
||||
}
|
||||
ic = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,6 +382,19 @@ public class CraftingCPUCluster implements IAECluster
|
||||
for (IAEItemStack out : details.getCondencedOutputs())
|
||||
waitingFor.add( out.copy() );
|
||||
|
||||
if ( details.isCraftable() )
|
||||
{
|
||||
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) getWorld() ),
|
||||
details.getOutput( ic, getWorld() ), ic );
|
||||
|
||||
for (int x = 0; x < ic.getSizeInventory(); x++)
|
||||
{
|
||||
ItemStack output = Platform.getContainerItem( ic.getStackInSlot( x ) );
|
||||
if ( output != null )
|
||||
waitingFor.add( AEItemStack.create( output ) );
|
||||
}
|
||||
}
|
||||
|
||||
ic = null; // hand off complete!
|
||||
|
||||
e.getValue().value--;
|
||||
@@ -299,7 +414,7 @@ public class CraftingCPUCluster implements IAECluster
|
||||
{
|
||||
ItemStack is = ic.getStackInSlot( x );
|
||||
if ( is != null )
|
||||
inventory.injectItems( AEItemStack.create( is ), Actionable.MODULATE, null );
|
||||
inventory.injectItems( AEItemStack.create( is ), Actionable.MODULATE, mySrc );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,13 +431,6 @@ public class CraftingCPUCluster implements IAECluster
|
||||
|
||||
public boolean submitJob(IGrid g, CraftingJob job, BaseActionSource src)
|
||||
{
|
||||
Iterator<Entry<ICraftingPatternDetails, TaskProgress>> i = tasks.entrySet().iterator();
|
||||
while (i.hasNext())
|
||||
{
|
||||
if ( i.next().getValue().value <= 0 )
|
||||
i.remove();
|
||||
}
|
||||
|
||||
if ( !tasks.isEmpty() || !waitingFor.isEmpty() )
|
||||
return false;
|
||||
|
||||
@@ -337,6 +445,8 @@ public class CraftingCPUCluster implements IAECluster
|
||||
ci.commit( src );
|
||||
finalOutput = job.getOutput();
|
||||
waiting = false;
|
||||
isComplete = false;
|
||||
mySrc = src;
|
||||
return true;
|
||||
}
|
||||
catch (CraftBranchFailure e)
|
||||
@@ -363,4 +473,16 @@ public class CraftingCPUCluster implements IAECluster
|
||||
|
||||
i.value += crafts;
|
||||
}
|
||||
|
||||
public boolean isBusy()
|
||||
{
|
||||
Iterator<Entry<ICraftingPatternDetails, TaskProgress>> i = tasks.entrySet().iterator();
|
||||
while (i.hasNext())
|
||||
{
|
||||
if ( i.next().getValue().value <= 0 )
|
||||
i.remove();
|
||||
}
|
||||
|
||||
return !tasks.isEmpty() || !waitingFor.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user