From bd860d8073c5eb71a07d5581afdb9dd84722a69b Mon Sep 17 00:00:00 2001 From: Justin Vitale <4002351+justinvvitale@users.noreply.github.com> Date: Thu, 9 Jul 2020 05:02:36 +1000 Subject: [PATCH] Allow players to rightclick tank unit with fluid to deposit (initial) --- .../blocks/storage/fluid/TankUnitBlock.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/main/java/techreborn/blocks/storage/fluid/TankUnitBlock.java b/src/main/java/techreborn/blocks/storage/fluid/TankUnitBlock.java index 58de15145..00d0278f7 100644 --- a/src/main/java/techreborn/blocks/storage/fluid/TankUnitBlock.java +++ b/src/main/java/techreborn/blocks/storage/fluid/TankUnitBlock.java @@ -24,11 +24,29 @@ package techreborn.blocks.storage.fluid; +import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.fluid.Fluid; +import net.minecraft.item.BucketItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ToolItem; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; +import net.minecraft.world.World; import reborncore.api.blockentity.IMachineGuiHandler; import reborncore.common.blocks.BlockMachineBase; +import reborncore.common.fluid.FluidValue; +import reborncore.common.fluid.RebornBucketItem; +import reborncore.common.fluid.container.FluidInstance; +import reborncore.common.fluid.container.ItemFluidInfo; +import reborncore.common.util.Tank; import techreborn.blockentity.storage.fluid.TankUnitBaseBlockEntity; +import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity; import techreborn.client.GuiType; import techreborn.init.TRContent; @@ -46,6 +64,44 @@ public class TankUnitBlock extends BlockMachineBase { return new TankUnitBaseBlockEntity(unitType); } + @Override + public ActionResult onUse(BlockState state, World worldIn, BlockPos pos, PlayerEntity playerIn, Hand hand, BlockHitResult hitResult) { + if (unitType == TRContent.TankUnit.CREATIVE || worldIn.isClient) { + return super.onUse(state, worldIn, pos, playerIn, hand, hitResult); + } + + final TankUnitBaseBlockEntity tankUnitEntity = (TankUnitBaseBlockEntity) worldIn.getBlockEntity(pos); + ItemStack stackInHand = playerIn.getStackInHand(Hand.MAIN_HAND); + Item itemInHand = stackInHand.getItem(); + + // Assuming ItemFluidInfo is 1 BUCKET, for now only allow exact amount or less + if (tankUnitEntity != null && itemInHand instanceof ItemFluidInfo) { + ItemFluidInfo itemFluid = (ItemFluidInfo)itemInHand; + Fluid fluid = itemFluid.getFluid(stackInHand); + int amount = stackInHand.getCount(); + + FluidValue fluidValue = FluidValue.BUCKET.multiply(amount); + Tank tankInstance = tankUnitEntity.getTank(); + + if(tankInstance.canFit(fluid, fluidValue)){ + if(tankInstance.getFluidInstance().isEmptyFluid()){ + tankInstance.setFluidInstance(new FluidInstance(fluid, fluidValue)); + }else{ + tankInstance.getFluidInstance().addAmount(fluidValue); + } + + ItemStack returnStack = itemFluid.getEmpty(); + returnStack.setCount(amount); + playerIn.setStackInHand(Hand.MAIN_HAND, returnStack); + } + + return ActionResult.SUCCESS; + } + + + return super.onUse(state, worldIn, pos, playerIn, hand, hitResult); + } + @Override public IMachineGuiHandler getGui() { return GuiType.TANK_UNIT;