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

196 lines
7.4 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFluidCollector.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
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
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;
public final FluidTank tank = new FluidTank(8*Util.BUCKET){
@Override
public boolean canFill(){
return TileEntityFluidCollector.this.isPlacer;
}
@Override
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
public TileEntityFluidCollector(String name){
super(name);
2015-10-03 10:16:18 +02:00
}
2015-12-19 10:30:39 +01:00
public TileEntityFluidCollector(){
this("fluidCollector");
2015-12-19 10:30:39 +01:00
this.isPlacer = false;
}
@Override
public boolean isRedstoneToggle(){
return true;
}
@Override
public void activateOnPulse(){
this.doWork();
}
2015-12-19 10:30:39 +01: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();
if(!this.isPlacer && blockToBreak != null && blockToBreak.getMetaFromState(stateToBreak) == 0 && Util.BUCKET <= this.tank.getCapacity()-this.tank.getFluidAmount()){
2016-05-16 22:52:27 +02:00
if(blockToBreak instanceof IFluidBlock && ((IFluidBlock)blockToBreak).getFluid() != null){
2016-06-05 02:16:52 +02:00
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
}
2016-05-16 22:52:27 +02:00
}
else if(blockToBreak == Blocks.LAVA || blockToBreak == Blocks.FLOWING_LAVA){
2016-06-05 02:16:52 +02:00
if(this.tank.fillInternal(new FluidStack(FluidRegistry.LAVA, Util.BUCKET), false) >= Util.BUCKET){
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
}
2016-05-16 22:52:27 +02:00
}
else if(blockToBreak == Blocks.WATER || blockToBreak == Blocks.FLOWING_WATER){
2016-06-05 02:16:52 +02:00
if(this.tank.fillInternal(new FluidStack(FluidRegistry.WATER, Util.BUCKET), false) >= Util.BUCKET){
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
}
}
2016-05-16 22:52:27 +02:00
}
2016-11-26 21:32:27 +01:00
else if(this.isPlacer && blockToBreak.isReplaceable(this.world, coordsBlock)){
2016-05-16 22:52:27 +02:00
if(this.tank.getFluidAmount() >= Util.BUCKET){
FluidStack stack = this.tank.getFluid();
Block fluid = stack.getFluid().getBlock();
2016-07-04 20:15:41 +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);
2016-05-16 22:52:27 +02:00
if(placeable){
2016-06-05 02:16:52 +02:00
this.tank.drainInternal(Util.BUCKET, true);
2016-11-26 21:32:27 +01: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);
2016-11-26 21:32:27 +01:00
if(this.world instanceof WorldServer){
for(int l = 0; l < 8; ++l){
2016-11-26 21:32:27 +01:00
((WorldServer)this.world).spawnParticle(EnumParticleTypes.SMOKE_LARGE, false, (double)offsetPos.getX()+Math.random(), (double)offsetPos.getY()+Math.random(), (double)offsetPos.getZ()+Math.random(), 1, 0.0D, 0.0D, 0.0D, 0);
}
}
}
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
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
2016-06-05 02:16:52 +02:00
public IFluidHandler getFluidHandler(EnumFacing facing){
return this.tank;
2015-05-30 17:47:57 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
compound.setInteger("CurrentTime", this.currentTime);
}
2016-02-01 17:49:55 +01:00
this.tank.writeToNBT(compound);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
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
public void updateEntity(){
super.updateEntity();
2016-11-26 21:32:27 +01:00
if(!this.world.isRemote){
if(!this.isRedstonePowered && !this.isPulseMode){
2015-05-30 17:47:57 +02:00
if(this.currentTime > 0){
this.currentTime--;
if(this.currentTime <= 0){
this.doWork();
2015-05-30 17:47:57 +02:00
}
}
2015-10-02 16:48:01 +02:00
else{
this.currentTime = 15;
2015-10-02 16:48:01 +02:00
}
2015-05-30 17:47:57 +02:00
}
if(this.lastCompare != this.getComparatorStrength()){
this.lastCompare = this.getComparatorStrength();
this.markDirty();
}
2016-05-02 17:46:53 +02:00
if(this.lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()){
this.lastTankAmount = this.tank.getFluidAmount();
2015-05-30 17:47:57 +02:00
}
}
}
@Override
2016-11-19 21:11:17 +01:00
public int getMaxFluidAmountToSplitShare(){
return this.tank.getFluidAmount();
}
@Override
public boolean doesShareFluid(){
return !this.isPlacer;
}
@Override
public EnumFacing[] getFluidShareSides(){
return EnumFacing.values();
}
2015-05-30 17:47:57 +02:00
}