ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java

181 lines
7 KiB
Java
Raw Normal View History

2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-05-30 17:47:57 +02:00
import de.ellpeck.actuallyadditions.mod.util.Util;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-30 17:47:57 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
2016-07-04 20:15:41 +02:00
import net.minecraft.block.state.IBlockState;
2015-05-30 17:47:57 +02:00
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
2015-05-30 17:47:57 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.SoundCategory;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
2016-11-26 21:32:27 +01:00
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidBlock;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.IFluidHandler;
2015-05-30 17:47:57 +02:00
2019-05-02 09:10:29 +02:00
public class TileEntityFluidCollector extends TileEntityBase implements ISharingFluidHandler {
2015-05-30 17:47:57 +02:00
2016-07-07 17:59:45 +02:00
public boolean isPlacer;
2019-05-02 09:10:29 +02:00
public final FluidTank tank = new FluidTank(8 * Util.BUCKET) {
@Override
2019-05-02 09:10:29 +02:00
public boolean canFill() {
return TileEntityFluidCollector.this.isPlacer;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean canDrain() {
return !TileEntityFluidCollector.this.isPlacer;
}
};
private int lastTankAmount;
2015-10-03 10:16:18 +02:00
private int currentTime;
private int lastCompare;
2015-10-03 10:16:18 +02:00
2019-05-02 09:10:29 +02:00
public TileEntityFluidCollector(String name) {
super(name);
2015-10-03 10:16:18 +02:00
}
2019-05-02 09:10:29 +02:00
public TileEntityFluidCollector() {
this("fluidCollector");
2015-12-19 10:30:39 +01:00
this.isPlacer = false;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public void activateOnPulse() {
this.doWork();
}
2019-05-02 09:10:29 +02:00
private void doWork() {
2016-11-26 21:32:27 +01:00
IBlockState state = this.world.getBlockState(this.pos);
2018-03-19 16:46:43 +01:00
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(state);
BlockPos coordsBlock = this.pos.offset(sideToManipulate);
2015-12-19 10:30:39 +01:00
2016-11-26 21:32:27 +01:00
IBlockState stateToBreak = this.world.getBlockState(coordsBlock);
2016-07-04 20:15:41 +02:00
Block blockToBreak = stateToBreak.getBlock();
2019-05-02 09:10:29 +02:00
if (!this.isPlacer && blockToBreak != null && blockToBreak.getMetaFromState(stateToBreak) == 0 && Util.BUCKET <= this.tank.getCapacity() - this.tank.getFluidAmount()) {
if (blockToBreak instanceof IFluidBlock && ((IFluidBlock) blockToBreak).getFluid() != null) {
if (this.tank.fillInternal(new FluidStack(((IFluidBlock) blockToBreak).getFluid(), Util.BUCKET), false) >= Util.BUCKET) {
this.tank.fillInternal(new FluidStack(((IFluidBlock) blockToBreak).getFluid(), Util.BUCKET), true);
2016-11-26 21:32:27 +01:00
this.world.setBlockToAir(coordsBlock);
2015-12-19 10:30:39 +01:00
}
2019-05-02 09:10:29 +02:00
} else if (blockToBreak == Blocks.LAVA || blockToBreak == Blocks.FLOWING_LAVA) {
if (this.tank.fillInternal(new FluidStack(FluidRegistry.LAVA, Util.BUCKET), false) >= Util.BUCKET) {
2016-06-05 02:16:52 +02:00
this.tank.fillInternal(new FluidStack(FluidRegistry.LAVA, Util.BUCKET), true);
2016-11-26 21:32:27 +01:00
this.world.setBlockToAir(coordsBlock);
2015-12-19 10:30:39 +01:00
}
2019-05-02 09:10:29 +02:00
} else if (blockToBreak == Blocks.WATER || blockToBreak == Blocks.FLOWING_WATER) {
if (this.tank.fillInternal(new FluidStack(FluidRegistry.WATER, Util.BUCKET), false) >= Util.BUCKET) {
2016-06-05 02:16:52 +02:00
this.tank.fillInternal(new FluidStack(FluidRegistry.WATER, Util.BUCKET), true);
2016-11-26 21:32:27 +01:00
this.world.setBlockToAir(coordsBlock);
2015-12-19 10:30:39 +01:00
}
}
2019-05-02 09:10:29 +02:00
} else if (this.isPlacer && blockToBreak.isReplaceable(this.world, coordsBlock)) {
if (this.tank.getFluidAmount() >= Util.BUCKET) {
FluidStack stack = this.tank.getFluid();
Block fluid = stack.getFluid().getBlock();
2019-05-02 09:10:29 +02:00
if (fluid != null) {
2016-05-16 22:52:27 +02:00
BlockPos offsetPos = this.pos.offset(sideToManipulate);
2016-11-26 21:32:27 +01:00
boolean placeable = !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && blockToBreak.isReplaceable(this.world, offsetPos);
2019-05-02 09:10:29 +02:00
if (placeable) {
2016-06-05 02:16:52 +02:00
this.tank.drainInternal(Util.BUCKET, true);
2019-05-02 09:10:29 +02:00
if (this.world.provider.doesWaterVaporize() && stack.getFluid().doesVaporize(stack)) {
this.world.playSound(null, offsetPos, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (this.world.rand.nextFloat() - this.world.rand.nextFloat()) * 0.8F);
2019-05-02 09:10:29 +02:00
if (this.world instanceof WorldServer) {
for (int l = 0; l < 8; ++l) {
((WorldServer) this.world).spawnParticle(EnumParticleTypes.SMOKE_LARGE, false, offsetPos.getX() + Math.random(), offsetPos.getY() + Math.random(), offsetPos.getZ() + Math.random(), 1, 0.0D, 0.0D, 0.0D, 0);
}
}
2019-05-02 09:10:29 +02:00
} else {
2016-11-26 21:32:27 +01:00
this.world.setBlockState(offsetPos, fluid.getDefaultState(), 3);
}
2015-12-19 10:30:39 +01:00
}
}
}
}
2015-10-03 10:16:18 +02:00
}
2015-05-30 17:47:57 +02:00
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
float calc = (float) this.tank.getFluidAmount() / (float) this.tank.getCapacity() * 15F;
return (int) calc;
}
2015-05-30 17:47:57 +02:00
@Override
2019-05-02 09:10:29 +02:00
public IFluidHandler getFluidHandler(EnumFacing facing) {
2016-06-05 02:16:52 +02:00
return this.tank;
2015-05-30 17:47:57 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("CurrentTime", this.currentTime);
}
2016-02-01 17:49:55 +01:00
this.tank.writeToNBT(compound);
}
@Override
2019-05-02 09:10:29 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
this.currentTime = compound.getInteger("CurrentTime");
}
2016-02-01 17:49:55 +01:00
this.tank.readFromNBT(compound);
}
2015-05-30 17:47:57 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
2015-05-30 17:47:57 +02:00
this.currentTime--;
2019-05-02 09:10:29 +02:00
if (this.currentTime <= 0) {
this.doWork();
2015-05-30 17:47:57 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
this.currentTime = 15;
2015-10-02 16:48:01 +02:00
}
2015-05-30 17:47:57 +02:00
}
2019-05-02 09:10:29 +02:00
if (this.lastCompare != this.getComparatorStrength()) {
this.lastCompare = this.getComparatorStrength();
this.markDirty();
}
2019-05-02 09:10:29 +02:00
if (this.lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()) {
2016-05-02 17:46:53 +02:00
this.lastTankAmount = this.tank.getFluidAmount();
2015-05-30 17:47:57 +02:00
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public int getMaxFluidAmountToSplitShare() {
return this.tank.getFluidAmount();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareFluid() {
return !this.isPlacer;
}
@Override
2019-05-02 09:10:29 +02:00
public EnumFacing[] getFluidShareSides() {
return EnumFacing.values();
}
2015-05-30 17:47:57 +02:00
}