Replaces old fluid handlers with the new capability based ones. (#2727)

* Changed ME Chest and P2P tunnels to use fluid capabilities.
* Renamed all occurrences of liquid to fluids.
This commit is contained in:
yueh 2016-12-31 16:01:29 +01:00 committed by GitHub
parent 7c5f777e41
commit ab89278274
9 changed files with 212 additions and 204 deletions

View file

@ -76,7 +76,7 @@ public interface IParts
IItemDefinition p2PTunnelItems();
IItemDefinition p2PTunnelLiquids();
IItemDefinition p2PTunnelFluids();
IItemDefinition p2PTunnelEU();

View file

@ -61,7 +61,7 @@ public final class ApiParts implements IParts
private final IItemDefinition p2PTunnelME;
private final IItemDefinition p2PTunnelRedstone;
private final IItemDefinition p2PTunnelItems;
private final IItemDefinition p2PTunnelLiquids;
private final IItemDefinition p2PTunnelFluids;
private final IItemDefinition p2PTunnelEU;
// private final IItemDefinition p2PTunnelRF;
private final IItemDefinition p2PTunnelLight;
@ -112,7 +112,7 @@ public final class ApiParts implements IParts
this.p2PTunnelME = new DamagedItemDefinition( "part.tunnel.me", itemPart.createPart( PartType.P2PTunnelME ) );
this.p2PTunnelRedstone = new DamagedItemDefinition( "part.tunnel.redstone", itemPart.createPart( PartType.P2PTunnelRedstone ) );
this.p2PTunnelItems = new DamagedItemDefinition( "part.tunnel.item", itemPart.createPart( PartType.P2PTunnelItems ) );
this.p2PTunnelLiquids = new DamagedItemDefinition( "part.tunnel.fluid", itemPart.createPart( PartType.P2PTunnelLiquids ) );
this.p2PTunnelFluids = new DamagedItemDefinition( "part.tunnel.fluid", itemPart.createPart( PartType.P2PTunnelFluids ) );
this.p2PTunnelEU = new DamagedItemDefinition( "part.tunnel.eu", itemPart.createPart( PartType.P2PTunnelEU ) );
// this.p2PTunnelRF = new DamagedItemDefinition( itemMultiPart.createPart( PartType.P2PTunnelRF ) );
this.p2PTunnelLight = new DamagedItemDefinition( "part.tunnel.light", itemPart.createPart( PartType.P2PTunnelLight ) );
@ -281,9 +281,9 @@ public final class ApiParts implements IParts
}
@Override
public IItemDefinition p2PTunnelLiquids()
public IItemDefinition p2PTunnelFluids()
{
return this.p2PTunnelLiquids;
return this.p2PTunnelFluids;
}
@Override

View file

@ -118,7 +118,7 @@ public enum AEFeature
P2P_TUNNEL_ITEMS( "P2PTunnelItems", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_REDSTONE( "P2PTunnelRedstone", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_EU( "P2PTunnelEU", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_LIQUIDS( "P2PTunnelLiquids", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_FLUIDS( "P2PTunnelFluids", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_LIGHT( "P2PTunnelLight", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_OPEN_COMPUTERS( "P2PTunnelOpenComputers", Constants.CATEGORY_P2P_TUNNELS ),
P2P_TUNNEL_PRESSURE( "P2PTunnelPressure", Constants.CATEGORY_P2P_TUNNELS ),

View file

@ -59,10 +59,10 @@ import appeng.parts.networking.PartCableGlass;
import appeng.parts.networking.PartCableSmart;
import appeng.parts.networking.PartDenseCable;
import appeng.parts.networking.PartQuartzFiber;
import appeng.parts.p2p.PartP2PFluids;
import appeng.parts.p2p.PartP2PIC2Power;
import appeng.parts.p2p.PartP2PItems;
import appeng.parts.p2p.PartP2PLight;
import appeng.parts.p2p.PartP2PLiquids;
import appeng.parts.p2p.PartP2PRedstone;
import appeng.parts.p2p.PartP2PTunnelME;
import appeng.parts.reporting.PartConversionMonitor;
@ -226,8 +226,8 @@ public enum PartType
}
},
P2PTunnelLiquids( 463, "p2p_tunnel_liquids", EnumSet.of( AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_LIQUIDS ), EnumSet
.noneOf( IntegrationType.class ), PartP2PLiquids.class, GuiText.FluidTunnel )
P2PTunnelFluids( 463, "p2p_tunnel_fluids", EnumSet.of( AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_FLUIDS ), EnumSet
.noneOf( IntegrationType.class ), PartP2PFluids.class, GuiText.FluidTunnel )
{
@Override
String getUnlocalizedName()

View file

@ -19,28 +19,42 @@
package appeng.parts.p2p;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.FluidTankProperties;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
import appeng.api.parts.IPartModel;
import appeng.items.parts.PartModels;
import appeng.me.GridAccessException;
public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFluidHandler
public class PartP2PFluids extends PartP2PTunnel<PartP2PFluids> implements IFluidHandler
{
private static final P2PModels MODELS = new P2PModels( "part/p2p/p2p_tunnel_liquids" );
private static final P2PModels MODELS = new P2PModels( "part/p2p/p2p_tunnel_fluids" );
private static final ThreadLocal<Deque<PartP2PFluids>> DEPTH = new ThreadLocal<>();
private static final FluidTankProperties[] ACTIVE_TANK = { new FluidTankProperties( null, 10000, true, false ) };
private static final FluidTankProperties[] INACTIVE_TANK = { new FluidTankProperties( null, 0, false, false ) };
private IFluidHandler cachedTank;
private int tmpUsed;
public PartP2PFluids( final ItemStack is )
{
super( is );
}
@PartModels
public static List<IPartModel> getModels()
@ -48,17 +62,6 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
return MODELS.getModels();
}
private static final ThreadLocal<Stack<PartP2PLiquids>> DEPTH = new ThreadLocal<Stack<PartP2PLiquids>>();
private static final FluidTankInfo[] ACTIVE_TANK = { new FluidTankInfo( null, 10000 ) };
private static final FluidTankInfo[] INACTIVE_TANK = { new FluidTankInfo( null, 0 ) };
private IFluidHandler cachedTank;
private int tmpUsed;
public PartP2PLiquids( final ItemStack is )
{
super( is );
}
public float getPowerDrainPerTick()
{
return 2.0f;
@ -74,9 +77,10 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
public void onNeighborChanged()
{
this.cachedTank = null;
if( this.isOutput() )
{
final PartP2PLiquids in = this.getInput();
final PartP2PFluids in = this.getInput();
if( in != null )
{
in.onTunnelNetworkChange();
@ -85,11 +89,54 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
}
@Override
public int fill( final EnumFacing from, final FluidStack resource, final boolean doFill )
public boolean hasCapability( Capability<?> capabilityClass )
{
final Stack<PartP2PLiquids> stack = this.getDepth();
if( capabilityClass == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY )
{
return true;
}
for( final PartP2PLiquids t : stack )
return super.hasCapability( capabilityClass );
}
@Override
public <T> T getCapability( Capability<T> capabilityClass )
{
if( capabilityClass == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY )
{
return (T) this;
}
return super.getCapability( capabilityClass );
}
@Override
public IPartModel getStaticModels()
{
return MODELS.getModel( isPowered(), isActive() );
}
@Override
public IFluidTankProperties[] getTankProperties()
{
if( !this.isOutput() )
{
final PartP2PFluids tun = this.getInput();
if( tun != null )
{
return ACTIVE_TANK;
}
}
return INACTIVE_TANK;
}
@Override
public int fill( FluidStack resource, boolean doFill )
{
final Deque<PartP2PFluids> stack = this.getDepth();
for( final PartP2PFluids t : stack )
{
if( t == this )
{
@ -99,17 +146,18 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
stack.push( this );
final List<PartP2PLiquids> list = this.getOutputs( resource.getFluid() );
final List<PartP2PFluids> list = this.getOutputs( resource.getFluid() );
int requestTotal = 0;
Iterator<PartP2PLiquids> i = list.iterator();
Iterator<PartP2PFluids> i = list.iterator();
while( i.hasNext() )
{
final PartP2PLiquids l = i.next();
final PartP2PFluids l = i.next();
final IFluidHandler tank = l.getTarget();
if( tank != null )
{
l.tmpUsed = tank.fill( l.getSide().getFacing().getOpposite(), resource.copy(), false );
l.tmpUsed = tank.fill( resource.copy(), false );
}
else
{
@ -150,9 +198,10 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
i = list.iterator();
int used = 0;
while( i.hasNext() )
{
final PartP2PLiquids l = i.next();
final PartP2PFluids l = i.next();
final FluidStack insert = resource.copy();
insert.amount = (int) Math.ceil( insert.amount * ( (double) l.tmpUsed / (double) requestTotal ) );
@ -164,7 +213,7 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
final IFluidHandler tank = l.getTarget();
if( tank != null )
{
l.tmpUsed = tank.fill( l.getSide().getFacing().getOpposite(), insert.copy(), true );
l.tmpUsed = tank.fill( insert.copy(), true );
}
else
{
@ -183,33 +232,43 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
return used;
}
private Stack<PartP2PLiquids> getDepth()
@Override
public FluidStack drain( FluidStack resource, boolean doDrain )
{
Stack<PartP2PLiquids> s = DEPTH.get();
return null;
}
@Override
public FluidStack drain( int maxDrain, boolean doDrain )
{
return null;
}
private Deque<PartP2PFluids> getDepth()
{
Deque<PartP2PFluids> s = DEPTH.get();
if( s == null )
{
DEPTH.set( s = new Stack<PartP2PLiquids>() );
DEPTH.set( s = new LinkedList<>() );
}
return s;
}
private List<PartP2PLiquids> getOutputs( final Fluid input )
private List<PartP2PFluids> getOutputs( final Fluid input )
{
final List<PartP2PLiquids> outs = new LinkedList<PartP2PLiquids>();
final List<PartP2PFluids> outs = new LinkedList<>();
try
{
for( final PartP2PLiquids l : this.getOutputs() )
for( final PartP2PFluids l : this.getOutputs() )
{
final IFluidHandler handler = l.getTarget();
if( handler != null )
{
if( handler.canFill( l.getSide().getFacing().getOpposite(), input ) )
{
outs.add( l );
}
outs.add( l );
}
}
}
@ -234,79 +293,14 @@ public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFl
}
final TileEntity te = this.getTile().getWorld().getTileEntity( this.getTile().getPos().offset( this.getSide().getFacing() ) );
if( te instanceof IFluidHandler )
if( te != null && te.hasCapability( CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, this.getSide().getFacing().getOpposite() ) )
{
return this.cachedTank = (IFluidHandler) te;
return this.cachedTank = te.getCapability( CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY,
this.getSide().getFacing().getOpposite() );
}
return null;
}
@Override
public FluidStack drain( final EnumFacing from, final FluidStack resource, final boolean doDrain )
{
return null;
}
@Override
public FluidStack drain( final EnumFacing from, final int maxDrain, final boolean doDrain )
{
return null;
}
@Override
public boolean canFill( final EnumFacing from, final Fluid fluid )
{
return !this.isOutput() && from == this.getSide().getFacing() && !this.getOutputs( fluid ).isEmpty();
}
@Override
public boolean canDrain( final EnumFacing from, final Fluid fluid )
{
return false;
}
@Override
public FluidTankInfo[] getTankInfo( final EnumFacing from )
{
if( from == this.getSide().getFacing() )
{
return this.getTank();
}
return new FluidTankInfo[0];
}
private FluidTankInfo[] getTank()
{
if( this.isOutput() )
{
final PartP2PLiquids tun = this.getInput();
if( tun != null )
{
return ACTIVE_TANK;
}
}
else
{
try
{
if( !this.getOutputs().isEmpty() )
{
return ACTIVE_TANK;
}
}
catch( final GridAccessException e )
{
// :(
}
}
return INACTIVE_TANK;
}
@Override
public IPartModel getStaticModels()
{
return MODELS.getModel( isPowered(), isActive() );
}
}

View file

@ -232,7 +232,7 @@ public abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
*/
case FLUID:
newType = parts.p2PTunnelLiquids().maybeStack( 1 ).orElse( null );
newType = parts.p2PTunnelFluids().maybeStack( 1 ).orElse( null );
break;
case IC2_POWER:

View file

@ -35,10 +35,11 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.FluidTankProperties;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
import appeng.api.AEApi;
import appeng.api.config.AccessRestriction;
@ -98,7 +99,7 @@ import appeng.util.Platform;
import appeng.util.item.AEFluidStack;
public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHandler, ITerminalHost, IPriorityHost, IConfigManagerHost, IColorableTile, ITickable
public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminalHost, IPriorityHost, IConfigManagerHost, IColorableTile, ITickable
{
private static final ChestNoHandler NO_HANDLER = new ChestNoHandler();
@ -118,7 +119,8 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
private ICellHandler cellHandler;
private MEMonitorHandler itemCell;
private MEMonitorHandler fluidCell;
private final Accessor accessor = new Accessor();
private Accessor accessor;
private IFluidHandler fluidHandler;
public TileChest()
{
@ -203,6 +205,9 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
this.itemCell = null;
this.fluidCell = null;
this.accessor = null;
this.fluidHandler = null;
final ItemStack is = this.inv.getStackInSlot( 1 );
if( is != null )
{
@ -228,6 +233,17 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
this.itemCell = this.wrap( itemCell );
this.fluidCell = this.wrap( fluidCell );
if( this.itemCell != null )
{
}
this.accessor = new Accessor();
if( this.fluidCell != null )
{
this.fluidHandler = new FluidHandler();
}
}
}
}
@ -468,7 +484,8 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
this.lastStateChange = this.worldObj.getTotalWorldTime();
return oldPaintedColor != this.paintedColor || ( this.state & 0xDB6DB6DB ) != ( oldState & 0xDB6DB6DB ) || !Platform.itemComparisons().isSameItem( oldType, this.storageType );
return oldPaintedColor != this.paintedColor || ( this.state & 0xDB6DB6DB ) != ( oldState & 0xDB6DB6DB ) || !Platform.itemComparisons()
.isSameItem( oldType, this.storageType );
}
@TileEvent( TileEventType.WORLD_NBT_READ )
@ -576,7 +593,8 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
try
{
final IMEInventory<IAEItemStack> cell = this.getHandler( StorageChannel.ITEMS );
final IAEItemStack returns = cell.injectItems( AEApi.instance().storage().createItemStack( this.inv.getStackInSlot( 0 ) ), Actionable.SIMULATE, this.mySrc );
final IAEItemStack returns = cell.injectItems( AEApi.instance().storage().createItemStack( this.inv.getStackInSlot( 0 ) ), Actionable.SIMULATE,
this.mySrc );
return returns == null || returns.getStackSize() != insertingItem.stackSize;
}
catch( final ChestNoHandler ignored )
@ -625,7 +643,8 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
{
final IMEInventory<IAEItemStack> cell = this.getHandler( StorageChannel.ITEMS );
final IAEItemStack returns = Platform.poweredInsert( this, cell, AEApi.instance().storage().createItemStack( this.inv.getStackInSlot( 0 ) ), this.mySrc );
final IAEItemStack returns = Platform.poweredInsert( this, cell, AEApi.instance().storage().createItemStack( this.inv.getStackInSlot( 0 ) ),
this.mySrc );
if( returns == null )
{
@ -699,84 +718,6 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
this.recalculateDisplay();
}
@Override
public int fill( final EnumFacing from, final FluidStack resource, final boolean doFill )
{
final double req = resource.amount / 500.0;
final double available = this.extractAEPower( req, Actionable.SIMULATE, PowerMultiplier.CONFIG );
if( available >= req - 0.01 )
{
try
{
final IMEInventoryHandler h = this.getHandler( StorageChannel.FLUIDS );
this.extractAEPower( req, Actionable.MODULATE, PowerMultiplier.CONFIG );
final IAEStack results = h.injectItems( AEFluidStack.create( resource ), doFill ? Actionable.MODULATE : Actionable.SIMULATE, this.mySrc );
if( results == null )
{
return resource.amount;
}
return resource.amount - (int) results.getStackSize();
}
catch( final ChestNoHandler ignored )
{
}
}
return 0;
}
@Override
public FluidStack drain( final EnumFacing from, final FluidStack resource, final boolean doDrain )
{
return null;
}
@Override
public FluidStack drain( final EnumFacing from, final int maxDrain, final boolean doDrain )
{
return null;
}
@Override
public boolean canFill( final EnumFacing from, final Fluid fluid )
{
try
{
final IMEInventoryHandler h = this.getHandler( StorageChannel.FLUIDS );
return h.canAccept( AEFluidStack.create( new FluidStack( fluid, 1 ) ) );
}
catch( final ChestNoHandler ignored )
{
}
return false;
}
@Override
public boolean canDrain( final EnumFacing from, final Fluid fluid )
{
return false;
}
@Override
public FluidTankInfo[] getTankInfo( final EnumFacing from )
{
try
{
final IMEInventoryHandler h = this.getHandler( StorageChannel.FLUIDS );
if( h.getChannel() == StorageChannel.FLUIDS )
{
return new FluidTankInfo[] { new FluidTankInfo( null, 1 ) }; // eh?
}
}
catch( final ChestNoHandler ignored )
{
}
return null;
}
public ItemStack getStorageType()
{
if( this.isPowered() )
@ -990,7 +931,11 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
@Override
public boolean hasCapability( Capability<?> capability, EnumFacing facing )
{
if( capability == Capabilities.STORAGE_MONITORABLE_ACCESSOR && facing != getForward() )
if( capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && this.fluidHandler != null && facing != getForward() )
{
return true;
}
if( capability == Capabilities.STORAGE_MONITORABLE_ACCESSOR && this.accessor != null && facing != getForward() )
{
return true;
}
@ -1001,7 +946,11 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
@Override
public <T> T getCapability( Capability<T> capability, @Nullable EnumFacing facing )
{
if( capability == Capabilities.STORAGE_MONITORABLE_ACCESSOR && facing != getForward() )
if( capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && this.fluidHandler != null && facing != getForward() )
{
return (T) this.fluidHandler;
}
if( capability == Capabilities.STORAGE_MONITORABLE_ACCESSOR && this.accessor != null && facing != getForward() )
{
return (T) accessor;
}
@ -1022,4 +971,69 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
}
}
private class FluidHandler implements IFluidHandler
{
@Override
public int fill( final FluidStack resource, final boolean doFill )
{
final double req = resource.amount / 500.0;
final double available = TileChest.this.extractAEPower( req, Actionable.SIMULATE, PowerMultiplier.CONFIG );
if( available >= req - 0.01 )
{
try
{
final IMEInventoryHandler h = TileChest.this.getHandler( StorageChannel.FLUIDS );
TileChest.this.extractAEPower( req, Actionable.MODULATE, PowerMultiplier.CONFIG );
final IAEStack results = h.injectItems( AEFluidStack.create( resource ), doFill ? Actionable.MODULATE : Actionable.SIMULATE,
TileChest.this.mySrc );
if( results == null )
{
return resource.amount;
}
return resource.amount - (int) results.getStackSize();
}
catch( final ChestNoHandler ignored )
{
}
}
return 0;
}
@Override
public FluidStack drain( final FluidStack resource, final boolean doDrain )
{
return null;
}
@Override
public FluidStack drain( final int maxDrain, final boolean doDrain )
{
return null;
}
@Override
public IFluidTankProperties[] getTankProperties()
{
try
{
final IMEInventoryHandler h = TileChest.this.getHandler( StorageChannel.FLUIDS );
if( h.getChannel() == StorageChannel.FLUIDS )
{
return new IFluidTankProperties[] { new FluidTankProperties( null, 1 ) }; // eh?
}
}
catch( final ChestNoHandler ignored )
{
}
return null;
}
}
}