TechReborn/src/main/java/techreborn/tiles/TilePump.java

147 lines
4.6 KiB
Java
Raw Normal View History

2016-05-08 22:42:09 +02:00
package techreborn.tiles;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
2016-05-13 08:46:17 +02:00
import net.minecraft.util.text.TextFormatting;
2016-05-08 22:42:09 +02:00
import net.minecraft.world.World;
import net.minecraftforge.fluids.*;
import reborncore.api.power.EnumPowerTier;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.tile.TilePowerAcceptor;
2016-05-08 22:42:09 +02:00
import reborncore.common.util.Tank;
import techreborn.config.ConfigTechReborn;
import java.util.List;
/**
* Created by modmuss50 on 08/05/2016.
*/
public class TilePump extends TilePowerAcceptor implements IFluidHandler {
public Tank tank = new Tank("TilePump", 10000, this);
@Override
public void update() {
super.update();
if (!worldObj.isRemote && worldObj.getTotalWorldTime() % 10 == 0 && !tank.isFull() && tank.getCapacity() - tank.getFluidAmount() >= 1000 && canUseEnergy(ConfigTechReborn.pumpExtractEU)) {
2016-05-08 22:42:09 +02:00
FluidStack fluidStack = drainBlock(worldObj, pos.down(), false);
if (fluidStack != null) {
2016-05-08 22:42:09 +02:00
tank.fill(drainBlock(worldObj, pos.down(), true), true);
useEnergy(ConfigTechReborn.pumpExtractEU);
}
tank.compareAndUpdate();
}
}
@Override
public void addInfo(List<String> info, boolean isRealTile) {
super.addInfo(info, isRealTile);
2016-05-13 08:46:17 +02:00
info.add(TextFormatting.LIGHT_PURPLE + "Eu per extract " + TextFormatting.GREEN
+ PowerSystem.getLocalizedPower(ConfigTechReborn.pumpExtractEU));
2016-05-13 08:46:17 +02:00
info.add(TextFormatting.LIGHT_PURPLE + "Speed: " + TextFormatting.GREEN
2016-05-08 22:42:09 +02:00
+ "1000mb/5 sec");
}
public static FluidStack drainBlock(World world, BlockPos pos, boolean doDrain) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
Fluid fluid = FluidRegistry.lookupFluidForBlock(block);
if (fluid != null && FluidRegistry.isFluidRegistered(fluid)) {
if (block instanceof IFluidBlock) {
IFluidBlock fluidBlock = (IFluidBlock) block;
if (!fluidBlock.canDrain(world, pos)) {
return null;
}
return fluidBlock.drain(world, pos, doDrain);
} else {
//Checks if source
int level = state.getValue(BlockLiquid.LEVEL);
if (level != 0) {
return null;
}
if (doDrain) {
world.setBlockToAir(pos);
}
return new FluidStack(fluid, 1000);
}
} else {
return null;
}
}
@Override
public double getMaxPower() {
return 16000;
2016-05-08 22:42:09 +02:00
}
@Override
public EnumPowerTier getTier() {
return EnumPowerTier.LOW;
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
2016-05-08 22:42:09 +02:00
super.readFromNBT(tagCompound);
readFromNBTWithoutCoords(tagCompound);
}
public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) {
2016-05-08 22:42:09 +02:00
tank.readFromNBT(tagCompound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
2016-05-08 22:42:09 +02:00
super.writeToNBT(tagCompound);
writeToNBTWithoutCoords(tagCompound);
2016-05-18 17:53:54 +02:00
return tagCompound;
2016-05-08 22:42:09 +02:00
}
public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound) {
2016-05-08 22:42:09 +02:00
tank.writeToNBT(tagCompound);
2016-05-18 17:53:54 +02:00
return tagCompound;
2016-05-08 22:42:09 +02:00
}
// IFluidHandler
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
2016-05-08 22:42:09 +02:00
int fill = tank.fill(resource, doFill);
tank.compareAndUpdate();
return fill;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
2016-05-08 22:42:09 +02:00
FluidStack drain = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
2016-05-08 22:42:09 +02:00
FluidStack drain = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid) {
2016-05-08 22:42:09 +02:00
return false;
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid) {
2016-05-08 22:42:09 +02:00
return tank.getFluid() == null || tank.getFluid().getFluid() == fluid;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from) {
return new FluidTankInfo[]{tank.getInfo()};
2016-05-08 22:42:09 +02:00
}
2016-05-08 22:42:09 +02:00
}