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

212 lines
7.8 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
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 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.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-05-30 17:47:57 +02:00
public class TileEntityFluidCollector extends TileEntityBase implements net.minecraftforge.fluids.IFluidHandler{
2015-05-30 17:47:57 +02:00
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;
}
};
2015-10-03 10:16:18 +02:00
public boolean isPlacer;
private int lastTankAmount;
2015-10-03 10:16:18 +02:00
private int currentTime;
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-07-04 20:15:41 +02:00
IBlockState state = this.worldObj.getBlockState(this.pos);
Block block = state.getBlock();
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(block.getMetaFromState(state));
BlockPos coordsBlock = this.pos.offset(sideToManipulate, 0);
2015-12-19 10:30:39 +01:00
2016-07-04 20:15:41 +02:00
IBlockState stateToBreak = this.worldObj.getBlockState(coordsBlock);
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-07-04 20:15:41 +02:00
this.worldObj.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-07-04 20:15:41 +02:00
this.worldObj.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-07-04 20:15:41 +02:00
this.worldObj.setBlockToAir(coordsBlock);
2015-12-19 10:30:39 +01:00
}
}
2016-05-16 22:52:27 +02:00
}
2016-07-04 20:15:41 +02:00
else if(this.isPlacer && blockToBreak.isReplaceable(this.worldObj, coordsBlock)){
2016-05-16 22:52:27 +02:00
if(this.tank.getFluidAmount() >= Util.BUCKET){
2016-07-04 20:15:41 +02:00
Block fluid = this.tank.getFluid().getFluid().getBlock();
if(fluid != null){
2016-05-16 22:52:27 +02:00
BlockPos offsetPos = this.pos.offset(sideToManipulate);
2016-07-04 20:15:41 +02:00
boolean placeable = !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && blockToBreak.isReplaceable(this.worldObj, offsetPos);
2016-05-16 22:52:27 +02:00
if(placeable){
2016-07-04 20:15:41 +02:00
this.worldObj.setBlockState(offsetPos, fluid.getDefaultState(), 3);
2016-06-05 02:16:52 +02:00
this.tank.drainInternal(Util.BUCKET, true);
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
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-05-02 17:46:53 +02:00
if(!this.worldObj.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
}
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
}
}
}
2016-02-01 17:49:55 +01:00
@SideOnly(Side.CLIENT)
public int getTankScaled(int i){
return this.tank.getFluidAmount()*i/this.tank.getCapacity();
}
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill){
IFluidHandler handler = this.getFluidHandler(from);
return handler == null ? 0 : handler.fill(resource, doFill);
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain){
IFluidHandler handler = this.getFluidHandler(from);
return handler == null ? null : handler.drain(resource, doDrain);
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain){
IFluidHandler handler = this.getFluidHandler(from);
return handler == null ? null : handler.drain(maxDrain, doDrain);
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid){
IFluidHandler handler = this.getFluidHandler(from);
if(handler != null){
for(IFluidTankProperties prop : handler.getTankProperties()){
if(prop != null && prop.canFillFluidType(new FluidStack(fluid, Integer.MAX_VALUE))){
return true;
}
}
}
return false;
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid){
IFluidHandler handler = this.getFluidHandler(from);
if(handler != null){
for(IFluidTankProperties prop : handler.getTankProperties()){
if(prop != null && prop.canDrainFluidType(new FluidStack(fluid, Integer.MAX_VALUE))){
return true;
}
}
}
return false;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from){
IFluidHandler handler = this.getFluidHandler(from);
if(handler instanceof IFluidTank){
return new FluidTankInfo[]{((IFluidTank)handler).getInfo()};
}
else{
return null;
}
}
2015-05-30 17:47:57 +02:00
}