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
@@ -0,0 +1,65 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.api;
import com.google.common.base.Preconditions;
import appeng.api.exceptions.FailedConnection;
import appeng.api.networking.IGridBlock;
import appeng.api.networking.IGridConnection;
import appeng.api.networking.IGridHelper;
import appeng.api.networking.IGridNode;
import appeng.api.util.AEPartLocation;
import appeng.me.GridConnection;
import appeng.me.GridNode;
import appeng.util.Platform;
/**
* @author yueh
* @version rv5
* @since rv5
*/
public class ApiGrid implements IGridHelper
{
@Override
public IGridNode createGridNode( final IGridBlock blk )
{
Preconditions.checkNotNull( blk );
if( Platform.isClient() )
{
throw new IllegalStateException( "Grid features for " + blk + " are server side only." );
}
return new GridNode( blk );
}
@Override
public IGridConnection createGridConnection( final IGridNode a, final IGridNode b ) throws FailedConnection
{
Preconditions.checkNotNull( a );
Preconditions.checkNotNull( b );
return new GridConnection( a, b, AEPartLocation.INTERNAL );
}
}
+151 -41
View File
@@ -20,6 +20,12 @@ package appeng.core.api;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import com.google.common.base.Preconditions;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;
import io.netty.buffer.ByteBuf;
@@ -32,9 +38,13 @@ import appeng.api.networking.crafting.ICraftingRequester;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.IStorageHelper;
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;
import appeng.api.storage.data.IItemList;
import appeng.crafting.CraftingLink;
import appeng.util.Platform;
@@ -47,57 +57,157 @@ import appeng.util.item.ItemList;
public class ApiStorage implements IStorageHelper
{
private final ClassToInstanceMap<IStorageChannel<?>> channels;
public ApiStorage()
{
this.channels = MutableClassToInstanceMap.create();
this.registerStorageChannel( IItemStorageChannel.class, new ItemStorageChannel() );
this.registerStorageChannel( IFluidStorageChannel.class, new FluidStorageChannel() );
}
@Override
public <T extends IAEStack<T>, C extends IStorageChannel<T>> void registerStorageChannel( Class<C> channel, C factory )
{
Preconditions.checkNotNull( channel );
Preconditions.checkNotNull( factory );
Preconditions.checkArgument( channel.isInstance( factory ) );
Preconditions.checkArgument( !this.channels.containsKey( channel ) );
this.channels.putInstance( channel, factory );
}
@Override
public <T extends IAEStack<T>, C extends IStorageChannel<T>> C getStorageChannel( Class<C> channel )
{
Preconditions.checkNotNull( channel );
final C type = this.channels.getInstance( channel );
Preconditions.checkNotNull( type );
return type;
}
@Override
public Collection<IStorageChannel<? extends IAEStack<?>>> storageChannels()
{
return Collections.unmodifiableCollection( this.channels.values() );
}
@Override
public ICraftingLink loadCraftingLink( final NBTTagCompound data, final ICraftingRequester req )
{
Preconditions.checkNotNull( data );
Preconditions.checkNotNull( req );
return new CraftingLink( data, req );
}
@Override
public IAEItemStack createItemStack( final ItemStack is )
private static final class ItemStorageChannel implements IItemStorageChannel
{
return AEItemStack.create( is );
@Override
public IItemList<IAEItemStack> createList()
{
return new ItemList();
}
@Override
public IAEItemStack createStack( Object input )
{
Preconditions.checkNotNull( input );
if( input instanceof ItemStack )
{
return AEItemStack.fromItemStack( (ItemStack) input );
}
return null;
}
@Override
public IAEItemStack readFromPacket( ByteBuf input ) throws IOException
{
Preconditions.checkNotNull( input );
return AEItemStack.fromPacket( input );
}
@Override
public IAEItemStack poweredExtraction( IEnergySource energy, IMEInventory<IAEItemStack> cell, IAEItemStack request, IActionSource src )
{
Preconditions.checkNotNull( energy );
Preconditions.checkNotNull( cell );
Preconditions.checkNotNull( request );
Preconditions.checkNotNull( src );
return Platform.poweredExtraction( energy, cell, request, src );
}
@Override
public IAEItemStack poweredInsert( IEnergySource energy, IMEInventory<IAEItemStack> cell, IAEItemStack input, IActionSource src )
{
Preconditions.checkNotNull( energy );
Preconditions.checkNotNull( cell );
Preconditions.checkNotNull( input );
Preconditions.checkNotNull( src );
return Platform.poweredInsert( energy, cell, input, src );
}
}
@Override
public IAEFluidStack createFluidStack( final FluidStack is )
private static final class FluidStorageChannel implements IFluidStorageChannel
{
return AEFluidStack.create( is );
@Override
public IItemList<IAEFluidStack> createList()
{
return new FluidList();
}
@Override
public IAEFluidStack createStack( Object input )
{
Preconditions.checkNotNull( input );
if( input instanceof FluidStack )
{
return AEFluidStack.fromFluidStack( (FluidStack) input );
}
return null;
}
@Override
public IAEFluidStack readFromPacket( ByteBuf input ) throws IOException
{
Preconditions.checkNotNull( input );
return AEFluidStack.fromPacket( input );
}
@Override
public IAEFluidStack poweredExtraction( IEnergySource energy, IMEInventory<IAEFluidStack> cell, IAEFluidStack request, IActionSource src )
{
Preconditions.checkNotNull( energy );
Preconditions.checkNotNull( cell );
Preconditions.checkNotNull( request );
Preconditions.checkNotNull( src );
return null;
}
@Override
public IAEFluidStack poweredInsert( IEnergySource energy, IMEInventory<IAEFluidStack> cell, IAEFluidStack input, IActionSource src )
{
Preconditions.checkNotNull( energy );
Preconditions.checkNotNull( cell );
Preconditions.checkNotNull( input );
Preconditions.checkNotNull( src );
return input;
}
}
@Override
public IItemList<IAEItemStack> createItemList()
{
return new ItemList();
}
@Override
public IItemList<IAEFluidStack> createFluidList()
{
return new FluidList();
}
@Override
public IAEItemStack readItemFromPacket( final ByteBuf input ) throws IOException
{
return AEItemStack.loadItemStackFromPacket( input );
}
@Override
public IAEFluidStack readFluidFromPacket( final ByteBuf input ) throws IOException
{
return AEFluidStack.loadFluidStackFromPacket( input );
}
@Override
public IAEItemStack poweredExtraction( final IEnergySource energy, final IMEInventory<IAEItemStack> cell, final IAEItemStack request, final IActionSource src )
{
return Platform.poweredExtraction( energy, cell, request, src );
}
@Override
public IAEItemStack poweredInsert( final IEnergySource energy, final IMEInventory<IAEItemStack> cell, final IAEItemStack input, final IActionSource src )
{
return Platform.poweredInsert( energy, cell, input, src );
}
}