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

231 lines
8.9 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
2021-11-13 18:02:42 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.fluids.AATank;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFluidCollector;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
2024-03-03 01:20:53 +01:00
import net.minecraft.tags.FluidTags;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
2021-02-27 16:33:00 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2015-05-30 17:47:57 +02:00
2024-03-02 21:23:08 +01:00
public class TileEntityFluidCollector extends TileEntityBase implements ISharingFluidHandler, MenuProvider {
2015-05-30 17:47:57 +02:00
2016-07-07 17:59:45 +02:00
public boolean isPlacer;
2024-03-03 01:20:53 +01:00
public final AATank tank = new AATank(8 * FluidType.BUCKET_VOLUME) {
@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) {
2021-02-27 16:33:00 +01:00
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) {
2021-02-27 16:33:00 +01:00
return FluidStack.EMPTY;
}
return super.drain(resource, action);
}
};
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
2024-03-02 21:23:08 +01:00
public TileEntityFluidCollector(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
2015-10-03 10:16:18 +02:00
}
2024-03-02 21:23:08 +01:00
public TileEntityFluidCollector(BlockPos pos, BlockState state) {
this(ActuallyBlocks.FLUID_COLLECTOR.getTileEntityType(), pos, state);
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() {
BlockState state = this.level.getBlockState(this.worldPosition);
Direction sideToManipulate = WorldUtil.getDirectionByPistonRotation(state);
BlockPos coordsBlock = this.worldPosition.relative(sideToManipulate);
2015-12-19 10:30:39 +01:00
BlockState stateToBreak = this.level.getBlockState(coordsBlock);
2016-07-04 20:15:41 +02:00
Block blockToBreak = stateToBreak.getBlock();
2024-03-03 01:20:53 +01:00
if (!this.isPlacer && FluidType.BUCKET_VOLUME <= this.tank.getCapacity() - this.tank.getFluidAmount()) {
2024-03-02 21:23:08 +01:00
if (blockToBreak instanceof LiquidBlock && stateToBreak.getFluidState().isSource() && ((LiquidBlock) blockToBreak).getFluid() != null) {
2024-03-03 01:20:53 +01:00
if (this.tank.fillInternal(new FluidStack(((LiquidBlock) blockToBreak).getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) >= FluidType.BUCKET_VOLUME) {
this.tank.fillInternal(new FluidStack(((LiquidBlock) blockToBreak).getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
this.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState());
2015-12-19 10:30:39 +01:00
}
}
2024-03-03 01:20:53 +01:00
} else if (this.isPlacer && blockToBreak.defaultBlockState().canBeReplaced()) {
if (this.tank.getFluidAmount() >= FluidType.BUCKET_VOLUME) {
FluidStack stack = this.tank.getFluid();
Block fluid = stack.getFluid().defaultFluidState().createLegacyBlock().getBlock();
2019-05-02 09:10:29 +02:00
if (fluid != null) {
BlockPos offsetPos = this.worldPosition.relative(sideToManipulate);
boolean placeable = !stateToBreak.getFluidState().isSource() && blockToBreak.defaultBlockState().canBeReplaced();
2019-05-02 09:10:29 +02:00
if (placeable) {
2024-03-03 01:20:53 +01:00
this.tank.drainInternal(FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
2021-02-27 16:33:00 +01:00
// TODO: [port] validate this check is still valid.
2024-03-03 01:20:53 +01:00
if (this.level.dimensionType().ultraWarm() && stack.getFluid().is(FluidTags.WATER)) {
2024-03-02 21:23:08 +01:00
this.level.playSound(null, offsetPos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5F, 2.6F + (this.level.random.nextFloat() - this.level.random.nextFloat()) * 0.8F);
2024-03-02 21:23:08 +01:00
if (this.level instanceof ServerLevel) {
2019-05-02 09:10:29 +02:00
for (int l = 0; l < 8; ++l) {
2024-03-02 21:23:08 +01:00
((ServerLevel) 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);
}
}
2019-05-02 09:10:29 +02:00
} else {
this.level.setBlock(offsetPos, fluid.defaultBlockState(), 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
2024-03-04 20:21:48 +01:00
public IFluidHandler getFluidHandler(Direction facing) {
2024-03-04 21:38:02 +01:00
return this.tank;
2015-05-30 17:47:57 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag 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
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag 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);
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFluidCollector tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFluidCollector tile) {
tile.serverTick();
}
}
2024-03-02 21:23:08 +01:00
@Override
protected void serverTick() {
super.serverTick();
if (!isRedstonePowered && !isPulseMode) {
if (currentTime > 0) {
currentTime--;
if (currentTime <= 0) {
doWork();
2015-10-02 16:48:01 +02:00
}
} else {
currentTime = 15;
2015-05-30 17:47:57 +02:00
}
}
2015-05-30 17:47:57 +02:00
if (lastCompare != getComparatorStrength()) {
lastCompare = getComparatorStrength();
setChanged();
}
if (lastTankAmount != tank.getFluidAmount() && sendUpdateWithInterval()) {
lastTankAmount = 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
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.translatable(isPlacer ? "container.actuallyadditions.fluidPlacer" : "container.actuallyadditions.fluidCollector");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
return new ContainerFluidCollector(windowId, playerInventory, this);
}
2015-05-30 17:47:57 +02:00
}