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
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
|
2016-05-06 23:23:29 +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;
|
2016-05-07 20:23:50 +02:00
|
|
|
import net.minecraft.block.BlockLiquid;
|
2015-05-30 17:47:57 +02:00
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-06-10 21:52:37 +02:00
|
|
|
import net.minecraftforge.fluids.*;
|
2016-06-05 02:16:52 +02:00
|
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
2016-06-10 21:52:37 +02:00
|
|
|
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
|
|
|
|
2016-06-10 21:52:37 +02:00
|
|
|
public class TileEntityFluidCollector extends TileEntityBase implements IFluidSaver, IRedstoneToggle, net.minecraftforge.fluids.IFluidHandler{
|
2015-05-30 17:47:57 +02:00
|
|
|
|
2016-06-19 22:30:18 +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;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastTankAmount;
|
2015-10-03 10:16:18 +02:00
|
|
|
private int currentTime;
|
2015-12-19 10:30:39 +01:00
|
|
|
private boolean activateOnceWithSignal;
|
2015-10-03 10:16:18 +02:00
|
|
|
|
2016-05-06 23:23:29 +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(){
|
2016-05-06 23:23:29 +02:00
|
|
|
this("fluidCollector");
|
2015-12-19 10:30:39 +01:00
|
|
|
this.isPlacer = false;
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public void toggle(boolean to){
|
|
|
|
this.activateOnceWithSignal = to;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public boolean isPulseMode(){
|
2015-12-17 18:47:46 +01:00
|
|
|
return this.activateOnceWithSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
private void doWork(){
|
2016-05-02 17:46:53 +02:00
|
|
|
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(PosUtil.getMetadata(this.pos, this.worldObj));
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos coordsBlock = WorldUtil.getCoordsFromSide(sideToManipulate, this.pos, 0);
|
2015-12-19 10:30:39 +01:00
|
|
|
|
2016-05-16 22:52:27 +02:00
|
|
|
Block blockToBreak = PosUtil.getBlock(coordsBlock, this.worldObj);
|
|
|
|
if(!this.isPlacer && blockToBreak != null && PosUtil.getMetadata(coordsBlock, this.worldObj) == 0 && Util.BUCKET <= this.tank.getCapacity()-this.tank.getFluidAmount()){
|
|
|
|
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-05-16 22:52:27 +02:00
|
|
|
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
|
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-05-16 22:52:27 +02:00
|
|
|
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
|
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-05-16 22:52:27 +02:00
|
|
|
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
|
2015-12-19 10:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 22:52:27 +02:00
|
|
|
}
|
|
|
|
else if(this.isPlacer && PosUtil.getBlock(coordsBlock, this.worldObj).isReplaceable(this.worldObj, coordsBlock)){
|
|
|
|
if(this.tank.getFluidAmount() >= Util.BUCKET){
|
|
|
|
Block block = this.tank.getFluid().getFluid().getBlock();
|
|
|
|
if(block != null){
|
|
|
|
BlockPos offsetPos = this.pos.offset(sideToManipulate);
|
|
|
|
Block blockPresent = PosUtil.getBlock(offsetPos, this.worldObj);
|
|
|
|
boolean placeable = !(blockPresent instanceof BlockLiquid) && !(blockPresent instanceof IFluidBlock) && blockPresent.isReplaceable(this.worldObj, offsetPos);
|
|
|
|
if(placeable){
|
|
|
|
PosUtil.setBlock(offsetPos, this.worldObj, block, 0, 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, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
|
|
|
this.tank.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.currentTime = compound.getInteger("CurrentTime");
|
|
|
|
this.tank.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
2015-05-30 17:47:57 +02:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2016-05-02 17:46:53 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2015-12-17 18:47:46 +01:00
|
|
|
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
|
2015-05-30 17:47:57 +02:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
2015-12-17 18:47:46 +01:00
|
|
|
this.doWork();
|
2015-05-30 17:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
else{
|
2015-11-28 19:02:01 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:51:13 +01:00
|
|
|
@Override
|
|
|
|
public FluidStack[] getFluids(){
|
|
|
|
return new FluidStack[]{this.tank.getFluid()};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFluids(FluidStack[] fluids){
|
|
|
|
this.tank.setFluid(fluids[0]);
|
|
|
|
}
|
|
|
|
|
2016-06-10 21:52:37 +02:00
|
|
|
@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
|
|
|
}
|