Further StorageChannel refactoring (#3152)

Updated Drives to support more than Item and Fluid cells.
Use Collections.emptyList() instead of creating empty ArrayLists.
Fixes a NPE with uninitialized ME Chests.

Fixes #3150
This commit is contained in:
yueh
2017-10-14 14:12:24 +02:00
committed by GitHub
parent 6e81f698c0
commit ab7f35a9ee
6 changed files with 36 additions and 46 deletions
@@ -33,6 +33,7 @@ import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.ISaveProvider; import appeng.api.storage.ISaveProvider;
import appeng.api.storage.IStorageChannel; import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel; import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEStack;
import appeng.api.util.AEPartLocation; import appeng.api.util.AEPartLocation;
import appeng.core.sync.GuiBridge; import appeng.core.sync.GuiBridge;
import appeng.me.storage.CellInventory; import appeng.me.storage.CellInventory;
@@ -50,7 +51,7 @@ public class BasicCellHandler implements ICellHandler
} }
@Override @Override
public IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) public <T extends IAEStack<T>> IMEInventoryHandler<T> getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel<T> channel )
{ {
if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
{ {
@@ -29,6 +29,7 @@ import appeng.api.storage.ICellRegistry;
import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.ISaveProvider; import appeng.api.storage.ISaveProvider;
import appeng.api.storage.IStorageChannel; import appeng.api.storage.IStorageChannel;
import appeng.api.storage.data.IAEStack;
public class CellRegistry implements ICellRegistry public class CellRegistry implements ICellRegistry
@@ -85,7 +86,7 @@ public class CellRegistry implements ICellRegistry
} }
@Override @Override
public IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel chan ) public <T extends IAEStack<T>> IMEInventoryHandler<T> getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel<T> chan )
{ {
if( is.isEmpty() ) if( is.isEmpty() )
{ {
@@ -20,6 +20,7 @@ package appeng.parts.automation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -400,11 +401,11 @@ public class PartFormationPlane extends PartUpgradeable implements ICellContaine
{ {
if( this.getProxy().isActive() && channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) if( this.getProxy().isActive() && channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
{ {
final List<IMEInventoryHandler> Handler = new ArrayList<>( 1 ); final List<IMEInventoryHandler> handler = new ArrayList<>( 1 );
Handler.add( this.myHandler ); handler.add( this.myHandler );
return Handler; return handler;
} }
return new ArrayList<>(); return Collections.emptyList();
} }
@Override @Override
@@ -577,7 +577,7 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
return Collections.singletonList( out ); return Collections.singletonList( out );
} }
} }
return Arrays.asList( new IMEInventoryHandler[] {} ); return Collections.emptyList();
} }
@Override @Override
@@ -643,14 +643,19 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal
{ {
try try
{ {
return Collections.singletonList( this.getHandler( channel ) ); final IMEInventoryHandler handler = this.getHandler( channel );
if( handler != null )
{
return Collections.singletonList( handler );
}
} }
catch( final ChestNoHandler e ) catch( final ChestNoHandler e )
{ {
// :P // :P
} }
} }
return new ArrayList<>(); return Collections.emptyList();
} }
@Override @Override
@@ -21,8 +21,11 @@ package appeng.tile.storage;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@@ -43,9 +46,8 @@ import appeng.api.storage.ICellHandler;
import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IStorageChannel; import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.util.AECableType; import appeng.api.util.AECableType;
import appeng.api.util.AEPartLocation; import appeng.api.util.AEPartLocation;
import appeng.api.util.DimensionalCoord; import appeng.api.util.DimensionalCoord;
@@ -73,8 +75,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
private final DriveWatcher<IAEItemStack>[] invBySlot = new DriveWatcher[10]; private final DriveWatcher<IAEItemStack>[] invBySlot = new DriveWatcher[10];
private final IActionSource mySrc; private final IActionSource mySrc;
private boolean isCached = false; private boolean isCached = false;
private List<MEInventoryHandler> items = new LinkedList<>(); private Map<IStorageChannel<? extends IAEStack<?>>, List<IMEInventoryHandler>> inventoryHandlers;
private List<MEInventoryHandler> fluids = new LinkedList<>();
private int priority = 0; private int priority = 0;
private boolean wasActive = false; private boolean wasActive = false;
@@ -98,6 +99,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
this.mySrc = new MachineSource( this ); this.mySrc = new MachineSource( this );
this.getProxy().setFlags( GridFlags.REQUIRE_CHANNEL ); this.getProxy().setFlags( GridFlags.REQUIRE_CHANNEL );
this.inv.setFilter( new CellValidInventoryFilter() ); this.inv.setFilter( new CellValidInventoryFilter() );
this.inventoryHandlers = new IdentityHashMap<>();
} }
@Override @Override
@@ -151,21 +153,10 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
return 0; return 0;
} }
if( handler.getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
{
if( ch != null ) if( ch != null )
{ {
return ch.getStatusForCell( cell, handler.getInternal() ); return ch.getStatusForCell( cell, handler.getInternal() );
} }
}
if( handler.getChannel() == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) )
{
if( ch != null )
{
return ch.getStatusForCell( cell, handler.getInternal() );
}
}
return 0; return 0;
} }
@@ -295,8 +286,8 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
{ {
if( !this.isCached ) if( !this.isCached )
{ {
this.items = new LinkedList(); final Collection<IStorageChannel<? extends IAEStack<?>>> storageChannels = AEApi.instance().storage().storageChannels();
this.fluids = new LinkedList(); storageChannels.forEach( channel -> this.inventoryHandlers.put( channel, new ArrayList<>( 10 ) ) );
double power = 2.0; double power = 2.0;
@@ -312,8 +303,10 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
if( this.handlersBySlot[x] != null ) if( this.handlersBySlot[x] != null )
{ {
IMEInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, for( IStorageChannel<? extends IAEStack<?>> channel : storageChannels )
AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); {
IMEInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, channel );
if( cell != null ) if( cell != null )
{ {
@@ -322,21 +315,9 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
final DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, this.handlersBySlot[x], this ); final DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, this.handlersBySlot[x], this );
ih.setPriority( this.priority ); ih.setPriority( this.priority );
this.invBySlot[x] = ih; this.invBySlot[x] = ih;
this.items.add( ih ); this.inventoryHandlers.get( channel ).add( ih );
}
else
{
cell = this.handlersBySlot[x].getCellInventory( is, this,
AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) );
if( cell != null ) break;
{
power += this.handlersBySlot[x].cellIdleDrain( is, cell );
final DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, this.handlersBySlot[x], this );
ih.setPriority( this.priority );
this.invBySlot[x] = ih;
this.fluids.add( ih );
} }
} }
} }
@@ -362,9 +343,10 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
if( this.getProxy().isActive() ) if( this.getProxy().isActive() )
{ {
this.updateState(); this.updateState();
return (List) ( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ? this.items : this.fluids );
return this.inventoryHandlers.get( channel );
} }
return new ArrayList(); return Collections.emptyList();
} }
@Override @Override