<commit_before>package exnihiloadscensio.tiles;

import java.util.ArrayList;

import lombok.Getter;
import exnihiloadscensio.barrel.BarrelFluidHandler;
import exnihiloadscensio.barrel.BarrelItemHandler;
import exnihiloadscensio.barrel.IBarrelMode;
import exnihiloadscensio.config.Config;
import exnihiloadscensio.networking.MessageBarrelModeUpdate;
import exnihiloadscensio.networking.PacketHandler;
import exnihiloadscensio.registries.BarrelModeRegistry;
import exnihiloadscensio.registries.BarrelModeRegistry.TriggerType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.CapabilityItemHandler;

public class TileBarrel extends TileEntity implements ITickable {

	@Getter
	private IBarrelMode mode;

	private BarrelItemHandler itemHandler;
	@Getter
	private BarrelFluidHandler tank;

	public TileBarrel()
	{
		itemHandler = new BarrelItemHandler(this);
		tank = new BarrelFluidHandler(this);
	}

	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
	{
		if (mode == null || mode.getName().equals("fluid")) {
			ItemStack stack = player.getHeldItemMainhand();
			if (stack != null && stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null)) {
				IFluidHandler handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
				FluidStack drainStack = handler.drain(Fluid.BUCKET_VOLUME, false);
				int amount = this.getTank().fill(drainStack, false);
				
				if (amount > 0) {
					handler.drain(Fluid.BUCKET_VOLUME, true);
					this.getTank().fill(drainStack, true);
					this.setMode("fluid");
					PacketHandler.sendToAllAround(new MessageBarrelModeUpdate("fluid", pos), this);
					return true;
				}
			}
		}
		if (mode == null)
		{
			if (player.getHeldItem(EnumHand.MAIN_HAND) != null)
			{				
				ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);
				ArrayList<IBarrelMode> modes = BarrelModeRegistry.getModes(TriggerType.ITEM);
				if (modes == null)
					return false;
				for (IBarrelMode possibleMode : modes)
				{
					if (possibleMode.isTriggerItemStack(stack))
					{
						setMode(possibleMode.getName());
						PacketHandler.sendToAllAround(new MessageBarrelModeUpdate(mode.getName(), this.pos), this);
						mode.onBlockActivated(world, this, pos, state, player, side, hitX, hitY, hitZ);
						this.markDirty();
						this.worldObj.setBlockState(pos, state);
						return true;
					}
				}
			}
		}
		else
		{
			return mode.onBlockActivated(world, this, pos, state, player, side, hitX, hitY, hitZ);
		}

		return true;
	}

	@Override
	public void update()
	{
		if (worldObj.isRemote)
			return;

		if (Config.shouldBarrelsFillWithRain && (mode == null || mode.getName() == "fluid")) {
			BlockPos plusY = new BlockPos(pos.getX(), pos.getY()+1, pos.getZ());
			if(worldObj.isRainingAt(plusY)) {
				FluidStack stack = new FluidStack(FluidRegistry.WATER, 2);
				tank.fill(stack, true);
			}
		}
		if (mode != null)
			mode.update(this);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound tag)
	{
		tank.writeToNBT(tag);

		if (mode != null)<commit_msg>Change method for interacting with fluid handlers for barrels<commit_after>package exnihiloadscensio.tiles;

import java.util.ArrayList;

import lombok.Getter;
import exnihiloadscensio.barrel.BarrelFluidHandler;
import exnihiloadscensio.barrel.BarrelItemHandler;
import exnihiloadscensio.barrel.IBarrelMode;
import exnihiloadscensio.config.Config;
import exnihiloadscensio.networking.MessageBarrelModeUpdate;
import exnihiloadscensio.networking.PacketHandler;
import exnihiloadscensio.registries.BarrelModeRegistry;
import exnihiloadscensio.registries.BarrelModeRegistry.TriggerType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.CapabilityItemHandler;

public class TileBarrel extends TileEntity implements ITickable {

	@Getter
	private IBarrelMode mode;

	private BarrelItemHandler itemHandler;
	@Getter
	private BarrelFluidHandler tank;

	public TileBarrel()
	{
		itemHandler = new BarrelItemHandler(this);
		tank = new BarrelFluidHandler(this);
	}

	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
	{
		if (mode == null || mode.getName().equals("fluid")) {
			ItemStack stack = player.getHeldItemMainhand();
			boolean result = FluidUtil.interactWithFluidHandler(stack, this.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side), player);
			
			if (result)
				return true;
		}
		if (mode == null)
		{
			if (player.getHeldItem(EnumHand.MAIN_HAND) != null)
			{				
				ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);
				ArrayList<IBarrelMode> modes = BarrelModeRegistry.getModes(TriggerType.ITEM);
				if (modes == null)
					return false;
				for (IBarrelMode possibleMode : modes)
				{
					if (possibleMode.isTriggerItemStack(stack))
					{
						setMode(possibleMode.getName());
						PacketHandler.sendToAllAround(new MessageBarrelModeUpdate(mode.getName(), this.pos), this);
						mode.onBlockActivated(world, this, pos, state, player, side, hitX, hitY, hitZ);
						this.markDirty();
						this.worldObj.setBlockState(pos, state);
						return true;
					}
				}
			}
		}
		else
		{
			mode.onBlockActivated(world, this, pos, state, player, side, hitX, hitY, hitZ);
			return true;
		}

		return true;
	}

	@Override
	public void update()
	{
		if (worldObj.isRemote)
			return;

		if (Config.shouldBarrelsFillWithRain && (mode == null || mode.getName() == "fluid")) {
			BlockPos plusY = new BlockPos(pos.getX(), pos.getY()+1, pos.getZ());
			if(worldObj.isRainingAt(plusY)) {
				FluidStack stack = new FluidStack(FluidRegistry.WATER, 2);
				tank.fill(stack, true);
			}
		}
		if (mode != null)
			mode.update(this);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound tag)
	{
		tank.writeToNBT(tag);

		if (mode != null)
{'fraction_non_alphanumeric': 0.10560796645702306, 'fraction_numerical': 0.0006551362683438155, 'mean_word_length': 5.0483359746434235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3397277', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2541, 'n_words': 465}