2014-05-09 06:10:12 +02:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2014-05-09 06:10:12 +02:00
|
|
|
import java.util.HashSet;
|
2014-05-19 03:05:03 +02:00
|
|
|
import java.util.List;
|
2014-06-06 06:26:01 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.TreeSet;
|
2014-05-09 06:10:12 +02:00
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
import appeng.api.config.AccessRestriction;
|
|
|
|
import appeng.api.config.Actionable;
|
2014-05-18 05:19:23 +02:00
|
|
|
import appeng.api.networking.IGrid;
|
2014-05-09 06:10:12 +02:00
|
|
|
import appeng.api.networking.IGridCache;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
2014-05-19 03:05:03 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingMedium;
|
2014-05-18 05:19:23 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
2014-05-09 06:10:12 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingProvider;
|
2014-05-18 05:19:23 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingProviderHelper;
|
|
|
|
import appeng.api.networking.events.MENetworkCraftingCpuChange;
|
|
|
|
import appeng.api.networking.events.MENetworkCraftingPatternChange;
|
2014-05-20 06:13:12 +02:00
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
2014-05-19 03:05:03 +02:00
|
|
|
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.storage.ICellProvider;
|
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-05-09 06:10:12 +02:00
|
|
|
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
2014-05-18 05:19:23 +02:00
|
|
|
import appeng.tile.crafting.TileCraftingStorageTile;
|
|
|
|
import appeng.tile.crafting.TileCraftingTile;
|
2014-05-09 06:10:12 +02:00
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler
|
2014-05-09 06:10:12 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
HashSet<CraftingCPUCluster> cpuClusters = new HashSet();
|
|
|
|
HashSet<ICraftingProvider> providers = new HashSet();
|
2014-05-18 05:19:23 +02:00
|
|
|
IGrid grid;
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
HashMap<ICraftingPatternDetails, List<ICraftingMedium>> craftingMethods = new HashMap();
|
2014-06-06 06:26:01 +02:00
|
|
|
HashMap<IAEItemStack, Set<ICraftingPatternDetails>> craftableItems = new HashMap();
|
2014-05-19 03:05:03 +02:00
|
|
|
|
2014-05-18 05:19:23 +02:00
|
|
|
boolean updateList = false;
|
|
|
|
|
|
|
|
public CraftingCache(IGrid g) {
|
|
|
|
grid = g;
|
|
|
|
}
|
|
|
|
|
2014-05-23 04:42:27 +02:00
|
|
|
@MENetworkEventSubscribe
|
2014-05-19 03:05:03 +02:00
|
|
|
public void afterCacheConstruction(MENetworkPostCacheConstruction cc)
|
|
|
|
{
|
|
|
|
IStorageGrid sg = grid.getCache( IStorageGrid.class );
|
2014-05-23 04:42:27 +02:00
|
|
|
sg.registerCellProvider( this );
|
2014-05-19 03:05:03 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
@Override
|
|
|
|
public void onUpdateTick()
|
|
|
|
{
|
2014-05-18 05:19:23 +02:00
|
|
|
if ( updateList )
|
|
|
|
{
|
|
|
|
updateList = false;
|
|
|
|
updateCPUClusters();
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 06:10:12 +02:00
|
|
|
|
2014-05-20 06:13:12 +02:00
|
|
|
@MENetworkEventSubscribe
|
2014-05-18 05:19:23 +02:00
|
|
|
public void updateCPUClusters(MENetworkCraftingCpuChange c)
|
|
|
|
{
|
|
|
|
updateList = true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 06:13:12 +02:00
|
|
|
@MENetworkEventSubscribe
|
2014-05-18 05:19:23 +02:00
|
|
|
public void updateCPUClusters(MENetworkCraftingPatternChange c)
|
|
|
|
{
|
|
|
|
updatePatterns();
|
2014-05-09 06:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeNode(IGridNode gridNode, IGridHost machine)
|
|
|
|
{
|
2014-05-18 05:19:23 +02:00
|
|
|
if ( machine instanceof TileCraftingTile )
|
|
|
|
updateList = true;
|
2014-05-09 06:10:12 +02:00
|
|
|
if ( machine instanceof ICraftingProvider )
|
2014-05-18 05:19:23 +02:00
|
|
|
{
|
2014-05-09 06:10:12 +02:00
|
|
|
providers.remove( machine );
|
2014-05-18 05:19:23 +02:00
|
|
|
updatePatterns();
|
|
|
|
}
|
2014-05-09 06:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addNode(IGridNode gridNode, IGridHost machine)
|
|
|
|
{
|
2014-05-18 05:19:23 +02:00
|
|
|
if ( machine instanceof TileCraftingTile )
|
|
|
|
updateList = true;
|
2014-05-09 06:10:12 +02:00
|
|
|
if ( machine instanceof ICraftingProvider )
|
2014-05-18 05:19:23 +02:00
|
|
|
{
|
2014-05-09 06:10:12 +02:00
|
|
|
providers.add( (ICraftingProvider) machine );
|
2014-05-18 05:19:23 +02:00
|
|
|
updatePatterns();
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 06:10:12 +02:00
|
|
|
|
2014-05-18 05:19:23 +02:00
|
|
|
private void updateCPUClusters()
|
|
|
|
{
|
|
|
|
cpuClusters.clear();
|
|
|
|
for (IGridNode cst : grid.getMachines( TileCraftingStorageTile.class ))
|
|
|
|
{
|
|
|
|
TileCraftingStorageTile tile = (TileCraftingStorageTile) cst.getMachine();
|
|
|
|
cpuClusters.add( (CraftingCPUCluster) tile.getCluster() );
|
|
|
|
}
|
2014-05-09 06:10:12 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 05:19:23 +02:00
|
|
|
@Override
|
2014-05-19 03:05:03 +02:00
|
|
|
public void addCraftingOption(ICraftingMedium medium, ICraftingPatternDetails api)
|
2014-05-09 06:10:12 +02:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
List<ICraftingMedium> details = craftingMethods.get( api );
|
|
|
|
if ( details == null )
|
|
|
|
{
|
|
|
|
details = new ArrayList<ICraftingMedium>();
|
|
|
|
details.add( medium );
|
|
|
|
craftingMethods.put( api, details );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
details.add( medium );
|
2014-05-09 06:10:12 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 05:19:23 +02:00
|
|
|
private void updatePatterns()
|
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
IStorageGrid sg = grid.getCache( IStorageGrid.class );
|
|
|
|
|
|
|
|
// update the stuff that was in the list...
|
2014-06-06 06:26:01 +02:00
|
|
|
for (IAEItemStack out : craftableItems.keySet())
|
|
|
|
{
|
|
|
|
out.reset();
|
|
|
|
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
|
|
|
|
}
|
2014-05-19 03:05:03 +02:00
|
|
|
|
|
|
|
// erase list.
|
|
|
|
craftingMethods.clear();
|
2014-06-06 06:26:01 +02:00
|
|
|
craftableItems.clear();
|
2014-05-19 03:05:03 +02:00
|
|
|
|
|
|
|
// re-create list..
|
2014-05-18 05:19:23 +02:00
|
|
|
for (ICraftingProvider cp : providers)
|
|
|
|
cp.provideCrafting( this );
|
2014-05-19 03:05:03 +02:00
|
|
|
|
|
|
|
// new craftables!
|
|
|
|
for (ICraftingPatternDetails details : craftingMethods.keySet())
|
|
|
|
for (IAEItemStack out : details.getOutputs())
|
|
|
|
{
|
|
|
|
out.reset();
|
|
|
|
out.setCraftable( true );
|
2014-06-06 06:26:01 +02:00
|
|
|
|
|
|
|
Set<ICraftingPatternDetails> methods = craftableItems.get( out );
|
|
|
|
|
|
|
|
if ( methods == null )
|
2014-06-08 18:25:46 +02:00
|
|
|
craftableItems.put( out, methods = new TreeSet() );
|
2014-06-06 06:26:01 +02:00
|
|
|
|
|
|
|
methods.add( details );
|
2014-05-19 03:05:03 +02:00
|
|
|
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
|
|
|
|
}
|
2014-05-18 05:19:23 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
@Override
|
|
|
|
public void onSplit(IGridStorage destinationStorage)
|
|
|
|
{ // nothing!
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJoin(IGridStorage sourceStorage)
|
|
|
|
{
|
|
|
|
// nothing!
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateGridStorage(IGridStorage destinationStorage)
|
|
|
|
{
|
|
|
|
// nothing!
|
|
|
|
}
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
@Override
|
|
|
|
public List<IMEInventoryHandler> getCellArray(StorageChannel channel)
|
|
|
|
{
|
|
|
|
ArrayList<IMEInventoryHandler> list = new ArrayList<IMEInventoryHandler>( 1 );
|
2014-05-23 04:42:27 +02:00
|
|
|
if ( channel == StorageChannel.ITEMS )
|
|
|
|
list.add( this );
|
2014-05-19 03:05:03 +02:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPriority()
|
|
|
|
{
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEStack extractItems(IAEStack request, Actionable mode, BaseActionSource src)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList getAvailableItems(IItemList out)
|
|
|
|
{
|
|
|
|
// add craftable items!
|
2014-06-06 06:26:01 +02:00
|
|
|
// for (ICraftingPatternDetails details : craftingMethods.keySet())
|
|
|
|
for (IAEItemStack st : craftableItems.keySet())
|
|
|
|
out.addCrafting( st );
|
2014-05-19 03:05:03 +02:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageChannel getChannel()
|
|
|
|
{
|
|
|
|
return StorageChannel.ITEMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AccessRestriction getAccess()
|
|
|
|
{
|
|
|
|
return AccessRestriction.WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPrioritized(IAEStack input)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src)
|
|
|
|
{
|
|
|
|
for (CraftingCPUCluster cpu : cpuClusters)
|
|
|
|
input = cpu.injectItems( input, type, src );
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAccept(IAEStack input)
|
|
|
|
{
|
|
|
|
for (CraftingCPUCluster cpu : cpuClusters)
|
|
|
|
if ( cpu.canAccept( (IAEItemStack) input ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlot()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-06 06:26:01 +02:00
|
|
|
public Set<ICraftingPatternDetails> getCraftingFor(IAEItemStack what)
|
|
|
|
{
|
2014-06-16 07:48:43 +02:00
|
|
|
Set<ICraftingPatternDetails> res = craftableItems.get( what );
|
|
|
|
if ( res == null )
|
|
|
|
return ImmutableSet.of();
|
|
|
|
return res;
|
2014-06-06 06:26:01 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
}
|