Merge pull request #253 from thatsIch/craftinggridcache
Craftinggridcache
This commit is contained in:
commit
6d2f399cca
4 changed files with 197 additions and 146 deletions
330
src/main/java/appeng/me/cache/CraftingGridCache.java
vendored
330
src/main/java/appeng/me/cache/CraftingGridCache.java
vendored
|
@ -1,21 +1,13 @@
|
||||||
package appeng.me.cache;
|
package appeng.me.cache;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
|
import com.google.common.collect.*;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import appeng.api.config.AccessRestriction;
|
import appeng.api.config.AccessRestriction;
|
||||||
import appeng.api.config.Actionable;
|
import appeng.api.config.Actionable;
|
||||||
|
@ -58,66 +50,65 @@ import appeng.tile.crafting.TileCraftingStorageTile;
|
||||||
import appeng.tile.crafting.TileCraftingTile;
|
import appeng.tile.crafting.TileCraftingTile;
|
||||||
import appeng.util.ItemSorters;
|
import appeng.util.ItemSorters;
|
||||||
|
|
||||||
import com.google.common.collect.HashMultimap;
|
public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler<IAEStack>
|
||||||
import com.google.common.collect.ImmutableCollection;
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
import com.google.common.collect.SetMultimap;
|
|
||||||
|
|
||||||
public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler
|
|
||||||
{
|
{
|
||||||
|
|
||||||
final HashSet<CraftingCPUCluster> cpuClusters = new HashSet<CraftingCPUCluster>();
|
private final Set<CraftingCPUCluster> craftingCPUClusters = new HashSet<CraftingCPUCluster>();
|
||||||
final HashSet<ICraftingProvider> providers = new HashSet<ICraftingProvider>();
|
private final Set<ICraftingProvider> craftingProviders = new HashSet<ICraftingProvider>();
|
||||||
|
|
||||||
private final HashMap<IGridNode, ICraftingWatcher> watchers = new HashMap<IGridNode, ICraftingWatcher>();
|
private final Map<IGridNode, ICraftingWatcher> craftingWatchers = new HashMap<IGridNode, ICraftingWatcher>();
|
||||||
|
|
||||||
final IGrid grid;
|
private final IGrid grid;
|
||||||
IStorageGrid sg;
|
private IStorageGrid storageGrid;
|
||||||
IEnergyGrid eg;
|
private IEnergyGrid energyGrid;
|
||||||
|
|
||||||
final HashMap<ICraftingPatternDetails, List<ICraftingMedium>> craftingMethods = new HashMap<ICraftingPatternDetails, List<ICraftingMedium>>();
|
private final Map<ICraftingPatternDetails, List<ICraftingMedium>> craftingMethods = new HashMap<ICraftingPatternDetails, List<ICraftingMedium>>();
|
||||||
HashMap<IAEItemStack, ImmutableList<ICraftingPatternDetails>> craftableItems = new HashMap<IAEItemStack, ImmutableList<ICraftingPatternDetails>>();
|
private final Map<IAEItemStack, ImmutableList<ICraftingPatternDetails>> craftableItems = new HashMap<IAEItemStack, ImmutableList<ICraftingPatternDetails>>();
|
||||||
final HashSet<IAEItemStack> emitableItems = new HashSet<IAEItemStack>();
|
private final Set<IAEItemStack> emitableItems = new HashSet<IAEItemStack>();
|
||||||
final HashMap<String, CraftingLinkNexus> links = new HashMap<String, CraftingLinkNexus>();
|
private final Map<String, CraftingLinkNexus> craftingLinks = new HashMap<String, CraftingLinkNexus>();
|
||||||
|
|
||||||
boolean updateList = false;
|
private boolean updateList = false;
|
||||||
final private SetMultimap<IAEStack, CraftingWatcher> interests = HashMultimap.create();
|
private final Multimap<IAEStack, CraftingWatcher> interests = HashMultimap.create();
|
||||||
final public GenericInterestManager<CraftingWatcher> interestManager = new GenericInterestManager<CraftingWatcher>( interests );
|
public final GenericInterestManager<CraftingWatcher> interestManager = new GenericInterestManager<CraftingWatcher>( this.interests );
|
||||||
|
|
||||||
static class ActiveCpuIterator implements Iterator<ICraftingCPU>
|
static class ActiveCpuIterator implements Iterator<ICraftingCPU>
|
||||||
{
|
{
|
||||||
|
|
||||||
final Iterator<CraftingCPUCluster> i;
|
private final Iterator<CraftingCPUCluster> iterator;
|
||||||
CraftingCPUCluster c = null;
|
private CraftingCPUCluster cpuCluster;
|
||||||
|
|
||||||
public ActiveCpuIterator(Collection<CraftingCPUCluster> o)
|
public ActiveCpuIterator(Collection<CraftingCPUCluster> o)
|
||||||
{
|
{
|
||||||
i = o.iterator();
|
this.iterator = o.iterator();
|
||||||
|
this.cpuCluster = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext()
|
public boolean hasNext()
|
||||||
{
|
{
|
||||||
findNext();
|
findNext();
|
||||||
return c != null;
|
|
||||||
|
return this.cpuCluster != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findNext()
|
private void findNext()
|
||||||
{
|
{
|
||||||
while (i.hasNext() && c == null)
|
while (this.iterator.hasNext() && this.cpuCluster == null)
|
||||||
{
|
{
|
||||||
c = i.next();
|
this.cpuCluster = this.iterator.next();
|
||||||
if ( !c.isActive() || c.isDestroyed )
|
if ( !this.cpuCluster.isActive() || this.cpuCluster.isDestroyed )
|
||||||
c = null;
|
{
|
||||||
|
this.cpuCluster = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ICraftingCPU next()
|
public ICraftingCPU next()
|
||||||
{
|
{
|
||||||
ICraftingCPU o = c;
|
final ICraftingCPU o = this.cpuCluster;
|
||||||
c = null;
|
this.cpuCluster = null;
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,59 +123,67 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
@Override
|
@Override
|
||||||
public ImmutableSet<ICraftingCPU> getCpus()
|
public ImmutableSet<ICraftingCPU> getCpus()
|
||||||
{
|
{
|
||||||
return ImmutableSet.copyOf( new ActiveCpuIterator( cpuClusters ) );
|
return ImmutableSet.copyOf( new ActiveCpuIterator( this.craftingCPUClusters ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public CraftingGridCache(IGrid g)
|
public CraftingGridCache(IGrid grid)
|
||||||
{
|
{
|
||||||
grid = g;
|
this.grid = grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@MENetworkEventSubscribe
|
@MENetworkEventSubscribe
|
||||||
public void afterCacheConstruction(MENetworkPostCacheConstruction cc)
|
public void afterCacheConstruction(MENetworkPostCacheConstruction cacheConstruction)
|
||||||
{
|
{
|
||||||
sg = grid.getCache( IStorageGrid.class );
|
this.storageGrid = this.grid.getCache( IStorageGrid.class );
|
||||||
eg = grid.getCache( IEnergyGrid.class );
|
this.energyGrid = this.grid.getCache( IEnergyGrid.class );
|
||||||
|
|
||||||
sg.registerCellProvider( this );
|
this.storageGrid.registerCellProvider( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLink(CraftingLink l)
|
public void addLink(CraftingLink link)
|
||||||
|
{
|
||||||
|
if ( link.isStandalone() )
|
||||||
{
|
{
|
||||||
if ( l.isStandalone() )
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CraftingLinkNexus n = links.get( l.getCraftingID() );
|
CraftingLinkNexus nexus = this.craftingLinks.get( link.getCraftingID() );
|
||||||
if ( n == null )
|
if ( nexus == null )
|
||||||
links.put( l.getCraftingID(), n = new CraftingLinkNexus( l.getCraftingID() ) );
|
{
|
||||||
|
this.craftingLinks.put( link.getCraftingID(), nexus = new CraftingLinkNexus( link.getCraftingID() ) );
|
||||||
|
}
|
||||||
|
|
||||||
l.setNexus( n );
|
link.setNexus( nexus );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdateTick()
|
public void onUpdateTick()
|
||||||
{
|
{
|
||||||
if ( updateList )
|
if ( this.updateList )
|
||||||
{
|
{
|
||||||
updateList = false;
|
this.updateList = false;
|
||||||
updateCPUClusters();
|
this.updateCPUClusters();
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<CraftingLinkNexus> i = links.values().iterator();
|
Iterator<CraftingLinkNexus> craftingLinkIterator = this.craftingLinks.values().iterator();
|
||||||
while (i.hasNext())
|
while (craftingLinkIterator.hasNext())
|
||||||
{
|
{
|
||||||
if ( i.next().isDead( grid, this ) )
|
if ( craftingLinkIterator.next().isDead( this.grid, this ) )
|
||||||
i.remove();
|
{
|
||||||
|
craftingLinkIterator.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (CraftingCPUCluster cpu : cpuClusters)
|
for (CraftingCPUCluster cpu : this.craftingCPUClusters)
|
||||||
cpu.updateCraftingLogic( grid, eg, this );
|
{
|
||||||
|
cpu.updateCraftingLogic( this.grid, this.energyGrid, this );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MENetworkEventSubscribe
|
@MENetworkEventSubscribe
|
||||||
public void updateCPUClusters(MENetworkCraftingCpuChange c)
|
public void updateCPUClusters(MENetworkCraftingCpuChange c)
|
||||||
{
|
{
|
||||||
updateList = true;
|
this.updateList = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@MENetworkEventSubscribe
|
@MENetworkEventSubscribe
|
||||||
|
@ -198,31 +197,34 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
{
|
{
|
||||||
if ( machine instanceof ICraftingWatcherHost )
|
if ( machine instanceof ICraftingWatcherHost )
|
||||||
{
|
{
|
||||||
ICraftingWatcher myWatcher = watchers.get( machine );
|
ICraftingWatcher craftingWatcher = this.craftingWatchers.get( machine );
|
||||||
if ( myWatcher != null )
|
if ( craftingWatcher != null )
|
||||||
{
|
{
|
||||||
myWatcher.clear();
|
craftingWatcher.clear();
|
||||||
watchers.remove( machine );
|
this.craftingWatchers.remove( machine );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( machine instanceof ICraftingRequester )
|
if ( machine instanceof ICraftingRequester )
|
||||||
{
|
{
|
||||||
for (CraftingLinkNexus n : links.values())
|
for (CraftingLinkNexus link : this.craftingLinks.values())
|
||||||
{
|
{
|
||||||
if ( n.isMachine( machine ) )
|
if ( link.isMachine( machine ) )
|
||||||
{
|
{
|
||||||
n.removeNode();
|
link.removeNode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( machine instanceof TileCraftingTile )
|
if ( machine instanceof TileCraftingTile )
|
||||||
updateList = true;
|
{
|
||||||
|
this.updateList = true;
|
||||||
|
}
|
||||||
|
|
||||||
if ( machine instanceof ICraftingProvider )
|
if ( machine instanceof ICraftingProvider )
|
||||||
{
|
{
|
||||||
providers.remove( machine );
|
this.craftingProviders.remove( machine );
|
||||||
updatePatterns();
|
this.updatePatterns();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,89 +233,102 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
{
|
{
|
||||||
if ( machine instanceof ICraftingWatcherHost )
|
if ( machine instanceof ICraftingWatcherHost )
|
||||||
{
|
{
|
||||||
ICraftingWatcherHost swh = (ICraftingWatcherHost) machine;
|
ICraftingWatcherHost watcherHost = (ICraftingWatcherHost) machine;
|
||||||
CraftingWatcher iw = new CraftingWatcher( this, swh );
|
CraftingWatcher watcher = new CraftingWatcher( this, watcherHost );
|
||||||
watchers.put( gridNode, iw );
|
craftingWatchers.put( gridNode, watcher );
|
||||||
swh.updateWatcher( iw );
|
watcherHost.updateWatcher( watcher );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( machine instanceof ICraftingRequester )
|
if ( machine instanceof ICraftingRequester )
|
||||||
{
|
{
|
||||||
for (ICraftingLink l : ((ICraftingRequester) machine).getRequestedJobs())
|
for (ICraftingLink link : ((ICraftingRequester) machine).getRequestedJobs())
|
||||||
{
|
{
|
||||||
if ( l instanceof CraftingLink )
|
if ( link instanceof CraftingLink )
|
||||||
addLink( (CraftingLink) l );
|
{
|
||||||
|
this.addLink( (CraftingLink) link );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( machine instanceof TileCraftingTile )
|
if ( machine instanceof TileCraftingTile )
|
||||||
updateList = true;
|
{
|
||||||
|
this.updateList = true;
|
||||||
|
}
|
||||||
|
|
||||||
if ( machine instanceof ICraftingProvider )
|
if ( machine instanceof ICraftingProvider )
|
||||||
{
|
{
|
||||||
providers.add( (ICraftingProvider) machine );
|
this.craftingProviders.add( (ICraftingProvider) machine );
|
||||||
updatePatterns();
|
this.updatePatterns();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateCPUClusters()
|
private void updateCPUClusters()
|
||||||
{
|
{
|
||||||
cpuClusters.clear();
|
this.craftingCPUClusters.clear();
|
||||||
for (IGridNode cst : grid.getMachines( TileCraftingStorageTile.class ))
|
|
||||||
|
for (IGridNode cst : this.grid.getMachines( TileCraftingStorageTile.class ))
|
||||||
{
|
{
|
||||||
TileCraftingStorageTile tile = (TileCraftingStorageTile) cst.getMachine();
|
TileCraftingStorageTile tile = (TileCraftingStorageTile) cst.getMachine();
|
||||||
CraftingCPUCluster cluster = (CraftingCPUCluster) tile.getCluster();
|
CraftingCPUCluster cluster = (CraftingCPUCluster) tile.getCluster();
|
||||||
if ( cluster != null )
|
if ( cluster != null )
|
||||||
{
|
{
|
||||||
cpuClusters.add( cluster );
|
this.craftingCPUClusters.add( cluster );
|
||||||
|
|
||||||
if ( cluster.myLastLink != null )
|
if ( cluster.myLastLink != null )
|
||||||
|
{
|
||||||
addLink( (CraftingLink) cluster.myLastLink );
|
addLink( (CraftingLink) cluster.myLastLink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCraftingOption(ICraftingMedium medium, ICraftingPatternDetails api)
|
public void addCraftingOption(ICraftingMedium medium, ICraftingPatternDetails api)
|
||||||
{
|
{
|
||||||
List<ICraftingMedium> details = craftingMethods.get( api );
|
List<ICraftingMedium> details = this.craftingMethods.get( api );
|
||||||
if ( details == null )
|
if ( details == null )
|
||||||
{
|
{
|
||||||
details = new ArrayList<ICraftingMedium>();
|
details = new ArrayList<ICraftingMedium>();
|
||||||
details.add( medium );
|
details.add( medium );
|
||||||
craftingMethods.put( api, details );
|
this.craftingMethods.put( api, details );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
details.add( medium );
|
details.add( medium );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static final Comparator<ICraftingPatternDetails> comp = new Comparator<ICraftingPatternDetails>(){
|
static final Comparator<ICraftingPatternDetails> comp = new Comparator<ICraftingPatternDetails>()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
public int compare(ICraftingPatternDetails o1,
|
public int compare(ICraftingPatternDetails firstDetail, ICraftingPatternDetails nextDetail)
|
||||||
ICraftingPatternDetails o2) {
|
{
|
||||||
return o2.getPriority() - o1.getPriority();
|
return nextDetail.getPriority() - firstDetail.getPriority();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void updatePatterns()
|
private void updatePatterns()
|
||||||
{
|
{
|
||||||
HashMap<IAEItemStack, ImmutableList<ICraftingPatternDetails>> oldItems = craftableItems;
|
Map<IAEItemStack, ImmutableList<ICraftingPatternDetails>> oldItems = this.craftableItems;
|
||||||
|
|
||||||
// erase list.
|
// erase list.
|
||||||
craftingMethods.clear();
|
this.craftingMethods.clear();
|
||||||
craftableItems = new HashMap<IAEItemStack, ImmutableList<ICraftingPatternDetails>>();
|
this.craftableItems.clear();
|
||||||
emitableItems.clear();
|
this.emitableItems.clear();
|
||||||
|
|
||||||
// update the stuff that was in the list...
|
// update the stuff that was in the list...
|
||||||
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() );
|
this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() );
|
||||||
|
|
||||||
// re-create list..
|
// re-create list..
|
||||||
for (ICraftingProvider cp : providers)
|
for (ICraftingProvider provider : this.craftingProviders)
|
||||||
cp.provideCrafting( this );
|
{
|
||||||
|
provider.provideCrafting( this );
|
||||||
|
}
|
||||||
|
|
||||||
HashMap<IAEItemStack, Set<ICraftingPatternDetails>> tmpCraft = new HashMap<IAEItemStack, Set<ICraftingPatternDetails>>();
|
Map<IAEItemStack, Set<ICraftingPatternDetails>> tmpCraft = new HashMap<IAEItemStack, Set<ICraftingPatternDetails>>();
|
||||||
|
|
||||||
// new craftables!
|
// new craftables!
|
||||||
for (ICraftingPatternDetails details : craftingMethods.keySet())
|
for (ICraftingPatternDetails details : this.craftingMethods.keySet())
|
||||||
{
|
{
|
||||||
for (IAEItemStack out : details.getOutputs())
|
for (IAEItemStack out : details.getOutputs())
|
||||||
{
|
{
|
||||||
|
@ -324,7 +339,9 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
Set<ICraftingPatternDetails> methods = tmpCraft.get( out );
|
Set<ICraftingPatternDetails> methods = tmpCraft.get( out );
|
||||||
|
|
||||||
if ( methods == null )
|
if ( methods == null )
|
||||||
tmpCraft.put( out, methods = new TreeSet<ICraftingPatternDetails>(comp) );
|
{
|
||||||
|
tmpCraft.put( out, methods = new TreeSet<ICraftingPatternDetails>( comp ) );
|
||||||
|
}
|
||||||
|
|
||||||
methods.add( details );
|
methods.add( details );
|
||||||
}
|
}
|
||||||
|
@ -332,9 +349,11 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
|
|
||||||
// make them immutable
|
// make them immutable
|
||||||
for (Entry<IAEItemStack, Set<ICraftingPatternDetails>> e : tmpCraft.entrySet())
|
for (Entry<IAEItemStack, Set<ICraftingPatternDetails>> e : tmpCraft.entrySet())
|
||||||
craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) );
|
{
|
||||||
|
this.craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) );
|
||||||
|
}
|
||||||
|
|
||||||
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, craftableItems.keySet(), new BaseActionSource() );
|
this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, this.craftableItems.keySet(), new BaseActionSource() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -357,9 +376,13 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
@Override
|
@Override
|
||||||
public List<IMEInventoryHandler> getCellArray(StorageChannel channel)
|
public List<IMEInventoryHandler> getCellArray(StorageChannel channel)
|
||||||
{
|
{
|
||||||
ArrayList<IMEInventoryHandler> list = new ArrayList<IMEInventoryHandler>( 1 );
|
List<IMEInventoryHandler> list = new ArrayList<IMEInventoryHandler>( 1 );
|
||||||
|
|
||||||
if ( channel == StorageChannel.ITEMS )
|
if ( channel == StorageChannel.ITEMS )
|
||||||
|
{
|
||||||
list.add( this );
|
list.add( this );
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,14 +399,18 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IItemList getAvailableItems(IItemList out)
|
public IItemList<IAEStack> getAvailableItems(IItemList<IAEStack> out)
|
||||||
{
|
{
|
||||||
// add craftable items!
|
// add craftable items!
|
||||||
for (IAEItemStack st : craftableItems.keySet())
|
for (IAEItemStack stack : this.craftableItems.keySet())
|
||||||
out.addCrafting( st );
|
{
|
||||||
|
out.addCrafting( stack );
|
||||||
|
}
|
||||||
|
|
||||||
for (IAEItemStack st : emitableItems)
|
for (IAEItemStack st : this.emitableItems)
|
||||||
|
{
|
||||||
out.addCrafting( st );
|
out.addCrafting( st );
|
||||||
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -409,8 +436,10 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
@Override
|
@Override
|
||||||
public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src)
|
public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src)
|
||||||
{
|
{
|
||||||
for (CraftingCPUCluster cpu : cpuClusters)
|
for (CraftingCPUCluster cpu : this.craftingCPUClusters)
|
||||||
|
{
|
||||||
input = cpu.injectItems( input, type, src );
|
input = cpu.injectItems( input, type, src );
|
||||||
|
}
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
@ -418,29 +447,36 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
@Override
|
@Override
|
||||||
public boolean canAccept(IAEStack input)
|
public boolean canAccept(IAEStack input)
|
||||||
{
|
{
|
||||||
for (CraftingCPUCluster cpu : cpuClusters)
|
for (CraftingCPUCluster cpu : this.craftingCPUClusters)
|
||||||
|
{
|
||||||
if ( cpu.canAccept( input ) )
|
if ( cpu.canAccept( input ) )
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ICraftingLink submitJob(ICraftingJob job, ICraftingRequester requestingMachine, ICraftingCPU target, final boolean prioritizePower,
|
public ICraftingLink submitJob(ICraftingJob job, ICraftingRequester requestingMachine, ICraftingCPU target, final boolean prioritizePower, BaseActionSource src)
|
||||||
BaseActionSource src)
|
|
||||||
{
|
{
|
||||||
if ( job.isSimulation() )
|
if ( job.isSimulation() )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
CraftingCPUCluster cpuCluster = null;
|
CraftingCPUCluster cpuCluster = null;
|
||||||
|
|
||||||
if ( target instanceof CraftingCPUCluster )
|
if ( target instanceof CraftingCPUCluster )
|
||||||
|
{
|
||||||
cpuCluster = (CraftingCPUCluster) target;
|
cpuCluster = (CraftingCPUCluster) target;
|
||||||
|
}
|
||||||
|
|
||||||
if ( target == null )
|
if ( target == null )
|
||||||
{
|
{
|
||||||
List<CraftingCPUCluster> validCpusClusters = new ArrayList<CraftingCPUCluster>();
|
List<CraftingCPUCluster> validCpusClusters = new ArrayList<CraftingCPUCluster>();
|
||||||
for (CraftingCPUCluster cpu : cpuClusters)
|
for (CraftingCPUCluster cpu : this.craftingCPUClusters)
|
||||||
{
|
{
|
||||||
if ( cpu.isActive() && !cpu.isBusy() && cpu.getAvailableStorage() >= job.getByteTotal() )
|
if ( cpu.isActive() && !cpu.isBusy() && cpu.getAvailableStorage() >= job.getByteTotal() )
|
||||||
{
|
{
|
||||||
|
@ -448,30 +484,32 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort( validCpusClusters, new Comparator<CraftingCPUCluster>() {
|
Collections.sort( validCpusClusters, new Comparator<CraftingCPUCluster>()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
public int compare(CraftingCPUCluster o1, CraftingCPUCluster o2)
|
public int compare(CraftingCPUCluster firstCluster, CraftingCPUCluster nextCluster)
|
||||||
{
|
{
|
||||||
if ( prioritizePower )
|
if ( prioritizePower )
|
||||||
{
|
{
|
||||||
int a = ItemSorters.compareLong( o2.getCoProcessors(), o1.getCoProcessors() );
|
int comparison = ItemSorters.compareLong( nextCluster.getCoProcessors(), firstCluster.getCoProcessors() );
|
||||||
if ( a != 0 )
|
if ( comparison != 0 )
|
||||||
return a;
|
return comparison;
|
||||||
return ItemSorters.compareLong( o2.getAvailableStorage(), o1.getAvailableStorage() );
|
return ItemSorters.compareLong( nextCluster.getAvailableStorage(), firstCluster.getAvailableStorage() );
|
||||||
}
|
}
|
||||||
|
|
||||||
int a = ItemSorters.compareLong( o1.getCoProcessors(), o2.getCoProcessors() );
|
int comparison = ItemSorters.compareLong( firstCluster.getCoProcessors(), nextCluster.getCoProcessors() );
|
||||||
if ( a != 0 )
|
if ( comparison != 0 )
|
||||||
return a;
|
return comparison;
|
||||||
return ItemSorters.compareLong( o1.getAvailableStorage(), o2.getAvailableStorage() );
|
return ItemSorters.compareLong( firstCluster.getAvailableStorage(), nextCluster.getAvailableStorage() );
|
||||||
}
|
}
|
||||||
|
|
||||||
} );
|
} );
|
||||||
|
|
||||||
if ( !validCpusClusters.isEmpty() )
|
if ( !validCpusClusters.isEmpty() )
|
||||||
|
{
|
||||||
cpuCluster = validCpusClusters.get( 0 );
|
cpuCluster = validCpusClusters.get( 0 );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( cpuCluster != null )
|
if ( cpuCluster != null )
|
||||||
{
|
{
|
||||||
|
@ -490,19 +528,19 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
@Override
|
@Override
|
||||||
public ImmutableCollection<ICraftingPatternDetails> getCraftingFor(IAEItemStack whatToCraft, ICraftingPatternDetails details, int slotIndex, World world)
|
public ImmutableCollection<ICraftingPatternDetails> getCraftingFor(IAEItemStack whatToCraft, ICraftingPatternDetails details, int slotIndex, World world)
|
||||||
{
|
{
|
||||||
ImmutableList<ICraftingPatternDetails> res = craftableItems.get( whatToCraft );
|
ImmutableList<ICraftingPatternDetails> res = this.craftableItems.get( whatToCraft );
|
||||||
|
|
||||||
if ( res == null )
|
if ( res == null )
|
||||||
{
|
{
|
||||||
if ( details != null && details.isCraftable() )
|
if ( details != null && details.isCraftable() )
|
||||||
{
|
{
|
||||||
for (IAEItemStack ais : craftableItems.keySet())
|
for (IAEItemStack ais : this.craftableItems.keySet())
|
||||||
{
|
{
|
||||||
if ( ais.getItem() == whatToCraft.getItem() && (!ais.getItem().getHasSubtypes() || ais.getItemDamage() == whatToCraft.getItemDamage()) )
|
if ( ais.getItem() == whatToCraft.getItem() && (!ais.getItem().getHasSubtypes() || ais.getItemDamage() == whatToCraft.getItemDamage()) )
|
||||||
{
|
{
|
||||||
if ( details.isValidItemForSlot( slotIndex, ais.getItemStack(), world ) )
|
if ( details.isValidItemForSlot( slotIndex, ais.getItemStack(), world ) )
|
||||||
{
|
{
|
||||||
return craftableItems.get( ais );
|
return this.craftableItems.get( ais );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -516,12 +554,14 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
|
|
||||||
public List<ICraftingMedium> getMediums(ICraftingPatternDetails key)
|
public List<ICraftingMedium> getMediums(ICraftingPatternDetails key)
|
||||||
{
|
{
|
||||||
List<ICraftingMedium> o = craftingMethods.get( key );
|
List<ICraftingMedium> mediums = craftingMethods.get( key );
|
||||||
|
|
||||||
if ( o == null )
|
if ( mediums == null )
|
||||||
o = ImmutableList.of();
|
{
|
||||||
|
mediums = ImmutableList.of();
|
||||||
|
}
|
||||||
|
|
||||||
return o;
|
return mediums;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -551,36 +591,44 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
|
||||||
public Future<ICraftingJob> beginCraftingJob(World world, IGrid grid, BaseActionSource actionSrc, IAEItemStack slotItem, ICraftingCallback cb)
|
public Future<ICraftingJob> beginCraftingJob(World world, IGrid grid, BaseActionSource actionSrc, IAEItemStack slotItem, ICraftingCallback cb)
|
||||||
{
|
{
|
||||||
if ( world == null || grid == null || actionSrc == null || slotItem == null )
|
if ( world == null || grid == null || actionSrc == null || slotItem == null )
|
||||||
|
{
|
||||||
throw new RuntimeException( "Invalid Crafting Job Request" );
|
throw new RuntimeException( "Invalid Crafting Job Request" );
|
||||||
|
}
|
||||||
|
|
||||||
CraftingJob cj = new CraftingJob( world, grid, actionSrc, slotItem, cb );
|
CraftingJob job = new CraftingJob( world, grid, actionSrc, slotItem, cb );
|
||||||
return craftingPool.submit( cj, (ICraftingJob) cj );
|
|
||||||
|
return craftingPool.submit( job, (ICraftingJob) job );
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasCpu(ICraftingCPU cpu)
|
public boolean hasCpu(ICraftingCPU cpu)
|
||||||
{
|
{
|
||||||
return cpuClusters.contains( cpu );
|
return this.craftingCPUClusters.contains( cpu );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRequesting(IAEItemStack what)
|
public boolean isRequesting(IAEItemStack what)
|
||||||
{
|
{
|
||||||
for (CraftingCPUCluster c : cpuClusters)
|
for (CraftingCPUCluster cluster : this.craftingCPUClusters)
|
||||||
if ( c.isMaking( what ) )
|
{
|
||||||
|
if ( cluster.isMaking( what ) )
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canEmitFor(IAEItemStack what)
|
public boolean canEmitFor(IAEItemStack someItem)
|
||||||
{
|
{
|
||||||
return emitableItems.contains( what );
|
return this.emitableItems.contains( someItem );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEmitable(IAEItemStack what)
|
public void setEmitable(IAEItemStack someItem)
|
||||||
{
|
{
|
||||||
emitableItems.add( what.copy() );
|
this.emitableItems.add( someItem.copy() );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package appeng.me.cache;
|
package appeng.me.cache;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
@ -75,7 +76,7 @@ public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
|
||||||
|
|
||||||
if ( myGridCache.interestManager.containsKey( changedItem ) )
|
if ( myGridCache.interestManager.containsKey( changedItem ) )
|
||||||
{
|
{
|
||||||
Set<ItemWatcher> list = myGridCache.interestManager.get( changedItem );
|
Collection<ItemWatcher> list = myGridCache.interestManager.get( changedItem );
|
||||||
if ( !list.isEmpty() )
|
if ( !list.isEmpty() )
|
||||||
{
|
{
|
||||||
IAEStack fullStack = myStorageList.findPrecise( changedItem );
|
IAEStack fullStack = myStorageList.findPrecise( changedItem );
|
||||||
|
|
|
@ -285,7 +285,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU
|
||||||
|
|
||||||
if ( sg.interestManager.containsKey( diff ) )
|
if ( sg.interestManager.containsKey( diff ) )
|
||||||
{
|
{
|
||||||
Set<CraftingWatcher> list = sg.interestManager.get( diff );
|
Collection<CraftingWatcher> list = sg.interestManager.get( diff );
|
||||||
|
|
||||||
if ( !list.isEmpty() )
|
if ( !list.isEmpty() )
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package appeng.me.helpers;
|
package appeng.me.helpers;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import appeng.api.storage.data.IAEStack;
|
import appeng.api.storage.data.IAEStack;
|
||||||
|
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.common.collect.SetMultimap;
|
import com.google.common.collect.SetMultimap;
|
||||||
|
|
||||||
public class GenericInterestManager<T>
|
public class GenericInterestManager<T>
|
||||||
|
@ -24,11 +26,11 @@ public class GenericInterestManager<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final SetMultimap<IAEStack, T> container;
|
private final Multimap<IAEStack, T> container;
|
||||||
private LinkedList<SavedTransactions> transactions = null;
|
private LinkedList<SavedTransactions> transactions = null;
|
||||||
private int transDepth = 0;
|
private int transDepth = 0;
|
||||||
|
|
||||||
public GenericInterestManager(SetMultimap<IAEStack, T> interests) {
|
public GenericInterestManager(Multimap<IAEStack, T> interests) {
|
||||||
container = interests;
|
container = interests;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +66,7 @@ public class GenericInterestManager<T>
|
||||||
return container.containsKey( stack );
|
return container.containsKey( stack );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<T> get(IAEStack stack)
|
public Collection<T> get(IAEStack stack)
|
||||||
{
|
{
|
||||||
return container.get( stack );
|
return container.get( stack );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue