Refactored StorageChannel enum into an interface (#3138)

This replaces the static enum with a more dynamic interface providing
factory methods for handling network storage.
This commit is contained in:
yueh
2017-10-08 17:59:30 +02:00
committed by GitHub
parent 8ad8ce68b5
commit 6e81f698c0
115 changed files with 1255 additions and 943 deletions
+17 -13
View File
@@ -44,6 +44,7 @@ import com.google.common.collect.Multimap;
import net.minecraft.world.World;
import appeng.api.AEApi;
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.networking.IGrid;
@@ -71,7 +72,8 @@ import appeng.api.networking.security.IActionSource;
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.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
@@ -87,7 +89,7 @@ import appeng.tile.crafting.TileCraftingTile;
import appeng.util.ItemSorters;
public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler<IAEStack>
public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler<IAEItemStack>
{
private static final ExecutorService CRAFTING_POOL;
@@ -264,7 +266,8 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
this.emitableItems.clear();
// update the stuff that was in the list...
this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() );
this.storageGrid.postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ), oldItems.keySet(),
new BaseActionSource() );
// re-create list..
for( final ICraftingProvider provider : this.craftingProviders )
@@ -300,7 +303,8 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
this.craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) );
}
this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, this.craftableItems.keySet(), new BaseActionSource() );
this.storageGrid.postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ), this.craftableItems.keySet(),
new BaseActionSource() );
}
private void updateCPUClusters()
@@ -375,11 +379,11 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
}
@Override
public List<IMEInventoryHandler> getCellArray( final StorageChannel channel )
public List<IMEInventoryHandler> getCellArray( final IStorageChannel channel )
{
final List<IMEInventoryHandler> list = new ArrayList<>( 1 );
if( channel == StorageChannel.ITEMS )
if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
{
list.add( this );
}
@@ -400,13 +404,13 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
}
@Override
public boolean isPrioritized( final IAEStack input )
public boolean isPrioritized( final IAEItemStack input )
{
return true;
}
@Override
public boolean canAccept( final IAEStack input )
public boolean canAccept( final IAEItemStack input )
{
for( final CraftingCPUCluster cpu : this.craftingCPUClusters )
{
@@ -432,7 +436,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
}
@Override
public IAEStack injectItems( IAEStack input, final Actionable type, final IActionSource src )
public IAEItemStack injectItems( IAEItemStack input, final Actionable type, final IActionSource src )
{
for( final CraftingCPUCluster cpu : this.craftingCPUClusters )
{
@@ -443,13 +447,13 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
}
@Override
public IAEStack extractItems( final IAEStack request, final Actionable mode, final IActionSource src )
public IAEItemStack extractItems( final IAEItemStack request, final Actionable mode, final IActionSource src )
{
return null;
}
@Override
public IItemList<IAEStack> getAvailableItems( final IItemList<IAEStack> out )
public IItemList<IAEItemStack> getAvailableItems( final IItemList<IAEItemStack> out )
{
// add craftable items!
for( final IAEItemStack stack : this.craftableItems.keySet() )
@@ -466,9 +470,9 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
}
@Override
public StorageChannel getChannel()
public IStorageChannel<IAEItemStack> getChannel()
{
return StorageChannel.ITEMS;
return AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class );
}
@Override
+61 -133
View File
@@ -21,8 +21,10 @@ package appeng.me.cache;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
@@ -44,8 +46,7 @@ import appeng.api.storage.ICellContainer;
import appeng.api.storage.ICellProvider;
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.IStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
@@ -64,22 +65,23 @@ public class GridStorageCache implements IStorageGrid
private final HashSet<ICellProvider> inactiveCellProviders = new HashSet<>();
private final SetMultimap<IAEStack, ItemWatcher> interests = HashMultimap.create();
private final GenericInterestManager<ItemWatcher> interestManager = new GenericInterestManager<>( this.interests );
private final NetworkMonitor<IAEItemStack> itemMonitor = new NetworkMonitor<>( this, StorageChannel.ITEMS );
private final NetworkMonitor<IAEFluidStack> fluidMonitor = new NetworkMonitor<>( this, StorageChannel.FLUIDS );
private final HashMap<IGridNode, IStackWatcher> watchers = new HashMap<>();
private NetworkInventoryHandler<IAEItemStack> myItemNetwork;
private NetworkInventoryHandler<IAEFluidStack> myFluidNetwork;
private Map<IStorageChannel<? extends IAEStack>, NetworkInventoryHandler<?>> storageNetworks;
private Map<IStorageChannel<? extends IAEStack>, NetworkMonitor<?>> storageMonitors;
public GridStorageCache( final IGrid g )
{
this.myGrid = g;
this.storageNetworks = new IdentityHashMap<>();
this.storageMonitors = new IdentityHashMap<>();
AEApi.instance().storage().storageChannels().forEach( channel -> this.storageMonitors.put( channel, new NetworkMonitor<>( this, channel ) ) );
}
@Override
public void onUpdateTick()
{
this.itemMonitor.onTick();
this.fluidMonitor.onTick();
this.storageMonitors.forEach( ( channel, monitor ) -> monitor.onTick() );
}
@Override
@@ -155,6 +157,17 @@ public class GridStorageCache implements IStorageGrid
}
public <T extends IAEStack<T>> IMEInventoryHandler<T> getInventoryHandler( IStorageChannel<T> channel )
{
return (IMEInventoryHandler<T>) this.storageNetworks.computeIfAbsent( channel, this::buildNetworkStorage );
}
@Override
public <T extends IAEStack<T>> IMEMonitor<T> getInventory( IStorageChannel<T> channel )
{
return (IMEMonitor<T>) this.storageMonitors.get( channel );
}
private CellChangeTracker addCellProvider( final ICellProvider cc, final CellChangeTracker tracker )
{
if( this.inactiveCellProviders.contains( cc ) )
@@ -162,21 +175,15 @@ public class GridStorageCache implements IStorageGrid
this.inactiveCellProviders.remove( cc );
this.activeCellProviders.add( cc );
IActionSource actionSrc = new BaseActionSource();
if( cc instanceof IActionHost )
{
actionSrc = new MachineSource( (IActionHost) cc );
}
final IActionSource actionSrc = cc instanceof IActionHost ? new MachineSource( (IActionHost) cc ) : new BaseActionSource();
for( final IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( StorageChannel.ITEMS ) )
this.storageMonitors.forEach( ( channel, monitor ) ->
{
tracker.postChanges( StorageChannel.ITEMS, 1, h, actionSrc );
}
for( final IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( StorageChannel.FLUIDS ) )
{
tracker.postChanges( StorageChannel.FLUIDS, 1, h, actionSrc );
}
for( final IMEInventoryHandler<?> h : cc.getCellArray( channel ) )
{
tracker.postChanges( channel, 1, h, actionSrc );
}
} );
}
return tracker;
@@ -189,22 +196,15 @@ public class GridStorageCache implements IStorageGrid
this.activeCellProviders.remove( cc );
this.inactiveCellProviders.add( cc );
IActionSource actionSrc = new BaseActionSource();
final IActionSource actionSrc = cc instanceof IActionHost ? new MachineSource( (IActionHost) cc ) : new BaseActionSource();
if( cc instanceof IActionHost )
this.storageMonitors.forEach( ( channel, monitor ) ->
{
actionSrc = new MachineSource( (IActionHost) cc );
}
for( final IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( StorageChannel.ITEMS ) )
{
tracker.postChanges( StorageChannel.ITEMS, -1, h, actionSrc );
}
for( final IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( StorageChannel.FLUIDS ) )
{
tracker.postChanges( StorageChannel.FLUIDS, -1, h, actionSrc );
}
for( final IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( channel ) )
{
tracker.postChanges( channel, -1, h, actionSrc );
}
} );
}
return tracker;
@@ -213,10 +213,9 @@ public class GridStorageCache implements IStorageGrid
@MENetworkEventSubscribe
public void cellUpdate( final MENetworkCellArrayUpdate ev )
{
this.myItemNetwork = null;
this.myFluidNetwork = null;
this.storageNetworks.clear();
final LinkedList<ICellProvider> ll = new LinkedList();
final List<ICellProvider> ll = new LinkedList<ICellProvider>();
ll.addAll( this.inactiveCellProviders );
ll.addAll( this.activeCellProviders );
@@ -249,85 +248,37 @@ public class GridStorageCache implements IStorageGrid
}
}
this.itemMonitor.forceUpdate();
this.fluidMonitor.forceUpdate();
this.storageMonitors.forEach( ( channel, monitor ) -> monitor.forceUpdate() );
tracker.applyChanges();
}
private void postChangesToNetwork( final StorageChannel chan, final int upOrDown, final IItemList availableItems, final IActionSource src )
private <T extends IAEStack<T>, C extends IStorageChannel<T>> void postChangesToNetwork( final C chan, final int upOrDown, final IItemList<T> availableItems, final IActionSource src )
{
switch( chan )
{
case FLUIDS:
this.fluidMonitor.postChange( upOrDown > 0, availableItems, src );
break;
case ITEMS:
this.itemMonitor.postChange( upOrDown > 0, availableItems, src );
break;
default:
}
this.storageMonitors.get( chan ).postChange( upOrDown > 0, (Iterable) availableItems, src );
}
IMEInventoryHandler<IAEItemStack> getItemInventoryHandler()
{
if( this.myItemNetwork == null )
{
this.buildNetworkStorage( StorageChannel.ITEMS );
}
return this.myItemNetwork;
}
private void buildNetworkStorage( final StorageChannel chan )
private <T extends IAEStack<T>, C extends IStorageChannel<T>> NetworkInventoryHandler<T> buildNetworkStorage( final C chan )
{
final SecurityCache security = this.getGrid().getCache( ISecurityGrid.class );
switch( chan )
{
case FLUIDS:
this.myFluidNetwork = new NetworkInventoryHandler<>( StorageChannel.FLUIDS, security );
for( final ICellProvider cc : this.activeCellProviders )
{
for( final IMEInventoryHandler<IAEFluidStack> h : cc.getCellArray( chan ) )
{
this.myFluidNetwork.addNewStorage( h );
}
}
break;
case ITEMS:
this.myItemNetwork = new NetworkInventoryHandler<>( StorageChannel.ITEMS, security );
for( final ICellProvider cc : this.activeCellProviders )
{
for( final IMEInventoryHandler<IAEItemStack> h : cc.getCellArray( chan ) )
{
this.myItemNetwork.addNewStorage( h );
}
}
break;
default:
}
}
final NetworkInventoryHandler<T> storageNetwork = new NetworkInventoryHandler<>( chan, security );
IMEInventoryHandler<IAEFluidStack> getFluidInventoryHandler()
{
if( this.myFluidNetwork == null )
for( final ICellProvider cc : this.activeCellProviders )
{
this.buildNetworkStorage( StorageChannel.FLUIDS );
for( final IMEInventoryHandler<T> h : cc.getCellArray( chan ) )
{
storageNetwork.addNewStorage( h );
}
}
return this.myFluidNetwork;
return storageNetwork;
}
@Override
public void postAlterationOfStoredItems( final StorageChannel chan, final Iterable<? extends IAEStack> input, final IActionSource src )
public void postAlterationOfStoredItems( final IStorageChannel chan, final Iterable<? extends IAEStack> input, final IActionSource src )
{
if( chan == StorageChannel.ITEMS )
{
this.itemMonitor.postChange( true, (Iterable<IAEItemStack>) input, src );
}
else if( chan == StorageChannel.FLUIDS )
{
this.fluidMonitor.postChange( true, (Iterable<IAEFluidStack>) input, src );
}
this.storageMonitors.get( chan ).postChange( true, (Iterable) input, src );
}
@Override
@@ -344,18 +295,6 @@ public class GridStorageCache implements IStorageGrid
this.inactiveCellProviders.remove( provider );
}
@Override
public IMEMonitor<IAEItemStack> getItemInventory()
{
return this.itemMonitor;
}
@Override
public IMEMonitor<IAEFluidStack> getFluidInventory()
{
return this.fluidMonitor;
}
public GenericInterestManager<ItemWatcher> getInterestManager()
{
return this.interestManager;
@@ -366,32 +305,21 @@ public class GridStorageCache implements IStorageGrid
return this.myGrid;
}
private class CellChangeTrackerRecord
private class CellChangeTrackerRecord<T extends IAEStack<T>>
{
final StorageChannel channel;
final IStorageChannel<T> channel;
final int up_or_down;
final IItemList list;
final IItemList<T> list;
final IActionSource src;
public CellChangeTrackerRecord( final StorageChannel channel, final int i, final IMEInventoryHandler<? extends IAEStack> h, final IActionSource actionSrc )
public CellChangeTrackerRecord( final IStorageChannel<T> channel, final int i, final IMEInventoryHandler<T> h, final IActionSource actionSrc )
{
this.channel = channel;
this.up_or_down = i;
this.src = actionSrc;
if( channel == StorageChannel.ITEMS )
{
this.list = ( (IMEInventoryHandler<IAEItemStack>) h ).getAvailableItems( AEApi.instance().storage().createItemList() );
}
else if( channel == StorageChannel.FLUIDS )
{
this.list = ( (IMEInventoryHandler<IAEFluidStack>) h ).getAvailableItems( AEApi.instance().storage().createFluidList() );
}
else
{
this.list = null;
}
this.list = h.getAvailableItems( channel.createList() );
}
public void applyChanges()
@@ -400,19 +328,19 @@ public class GridStorageCache implements IStorageGrid
}
}
private class CellChangeTracker
private class CellChangeTracker<T extends IAEStack<T>>
{
final List<CellChangeTrackerRecord> data = new LinkedList<>();
final List<CellChangeTrackerRecord<T>> data = new LinkedList<>();
public void postChanges( final StorageChannel channel, final int i, final IMEInventoryHandler<? extends IAEStack> h, final IActionSource actionSrc )
public void postChanges( final IStorageChannel<T> channel, final int i, final IMEInventoryHandler<T> h, final IActionSource actionSrc )
{
this.data.add( new CellChangeTrackerRecord( channel, i, h, actionSrc ) );
this.data.add( new CellChangeTrackerRecord<T>( channel, i, h, actionSrc ) );
}
public void applyChanges()
{
for( final CellChangeTrackerRecord rec : this.data )
for( final CellChangeTrackerRecord<T> rec : this.data )
{
rec.applyChanges();
}
+10 -20
View File
@@ -40,7 +40,7 @@ import appeng.api.networking.security.IActionSource;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.me.storage.ItemWatcher;
@@ -54,7 +54,7 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
@Nonnull
private final GridStorageCache myGridCache;
@Nonnull
private final StorageChannel myChannel;
private final IStorageChannel<T> myChannel;
@Nonnull
private final IItemList<T> cachedList;
@Nonnull
@@ -65,11 +65,11 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
@Nonnegative
private int localDepthSemaphore = 0;
public NetworkMonitor( final GridStorageCache cache, final StorageChannel chan )
public NetworkMonitor( final GridStorageCache cache, final IStorageChannel<T> chan )
{
this.myGridCache = cache;
this.myChannel = chan;
this.cachedList = (IItemList<T>) chan.createList();
this.cachedList = chan.createList();
this.listeners = new HashMap<>();
}
@@ -112,13 +112,13 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
}
@Override
public IItemList<T> getAvailableItems( final IItemList out )
public IItemList<T> getAvailableItems( final IItemList<T> out )
{
return this.getHandler().getAvailableItems( out );
}
@Override
public StorageChannel getChannel()
public IStorageChannel<T> getChannel()
{
return this.getHandler().getChannel();
}
@@ -188,18 +188,9 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
}
@Nullable
@SuppressWarnings( "unchecked" )
private IMEInventoryHandler<T> getHandler()
{
switch( this.myChannel )
{
case ITEMS:
return (IMEInventoryHandler<T>) this.myGridCache.getItemInventoryHandler();
case FLUIDS:
return (IMEInventoryHandler<T>) this.myGridCache.getFluidInventoryHandler();
default:
}
return null;
return this.myGridCache.getInventoryHandler( this.myChannel );
}
private Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> getListeners()
@@ -207,9 +198,9 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
return this.listeners.entrySet().iterator();
}
private T monitorDifference( final IAEStack original, final T leftOvers, final boolean extraction, final IActionSource src )
private T monitorDifference( final IAEStack<T> original, final T leftOvers, final boolean extraction, final IActionSource src )
{
final T diff = (T) original.copy();
final T diff = original.copy();
if( extraction )
{
@@ -283,7 +274,7 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
if( !list.isEmpty() )
{
IAEStack fullStack = this.getStorageList().findPrecise( changedItem );
IAEStack<T> fullStack = this.getStorageList().findPrecise( changedItem );
if( fullStack == null )
{
@@ -341,5 +332,4 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
this.myGridCache.getGrid().postEvent( new MENetworkStorageEvent( this, this.myChannel ) );
}
}
}