/*
 * This file is part of TechReborn, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2020 TechReborn
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package techreborn.blockentity.machine.misc;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidDrainable;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import reborncore.api.IToolDrop;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.fluid.container.ItemFluidInfo;
import reborncore.common.util.Tank;
import techreborn.TechReborn;
import techreborn.config.TechRebornConfig;
import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent;

public class DrainBlockEntity extends MachineBaseBlockEntity implements IToolDrop {

	protected Tank internalTank = new Tank("tank", FluidValue.BUCKET, this);

	public DrainBlockEntity(BlockPos pos, BlockState state) {
		super(TRBlockEntities.DRAIN, pos, state);
	}

	@Override
	public void tick(World world, BlockPos pos, BlockState state, MachineBaseBlockEntity blockEntity) {
		super.tick(world, pos, state, blockEntity);
		if (world == null || world.isClient) {
			return;
		}

		int ticks = TechRebornConfig.ticksUntilNextDrainAttempt;
		if (ticks > 0 && world.getTime() % ticks == 0) {

			if (internalTank.isEmpty()) {
				tryDrain();
			}
		}
	}

	@Nullable
	@Override
	public Tank getTank() {
		return internalTank;
	}

	private void tryDrain() {
		// Position above drain
		BlockPos above = this.getPos().up();

		// Block and state above drain
		BlockState aboveBlockState = world.getBlockState(above);
		Block aboveBlock = aboveBlockState.getBlock();

		if (aboveBlock instanceof FluidDrainable) {
			ItemStack fluidContainer = ((FluidDrainable) aboveBlock).tryDrainFluid(world, above, aboveBlockState);
			if (fluidContainer.getItem() instanceof ItemFluidInfo) {
				Fluid drainFluid = ((ItemFluidInfo) fluidContainer.getItem()).getFluid(fluidContainer);
				internalTank.setFluidInstance(new FluidInstance(drainFluid, FluidValue.BUCKET));
			} else {
				TechReborn.LOGGER.debug("Could not get Fluid from ItemStack " + fluidContainer.getItem());
			}
		}
	}

	@Override
	public ItemStack getToolDrop(PlayerEntity p0) {
		return TRContent.Machine.DRAIN.getStack();
	}
}

@@ -1259,21 +1259,17 @@
 t.block.
-Block
+*
 ;%0Aimport
@@ -1287,116 +1287,68 @@
 aft.
-block.BlockState;%0Aimport net.minecraft.block.FluidDrainable;%0Aimport net.minecraft.entity.player.PlayerEntity
+entity.player.PlayerEntity;%0Aimport net.minecraft.fluid.Fluid
 ;%0Aim
@@ -1369,32 +1369,33 @@
 raft.fluid.Fluid
+s
 ;%0Aimport net.min
@@ -3433,24 +3433,755 @@
 );%0A%09%09%09%7D%0A%09%09%7D%0A
+%09%09if (aboveBlock instanceof LeveledCauldronBlock && aboveBlockState.isOf(Blocks.WATER_CAULDRON)) %7B //ensure Water cauldron%0A%09%09%09Fluid drainFluid = Fluids.WATER;%0A%09%09%09int level;%0A%09%09%09if (aboveBlockState.contains(LeveledCauldronBlock.LEVEL))%7B%0A%09%09%09%09level = aboveBlockState.get(LeveledCauldronBlock.LEVEL);%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09world.setBlockState(above, Blocks.CAULDRON.getDefaultState());%0A%09%09%09internalTank.setFluidInstance(%0A%09%09%09%09new FluidInstance(drainFluid, FluidValue.BUCKET.fraction(3).multiply(level))%0A%09%09%09);%0A%09%09%7D%0A%09%09if (aboveBlock instanceof LavaCauldronBlock)%7B%0A%09%09%09world.setBlockState(above, Blocks.CAULDRON.getDefaultState());%0A%09%09%09internalTank.setFluidInstance(%0A%09%09%09%09new FluidInstance(Fluids.LAVA, FluidValue.BUCKET)%0A%09%09%09);%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Overri

Allow Drain drain Cauldrons. Thanks to aria1th