diff --git a/crafting/CraftingJob.java b/crafting/CraftingJob.java index ac51afff..5f8a38d1 100644 --- a/crafting/CraftingJob.java +++ b/crafting/CraftingJob.java @@ -4,10 +4,12 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.Set; import net.minecraft.nbt.NBTTagCompound; import appeng.api.AEApi; import appeng.api.config.Actionable; +import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.networking.storage.IStorageGrid; import appeng.api.storage.IMEInventory; import appeng.api.storage.data.IAEItemStack; @@ -47,33 +49,74 @@ public class CraftingJob implements ICraftingParent CraftingCache cc = host.getGrid().getCache( CraftingCache.class ); IStorageGrid sg = host.getGrid().getCache( IStorageGrid.class ); - - IItemList available = AEApi.instance().storage().createItemList(); IItemList missing = AEApi.instance().storage().createItemList(); - - calculateCrafting( cc, this, sg.getItemInventory().getAvailableItems( available ), missing, what, mode ); - - if ( ! missing.isEmpty() ) + + MECraftingInventory meci = new MECraftingInventory( sg.getItemInventory() ); + + calculateCrafting( cc, this, meci, missing, what, mode ); + + if ( !missing.isEmpty() ) { if ( mode == Actionable.MODULATE ) { IMEInventory netStorage = sg.getItemInventory(); - + Iterator i = storage.iterator(); - while ( i.hasNext() ) + while (i.hasNext()) { IAEItemStack item = i.next(); netStorage.injectItems( item, mode, host.getActionSrc() ); } } - + throw new CraftingMissingItemsException( missing ); } + + if ( mode == Actionable.MODULATE ) + meci.moveItemsToStorage( storage ); + } + + public void calculateCrafting(CraftingCache cc, ICraftingParent parent, IMEInventory inv, IItemList missing, IAEItemStack what, + Actionable mode) + { + Set patterns = cc.getCraftingFor( what ); + + for (ICraftingPatternDetails details : patterns) + { + IAEItemStack[] requirements = details.getCondencedInputs(); + if ( canMake( requirements, inv ) ) + { + extractItems( requirements, inv, storage, missing ); + return; + } + } + + for (ICraftingPatternDetails details : patterns) + { + IAEItemStack[] requirements = details.getCondencedInputs(); + extractItems( requirements, inv, storage, missing ); + return; + } } - public void calculateCrafting( CraftingCache cc, ICraftingParent parent, IItemList available, IItemList missing, IAEItemStack what, Actionable mode) { - + private void extractItems(IAEItemStack[] requirements, IMEInventory inv, IItemList storage2, IItemList missing) + { + for (IAEItemStack is : requirements) + { + inv.extractItems( is, Actionable.MODULATE, jobHost.getActionSrc() ); + } + } + + private boolean canMake(IAEItemStack[] requirements, IMEInventory inv) + { + + for (IAEItemStack is : requirements) + { + IAEItemStack avail = inv.extractItems( is, Actionable.SIMULATE, jobHost.getActionSrc() ); + } + + return true; } public Collection getBottom() diff --git a/crafting/MECraftingInventory.java b/crafting/MECraftingInventory.java new file mode 100644 index 00000000..69a15fc1 --- /dev/null +++ b/crafting/MECraftingInventory.java @@ -0,0 +1,91 @@ +package appeng.crafting; + +import appeng.api.AEApi; +import appeng.api.config.Actionable; +import appeng.api.networking.security.BaseActionSource; +import appeng.api.storage.IMEInventory; +import appeng.api.storage.StorageChannel; +import appeng.api.storage.data.IAEItemStack; +import appeng.api.storage.data.IItemList; + +public class MECraftingInventory implements IMEInventory +{ + + final IMEInventory target; + final IItemList localCache; + final IItemList extractedCache; + + public MECraftingInventory(IMEInventory target) { + this.target = target; + extractedCache = AEApi.instance().storage().createItemList(); + localCache = target.getAvailableItems( AEApi.instance().storage().createItemList() ); + } + + @Override + public IAEItemStack injectItems(IAEItemStack input, Actionable mode, BaseActionSource src) + { + if ( input == null ) + return null; + + if ( mode == Actionable.SIMULATE ) + localCache.add( input ); + + return null; + } + + @Override + public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src) + { + if ( request == null ) + return null; + + IAEItemStack list = localCache.findPrecise( request ); + if ( list == null || list.getStackSize() == 0 ) + return null; + + if ( mode == Actionable.MODULATE ) + extractedCache.add( request ); + + if ( list.getStackSize() >= request.getStackSize() ) + { + if ( mode == Actionable.MODULATE ) + list.decStackSize( request.getStackSize() ); + + return request; + } + + IAEItemStack ret = request.copy(); + ret.setStackSize( list.getStackSize() ); + if ( mode == Actionable.MODULATE ) + list.reset(); + + return ret; + } + + @Override + public IItemList getAvailableItems(IItemList out) + { + for (IAEItemStack is : localCache) + out.add( is ); + + return out; + } + + public IItemList getItemList() + { + return localCache; + } + + @Override + public StorageChannel getChannel() + { + return StorageChannel.ITEMS; + } + + public void moveItemsToStorage(IItemList storage) + { + // TODO Auto-generated method stub + + } + +} diff --git a/me/cache/CraftingCache.java b/me/cache/CraftingCache.java index 7378935f..98541547 100644 --- a/me/cache/CraftingCache.java +++ b/me/cache/CraftingCache.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.TreeSet; import appeng.api.config.AccessRestriction; import appeng.api.config.Actionable; @@ -40,6 +42,7 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell IGrid grid; HashMap> craftingMethods = new HashMap(); + HashMap> craftableItems = new HashMap(); boolean updateList = false; @@ -129,15 +132,15 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell IStorageGrid sg = grid.getCache( IStorageGrid.class ); // update the stuff that was in the list... - for (ICraftingPatternDetails details : craftingMethods.keySet()) - for (IAEItemStack out : details.getOutputs()) - { - out.reset(); - sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() ); - } + for (IAEItemStack out : craftableItems.keySet()) + { + out.reset(); + sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() ); + } // erase list. craftingMethods.clear(); + craftableItems.clear(); // re-create list.. for (ICraftingProvider cp : providers) @@ -149,6 +152,13 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell { out.reset(); out.setCraftable( true ); + + Set methods = craftableItems.get( out ); + + if ( methods == null ) + methods = craftableItems.put( out, new TreeSet() ); + + methods.add( details ); sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() ); } } @@ -195,9 +205,9 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell public IItemList getAvailableItems(IItemList out) { // add craftable items! - for (ICraftingPatternDetails details : craftingMethods.keySet()) - for (IAEItemStack st : details.getOutputs()) - out.addCrafting( st ); + // for (ICraftingPatternDetails details : craftingMethods.keySet()) + for (IAEItemStack st : craftableItems.keySet()) + out.addCrafting( st ); return out; } @@ -245,4 +255,9 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell return 0; } + public Set getCraftingFor(IAEItemStack what) + { + return craftableItems.get( what ); + } + }