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

243 lines
8.8 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityFluidCollector.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 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-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.init.Blocks;
import net.minecraft.init.Items;
2015-05-30 17:47:57 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2015-05-30 17:47:57 +02:00
import net.minecraftforge.fluids.*;
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 TileEntityInventoryBase implements IFluidHandler, IFluidSaver, IRedstoneToggle{
2015-05-30 17:47:57 +02:00
public FluidTank tank = new FluidTank(8*FluidContainerRegistry.BUCKET_VOLUME);
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;
2015-12-19 10:30:39 +01:00
private boolean activateOnceWithSignal;
2015-10-03 10:16:18 +02:00
public TileEntityFluidCollector(int slots, String name){
super(slots, name);
}
2015-12-19 10:30:39 +01:00
public TileEntityFluidCollector(){
super(2, "fluidCollector");
this.isPlacer = false;
}
@Override
public void toggle(boolean to){
this.activateOnceWithSignal = to;
}
@Override
public boolean isPulseMode(){
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
if(coordsBlock != null){
2016-05-02 17:46:53 +02:00
Block blockToBreak = PosUtil.getBlock(coordsBlock, this.worldObj);
if(!this.isPlacer && blockToBreak != null && PosUtil.getMetadata(coordsBlock, this.worldObj) == 0 && FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount()){
2015-12-19 10:30:39 +01:00
if(blockToBreak instanceof IFluidBlock && ((IFluidBlock)blockToBreak).getFluid() != null){
if(this.tank.fill(new FluidStack(((IFluidBlock)blockToBreak).getFluid(), FluidContainerRegistry.BUCKET_VOLUME), false) >= FluidContainerRegistry.BUCKET_VOLUME){
this.tank.fill(new FluidStack(((IFluidBlock)blockToBreak).getFluid(), FluidContainerRegistry.BUCKET_VOLUME), true);
2016-05-02 17:46:53 +02:00
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
2015-12-19 10:30:39 +01:00
}
}
2016-04-20 21:39:03 +02:00
else if(blockToBreak == Blocks.LAVA || blockToBreak == Blocks.FLOWING_LAVA){
2015-12-19 10:30:39 +01:00
if(this.tank.fill(new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME), false) >= FluidContainerRegistry.BUCKET_VOLUME){
this.tank.fill(new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME), true);
2016-05-02 17:46:53 +02:00
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
2015-12-19 10:30:39 +01:00
}
}
2016-04-20 21:39:03 +02:00
else if(blockToBreak == Blocks.WATER || blockToBreak == Blocks.FLOWING_WATER){
2015-12-19 10:30:39 +01:00
if(this.tank.fill(new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME), false) >= FluidContainerRegistry.BUCKET_VOLUME){
this.tank.fill(new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME), true);
2016-05-02 17:46:53 +02:00
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.pos);
2015-12-19 10:30:39 +01:00
}
}
}
2016-05-02 17:46:53 +02:00
else if(this.isPlacer && PosUtil.getBlock(coordsBlock, this.worldObj).isReplaceable(this.worldObj, coordsBlock)){
2015-12-19 10:30:39 +01:00
if(this.tank.getFluidAmount() >= FluidContainerRegistry.BUCKET_VOLUME){
2016-05-03 18:29:02 +02:00
Block block = this.tank.getFluid().getFluid().getBlock();
if(block != null){
BlockPos offsetPos = this.pos.offset(sideToManipulate);
Block blockPresent = PosUtil.getBlock(offsetPos, this.worldObj);
boolean replaceable = blockPresent.isReplaceable(this.worldObj, offsetPos);
if(replaceable){
PosUtil.setBlock(offsetPos, this.worldObj, block, 0, 3);
this.tank.drain(FluidContainerRegistry.BUCKET_VOLUME, 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
public int fill(EnumFacing from, FluidStack resource, boolean doFill){
2015-05-30 17:47:57 +02:00
if(this.isPlacer){
return this.tank.fill(resource, doFill);
}
return 0;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain){
2015-05-30 17:47:57 +02:00
if(!this.isPlacer){
return this.tank.drain(resource.amount, doDrain);
}
return null;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain){
2015-05-30 17:47:57 +02:00
if(!this.isPlacer){
return this.tank.drain(maxDrain, doDrain);
}
return null;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid){
return this.isPlacer;
2015-05-30 17:47:57 +02:00
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid){
return !this.isPlacer;
2015-05-30 17:47:57 +02:00
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from){
2015-05-30 17:47:57 +02:00
return new FluidTankInfo[]{this.tank.getInfo()};
}
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
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
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
}
2015-10-02 16:48:01 +02:00
if(!this.isPlacer){
2016-05-02 17:46:53 +02:00
WorldUtil.fillBucket(this.tank, this.slots, 0, 1);
2015-10-02 16:48:01 +02:00
}
else{
2016-05-02 17:46:53 +02:00
WorldUtil.emptyBucket(this.tank, this.slots, 0, 1);
2015-10-02 16:48:01 +02:00
}
2015-05-30 17:47:57 +02:00
if(!this.isPlacer && this.tank.getFluidAmount() > 0){
2016-05-02 17:46:53 +02:00
WorldUtil.pushFluid(this.worldObj, this.pos, EnumFacing.DOWN, this.tank);
if(!this.isRedstonePowered){
2016-05-02 17:46:53 +02:00
WorldUtil.pushFluid(this.worldObj, this.pos, EnumFacing.NORTH, this.tank);
WorldUtil.pushFluid(this.worldObj, this.pos, EnumFacing.EAST, this.tank);
WorldUtil.pushFluid(this.worldObj, this.pos, EnumFacing.SOUTH, this.tank);
WorldUtil.pushFluid(this.worldObj, this.pos, EnumFacing.WEST, this.tank);
}
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
}
}
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
if(i == 0){
2015-10-02 16:48:01 +02:00
if(this.isPlacer){
return FluidContainerRegistry.isFilledContainer(stack);
}
else{
return stack.isItemEqual(FluidContainerRegistry.EMPTY_BUCKET);
}
2015-05-30 17:47:57 +02:00
}
return false;
}
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 boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
return this.isItemValidForSlot(slot, stack);
}
2015-05-30 17:47:57 +02:00
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
2015-05-30 17:47:57 +02:00
return slot == 1;
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
2015-10-03 10:16:18 +02:00
public static class TileEntityFluidPlacer extends TileEntityFluidCollector{
public TileEntityFluidPlacer(){
super(2, "fluidPlacer");
this.isPlacer = true;
}
}
2015-05-30 17:47:57 +02:00
}