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

232 lines
9.2 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.inventory.ContainerFluidCollector;
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.BlockState;
2021-02-27 16:33:00 +01:00
import net.minecraft.block.Blocks;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
2021-02-27 16:33:00 +01:00
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2021-02-27 16:33:00 +01:00
import net.minecraft.particles.ParticleTypes;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
2021-02-27 16:33:00 +01:00
import net.minecraft.util.SoundEvents;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2021-02-27 16:33:00 +01:00
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.LazyOptional;
2016-11-26 21:32:27 +01:00
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidBlock;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.IFluidHandler;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.fluids.capability.templates.FluidTank;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2015-05-30 17:47:57 +02:00
public class TileEntityFluidCollector extends TileEntityBase implements ISharingFluidHandler, INamedContainerProvider {
2015-05-30 17:47:57 +02:00
2016-07-07 17:59:45 +02:00
public boolean isPlacer;
2019-05-02 09:10:29 +02:00
public final FluidTank tank = new FluidTank(8 * Util.BUCKET) {
@Override
2021-02-27 16:33:00 +01:00
public int fill(FluidStack resource, FluidAction action) {
if (!TileEntityFluidCollector.this.isPlacer) {
return 0;
}
return super.fill(resource, action);
}
@Nonnull
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
if (!TileEntityFluidCollector.this.isPlacer) {
return FluidStack.EMPTY;
}
return super.drain(maxDrain, action);
}
2021-02-27 16:33:00 +01:00
@Nonnull
@Override
2021-02-27 16:33:00 +01:00
public FluidStack drain(FluidStack resource, FluidAction action) {
if (!TileEntityFluidCollector.this.isPlacer) {
return FluidStack.EMPTY;
}
return super.drain(resource, action);
}
};
2021-02-27 16:33:00 +01:00
public final LazyOptional<IFluidHandler> lazyTank = LazyOptional.of(() -> this.tank);
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
2021-02-27 16:33:00 +01:00
public TileEntityFluidCollector(TileEntityType<?> type) {
super(type);
2015-10-03 10:16:18 +02:00
}
2019-05-02 09:10:29 +02:00
public TileEntityFluidCollector() {
2021-02-27 16:33:00 +01:00
this(ActuallyTiles.FLUIDCOLLECTOR_TILE.get());
2015-12-19 10:30:39 +01:00
this.isPlacer = false;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public void activateOnPulse() {
this.doWork();
}
2021-02-27 16:33:00 +01:00
// TODO: [port] big old check on this entire functionality, I've not worked with fluids before
2019-05-02 09:10:29 +02:00
private void doWork() {
2021-02-26 22:15:48 +01:00
BlockState state = this.world.getBlockState(this.pos);
Direction sideToManipulate = WorldUtil.getDirectionByPistonRotation(state);
BlockPos coordsBlock = this.pos.offset(sideToManipulate);
2015-12-19 10:30:39 +01:00
2021-02-26 22:15:48 +01:00
BlockState stateToBreak = this.world.getBlockState(coordsBlock);
2016-07-04 20:15:41 +02:00
Block blockToBreak = stateToBreak.getBlock();
2021-02-27 16:33:00 +01:00
if (!this.isPlacer && Util.BUCKET <= this.tank.getCapacity() - this.tank.getFluidAmount()) {
2019-05-02 09:10:29 +02:00
if (blockToBreak instanceof IFluidBlock && ((IFluidBlock) blockToBreak).getFluid() != null) {
2021-02-27 16:33:00 +01:00
if (this.tank.fill(new FluidStack(((IFluidBlock) blockToBreak).getFluid(), Util.BUCKET), IFluidHandler.FluidAction.SIMULATE) >= Util.BUCKET) {
this.tank.fill(new FluidStack(((IFluidBlock) blockToBreak).getFluid(), Util.BUCKET), IFluidHandler.FluidAction.EXECUTE);
this.world.setBlockState(coordsBlock, Blocks.AIR.getDefaultState());
2015-12-19 10:30:39 +01:00
}
2021-02-27 16:33:00 +01:00
} else if (blockToBreak == Blocks.LAVA) {
if (this.tank.fill(new FluidStack(Fluids.LAVA, Util.BUCKET), IFluidHandler.FluidAction.SIMULATE) >= Util.BUCKET) {
this.tank.fill(new FluidStack(Fluids.LAVA, Util.BUCKET), IFluidHandler.FluidAction.EXECUTE);
this.world.setBlockState(coordsBlock, Blocks.AIR.getDefaultState());
2015-12-19 10:30:39 +01:00
}
2021-02-27 16:33:00 +01:00
} else if (blockToBreak == Blocks.WATER) {
if (this.tank.fill(new FluidStack(Fluids.WATER, Util.BUCKET), IFluidHandler.FluidAction.SIMULATE) >= Util.BUCKET) {
this.tank.fill(new FluidStack(Fluids.WATER, Util.BUCKET), IFluidHandler.FluidAction.EXECUTE);
this.world.setBlockState(coordsBlock, Blocks.AIR.getDefaultState());
2015-12-19 10:30:39 +01:00
}
}
2021-02-27 16:33:00 +01:00
} else if (this.isPlacer && blockToBreak.getDefaultState().getMaterial().isReplaceable()) {
2019-05-02 09:10:29 +02:00
if (this.tank.getFluidAmount() >= Util.BUCKET) {
FluidStack stack = this.tank.getFluid();
2021-02-27 16:33:00 +01:00
Block fluid = stack.getFluid().getDefaultState().getBlockState().getBlock();
2019-05-02 09:10:29 +02:00
if (fluid != null) {
2016-05-16 22:52:27 +02:00
BlockPos offsetPos = this.pos.offset(sideToManipulate);
2021-02-27 16:33:00 +01:00
boolean placeable = !(blockToBreak instanceof IFluidBlock) && blockToBreak.getDefaultState().getMaterial().isReplaceable();
2019-05-02 09:10:29 +02:00
if (placeable) {
2021-02-27 16:33:00 +01:00
this.tank.drain(Util.BUCKET, IFluidHandler.FluidAction.EXECUTE);
// TODO: [port] validate this check is still valid.
if (this.world.getDimensionType().isUltrawarm() && fluid.getDefaultState().getMaterial() == Material.WATER) {
2019-05-02 09:10:29 +02:00
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);
2021-02-27 16:33:00 +01:00
if (this.world instanceof ServerWorld) {
2019-05-02 09:10:29 +02:00
for (int l = 0; l < 8; ++l) {
2021-02-27 16:33:00 +01:00
((ServerWorld) this.world).spawnParticle(ParticleTypes.SMOKE, offsetPos.getX() + Math.random(), offsetPos.getY() + Math.random(), offsetPos.getZ() + Math.random(), 1, 0.0D, 0.0D, 0.0D, 0);
}
}
2019-05-02 09:10:29 +02:00
} 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
2019-05-02 09:10:29 +02:00
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
2021-02-27 16:33:00 +01:00
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
return this.lazyTank;
2015-05-30 17:47:57 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("CurrentTime", this.currentTime);
}
2016-02-01 17:49:55 +01:00
this.tank.writeToNBT(compound);
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.currentTime = compound.getInt("CurrentTime");
}
2016-02-01 17:49:55 +01:00
this.tank.readFromNBT(compound);
}
2015-05-30 17:47:57 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
2015-05-30 17:47:57 +02:00
this.currentTime--;
2019-05-02 09:10:29 +02:00
if (this.currentTime <= 0) {
this.doWork();
2015-05-30 17:47:57 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
this.currentTime = 15;
2015-10-02 16:48:01 +02:00
}
2015-05-30 17:47:57 +02:00
}
2019-05-02 09:10:29 +02:00
if (this.lastCompare != this.getComparatorStrength()) {
this.lastCompare = this.getComparatorStrength();
this.markDirty();
}
2019-05-02 09:10:29 +02:00
if (this.lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()) {
2016-05-02 17:46:53 +02:00
this.lastTankAmount = this.tank.getFluidAmount();
2015-05-30 17:47:57 +02:00
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public int getMaxFluidAmountToSplitShare() {
return this.tank.getFluidAmount();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareFluid() {
return !this.isPlacer;
}
@Override
public Direction[] getFluidShareSides() {
return Direction.values();
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
return new ContainerFluidCollector(windowId, playerInventory, this);
}
2015-05-30 17:47:57 +02:00
}