2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
import java.util.HashMap;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashSet;
|
2014-02-14 03:27:04 +01:00
|
|
|
import java.util.LinkedList;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-21 21:36:40 +01:00
|
|
|
import appeng.api.AEApi;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.events.MENetworkCellArrayUpdate;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
2014-01-05 09:49:08 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2014-05-19 03:05:03 +02:00
|
|
|
import appeng.api.networking.security.IActionHost;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.networking.security.ISecurityGrid;
|
2014-01-05 09:49:08 +01:00
|
|
|
import appeng.api.networking.security.MachineSource;
|
|
|
|
import appeng.api.networking.storage.IStackWatcher;
|
|
|
|
import appeng.api.networking.storage.IStackWatcherHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.storage.ICellContainer;
|
2014-05-19 03:05:03 +02:00
|
|
|
import appeng.api.storage.ICellProvider;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEFluidStack;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-08-11 07:08:31 +02:00
|
|
|
import appeng.me.helpers.GenericInterestManager;
|
2014-01-05 09:49:08 +01:00
|
|
|
import appeng.me.storage.ItemWatcher;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.storage.NetworkInventoryHandler;
|
2014-03-28 05:31:06 +01:00
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
import com.google.common.collect.HashMultimap;
|
|
|
|
import com.google.common.collect.SetMultimap;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public class GridStorageCache implements IStorageGrid
|
|
|
|
{
|
|
|
|
|
2014-06-10 19:16:14 +02:00
|
|
|
final private SetMultimap<IAEStack, ItemWatcher> interests = HashMultimap.create();
|
2014-08-11 07:08:31 +02:00
|
|
|
final public GenericInterestManager interestManager = new GenericInterestManager( interests );
|
2014-01-05 09:49:08 +01:00
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
final HashSet<ICellProvider> activeCellProviders = new HashSet();
|
|
|
|
final HashSet<ICellProvider> inactiveCellProviders = new HashSet();
|
2014-01-05 09:49:08 +01:00
|
|
|
final public IGrid myGrid;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
private NetworkInventoryHandler<IAEItemStack> myItemNetwork;
|
|
|
|
private NetworkMonitor<IAEItemStack> itemMonitor = new NetworkMonitor<IAEItemStack>( this, StorageChannel.ITEMS );
|
|
|
|
|
|
|
|
private NetworkInventoryHandler<IAEFluidStack> myFluidNetwork;
|
|
|
|
private NetworkMonitor<IAEFluidStack> fluidMonitor = new NetworkMonitor<IAEFluidStack>( this, StorageChannel.FLUIDS );
|
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
private HashMap<IGridNode, IStackWatcher> watchers = new HashMap<IGridNode, IStackWatcher>();
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public GridStorageCache(IGrid g) {
|
|
|
|
myGrid = g;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void onUpdateTick()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
itemMonitor.onTick();
|
|
|
|
fluidMonitor.onTick();
|
|
|
|
}
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
@Override
|
2014-05-23 04:42:27 +02:00
|
|
|
public void registerCellProvider(ICellProvider provider)
|
|
|
|
{
|
|
|
|
inactiveCellProviders.add( provider );
|
|
|
|
addCellProvider( provider );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unregisterCellProvider(ICellProvider provider)
|
|
|
|
{
|
|
|
|
removeCellProvider( provider );
|
|
|
|
inactiveCellProviders.remove( provider );
|
|
|
|
}
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
public void addCellProvider(ICellProvider cc)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
if ( inactiveCellProviders.contains( cc ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
inactiveCellProviders.remove( cc );
|
|
|
|
activeCellProviders.add( cc );
|
|
|
|
|
|
|
|
BaseActionSource actionSrc = new BaseActionSource();
|
|
|
|
if ( cc instanceof IActionHost )
|
|
|
|
actionSrc = new MachineSource( (IActionHost) cc );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-14 03:27:04 +01:00
|
|
|
for (IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( StorageChannel.ITEMS ))
|
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
postChanges( StorageChannel.ITEMS, 1, h.getAvailableItems( AEApi.instance().storage().createItemList() ), actionSrc );
|
2014-02-14 03:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( StorageChannel.FLUIDS ))
|
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
postChanges( StorageChannel.FLUIDS, 1, h.getAvailableItems( AEApi.instance().storage().createFluidList() ), actionSrc );
|
2014-02-14 03:27:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
public void removeCellProvider(ICellProvider cc)
|
2014-02-14 03:27:04 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
if ( activeCellProviders.contains( cc ) )
|
2014-02-14 03:27:04 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
inactiveCellProviders.add( cc );
|
|
|
|
activeCellProviders.remove( cc );
|
|
|
|
|
|
|
|
BaseActionSource actionSrc = new BaseActionSource();
|
|
|
|
if ( cc instanceof IActionHost )
|
|
|
|
actionSrc = new MachineSource( (IActionHost) cc );
|
2014-01-05 09:49:08 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
for (IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( StorageChannel.ITEMS ))
|
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
postChanges( StorageChannel.ITEMS, -1, h.getAvailableItems( AEApi.instance().storage().createItemList() ), actionSrc );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( StorageChannel.FLUIDS ))
|
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
postChanges( StorageChannel.FLUIDS, -1, h.getAvailableItems( AEApi.instance().storage().createFluidList() ), actionSrc );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 03:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void cellUpdate(MENetworkCellArrayUpdate ev)
|
|
|
|
{
|
|
|
|
myItemNetwork = null;
|
|
|
|
myFluidNetwork = null;
|
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
LinkedList<ICellProvider> ll = new LinkedList();
|
|
|
|
ll.addAll( inactiveCellProviders );
|
|
|
|
ll.addAll( activeCellProviders );
|
2014-02-14 03:27:04 +01:00
|
|
|
|
2014-05-19 03:05:03 +02:00
|
|
|
for (ICellProvider cc : ll)
|
2014-02-14 03:27:04 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
boolean Active = true;
|
|
|
|
|
|
|
|
if ( cc instanceof IActionHost )
|
2014-02-14 06:27:42 +01:00
|
|
|
{
|
2014-05-19 03:05:03 +02:00
|
|
|
IGridNode node = ((IActionHost) cc).getActionableNode();
|
|
|
|
if ( node != null && node.isActive() )
|
|
|
|
Active = true;
|
2014-02-14 06:27:42 +01:00
|
|
|
else
|
2014-05-19 03:05:03 +02:00
|
|
|
Active = false;
|
2014-02-14 06:27:42 +01:00
|
|
|
}
|
2014-05-19 03:05:03 +02:00
|
|
|
|
|
|
|
if ( Active )
|
|
|
|
addCellProvider( cc );
|
|
|
|
else
|
|
|
|
removeCellProvider( cc );
|
2014-02-14 03:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
itemMonitor.forceUpdate();
|
|
|
|
fluidMonitor.forceUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeNode(IGridNode node, IGridHost machine)
|
|
|
|
{
|
|
|
|
if ( machine instanceof ICellContainer )
|
|
|
|
{
|
|
|
|
ICellContainer cc = (ICellContainer) machine;
|
|
|
|
|
|
|
|
myGrid.postEvent( new MENetworkCellArrayUpdate() );
|
2014-05-19 03:05:03 +02:00
|
|
|
removeCellProvider( cc );
|
|
|
|
inactiveCellProviders.remove( cc );
|
2014-02-14 03:27:04 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
if ( machine instanceof IStackWatcherHost )
|
|
|
|
{
|
|
|
|
IStackWatcher myWatcher = watchers.get( machine );
|
|
|
|
if ( myWatcher != null )
|
|
|
|
{
|
|
|
|
myWatcher.clear();
|
|
|
|
watchers.remove( machine );
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void addNode(IGridNode node, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof ICellContainer )
|
|
|
|
{
|
|
|
|
ICellContainer cc = (ICellContainer) machine;
|
2014-05-19 03:05:03 +02:00
|
|
|
inactiveCellProviders.add( cc );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
myGrid.postEvent( new MENetworkCellArrayUpdate() );
|
2014-05-19 03:05:03 +02:00
|
|
|
if ( node.isActive() )
|
|
|
|
addCellProvider( cc );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
if ( machine instanceof IStackWatcherHost )
|
|
|
|
{
|
|
|
|
IStackWatcherHost swh = (IStackWatcherHost) machine;
|
|
|
|
ItemWatcher iw = new ItemWatcher( this, (IStackWatcherHost) swh );
|
|
|
|
watchers.put( node, iw );
|
|
|
|
swh.updateWatcher( iw );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void buildNetworkStorage(StorageChannel chan)
|
|
|
|
{
|
2014-01-27 05:00:36 +01:00
|
|
|
SecurityCache security = myGrid.getCache( ISecurityGrid.class );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
switch (chan)
|
|
|
|
{
|
|
|
|
case FLUIDS:
|
2014-01-27 05:00:36 +01:00
|
|
|
myFluidNetwork = new NetworkInventoryHandler<IAEFluidStack>( StorageChannel.FLUIDS, security );
|
2014-05-19 03:05:03 +02:00
|
|
|
for (ICellProvider cc : activeCellProviders)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
for (IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( chan ))
|
|
|
|
myFluidNetwork.addNewStorage( h );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ITEMS:
|
2014-01-27 05:00:36 +01:00
|
|
|
myItemNetwork = new NetworkInventoryHandler<IAEItemStack>( StorageChannel.ITEMS, security );
|
2014-05-19 03:05:03 +02:00
|
|
|
for (ICellProvider cc : activeCellProviders)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
for (IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( chan ))
|
|
|
|
myItemNetwork.addNewStorage( h );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 09:49:08 +01:00
|
|
|
private void postChanges(StorageChannel chan, int up_or_down, IItemList availableItems, BaseActionSource src)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
switch (chan)
|
|
|
|
{
|
|
|
|
case FLUIDS:
|
|
|
|
for (IAEFluidStack fs : ((IItemList<IAEFluidStack>) availableItems))
|
|
|
|
{
|
|
|
|
if ( up_or_down > 0 )
|
2014-01-05 09:49:08 +01:00
|
|
|
fluidMonitor.postChange( fs, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fs.setStackSize( -fs.getStackSize() );
|
2014-01-05 09:49:08 +01:00
|
|
|
fluidMonitor.postChange( fs, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ITEMS:
|
|
|
|
for (IAEItemStack fs : ((IItemList<IAEItemStack>) availableItems))
|
|
|
|
{
|
|
|
|
if ( up_or_down > 0 )
|
2014-01-05 09:49:08 +01:00
|
|
|
itemMonitor.postChange( fs, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fs.setStackSize( -fs.getStackSize() );
|
2014-01-05 09:49:08 +01:00
|
|
|
itemMonitor.postChange( fs, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IMEInventoryHandler<IAEItemStack> getItemInventoryHandler()
|
|
|
|
{
|
|
|
|
if ( myItemNetwork == null )
|
|
|
|
buildNetworkStorage( StorageChannel.ITEMS );
|
|
|
|
return myItemNetwork;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IMEInventoryHandler<IAEFluidStack> getFluidInventoryHandler()
|
|
|
|
{
|
|
|
|
if ( myFluidNetwork == null )
|
|
|
|
buildNetworkStorage( StorageChannel.FLUIDS );
|
|
|
|
return myFluidNetwork;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-01-05 09:49:08 +01:00
|
|
|
public void postAlterationOfStoredItems(StorageChannel chan, IAEStack input, BaseActionSource src)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( chan == StorageChannel.ITEMS )
|
2014-01-05 09:49:08 +01:00
|
|
|
itemMonitor.postChange( (IAEItemStack) input, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
else if ( chan == StorageChannel.FLUIDS )
|
2014-01-05 09:49:08 +01:00
|
|
|
fluidMonitor.postChange( (IAEFluidStack) input, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IMEMonitor<IAEFluidStack> getFluidInventory()
|
|
|
|
{
|
|
|
|
return fluidMonitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IMEMonitor<IAEItemStack> getItemInventory()
|
|
|
|
{
|
|
|
|
return itemMonitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSplit(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJoin(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateGridStorage(IGridStorage storage)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|