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
+20 -16
View File
@@ -99,7 +99,8 @@ import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
@@ -1135,9 +1136,9 @@ public class Platform
return 0;
}
public static <StackType extends IAEStack> StackType poweredExtraction( final IEnergySource energy, final IMEInventory<StackType> cell, final StackType request, final IActionSource src )
public static <T extends IAEStack<T>> T poweredExtraction( final IEnergySource energy, final IMEInventory<T> cell, final T request, final IActionSource src )
{
final StackType possible = cell.extractItems( (StackType) request.copy(), Actionable.SIMULATE, src );
final T possible = cell.extractItems( (T) request.copy(), Actionable.SIMULATE, src );
long retrieved = 0;
if( possible != null )
@@ -1154,7 +1155,7 @@ public class Platform
energy.extractAEPower( retrieved, Actionable.MODULATE, PowerMultiplier.CONFIG );
possible.setStackSize( itemToExtract );
final StackType ret = cell.extractItems( possible, Actionable.MODULATE, src );
final T ret = cell.extractItems( possible, Actionable.MODULATE, src );
if( ret != null )
{
@@ -1167,9 +1168,9 @@ public class Platform
return null;
}
public static <StackType extends IAEStack> StackType poweredInsert( final IEnergySource energy, final IMEInventory<StackType> cell, final StackType input, final IActionSource src )
public static <T extends IAEStack<T>> T poweredInsert( final IEnergySource energy, final IMEInventory<T> cell, final T input, final IActionSource src )
{
final StackType possible = cell.injectItems( (StackType) input.copy(), Actionable.SIMULATE, src );
final T possible = cell.injectItems( (T) input.copy(), Actionable.SIMULATE, src );
long stored = input.getStackSize();
if( possible != null )
@@ -1188,7 +1189,7 @@ public class Platform
if( itemToAdd < input.getStackSize() )
{
final long original = input.getStackSize();
final StackType split = (StackType) input.copy();
final T split = (T) input.copy();
split.decStackSize( itemToAdd );
input.setStackSize( itemToAdd );
split.add( cell.injectItems( input, Actionable.MODULATE, src ) );
@@ -1202,7 +1203,7 @@ public class Platform
return split;
}
final StackType ret = cell.injectItems( input, Actionable.MODULATE, src );
final T ret = cell.injectItems( input, Actionable.MODULATE, src );
src.player().ifPresent( player ->
{
@@ -1218,12 +1219,15 @@ public class Platform
public static void postChanges( final IStorageGrid gs, final ItemStack removed, final ItemStack added, final IActionSource src )
{
final IItemList<IAEItemStack> itemChanges = AEApi.instance().storage().createItemList();
final IItemList<IAEFluidStack> fluidChanges = AEApi.instance().storage().createFluidList();
final IItemStorageChannel itemChannel = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class );
final IFluidStorageChannel fluidChannel = AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class );
final IItemList<IAEItemStack> itemChanges = itemChannel.createList();
final IItemList<IAEFluidStack> fluidChanges = fluidChannel.createList();
if( !removed.isEmpty() )
{
final IMEInventory<IAEItemStack> myItems = AEApi.instance().registries().cell().getCellInventory( removed, null, StorageChannel.ITEMS );
final IMEInventory<IAEItemStack> myItems = AEApi.instance().registries().cell().getCellInventory( removed, null, itemChannel );
if( myItems != null )
{
@@ -1233,7 +1237,7 @@ public class Platform
}
}
final IMEInventory<IAEFluidStack> myFluids = AEApi.instance().registries().cell().getCellInventory( removed, null, StorageChannel.FLUIDS );
final IMEInventory<IAEFluidStack> myFluids = AEApi.instance().registries().cell().getCellInventory( removed, null, fluidChannel );
if( myFluids != null )
{
@@ -1246,14 +1250,14 @@ public class Platform
if( !added.isEmpty() )
{
final IMEInventory<IAEItemStack> myItems = AEApi.instance().registries().cell().getCellInventory( added, null, StorageChannel.ITEMS );
final IMEInventory<IAEItemStack> myItems = AEApi.instance().registries().cell().getCellInventory( added, null, itemChannel );
if( myItems != null )
{
myItems.getAvailableItems( itemChanges );
}
final IMEInventory<IAEFluidStack> myFluids = AEApi.instance().registries().cell().getCellInventory( added, null, StorageChannel.FLUIDS );
final IMEInventory<IAEFluidStack> myFluids = AEApi.instance().registries().cell().getCellInventory( added, null, fluidChannel );
if( myFluids != null )
{
@@ -1261,7 +1265,7 @@ public class Platform
}
}
gs.postAlterationOfStoredItems( StorageChannel.ITEMS, itemChanges, src );
gs.postAlterationOfStoredItems( itemChannel, itemChanges, src );
}
public static <T extends IAEStack<T>> void postListChanges( final IItemList<T> before, final IItemList<T> after, final IMEMonitorHandlerReceiver<T> meMonitorPassthrough, final IActionSource source )
@@ -1444,7 +1448,7 @@ public class Platform
return ItemStack.EMPTY;
}
final AEItemStack ae_req = AEItemStack.create( providedTemplate );
final AEItemStack ae_req = AEItemStack.fromItemStack( providedTemplate );
ae_req.setStackSize( 1 );
if( filter == null || filter.isListed( ae_req ) )
@@ -30,6 +30,7 @@ import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.util.InventoryAdaptor;
@@ -63,7 +64,7 @@ public class IMEAdaptor extends InventoryAdaptor
private IItemList<IAEItemStack> getList()
{
return this.target.getAvailableItems( AEApi.instance().storage().createItemList() );
return this.target.getAvailableItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() );
}
@Override
@@ -86,7 +87,7 @@ public class IMEAdaptor extends InventoryAdaptor
}
else
{
req = AEItemStack.create( filter );
req = AEItemStack.fromItemStack( filter );
}
IAEItemStack out = null;
@@ -123,7 +124,7 @@ public class IMEAdaptor extends InventoryAdaptor
private ItemStack doRemoveItemsFuzzy( final int amount, final ItemStack filter, final IInventoryDestination destination, final Actionable type, final FuzzyMode fuzzyMode )
{
final IAEItemStack reqFilter = AEItemStack.create( filter );
final IAEItemStack reqFilter = AEItemStack.fromItemStack( filter );
if( reqFilter == null )
{
return ItemStack.EMPTY;
@@ -160,7 +161,7 @@ public class IMEAdaptor extends InventoryAdaptor
@Override
public ItemStack addItems( final ItemStack toBeAdded )
{
final IAEItemStack in = AEItemStack.create( toBeAdded );
final IAEItemStack in = AEItemStack.fromItemStack( toBeAdded );
if( in != null )
{
final IAEItemStack out = this.target.injectItems( in, Actionable.MODULATE, this.src );
@@ -175,7 +176,7 @@ public class IMEAdaptor extends InventoryAdaptor
@Override
public ItemStack simulateAdd( final ItemStack toBeSimulated )
{
final IAEItemStack in = AEItemStack.create( toBeSimulated );
final IAEItemStack in = AEItemStack.fromItemStack( toBeSimulated );
if( in != null )
{
final IAEItemStack out = this.target.injectItems( in, Actionable.SIMULATE, this.src );
@@ -46,7 +46,7 @@ public class IMEInventoryDestination implements IInventoryDestination
return false;
}
final IAEItemStack failed = this.me.injectItems( AEItemStack.create( stack ), Actionable.SIMULATE, null );
final IAEItemStack failed = this.me.injectItems( AEItemStack.fromItemStack( stack ), Actionable.SIMULATE, null );
if( failed == null )
{
@@ -27,7 +27,7 @@ import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
public class ItemListIgnoreCrafting<T extends IAEStack> implements IItemList<T>
public class ItemListIgnoreCrafting<T extends IAEStack<T>> implements IItemList<T>
{
private final IItemList<T> target;
+1 -1
View File
@@ -48,7 +48,7 @@ public class ItemSlot
public IAEItemStack getAEItemStack()
{
return this.aeItemStack == null ? ( this.itemStack.isEmpty() ? null : ( this.aeItemStack = AEItemStack.create( this.itemStack ) ) ) : this.aeItemStack;
return this.aeItemStack == null ? ( this.itemStack.isEmpty() ? null : ( this.aeItemStack = AEItemStack.fromItemStack( this.itemStack ) ) ) : this.aeItemStack;
}
void setAEItemStack( final IAEItemStack is )
@@ -37,10 +37,11 @@ import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEStack;
import appeng.util.Platform;
@@ -80,14 +81,26 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
this.myHash = this.fluid.hashCode() ^ ( this.tagCompound == null ? 0 : System.identityHashCode( this.tagCompound ) );
}
public static IAEFluidStack loadFluidStackFromNBT( final NBTTagCompound i )
public static AEFluidStack fromFluidStack( final FluidStack input )
{
final ItemStack itemstack = new ItemStack( i );
if( itemstack.isEmpty() )
if( input == null )
{
return null;
}
final AEFluidStack fluid = AEFluidStack.create( itemstack );
return new AEFluidStack( input );
}
public static IAEFluidStack fromNBT( final NBTTagCompound i )
{
final FluidStack fluidStack = FluidStack.loadFluidStackFromNBT( i );
if( fluidStack == null )
{
return null;
}
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
// fluid.priority = i.getInteger( "Priority" );
fluid.setStackSize( i.getLong( "Cnt" ) );
fluid.setCountRequestable( i.getLong( "Req" ) );
@@ -95,24 +108,7 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
return fluid;
}
public static AEFluidStack create( final Object a )
{
if( a == null )
{
return null;
}
if( a instanceof AEFluidStack )
{
( (IAEStack<IAEFluidStack>) a ).copy();
}
if( a instanceof FluidStack )
{
return new AEFluidStack( (FluidStack) a );
}
return null;
}
public static IAEFluidStack loadFluidStackFromPacket( final ByteBuf data ) throws IOException
public static IAEFluidStack fromPacket( final ByteBuf data ) throws IOException
{
final byte mask = data.readByte();
// byte PriorityType = (byte) (mask & 0x03);
@@ -159,7 +155,7 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
return null;
}
final AEFluidStack fluid = AEFluidStack.create( fluidStack );
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
// fluid.priority = (int) priority;
fluid.setStackSize( stackSize );
fluid.setCountRequestable( countRequestable );
@@ -275,9 +271,9 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
}
@Override
public StorageChannel getChannel()
public IStorageChannel<IAEFluidStack> getChannel()
{
return StorageChannel.FLUIDS;
return AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class );
}
@Override
+21 -18
View File
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import io.netty.buffer.ByteBuf;
@@ -36,8 +37,10 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.Platform;
@@ -72,7 +75,18 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
this.oreReference = OreHelper.INSTANCE.getOre( is.getDefinition() );
}
public static IAEItemStack loadItemStackFromNBT( final NBTTagCompound i )
@Nullable
public static AEItemStack fromItemStack( @Nonnull final ItemStack stack )
{
if( stack.isEmpty() )
{
return null;
}
return new AEItemStack( AEItemStackRegistry.getRegisteredStack( stack ), stack.getCount() );
}
public static IAEItemStack fromNBT( final NBTTagCompound i )
{
if( i == null )
{
@@ -85,25 +99,14 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return null;
}
final AEItemStack item = AEItemStack.create( itemstack );
final AEItemStack item = AEItemStack.fromItemStack( itemstack );
item.setStackSize( i.getLong( "Cnt" ) );
item.setCountRequestable( i.getLong( "Req" ) );
item.setCraftable( i.getBoolean( "Craft" ) );
return item;
}
@Nullable
public static AEItemStack create( final ItemStack stack )
{
if( stack.isEmpty() )
{
return null;
}
return new AEItemStack( AEItemStackRegistry.getRegisteredStack( stack ), stack.getCount() );
}
public static IAEItemStack loadItemStackFromPacket( final ByteBuf data ) throws IOException
public static IAEItemStack fromPacket( final ByteBuf data ) throws IOException
{
final byte mask = data.readByte();
// byte PriorityType = (byte) (mask & 0x03);
@@ -120,7 +123,7 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return null;
}
final AEItemStack item = AEItemStack.create( itemstack );
final AEItemStack item = AEItemStack.fromItemStack( itemstack );
item.setStackSize( stackSize );
item.setCountRequestable( countRequestable );
item.setCraftable( isCraftable );
@@ -298,9 +301,9 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
}
@Override
public StorageChannel getChannel()
public IStorageChannel<IAEItemStack> getChannel()
{
return StorageChannel.ITEMS;
return AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class );
}
@Override
@@ -23,6 +23,7 @@ import java.util.Collection;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemContainer;
@@ -31,7 +32,7 @@ public class ItemModList implements IItemContainer<IAEItemStack>
{
private final IItemContainer<IAEItemStack> backingStore;
private final IItemContainer<IAEItemStack> overrides = AEApi.instance().storage().createItemList();
private final IItemContainer<IAEItemStack> overrides = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList();
public ItemModList( final IItemContainer<IAEItemStack> backend )
{
@@ -57,7 +57,7 @@ public class OreReference
{
if( is.getItem() != Items.AIR )
{
this.aeOtherOptions.add( AEItemStack.create( is ) );
this.aeOtherOptions.add( AEItemStack.fromItemStack( is ) );
}
}
}
@@ -19,8 +19,7 @@
package appeng.util.prioritylist;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import appeng.api.storage.data.IAEStack;
@@ -28,8 +27,6 @@ import appeng.api.storage.data.IAEStack;
public class DefaultPriorityList<T extends IAEStack<T>> implements IPartitionList<T>
{
private static final List NULL_LIST = new ArrayList();
@Override
public boolean isListed( final T input )
{
@@ -45,6 +42,6 @@ public class DefaultPriorityList<T extends IAEStack<T>> implements IPartitionLis
@Override
public Iterable<T> getItems()
{
return NULL_LIST;
return Collections.emptyList();
}
}