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

237 lines
9.1 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
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2015-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2015-05-30 17:47:57 +02:00
package ellpeck.actuallyadditions.tile;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.util.Position;
2015-05-30 17:47:57 +02:00
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
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(){
ForgeDirection sideToManipulate = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
Position coordsBlock = WorldUtil.getCoordsFromSide(sideToManipulate, xCoord, yCoord, zCoord, 0);
2015-12-19 10:30:39 +01:00
if(coordsBlock != null){
Block blockToBreak = worldObj.getBlock(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
if(!this.isPlacer && blockToBreak != null && worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()) == 0 && FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount()){
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);
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord);
}
}
else if(blockToBreak == Blocks.lava || blockToBreak == Blocks.flowing_lava){
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);
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord);
}
}
else if(blockToBreak == Blocks.water || blockToBreak == Blocks.flowing_water){
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);
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord);
}
}
}
else if(this.isPlacer && worldObj.getBlock(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()).isReplaceable(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ())){
if(this.tank.getFluidAmount() >= FluidContainerRegistry.BUCKET_VOLUME){
if(this.tank.getFluid().getFluid().getBlock() != null){
Block block = worldObj.getBlock(xCoord+sideToManipulate.offsetX, yCoord+sideToManipulate.offsetY, zCoord+sideToManipulate.offsetZ);
if(!(block instanceof IFluidBlock) && block != Blocks.lava && block != Blocks.water && block != Blocks.flowing_lava && block != Blocks.flowing_water){
WorldUtil.placeBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, new ItemStack(this.tank.getFluid().getFluid().getBlock()));
this.tank.drain(FluidContainerRegistry.BUCKET_VOLUME, true);
}
}
}
}
}
2015-10-03 10:16:18 +02:00
}
2015-05-30 17:47:57 +02:00
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill){
if(this.isPlacer){
return this.tank.fill(resource, doFill);
}
return 0;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain){
if(!this.isPlacer){
return this.tank.drain(resource.amount, doDrain);
}
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain){
if(!this.isPlacer){
return this.tank.drain(maxDrain, doDrain);
}
return null;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid){
return this.isPlacer;
2015-05-30 17:47:57 +02:00
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid){
return !this.isPlacer;
2015-05-30 17:47:57 +02:00
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from){
return new FluidTankInfo[]{this.tank.getInfo()};
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
2015-05-30 17:47:57 +02:00
if(!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){
WorldUtil.fillBucket(tank, slots, 0, 1);
}
else{
WorldUtil.emptyBucket(tank, slots, 0, 1);
}
2015-05-30 17:47:57 +02:00
if(!this.isPlacer && this.tank.getFluidAmount() > 0){
WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, this.tank);
if(!this.isRedstonePowered){
WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.NORTH, this.tank);
WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.EAST, this.tank);
WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.SOUTH, this.tank);
WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.WEST, this.tank);
}
2015-05-30 17:47:57 +02:00
}
if(lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()){
lastTankAmount = this.tank.getFluidAmount();
2015-05-30 17:47:57 +02:00
}
}
}
2015-10-03 10:19:40 +02:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
2015-10-03 10:19:40 +02:00
compound.setInteger("CurrentTime", this.currentTime);
this.tank.writeToNBT(compound);
}
2015-10-18 15:31:01 +02:00
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.currentTime = compound.getInteger("CurrentTime");
this.tank.readFromNBT(compound);
}
2015-12-19 10:30:39 +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, int side){
return this.isItemValidForSlot(slot, stack);
}
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;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
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
}