From 660e976a1f5690e47492b6209af6372e38551cc1 Mon Sep 17 00:00:00 2001 From: Flanks255 <32142731+Flanks255@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:11:27 -0500 Subject: [PATCH] 1st pass at the fluid placer and collector. --- .../mod/blocks/ActuallyBlocks.java | 6 +-- .../mod/blocks/BlockFluidCollector.java | 37 ++++++++++++++++--- .../actuallyadditions/mod/fluids/AATank.java | 26 +++++++++++++ .../mod/inventory/gui/GuiFluidCollector.java | 9 ++--- .../mod/tile/TileEntityFluidCollector.java | 34 +++++++---------- 5 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/fluids/AATank.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/ActuallyBlocks.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/ActuallyBlocks.java index e46ac97df..23d86fee5 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/ActuallyBlocks.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/ActuallyBlocks.java @@ -84,10 +84,10 @@ public final class ActuallyBlocks { (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityPlacer::new); public static final AABlockReg DROPPER = new AABlockReg<>("dropper", BlockDropper::new, (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityDropper::new); - public static final AABlockReg FLUID_PLACER = new AABlockReg<>("fluid_placer", () -> new BlockFluidCollector(true), - (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityFluidCollector::new); - public static final AABlockReg FLUID_COLLECTOR = new AABlockReg<>("fluid_collector", () -> new BlockFluidCollector(false), + public static final AABlockReg FLUID_PLACER = new AABlockReg<>("fluid_placer", () -> new BlockFluidCollector(true), (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityFluidPlacer::new); + public static final AABlockReg FLUID_COLLECTOR = new AABlockReg<>("fluid_collector", () -> new BlockFluidCollector(false), + (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityFluidCollector::new); public static final AABlockReg FARMER = new AABlockReg<>("farmer", BlockFarmer::new, (b) -> new AABlockItem(b, defaultBlockItemProperties), TileEntityFarmer::new); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockFluidCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockFluidCollector.java index a086939f6..d6401687c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockFluidCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockFluidCollector.java @@ -11,20 +11,33 @@ package de.ellpeck.actuallyadditions.mod.blocks; import de.ellpeck.actuallyadditions.mod.blocks.base.FullyDirectionalBlock; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityDropper; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityFermentingBarrel; import de.ellpeck.actuallyadditions.mod.tile.TileEntityFluidCollector; import de.ellpeck.actuallyadditions.mod.tile.TileEntityFluidPlacer; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; import net.minecraft.util.Hand; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvents; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; +import net.minecraftforge.fluids.FluidUtil; +import net.minecraftforge.fml.network.NetworkHooks; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +@SuppressWarnings("deprecation") public class BlockFluidCollector extends FullyDirectionalBlock.Container { private final boolean isPlacer; @@ -33,24 +46,38 @@ public class BlockFluidCollector extends FullyDirectionalBlock.Container { this.isPlacer = isPlacer; } - //@Override - public TileEntity newBlockEntity(IBlockReader worldIn) { + @Override + public boolean hasTileEntity(BlockState state) { + return true; + } + + @Nullable + @Override + public TileEntity createTileEntity(BlockState state, IBlockReader world) { return this.isPlacer ? new TileEntityFluidPlacer() : new TileEntityFluidCollector(); } + @Nonnull @Override - public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) { + public ActionResultType use(@Nonnull BlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand handIn, @Nonnull BlockRayTraceResult hit) { + if (world.isClientSide) + return ActionResultType.SUCCESS; + if (this.tryToggleRedstone(world, pos, player)) { - return ActionResultType.PASS; + return ActionResultType.CONSUME; + } + if (FluidUtil.interactWithFluidHandler(player, handIn, world, pos, hit.getDirection())) { + return ActionResultType.SUCCESS; } return this.openGui(world, player, pos, TileEntityFluidCollector.class); } + @Nonnull @Override - public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { + public VoxelShape getShape(BlockState state, @Nonnull IBlockReader worldIn, @Nonnull BlockPos pos, @Nonnull ISelectionContext context) { switch (state.getValue(FACING)) { case UP: return Shapes.FluidCollectorShapes.SHAPE_U; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/fluids/AATank.java b/src/main/java/de/ellpeck/actuallyadditions/mod/fluids/AATank.java new file mode 100644 index 000000000..dd05ac97d --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/fluids/AATank.java @@ -0,0 +1,26 @@ +package de.ellpeck.actuallyadditions.mod.fluids; + +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.capability.templates.FluidTank; + +import javax.annotation.Nonnull; +import java.util.function.Predicate; + +public class AATank extends FluidTank { + public AATank(int capacity) { + super(capacity); + } + + public AATank(int capacity, Predicate validator) { + super(capacity, validator); + } + + public int fillInternal(FluidStack res, FluidAction action) { + return super.fill(res, action); + } + + @Nonnull + public FluidStack drainInternal(int maxDrain, FluidAction action) { + return super.drain(maxDrain, action); + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFluidCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFluidCollector.java index 06dbe3e25..a9da139bb 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFluidCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFluidCollector.java @@ -48,12 +48,9 @@ public class GuiFluidCollector extends AAScreen { @Override public void init() { super.init(); - //this.fluid = new FluidDisplay(this.leftPos + 67, this.topPos + 5, this.collector.tank); - } - - @Override - public void renderLabels(@Nonnull MatrixStack matrices, int x, int y) { - AssetUtil.displayNameString(matrices, this.font, this.imageWidth, -10, this.collector); + this.fluid = new FluidDisplay(this.leftPos + 67, this.topPos + 5, this.collector.tank); + titleLabelX = (int) (imageWidth / 2.0f - font.width(title) / 2.0f); + titleLabelY = -10; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java index 3e02d749c..a68442b71 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java @@ -11,12 +11,14 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks; +import de.ellpeck.actuallyadditions.mod.fluids.AATank; import de.ellpeck.actuallyadditions.mod.inventory.ContainerFluidCollector; import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.block.FlowingFluidBlock; import net.minecraft.block.material.Material; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; @@ -32,8 +34,10 @@ import net.minecraft.util.SoundEvents; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.server.ServerWorld; import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.fluids.FluidAttributes; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidBlock; import net.minecraftforge.fluids.capability.IFluidHandler; @@ -45,7 +49,7 @@ import javax.annotation.Nullable; public class TileEntityFluidCollector extends TileEntityBase implements ISharingFluidHandler, INamedContainerProvider { public boolean isPlacer; - public final FluidTank tank = new FluidTank(8 * Util.BUCKET) { + public final AATank tank = new AATank(8 * FluidAttributes.BUCKET_VOLUME) { @Override public int fill(FluidStack resource, FluidAction action) { if (!TileEntityFluidCollector.this.isPlacer) { @@ -57,7 +61,7 @@ public class TileEntityFluidCollector extends TileEntityBase implements ISharing @Nonnull @Override public FluidStack drain(int maxDrain, FluidAction action) { - if (!TileEntityFluidCollector.this.isPlacer) { + if (TileEntityFluidCollector.this.isPlacer) { return FluidStack.EMPTY; } return super.drain(maxDrain, action); @@ -66,7 +70,7 @@ public class TileEntityFluidCollector extends TileEntityBase implements ISharing @Nonnull @Override public FluidStack drain(FluidStack resource, FluidAction action) { - if (!TileEntityFluidCollector.this.isPlacer) { + if (TileEntityFluidCollector.this.isPlacer) { return FluidStack.EMPTY; } return super.drain(resource, action); @@ -104,32 +108,22 @@ public class TileEntityFluidCollector extends TileEntityBase implements ISharing BlockState stateToBreak = this.level.getBlockState(coordsBlock); Block blockToBreak = stateToBreak.getBlock(); - if (!this.isPlacer && Util.BUCKET <= this.tank.getCapacity() - this.tank.getFluidAmount()) { - if (blockToBreak instanceof IFluidBlock && ((IFluidBlock) blockToBreak).getFluid() != null) { - 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.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState()); - } - } 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.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState()); - } - } 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); + 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); this.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState()); } } } else if (this.isPlacer && blockToBreak.defaultBlockState().getMaterial().isReplaceable()) { - if (this.tank.getFluidAmount() >= Util.BUCKET) { + if (this.tank.getFluidAmount() >= FluidAttributes.BUCKET_VOLUME) { FluidStack stack = this.tank.getFluid(); Block fluid = stack.getFluid().defaultFluidState().createLegacyBlock().getBlock(); if (fluid != null) { BlockPos offsetPos = this.worldPosition.relative(sideToManipulate); boolean placeable = !(blockToBreak instanceof IFluidBlock) && blockToBreak.defaultBlockState().getMaterial().isReplaceable(); if (placeable) { - this.tank.drain(Util.BUCKET, IFluidHandler.FluidAction.EXECUTE); + this.tank.drainInternal(FluidAttributes.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE); // TODO: [port] validate this check is still valid. 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); @@ -221,7 +215,7 @@ public class TileEntityFluidCollector extends TileEntityBase implements ISharing @Override public ITextComponent getDisplayName() { - return StringTextComponent.EMPTY; + return new TranslationTextComponent(isPlacer ? "container.actuallyadditions.fluidPlacer" : "container.actuallyadditions.fluidCollector"); } @Nullable