Converted pump to scala, and externalized PumpNode as its own class

This commit is contained in:
Robert S 2014-09-27 14:58:24 -04:00
parent ba45bdd1a6
commit afb718af8a
3 changed files with 136 additions and 157 deletions

View file

@ -0,0 +1,34 @@
package resonantinduction.mechanical.fluid.transport
import net.minecraftforge.common.util.ForgeDirection
import resonantinduction.core.prefab.node.NodePressure
import universalelectricity.api.core.grid.INodeProvider
/**
* Created by robert on 9/27/2014.
*/
class PumpNode(parent: INodeProvider) extends NodePressure(parent)
{
def pump : TilePump = getParent.asInstanceOf[TilePump]
override def getPressure(dir: ForgeDirection): Int =
{
if (pump.mechanicalNode.getPower > 0)
{
if (dir == pump.getDirection)
{
return Math.max(Math.abs(pump.mechanicalNode.getForce(ForgeDirection.UNKNOWN) / 8000d), 2).asInstanceOf[Int]
}
else if (dir == pump.getDirection.getOpposite)
{
return -Math.max(Math.abs(pump.mechanicalNode.getForce(ForgeDirection.UNKNOWN) / 8000d), 2).asInstanceOf[Int]
}
}
return 0
}
override def canConnect(from: ForgeDirection, source: AnyRef): Boolean =
{
return super.canConnect(from, source) && (from == pump.getDirection || from == pump.getDirection.getOpposite)
}
}

View file

@ -1,157 +0,0 @@
package resonantinduction.mechanical.fluid.transport;
import net.minecraft.block.material.Material;
import resonantinduction.core.prefab.node.NodePressure;
import resonantinduction.mechanical.mech.TileMechanical;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import resonant.api.IRotatable;
import universalelectricity.api.core.grid.INode;
import universalelectricity.core.transform.vector.Vector3;
public class TilePump extends TileMechanical implements IRotatable, IFluidHandler
{
private final NodePressure pressureNode;
public TilePump()
{
super(Material.iron);
normalRender(false);
isOpaqueCube(false);
customItemRender(true);
setTextureName("material_steel");
pressureNode = new NodePressure(this)
{
@Override
public int getPressure(ForgeDirection dir)
{
if (mechanicalNode.getPower() > 0)
{
if (dir == getDirection())
{
return (int) Math.max(Math.abs(mechanicalNode.getForce(ForgeDirection.UNKNOWN) / 8000d), 2);
}
else if (dir == getDirection().getOpposite())
{
return (int) -Math.max(Math.abs(mechanicalNode.getForce(ForgeDirection.UNKNOWN) / 8000d), 2);
}
}
return 0;
}
@Override
public boolean canConnect(ForgeDirection from, Object source)
{
return super.canConnect(from, source) && (from == getDirection() || from == getDirection().getOpposite());
}
};
}
@Override
public void start()
{
pressureNode.reconstruct();
super.start();
}
@Override
public void invalidate()
{
super.invalidate();
pressureNode.deconstruct();
}
@Override
public void update()
{
super.update();
if (!worldObj.isRemote && mechanicalNode.getPower() > 0)
{
/**
* Try to suck fluid in
*/
TileEntity tileIn = new Vector3(this).add(getDirection().getOpposite()).getTileEntity(this.worldObj);
if (tileIn instanceof IFluidHandler)
{
FluidStack drain = ((IFluidHandler) tileIn).drain(getDirection(), pressureNode.getCapacity(), false);
if (drain != null)
{
((IFluidHandler) tileIn).drain(getDirection(), fill(getDirection().getOpposite(), drain, true), true);
}
}
}
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
{
if (from == getDirection().getOpposite())
{
TileEntity tileOut = new Vector3(this).add(from.getOpposite()).getTileEntity(this.worldObj);
if (tileOut instanceof IFluidHandler)
return ((IFluidHandler) tileOut).fill(from, resource, doFill);
}
return 0;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain)
{
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
{
return null;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid)
{
return from == this.getDirection().getOpposite();
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid)
{
return from == this.getDirection();
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from)
{
return null;
}
@Override
public INode getNode(Class<? extends INode> nodeType, ForgeDirection from)
{
if (nodeType.isAssignableFrom(pressureNode.getClass()))
return pressureNode;
return super.getNode(nodeType, from);
}
@Override
public ForgeDirection getDirection() {
return null;
}
@Override
public void setDirection(ForgeDirection direction) {
}
}

View file

@ -0,0 +1,102 @@
package resonantinduction.mechanical.fluid.transport
import net.minecraft.block.material.Material
import net.minecraft.tileentity.TileEntity
import net.minecraftforge.common.util.ForgeDirection
import net.minecraftforge.fluids.{Fluid, FluidStack, FluidTankInfo, IFluidHandler}
import resonant.api.IRotatable
import resonantinduction.mechanical.mech.TileMechanical
import universalelectricity.api.core.grid.INode
import universalelectricity.core.transform.vector.Vector3
class TilePump extends TileMechanical(Material.iron) with IRotatable with IFluidHandler
{
var pressureNode : PumpNode = null
//Constructor
normalRender(false)
isOpaqueCube(false)
customItemRender(true)
setTextureName("material_steel")
pressureNode = new PumpNode(this)
override def start
{
pressureNode.reconstruct
super.start
}
override def invalidate
{
super.invalidate
pressureNode.deconstruct
}
override def update
{
super.update
if (!worldObj.isRemote && mechanicalNode.getPower > 0)
{
val tileIn: TileEntity = new Vector3(this).add(getDirection.getOpposite).getTileEntity(this.worldObj)
if (tileIn.isInstanceOf[IFluidHandler])
{
val drain: FluidStack = (tileIn.asInstanceOf[IFluidHandler]).drain(getDirection, pressureNode.getCapacity, false)
if (drain != null)
{
(tileIn.asInstanceOf[IFluidHandler]).drain(getDirection, fill(getDirection.getOpposite, drain, true), true)
}
}
}
}
def fill(from: ForgeDirection, resource: FluidStack, doFill: Boolean): Int =
{
if (from eq getDirection.getOpposite)
{
val tileOut: TileEntity = new Vector3(this).add(from.getOpposite).getTileEntity(this.worldObj)
if (tileOut.isInstanceOf[IFluidHandler]) return (tileOut.asInstanceOf[IFluidHandler]).fill(from, resource, doFill)
}
return 0
}
def drain(from: ForgeDirection, resource: FluidStack, doDrain: Boolean): FluidStack =
{
return null
}
def drain(from: ForgeDirection, maxDrain: Int, doDrain: Boolean): FluidStack =
{
return null
}
def canFill(from: ForgeDirection, fluid: Fluid): Boolean =
{
return from eq this.getDirection.getOpposite
}
def canDrain(from: ForgeDirection, fluid: Fluid): Boolean =
{
return from eq this.getDirection
}
def getTankInfo(from: ForgeDirection): Array[FluidTankInfo] =
{
return null
}
override def getNode(nodeType: Class[_ <: INode], from: ForgeDirection): INode =
{
if (nodeType.isAssignableFrom(pressureNode.getClass)) return pressureNode
return super.getNode(nodeType, from)
}
override def getDirection: ForgeDirection =
{
return null
}
override def setDirection(direction: ForgeDirection)
{
}
}