Crafting API Added
Fixed tons of issues with CPU multi block weirdness.
This commit is contained in:
+60
-11
@@ -4,16 +4,25 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridCache;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.IGridStorage;
|
||||
import appeng.api.networking.crafting.ICraftingCPU;
|
||||
import appeng.api.networking.crafting.ICraftingCallback;
|
||||
import appeng.api.networking.crafting.ICraftingGrid;
|
||||
import appeng.api.networking.crafting.ICraftingJob;
|
||||
import appeng.api.networking.crafting.ICraftingMedium;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.crafting.ICraftingProvider;
|
||||
@@ -36,10 +45,11 @@ import appeng.me.cluster.implementations.CraftingCPUCluster;
|
||||
import appeng.tile.crafting.TileCraftingStorageTile;
|
||||
import appeng.tile.crafting.TileCraftingTile;
|
||||
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler
|
||||
public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler
|
||||
{
|
||||
|
||||
HashSet<CraftingCPUCluster> cpuClusters = new HashSet();
|
||||
@@ -50,11 +60,11 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
IEnergyGrid eg;
|
||||
|
||||
HashMap<ICraftingPatternDetails, List<ICraftingMedium>> craftingMethods = new HashMap();
|
||||
HashMap<IAEItemStack, Set<ICraftingPatternDetails>> craftableItems = new HashMap();
|
||||
HashMap<IAEItemStack, ImmutableSet<ICraftingPatternDetails>> craftableItems = new HashMap();
|
||||
|
||||
boolean updateList = false;
|
||||
|
||||
public CraftingCache(IGrid g) {
|
||||
public CraftingGridCache(IGrid g) {
|
||||
grid = g;
|
||||
}
|
||||
|
||||
@@ -159,22 +169,32 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
for (ICraftingProvider cp : providers)
|
||||
cp.provideCrafting( this );
|
||||
|
||||
HashMap<IAEItemStack, Set<ICraftingPatternDetails>> tmpCraft = new HashMap();
|
||||
|
||||
// new craftables!
|
||||
for (ICraftingPatternDetails details : craftingMethods.keySet())
|
||||
{
|
||||
for (IAEItemStack out : details.getOutputs())
|
||||
{
|
||||
out = out.copy();
|
||||
out.reset();
|
||||
out.setCraftable( true );
|
||||
|
||||
Set<ICraftingPatternDetails> methods = craftableItems.get( out );
|
||||
Set<ICraftingPatternDetails> methods = tmpCraft.get( out );
|
||||
|
||||
if ( methods == null )
|
||||
craftableItems.put( out, methods = new TreeSet() );
|
||||
tmpCraft.put( out, methods = new TreeSet() );
|
||||
|
||||
methods.add( details );
|
||||
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
|
||||
}
|
||||
}
|
||||
|
||||
// make them immutable
|
||||
for (Entry<IAEItemStack, Set<ICraftingPatternDetails>> e : tmpCraft.entrySet())
|
||||
{
|
||||
craftableItems.put( e.getKey(), ImmutableSet.copyOf( e.getValue() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -263,11 +283,16 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean submitJob(CraftingJob job, CraftingCPUCluster target, BaseActionSource src)
|
||||
public boolean submitJob(ICraftingJob job, ICraftingCPU target, BaseActionSource src)
|
||||
{
|
||||
if ( job.isSimulation() )
|
||||
return false;
|
||||
|
||||
CraftingCPUCluster cpuClust = null;
|
||||
|
||||
if ( target instanceof CraftingCPUCluster )
|
||||
cpuClust = (CraftingCPUCluster) target;
|
||||
|
||||
if ( target == null )
|
||||
{
|
||||
// TODO real stuff...
|
||||
@@ -275,13 +300,13 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
{
|
||||
if ( !cpu.isBusy() )
|
||||
{
|
||||
target = cpu;
|
||||
cpuClust = cpu;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( target != null && target.submitJob( grid, job, src ) )
|
||||
if ( cpuClust != null && cpuClust.submitJob( grid, job, src ) )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -293,9 +318,9 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Set<ICraftingPatternDetails> getCraftingFor(IAEItemStack what)
|
||||
public ImmutableCollection<ICraftingPatternDetails> getCraftingFor(IAEItemStack what)
|
||||
{
|
||||
Set<ICraftingPatternDetails> res = craftableItems.get( what );
|
||||
ImmutableSet<ICraftingPatternDetails> res = craftableItems.get( what );
|
||||
if ( res == null )
|
||||
return ImmutableSet.of();
|
||||
return res;
|
||||
@@ -317,4 +342,28 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
|
||||
return i == 1;
|
||||
}
|
||||
|
||||
final public static ExecutorService craftingPool;
|
||||
|
||||
static
|
||||
{
|
||||
ThreadFactory factory = new ThreadFactory() {
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable ar)
|
||||
{
|
||||
return new Thread( ar, "AE Crafting Calculator" );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
craftingPool = Executors.newCachedThreadPool( factory );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<ICraftingJob> beginCraftingJob(World world, IGrid grid, BaseActionSource actionSrc, IAEItemStack slotItem, ICraftingCallback cb)
|
||||
{
|
||||
CraftingJob cj = new CraftingJob( world, grid, actionSrc, slotItem, cb );
|
||||
return craftingPool.submit( cj, (ICraftingJob) cj );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,13 +21,14 @@ import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.crafting.CraftingItemList;
|
||||
import appeng.api.networking.crafting.ICraftingCPU;
|
||||
import appeng.api.networking.crafting.ICraftingJob;
|
||||
import appeng.api.networking.crafting.ICraftingMedium;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.events.MENetworkCraftingCpuChange;
|
||||
import appeng.api.networking.security.BaseActionSource;
|
||||
import appeng.api.networking.security.MachineSource;
|
||||
import appeng.api.networking.storage.IBaseMonitor;
|
||||
import appeng.api.networking.storage.IStorageGrid;
|
||||
import appeng.api.storage.IMEInventory;
|
||||
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
||||
@@ -40,14 +41,14 @@ import appeng.core.AELog;
|
||||
import appeng.crafting.CraftBranchFailure;
|
||||
import appeng.crafting.CraftingJob;
|
||||
import appeng.crafting.MECraftingInventory;
|
||||
import appeng.me.cache.CraftingCache;
|
||||
import appeng.me.cache.CraftingGridCache;
|
||||
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, IBaseMonitor<IAEItemStack>
|
||||
public class CraftingCPUCluster implements IAECluster, ICraftingCPU
|
||||
{
|
||||
|
||||
class TaskProgress
|
||||
@@ -73,6 +74,8 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
private LinkedList<TileCraftingTile> storage = new LinkedList<TileCraftingTile>();
|
||||
private LinkedList<TileCraftingTile> status = new LinkedList<TileCraftingTile>();
|
||||
|
||||
long availableStorage = 0;
|
||||
|
||||
MachineSource machineSrc = null;
|
||||
|
||||
int accelerator = 0;
|
||||
@@ -182,9 +185,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
public void updateStatus(boolean updateGrid)
|
||||
{
|
||||
for (TileCraftingTile r : tiles)
|
||||
{
|
||||
r.updateMeta();
|
||||
}
|
||||
r.updateMeta( true );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -213,6 +214,18 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getAvailableStorage()
|
||||
{
|
||||
return availableStorage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCoProcessors()
|
||||
{
|
||||
return accelerator;
|
||||
}
|
||||
|
||||
public void addTile(TileCraftingTile te)
|
||||
{
|
||||
if ( machineSrc == null || te.isCoreBlock )
|
||||
@@ -223,7 +236,10 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
tiles.add( te );
|
||||
|
||||
if ( te.isStorage() )
|
||||
{
|
||||
availableStorage += te.getStorageBytes();
|
||||
storage.add( te );
|
||||
}
|
||||
else if ( te.isStatus() )
|
||||
status.add( te );
|
||||
else if ( te.isAccelerator() )
|
||||
@@ -373,7 +389,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
storeItems(); // marks dirty
|
||||
}
|
||||
|
||||
public void updateCraftingLogic(IGrid grid, IEnergyGrid eg, CraftingCache cc)
|
||||
public void updateCraftingLogic(IGrid grid, IEnergyGrid eg, CraftingGridCache cc)
|
||||
{
|
||||
if ( isComplete )
|
||||
{
|
||||
@@ -566,11 +582,14 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean submitJob(IGrid g, CraftingJob job, BaseActionSource src)
|
||||
public boolean submitJob(IGrid g, ICraftingJob job, BaseActionSource src)
|
||||
{
|
||||
if ( !tasks.isEmpty() || !waitingFor.isEmpty() )
|
||||
return false;
|
||||
|
||||
if ( !(job instanceof CraftingJob) )
|
||||
return false;
|
||||
|
||||
IStorageGrid sg = g.getCache( IStorageGrid.class );
|
||||
IMEInventory<IAEItemStack> storage = sg.getItemInventory();
|
||||
MECraftingInventory ci = new MECraftingInventory( storage, true, false, false );
|
||||
@@ -578,7 +597,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
try
|
||||
{
|
||||
waitingFor.resetStatus();
|
||||
job.tree.setJob( ci, this, src );
|
||||
((CraftingJob) job).tree.setJob( ci, this, src );
|
||||
ci.commit( src );
|
||||
finalOutput = job.getOutput();
|
||||
waiting = false;
|
||||
@@ -621,6 +640,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
i.value += crafts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBusy()
|
||||
{
|
||||
Iterator<Entry<ICraftingPatternDetails, TaskProgress>> i = tasks.entrySet().iterator();
|
||||
@@ -768,6 +788,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseActionSource getActionSource()
|
||||
{
|
||||
return machineSrc;
|
||||
|
||||
Reference in New Issue
Block a user