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
|
|
|
|
2021-11-13 18:02:42 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2022-04-09 20:11:27 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.fluids.AATank;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFluidCollector;
|
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;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.block.Blocks;
|
2022-04-09 20:11:27 +02:00
|
|
|
import net.minecraft.block.FlowingFluidBlock;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.block.material.Material;
|
2021-03-01 18:46:12 +01:00
|
|
|
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;
|
2021-03-01 18:46:12 +01:00
|
|
|
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;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-08-21 11:45:57 +02:00
|
|
|
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;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2022-04-09 20:11:27 +02:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2022-04-09 20:11:27 +02:00
|
|
|
import net.minecraftforge.fluids.FluidAttributes;
|
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;
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2015-05-30 17:47:57 +02:00
|
|
|
|
2021-03-01 18:46:12 +01: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;
|
2022-04-09 20:11:27 +02:00
|
|
|
public final AATank tank = new AATank(8 * FluidAttributes.BUCKET_VOLUME) {
|
2016-06-19 22:30:18 +02:00
|
|
|
@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) {
|
2022-04-09 20:11:27 +02:00
|
|
|
if (TileEntityFluidCollector.this.isPlacer) {
|
2021-02-27 16:33:00 +01:00
|
|
|
return FluidStack.EMPTY;
|
|
|
|
}
|
|
|
|
return super.drain(maxDrain, action);
|
2016-06-19 22:30:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
@Nonnull
|
2016-06-19 22:30:18 +02:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public FluidStack drain(FluidStack resource, FluidAction action) {
|
2022-04-09 20:11:27 +02:00
|
|
|
if (TileEntityFluidCollector.this.isPlacer) {
|
2021-02-27 16:33:00 +01:00
|
|
|
return FluidStack.EMPTY;
|
|
|
|
}
|
|
|
|
return super.drain(resource, action);
|
2016-06-19 22:30:18 +02:00
|
|
|
}
|
|
|
|
};
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IFluidHandler> lazyTank = LazyOptional.of(() -> this.tank);
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastTankAmount;
|
2015-10-03 10:16:18 +02:00
|
|
|
private int currentTime;
|
2016-12-18 11:58:39 +01:00
|
|
|
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-11-13 18:02:42 +01:00
|
|
|
this(ActuallyBlocks.FLUID_COLLECTOR.getTileEntityType());
|
2015-12-19 10:30:39 +01:00
|
|
|
this.isPlacer = false;
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean isRedstoneToggle() {
|
2016-07-02 15:01:46 +02:00
|
|
|
return true;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void activateOnPulse() {
|
2015-12-17 18:47:46 +01:00
|
|
|
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-08-22 17:09:06 +02:00
|
|
|
BlockState state = this.level.getBlockState(this.worldPosition);
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction sideToManipulate = WorldUtil.getDirectionByPistonRotation(state);
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos coordsBlock = this.worldPosition.relative(sideToManipulate);
|
2015-12-19 10:30:39 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockState stateToBreak = this.level.getBlockState(coordsBlock);
|
2016-07-04 20:15:41 +02:00
|
|
|
Block blockToBreak = stateToBreak.getBlock();
|
2022-04-09 20:11:27 +02:00
|
|
|
if (!this.isPlacer && FluidAttributes.BUCKET_VOLUME <= this.tank.getCapacity() - this.tank.getFluidAmount()) {
|
|
|
|
if (blockToBreak instanceof FlowingFluidBlock && stateToBreak.getFluidState().isSource() && ((FlowingFluidBlock) blockToBreak).getFluid() != null) {
|
|
|
|
if (this.tank.fillInternal(new FluidStack(((FlowingFluidBlock) blockToBreak).getFluid(), FluidAttributes.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) >= FluidAttributes.BUCKET_VOLUME) {
|
|
|
|
this.tank.fillInternal(new FluidStack(((FlowingFluidBlock) blockToBreak).getFluid(), FluidAttributes.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
|
2021-08-22 17:09:06 +02:00
|
|
|
this.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState());
|
2015-12-19 10:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-22 17:09:06 +02:00
|
|
|
} else if (this.isPlacer && blockToBreak.defaultBlockState().getMaterial().isReplaceable()) {
|
2022-04-09 20:11:27 +02:00
|
|
|
if (this.tank.getFluidAmount() >= FluidAttributes.BUCKET_VOLUME) {
|
2016-08-21 11:45:57 +02:00
|
|
|
FluidStack stack = this.tank.getFluid();
|
2021-08-22 17:09:06 +02:00
|
|
|
Block fluid = stack.getFluid().defaultFluidState().createLegacyBlock().getBlock();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (fluid != null) {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos offsetPos = this.worldPosition.relative(sideToManipulate);
|
|
|
|
boolean placeable = !(blockToBreak instanceof IFluidBlock) && blockToBreak.defaultBlockState().getMaterial().isReplaceable();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (placeable) {
|
2022-04-09 20:11:27 +02:00
|
|
|
this.tank.drainInternal(FluidAttributes.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] validate this check is still valid.
|
2021-08-22 17:09:06 +02:00
|
|
|
if (this.level.dimensionType().ultraWarm() && fluid.defaultBlockState().getMaterial() == Material.WATER) {
|
|
|
|
this.level.playSound(null, offsetPos, SoundEvents.FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (this.level.random.nextFloat() - this.level.random.nextFloat()) * 0.8F);
|
2016-08-21 11:45:57 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (this.level instanceof ServerWorld) {
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int l = 0; l < 8; ++l) {
|
2021-08-22 17:09:06 +02:00
|
|
|
((ServerWorld) this.level).sendParticles(ParticleTypes.SMOKE, offsetPos.getX() + Math.random(), offsetPos.getY() + Math.random(), offsetPos.getZ() + Math.random(), 1, 0.0D, 0.0D, 0.0D, 0);
|
2016-08-21 11:45:57 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-08-22 17:09:06 +02:00
|
|
|
this.level.setBlock(offsetPos, fluid.defaultBlockState(), 3);
|
2016-08-21 11:45:57 +02:00
|
|
|
}
|
2015-12-19 10:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
2015-05-30 17:47:57 +02:00
|
|
|
|
2016-12-18 11:58:39 +01: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;
|
2016-12-18 11:58:39 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-07-02 15:01:46 +02:00
|
|
|
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-07-02 15:01:46 +02:00
|
|
|
}
|
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) {
|
2016-07-02 15:01:46 +02:00
|
|
|
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-07-02 15:01:46 +02:00
|
|
|
}
|
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() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
2019-05-02 09:10:29 +02:00
|
|
|
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) {
|
2015-12-17 18:47:46 +01:00
|
|
|
this.doWork();
|
2015-05-30 17:47:57 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +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
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.lastCompare != this.getComparatorStrength()) {
|
2016-12-18 11:58:39 +01:00
|
|
|
this.lastCompare = this.getComparatorStrength();
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2016-12-18 11:58:39 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:16:36 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getMaxFluidAmountToSplitShare() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return this.tank.getFluidAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean doesShareFluid() {
|
2016-10-29 12:00:00 +02:00
|
|
|
return !this.isPlacer;
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction[] getFluidShareSides() {
|
|
|
|
return Direction.values();
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
2022-04-09 20:11:27 +02:00
|
|
|
return new TranslationTextComponent(isPlacer ? "container.actuallyadditions.fluidPlacer" : "container.actuallyadditions.fluidCollector");
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|