Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Block:
- package com.lordmau5.ffs.blocks;
- import com.lordmau5.ffs.FancyFluidStorage;
- import com.lordmau5.ffs.tile.TileEntityValve;
- import com.lordmau5.ffs.util.FFSStateProps;
- import com.lordmau5.ffs.util.GenericUtil;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.state.BlockState;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.creativetab.CreativeTabs;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.item.Item;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.BlockPos;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumWorldBlockLayer;
- import net.minecraft.world.Explosion;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import java.util.Random;
- /**
- * Created by Dustin on 28.06.2015.
- */
- public class BlockValve extends Block {
- public BlockValve() {
- super(Material.iron);
- setUnlocalizedName("blockValve");
- setRegistryName("blockValve");
- setCreativeTab(CreativeTabs.tabRedstone);
- setHardness(5.0F); // Same hardness as an iron block
- setResistance(10.0F); // Same as hardness
- setDefaultState((blockState.getBaseState())
- .withProperty(FFSStateProps.VALVE_VALID, false)
- .withProperty(FFSStateProps.VALVE_MASTER, false)
- .withProperty(FFSStateProps.VALVE_INSIDE, EnumFacing.Axis.X));
- }
- @Override
- public boolean hasTileEntity(IBlockState state) {
- return true;
- }
- @Override
- public TileEntity createTileEntity(World world, IBlockState state) {
- return new TileEntityValve();
- }
- @Override
- public void onBlockExploded(World world, BlockPos pos, Explosion explosion) {
- TileEntity tile = world.getTileEntity(pos);
- if(tile != null && tile instanceof TileEntityValve) {
- TileEntityValve valve = (TileEntityValve) world.getTileEntity(pos);
- valve.breakTank(null);
- }
- super.onBlockDestroyedByExplosion(world, pos, explosion);
- }
- @Override
- public void breakBlock(World world, BlockPos pos, IBlockState state) {
- if(!world.isRemote) {
- TileEntityValve valve = (TileEntityValve) world.getTileEntity(pos);
- valve.breakTank(null);
- }
- super.breakBlock(world, pos, state);
- }
- @Override
- public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
- if (super.onBlockActivated(world, pos, state, player, side, hitX, hitY, hitZ)) {
- return true;
- }
- if (player.isSneaking()) return false;
- TileEntityValve valve = (TileEntityValve) world.getTileEntity(pos);
- if(valve.isValid()) {
- if(GenericUtil.isFluidContainer(player.getHeldItem()))
- return GenericUtil.fluidContainerHandler(world, pos, valve, player, side);
- player.openGui(FancyFluidStorage.instance, 0, world, pos.getX(), pos.getY(), pos.getZ());
- return true;
- }
- else {
- valve.buildTank(side.getOpposite());
- }
- return true;
- }
- @Override
- public Item getItemDropped(IBlockState state, Random rand, int fortune) {
- return Item.getItemFromBlock(FancyFluidStorage.blockValve);
- }
- @Override
- public boolean canRenderInLayer(EnumWorldBlockLayer layer) {
- return layer == EnumWorldBlockLayer.SOLID || layer == EnumWorldBlockLayer.TRANSLUCENT;
- }
- @Override
- protected BlockState createBlockState() {
- return new BlockState(this, FFSStateProps.VALVE_VALID, FFSStateProps.VALVE_MASTER, FFSStateProps.VALVE_INSIDE);
- }
- @Override
- public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
- TileEntity tile = world.getTileEntity(pos);
- if(tile != null && tile instanceof TileEntityValve) {
- TileEntityValve valve = (TileEntityValve) tile;
- state = state.withProperty(FFSStateProps.VALVE_VALID, valve.isValid())
- .withProperty(FFSStateProps.VALVE_MASTER, valve.isMaster())
- .withProperty(FFSStateProps.VALVE_INSIDE, (valve.getInside() == null) ? EnumFacing.Axis.X : valve.getInside().getAxis());
- }
- return state;
- }
- @Override
- public IBlockState getStateFromMeta(int meta) {
- return getDefaultState();
- }
- @Override
- public int getMetaFromState(IBlockState state) {
- return 0;
- }
- @SideOnly(Side.CLIENT)
- @Override
- public int getRenderType() {
- return 3;
- }
- @Override
- public boolean isOpaqueCube() {
- return false;
- }
- @Override
- public boolean isNormalCube(IBlockAccess world, BlockPos pos) {
- return true;
- }
- @Override
- public boolean isNormalCube() {
- return true;
- }
- @Override
- public boolean hasComparatorInputOverride() {
- return true;
- }
- @Override
- public boolean canCreatureSpawn(IBlockAccess world, BlockPos pos, EntityLiving.SpawnPlacementType type) {
- return false;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Tile:
- package com.lordmau5.ffs.tile;
- import com.lordmau5.ffs.FancyFluidStorage;
- import com.lordmau5.ffs.util.GenericUtil;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.Packet;
- import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.BlockPos;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraft.world.ChunkCoordIntPair;
- import net.minecraft.world.chunk.Chunk;
- import net.minecraftforge.common.ForgeChunkManager;
- import net.minecraftforge.fluids.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by Dustin on 28.06.2015.
- */
- public class TileEntityValve extends TileEntity implements IFluidTank, IFluidHandler, ITickable {
- private final int maxSize = FancyFluidStorage.instance.MAX_SIZE;
- protected int mbPerVirtualTank = FancyFluidStorage.instance.MB_PER_TANK_BLOCK;
- private boolean wantsUpdate;
- private String valveName = "";
- private boolean isValid;
- private boolean isMaster;
- private BlockPos masterValvePos;
- public boolean initiated;
- public int tankHeight = 0;
- public int valveHeightPosition = 0;
- private boolean autoOutput;
- private EnumFacing inside = EnumFacing.UP;
- private TileEntityValve master;
- public List<TileEntityTankFrame> tankFrames;
- public List<TileEntityValve> otherValves;
- private Map<BlockPos, IBlockState>[] maps;
- /**
- * Length of the inside
- *
- * 0 = Down
- * 1 = Up
- * 2 = North
- * 3 = South
- * 4 = West
- * 5 = East
- */
- private int[] length = new int[6];
- public BlockPos bottomDiagFrame, topDiagFrame;
- public int initialWaitTick = 20;
- // TANK LOGIC
- private FluidStack fluidStack;
- private int fluidCapacity = 0;
- // ---------------
- public TileEntityValve() {
- tankFrames = new ArrayList<>();
- otherValves = new ArrayList<>();
- }
- @Override
- public void validate() {
- super.validate();
- initiated = true;
- initialWaitTick = 20;
- }
- @Override
- public void update() {
- if(getWorld().isRemote)
- return;
- if(wantsUpdate && getWorld() != null) {
- if(initiated) {
- if (isMaster()) {
- if (bottomDiagFrame != null && topDiagFrame != null) { // Potential fix for huge-ass tanks not loading properly on master-valve chunk load.
- Chunk chunkBottom = worldObj.getChunkFromBlockCoords(bottomDiagFrame);
- Chunk chunkTop = worldObj.getChunkFromBlockCoords(topDiagFrame);
- BlockPos pos_chunkBottom = new BlockPos(chunkBottom.xPosition, 0, chunkBottom.zPosition);
- BlockPos pos_chunkTop = new BlockPos(chunkTop.xPosition, 0, chunkTop.zPosition);
- BlockPos diff = pos_chunkTop.subtract(pos_chunkBottom);
- for (int x = 0; x <= diff.getX(); x++) {
- for (int z = 0; z <= diff.getZ(); z++) {
- ForgeChunkManager.forceChunk(ForgeChunkManager.requestTicket(FancyFluidStorage.instance, worldObj, ForgeChunkManager.Type.NORMAL), new ChunkCoordIntPair(pos_chunkTop.getX() + x, pos_chunkTop.getZ() + z));
- }
- }
- updateBlockAndNeighbors();
- }
- if (initialWaitTick-- <= 0) {
- initiated = false;
- buildTank(getInside());
- return;
- }
- }
- }
- }
- if(!isValid())
- return;
- if(!isMaster() && getMaster() == null) {
- setValid(false);
- updateBlockAndNeighbors();
- }
- }
- private List<TileEntityValve> getAllValves() {
- if(!isMaster() && getMaster() != this)
- return getMaster().getAllValves();
- List<TileEntityValve> valves = new ArrayList<>();
- valves.add(this);
- if(otherValves.isEmpty())
- return valves;
- for(TileEntityValve valve : otherValves)
- valves.add(valve);
- return valves;
- }
- public String getValveName() {
- if(this.valveName.isEmpty())
- setValveName(GenericUtil.getUniqueValveName(this));
- return this.valveName;
- }
- public void setValveName(String valveName) {
- this.valveName = valveName;
- }
- public void setNeedsUpdate() {
- wantsUpdate = true;
- }
- public int getTankHeight() {
- return isMaster() ? tankHeight : getMaster().tankHeight;
- }
- public void setInside(EnumFacing inside) {
- this.inside = inside;
- }
- public EnumFacing getInside() {
- return this.inside;
- }
- /**
- * Let's build a tank!
- * @param inside - The direction of the inside of the tank
- */
- public void buildTank(EnumFacing inside) {
- /**
- * Don't build if it's on the client!
- */
- if (getWorld().isRemote)
- return;
- /**
- * Let's first set the tank to be invalid,
- * since it should stay like that if the building fails.
- * Also, let's reset variables.
- */
- setValid(false);
- fluidCapacity = 0;
- tankFrames.clear();
- otherValves.clear();
- /**
- * Now, set the inside direction according to the variable,
- * *IF* our current inside is null.
- */
- if(inside != null)
- setInside(inside);
- if(!calculateInside())
- return;
- /**
- * Actually setup the tank here
- */
- if(!setupTank())
- return;
- /**
- * Just in case, set *initiated* to false again.
- * Also, update our neighbor blocks, e.g. pipes or similar.
- */
- initiated = false;
- updateBlockAndNeighbors();
- }
- /**
- * Over here, let's calculate the inside length,
- * which is required to get the corner points later on.
- * Also, to know if the inside of the tank is completely air.
- */
- public boolean calculateInside() {
- BlockPos insidePos = getPos().offset(getInside());
- for(EnumFacing dr : EnumFacing.VALUES) {
- for(int i=0; i<maxSize; i++) {
- BlockPos offsetPos = insidePos.offset(dr, i);
- if(!worldObj.isAirBlock(offsetPos)) {
- length[dr.ordinal()] = i - 1;
- break;
- }
- }
- }
- for(int i=0; i<6; i += 2) {
- if(length[i] + length[i + 1] > maxSize)
- return false;
- }
- return length[0] != -1;
- }
- private void setSlaveValveInside(Map<BlockPos, IBlockState> airBlocks, TileEntityValve slave) {
- List<BlockPos> possibleAirBlocks = new ArrayList<>();
- for(EnumFacing dr : EnumFacing.VALUES) {
- if(worldObj.isAirBlock(slave.getPos().offset(dr)))
- possibleAirBlocks.add(slave.getPos().offset(dr));
- }
- BlockPos insideAir = null;
- for(BlockPos pos : possibleAirBlocks) {
- if (airBlocks.containsKey(pos)) {
- insideAir = pos;
- break;
- }
- }
- if(insideAir == null)
- return;
- BlockPos dist = insideAir.subtract(slave.getPos());
- for(EnumFacing dr : EnumFacing.VALUES) {
- if(dist.equals(new BlockPos(dr.getFrontOffsetX(), dr.getFrontOffsetY(), dr.getFrontOffsetZ()))) {
- slave.setInside(dr);
- break;
- }
- }
- }
- /**
- * Let's get the corner frames based on the inside position and length,
- * so we can set the BlockPos according to the corners.
- */
- public void updateCornerFrames() {
- bottomDiagFrame = getPos().add(inside.getDirectionVec()).add(length[EnumFacing.WEST.ordinal()] * EnumFacing.WEST.getFrontOffsetX() + EnumFacing.WEST.getFrontOffsetX(),
- length[EnumFacing.DOWN.ordinal()] * EnumFacing.DOWN.getFrontOffsetY() + EnumFacing.DOWN.getFrontOffsetY(),
- length[EnumFacing.NORTH.ordinal()] * EnumFacing.NORTH.getFrontOffsetZ() + EnumFacing.NORTH.getFrontOffsetZ());
- topDiagFrame = getPos().add(inside.getDirectionVec()).add(length[EnumFacing.EAST.ordinal()] * EnumFacing.EAST.getFrontOffsetX() + EnumFacing.EAST.getFrontOffsetX(),
- length[EnumFacing.UP.ordinal()] * EnumFacing.UP.getFrontOffsetY() + EnumFacing.UP.getFrontOffsetY(),
- length[EnumFacing.SOUTH.ordinal()] * EnumFacing.SOUTH.getFrontOffsetZ() + EnumFacing.SOUTH.getFrontOffsetZ());
- }
- /**
- * The maps contain the blocks on the:
- * inside,
- * outter frame,
- * inner frame
- */
- private void fetchMaps() {
- maps = GenericUtil.getTankFrame(worldObj, bottomDiagFrame, topDiagFrame);
- }
- private boolean setupTank() {
- updateCornerFrames();
- fetchMaps();
- otherValves = new ArrayList<>();
- tankFrames = new ArrayList<>();
- valveHeightPosition = Math.abs(bottomDiagFrame.subtract(getPos()).getY());
- tankHeight = topDiagFrame.subtract(bottomDiagFrame).getY() - 1;
- IBlockState bottomDiagBlock = worldObj.getBlockState(bottomDiagFrame);
- IBlockState topDiagBlock = worldObj.getBlockState(topDiagFrame);
- if(!GenericUtil.isValidTankBlock(worldObj, bottomDiagFrame, bottomDiagBlock))
- return false;
- if (!GenericUtil.areTankBlocksValid(bottomDiagBlock, topDiagBlock, worldObj, bottomDiagFrame))
- return false;
- BlockPos pos;
- if (FancyFluidStorage.instance.INSIDE_CAPACITY) {
- fluidCapacity = (maps[2].size()) * mbPerVirtualTank;
- } else {
- fluidCapacity = (maps[0].size() + maps[1].size() + maps[2].size()) * mbPerVirtualTank;
- }
- for (Map.Entry<BlockPos, IBlockState> frameCheck : maps[0].entrySet()) {
- pos = frameCheck.getKey();
- IBlockState fBlock = frameCheck.getValue();
- if (!GenericUtil.areTankBlocksValid(fBlock, bottomDiagBlock, worldObj, pos))
- return false;
- }
- List<TileEntityValve> valves = new ArrayList<>();
- for (Map.Entry<BlockPos, IBlockState> insideFrameCheck : maps[1].entrySet()) {
- pos = insideFrameCheck.getKey();
- IBlockState check = insideFrameCheck.getValue();
- if (GenericUtil.areTankBlocksValid(check, bottomDiagBlock, worldObj, pos) || GenericUtil.isBlockGlass(check.getBlock(), check.getBlock().getMetaFromState(check)))
- continue;
- TileEntity tile = worldObj.getTileEntity(pos);
- if (tile != null) {
- if (tile instanceof TileEntityValve) {
- TileEntityValve valve = (TileEntityValve) tile;
- if (valve == this)
- continue;
- if (valve.fluidStack != null) {
- this.fluidStack = valve.fluidStack;
- }
- valves.add(valve);
- continue;
- }
- else if (tile instanceof TileEntityTankFrame) {
- continue;
- }
- return false;
- }
- return false;
- }
- // Make sure we don't overfill a tank. If the new tank is smaller than the old one, excess liquid disappear.
- if (this.fluidStack != null)
- this.fluidStack.amount = Math.min(this.fluidStack.amount, this.fluidCapacity);
- for (TileEntityValve valve : valves) {
- pos = valve.getPos();
- valve.valveHeightPosition = Math.abs(bottomDiagFrame.subtract(pos).getY());
- valve.isMaster = false;
- valve.setMasterPos(getPos());
- setSlaveValveInside(maps[2], valve);
- }
- isMaster = true;
- for (Map.Entry<BlockPos, IBlockState> setTiles : maps[0].entrySet()) {
- pos = setTiles.getKey();
- TileEntityTankFrame tankFrame;
- if (setTiles.getValue().getBlock() != FancyFluidStorage.blockTankFrame) {
- getWorld().setBlockState(pos, FancyFluidStorage.blockTankFrame.getDefaultState());
- }
- tankFrame = (TileEntityTankFrame) getWorld().getTileEntity(pos);
- tankFrame.initialize(getPos(), setTiles.getValue());
- tankFrames.add(tankFrame);
- }
- for (Map.Entry<BlockPos, IBlockState> setTiles : maps[1].entrySet()) {
- pos = setTiles.getKey();
- TileEntity tile = getWorld().getTileEntity(pos);
- if (tile != null) {
- if (tile instanceof TileEntityValve && tile != this)
- otherValves.add((TileEntityValve) tile);
- else if (tile instanceof TileEntityTankFrame) {
- ((TileEntityTankFrame) tile).setValvePos(getPos());
- tankFrames.add((TileEntityTankFrame) tile);
- }
- else if (GenericUtil.isTileEntityAcceptable(setTiles.getValue().getBlock(), tile)) {
- getWorld().setBlockState(pos, FancyFluidStorage.blockTankFrame.getDefaultState());
- TileEntityTankFrame tankFrame = (TileEntityTankFrame) getWorld().getTileEntity(pos);
- tankFrame.initialize(getPos(), setTiles.getValue());
- tankFrames.add(tankFrame);
- }
- } else {
- getWorld().setBlockState(pos, FancyFluidStorage.blockTankFrame.getDefaultState());
- TileEntityTankFrame tankFrame = (TileEntityTankFrame) getWorld().getTileEntity(pos);
- tankFrame.initialize(getPos(), setTiles.getValue());
- tankFrames.add(tankFrame);
- }
- }
- setValid(true);
- return true;
- }
- public void breakTank(TileEntity frame) {
- if (getWorld().isRemote)
- return;
- if(!isMaster() && getMaster() != null) {
- if(getMaster() != this)
- getMaster().breakTank(frame);
- return;
- }
- for(TileEntityValve valve : otherValves) {
- valve.fluidStack = getFluid();
- valve.master = null;
- valve.setValid(false);
- valve.updateBlockAndNeighbors();
- }
- for(TileEntityTankFrame tankFrame : tankFrames) {
- if(frame == tankFrame)
- continue;
- tankFrame.breakFrame();
- }
- tankFrames.clear();
- otherValves.clear();
- setValid(false);
- this.updateBlockAndNeighbors();
- }
- public void setValid(boolean isValid) {
- this.isValid = isValid;
- }
- public boolean isValid() {
- return getMaster() != null && getMaster().isValid;
- }
- public void updateBlockAndNeighbors() {
- updateBlockAndNeighbors(false);
- }
- public void updateBlockAndNeighbors(boolean onlyThis) {
- if(getWorld().isRemote)
- return;
- this.markForUpdate(onlyThis);
- if(otherValves != null) {
- for(TileEntityValve otherValve : otherValves) {
- otherValve.markForUpdate(true);
- }
- }
- }
- private void markForUpdate(boolean onlyThis) {
- if (!onlyThis) {
- for (TileEntityTankFrame frame : tankFrames) {
- frame.markForUpdate();
- }
- }
- wantsUpdate = true;
- }
- public boolean isMaster() {
- return isMaster;
- }
- public TileEntityValve getMaster() {
- if(isMaster())
- return this;
- if(masterValvePos != null) {
- TileEntity tile = worldObj.getTileEntity(masterValvePos);
- master = tile instanceof TileEntityValve ? (TileEntityValve) tile : null;
- }
- return master;
- }
- public void setMasterPos(BlockPos masterValvePos) {
- this.masterValvePos = masterValvePos;
- this.master = null;
- }
- public boolean getAutoOutput() {
- return isValid() && this.autoOutput;
- }
- public void setAutoOutput(boolean autoOutput) {
- this.autoOutput = autoOutput;
- updateBlockAndNeighbors(true);
- }
- @Override
- public void readFromNBT(NBTTagCompound tag) {
- super.readFromNBT(tag);
- setInside(EnumFacing.getFront(tag.getInteger("inside")));
- isMaster = tag.getBoolean("master");
- if(isMaster()) {
- setValid(tag.getBoolean("isValid"));
- if(tag.getBoolean("hasFluid")) {
- if(tag.hasKey("fluidName"))
- fluidStack = new FluidStack(FluidRegistry.getFluid(tag.getString("fluidName")), tag.getInteger("fluidAmount"));
- }
- else {
- fluidStack = null;
- }
- tankHeight = tag.getInteger("tankHeight");
- fluidCapacity = tag.getInteger("fluidCapacity");
- }
- else {
- if(getMaster() == null && tag.hasKey("masterValve")) {
- int[] masterValveP = tag.getIntArray("masterValve");
- setMasterPos(new BlockPos(masterValveP[0], masterValveP[1], masterValveP[2]));
- }
- }
- autoOutput = tag.getBoolean("autoOutput");
- if(tag.hasKey("valveName"))
- setValveName(tag.getString("valveName"));
- else
- setValveName(GenericUtil.getUniqueValveName(this));
- if(tag.hasKey("bottomDiagF") && tag.hasKey("topDiagF")) {
- int[] bottomDiagF = tag.getIntArray("bottomDiagF");
- int[] topDiagF = tag.getIntArray("topDiagF");
- bottomDiagFrame = new BlockPos(bottomDiagF[0], bottomDiagF[1], bottomDiagF[2]);
- topDiagFrame = new BlockPos(topDiagF[0], topDiagF[1], topDiagF[2]);
- }
- markForUpdate(true);
- }
- @Override
- public void writeToNBT(NBTTagCompound tag) {
- tag.setInteger("inside", getInside().ordinal());
- tag.setBoolean("master", isMaster());
- if(isMaster()) {
- tag.setBoolean("isValid", isValid());
- tag.setBoolean("hasFluid", fluidStack != null);
- if(fluidStack != null) {
- tag.setString("fluidName", FluidRegistry.getFluidName(fluidStack));
- tag.setInteger("fluidAmount", fluidStack.amount);
- }
- tag.setInteger("tankHeight", tankHeight);
- tag.setInteger("fluidCapacity", fluidCapacity);
- }
- else {
- if(getMaster() != null) {
- BlockPos pos = getMaster().getPos();
- int[] masterPos = new int[]{pos.getX(), pos.getY(), pos.getZ()};
- tag.setIntArray("masterValve", masterPos);
- }
- }
- tag.setBoolean("autoOutput", autoOutput);
- if(!getValveName().isEmpty())
- tag.setString("valveName", getValveName());
- if(bottomDiagFrame != null && topDiagFrame != null) {
- tag.setIntArray("bottomDiagF", new int[]{bottomDiagFrame.getX(), bottomDiagFrame.getY(), bottomDiagFrame.getZ()});
- tag.setIntArray("topDiagF", new int[]{topDiagFrame.getX(), topDiagFrame.getY(), topDiagFrame.getZ()});
- }
- super.writeToNBT(tag);
- }
- @Override
- public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
- readFromNBT(pkt.getNbtCompound());
- }
- @Override
- public Packet getDescriptionPacket() {
- NBTTagCompound tag = new NBTTagCompound();
- writeToNBT(tag);
- return new S35PacketUpdateTileEntity(getPos(), 0, tag);
- }
- // Tank logic!
- @Override
- public FluidStack getFluid() {
- if(!isValid())
- return null;
- return getMaster() == this ? fluidStack : getMaster().fluidStack;
- }
- @Override
- public int getFluidAmount() {
- if(!isValid() || getFluid() == null)
- return 0;
- return getFluid().amount;
- }
- @Override
- public int getCapacity() {
- if(!isValid())
- return 0;
- return getMaster() == this ? fluidCapacity : getMaster().fluidCapacity;
- }
- @Override
- public FluidTankInfo getInfo() {
- if(!isValid())
- return null;
- return new FluidTankInfo(getMaster());
- }
- @Override
- public int fill(FluidStack resource, boolean doFill) {
- if(getMaster() == this) {
- if (!isValid() || fluidStack != null && !fluidStack.isFluidEqual(resource))
- return 0;
- if (getFluidAmount() >= getCapacity()) {
- for(TileEntityValve valve : getAllValves()) {
- if (valve == this)
- continue;
- EnumFacing outside = valve.getInside().getOpposite();
- TileEntity tile = worldObj.getTileEntity(valve.getPos().offset(outside));
- if (tile != null && tile instanceof TileEntityValve) {
- return ((TileEntityValve) tile).fill(getInside(), resource, doFill);
- }
- }
- }
- if (!doFill)
- {
- if (fluidStack == null) {
- return Math.min(fluidCapacity, resource.amount);
- }
- return Math.min(fluidCapacity - fluidStack.amount, resource.amount);
- }
- if (fluidStack == null)
- {
- fluidStack = new FluidStack(resource, Math.min(fluidCapacity, resource.amount));
- setNeedsUpdate();
- return fluidStack.amount;
- }
- int filled = fluidCapacity - fluidStack.amount;
- if (resource.amount < filled) {
- fluidStack.amount += resource.amount;
- filled = resource.amount;
- }
- else {
- fluidStack.amount = fluidCapacity;
- }
- getMaster().setNeedsUpdate();
- return filled;
- }
- else
- return getMaster().fill(resource, doFill);
- }
- @Override
- public FluidStack drain(int maxDrain, boolean doDrain) {
- if(getMaster() == this) {
- if(!isValid() || fluidStack == null)
- return null;
- int drained = maxDrain;
- if (fluidStack.amount < drained) {
- drained = fluidStack.amount;
- }
- FluidStack stack = new FluidStack(fluidStack, drained);
- if (doDrain) {
- fluidStack.amount -= drained;
- if (fluidStack.amount <= 0) {
- fluidStack = null;
- }
- getMaster().setNeedsUpdate();
- }
- return stack;
- }
- else
- return getMaster().drain(maxDrain, doDrain);
- }
- // IFluidHandler
- /**
- * @return 0-100 in % of how much is filled
- */
- public double getFillPercentage() {
- if(!isValid() || getFluid() == null)
- return 0;
- return Math.floor((double) getFluidAmount() / (double) getCapacity() * 100);
- }
- @Override
- public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
- if(!canFill(from, resource.getFluid()))
- return 0;
- return getMaster() == this ? fill(resource, doFill) : getMaster().fill(resource, doFill);
- }
- @Override
- public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
- return getMaster() == this ? drain(resource.amount, doDrain) : getMaster().drain(resource.amount, doDrain);
- }
- @Override
- public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
- return getMaster() == this ? drain(maxDrain, doDrain) : getMaster().drain(maxDrain, doDrain);
- }
- @Override
- public boolean canFill(EnumFacing from, Fluid fluid) {
- if (!isValid())
- return false;
- if (getFluid() != null && getFluid().getFluid() != fluid)
- return false;
- if (getFluidAmount() >= getCapacity()) {
- for (TileEntityValve valve : getAllValves()) {
- if (valve == this)
- continue;
- if (valve.valveHeightPosition > getTankHeight()) {
- EnumFacing outside = valve.getInside().getOpposite();
- TileEntity tile = worldObj.getTileEntity(valve.getPos().offset(outside));
- if (tile != null && tile instanceof TileEntityValve) {
- return ((TileEntityValve) tile).canFill(valve.getInside(), fluid);
- }
- }
- }
- return false;
- }
- return !getAutoOutput() || valveHeightPosition > getTankHeight() || valveHeightPosition + 0.5f >= getTankHeight() * getFillPercentage();
- }
- @Override
- public boolean canDrain(EnumFacing from, Fluid fluid) {
- return isValid() && getFluid() != null && getFluid().getFluid() == fluid && getFluidAmount() > 0;
- }
- @Override
- public FluidTankInfo[] getTankInfo(EnumFacing from) {
- if(!isValid())
- return null;
- return getMaster() == this ? new FluidTankInfo[]{ getInfo() } : getMaster().getTankInfo(from);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement