<commit_before>package mcjty.rftools.blocks.logic.sensor;

import mcjty.lib.container.DefaultSidedInventory;
import mcjty.lib.container.InventoryHelper;
import mcjty.lib.network.Argument;
import mcjty.rftools.blocks.logic.generic.LogicTileEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.BlockNetherWart;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
import net.minecraftforge.fluids.capability.wrappers.*;

import java.util.List;
import java.util.Map;

public class SensorTileEntity extends LogicTileEntity implements ITickable, DefaultSidedInventory {

    public static final String CMD_SETNUMBER = "setNumber";
    public static final String CMD_SETTYPE = "setType";
    public static final String CMD_SETAREA = "setArea";
    public static final String CMD_SETGROUP = "setGroup";


    private int number = 0;
    private SensorType sensorType = SensorType.SENSOR_BLOCK;
    private AreaType areaType = AreaType.AREA_1;
    private GroupType groupType = GroupType.GROUP_ONE;

    private int checkCounter = 0;
    private AxisAlignedBB cachedBox = null;

    private InventoryHelper inventoryHelper = new InventoryHelper(this, SensorContainer.factory, 1);

    @Override
    protected boolean needsCustomInvWrapper() {
        return true;
    }


    public SensorTileEntity() {
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        cachedBox = null;
        markDirty();
    }

    public SensorType getSensorType() {
        return sensorType;
    }

    public void setSensorType(SensorType sensorType) {
        this.sensorType = sensorType;
        cachedBox = null;
        markDirty();
    }

    public AreaType getAreaType() {
        return areaType;
    }

    public void setAreaType(AreaType areaType) {
        this.areaType = areaType;
        cachedBox = null;
        markDirty();
    }

    public GroupType getGroupType() {
        return groupType;
    }

    public void setGroupType(GroupType groupType) {
        this.groupType = groupType;
        cachedBox = null;
        markDirty();
    }

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

        checkCounter--;
        if (checkCounter > 0) {
            return;
        }
        checkCounter = 10;

        setRedstoneState(checkSensor());
    }

    public boolean checkSensor() {
        boolean newout;EnumFacing inputSide = getFacing(worldObj.getBlockState(getPos())).getInputSide();
        BlockPos newpos = getPos().offset(inputSide);

        switch (sensorType) {
            case SENSOR_BLOCK:
                newout = checkBlock(newpos, inputSide);
                break;
            case SENSOR_GROWTHLEVEL:
                newout = checkGrowthLevel(newpos, inputSide);
                break;
            case SENSOR_ENTITIES:
                newout = checkEntities(newpos, inputSide, Entity.class);
                break;
            case SENSOR_PLAYERS:
                newout = checkEntities(newpos, inputSide, EntityPlayer.class);
                break;
            case SENSOR_HOSTILE:
                newout = checkEntitiesHostile(newpos, inputSide);
                break;
            case SENSOR_PASSIVE:
                newout = checkEntitiesPassive(newpos, inputSide);
                break;
            default:
                newout = false;
        }
        return newout;
    }

    private boolean checkBlock(BlockPos newpos, EnumFacing dir) {
        int blockCount = areaType.getBlockCount();
        for (int i = 0 ; i < blockCount ; i++) {
            boolean result = checkBlock(newpos);
            if (result && groupType == GroupType.GROUP_ONE) {
                return true;
            }
            if ((!result) && groupType == GroupType.GROUP_ALL) {
                return false;
            }
            newpos = newpos.offset(dir);
        }
        return groupType == GroupType.GROUP_ALL;
    }

    private boolean checkBlock(BlockPos newpos) {
        IBlockState state = worldObj.getBlockState(newpos);
        ItemStack matcher = inventoryHelper.getStackInSlot(0);
        Block block = state.getBlock();
        if (matcher == null) {
        	if (block instanceof BlockLiquid || block instanceof BlockFluidBase) {
        		return !block.isAir(state, worldObj, newpos);
        	}
        	
            return block.isFullBlock(state);
        }
        ItemStack stack = block.getItem(worldObj, newpos, state);
        Item matcherItem = matcher.getItem();
        
    	FluidStack matcherFluidStack = new FluidBucketWrapper(matcher).getFluid();
	    if (matcherFluidStack != null) {
	    	Fluid matcherFluid = matcherFluidStack.getFluid();
	    	if (matcherFluid == null) {
	    		return false;
	    	}
	    	
    		Block matcherFluidBlock = matcherFluid.getBlock();
    		if (matcherFluidBlock == null) {
    			return false;
    		}
    		
			String matcherBlockName = matcherFluidBlock.getUnlocalizedName();
			String blockName = block.getUnlocalizedName();
			return blockName.equals(matcherBlockName);
	    } else if (stack != null) {
        	Item stackItem = stack.getItem();
            return matcherItem == stackItem;
        } else {
        	Item blockItem = Item.getItemFromBlock(block);
            return matcherItem == blockItem;
        }
    }

    private boolean checkGrowthLevel(BlockPos newpos, EnumFacing dir) {
        int blockCount = areaType.getBlockCount();
        for (int i = 0 ; i < blockCount ; i++) {
            boolean result = checkGrowthLevel(newpos);<commit_msg>Allow the player to use non-bucket fluid containers for filtering<commit_after>package mcjty.rftools.blocks.logic.sensor;

import mcjty.lib.container.DefaultSidedInventory;
import mcjty.lib.container.InventoryHelper;
import mcjty.lib.network.Argument;
import mcjty.rftools.blocks.logic.generic.LogicTileEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.BlockNetherWart;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
import net.minecraftforge.fluids.capability.wrappers.*;

import java.util.List;
import java.util.Map;

import ic2.core.item.ItemFluidCell;

public class SensorTileEntity extends LogicTileEntity implements ITickable, DefaultSidedInventory {

    public static final String CMD_SETNUMBER = "setNumber";
    public static final String CMD_SETTYPE = "setType";
    public static final String CMD_SETAREA = "setArea";
    public static final String CMD_SETGROUP = "setGroup";


    private int number = 0;
    private SensorType sensorType = SensorType.SENSOR_BLOCK;
    private AreaType areaType = AreaType.AREA_1;
    private GroupType groupType = GroupType.GROUP_ONE;

    private int checkCounter = 0;
    private AxisAlignedBB cachedBox = null;

    private InventoryHelper inventoryHelper = new InventoryHelper(this, SensorContainer.factory, 1);

    @Override
    protected boolean needsCustomInvWrapper() {
        return true;
    }


    public SensorTileEntity() {
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        cachedBox = null;
        markDirty();
    }

    public SensorType getSensorType() {
        return sensorType;
    }

    public void setSensorType(SensorType sensorType) {
        this.sensorType = sensorType;
        cachedBox = null;
        markDirty();
    }

    public AreaType getAreaType() {
        return areaType;
    }

    public void setAreaType(AreaType areaType) {
        this.areaType = areaType;
        cachedBox = null;
        markDirty();
    }

    public GroupType getGroupType() {
        return groupType;
    }

    public void setGroupType(GroupType groupType) {
        this.groupType = groupType;
        cachedBox = null;
        markDirty();
    }

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

        checkCounter--;
        if (checkCounter > 0) {
            return;
        }
        checkCounter = 10;

        setRedstoneState(checkSensor());
    }

    public boolean checkSensor() {
        boolean newout;EnumFacing inputSide = getFacing(worldObj.getBlockState(getPos())).getInputSide();
        BlockPos newpos = getPos().offset(inputSide);

        switch (sensorType) {
            case SENSOR_BLOCK:
                newout = checkBlock(newpos, inputSide);
                break;
            case SENSOR_GROWTHLEVEL:
                newout = checkGrowthLevel(newpos, inputSide);
                break;
            case SENSOR_ENTITIES:
                newout = checkEntities(newpos, inputSide, Entity.class);
                break;
            case SENSOR_PLAYERS:
                newout = checkEntities(newpos, inputSide, EntityPlayer.class);
                break;
            case SENSOR_HOSTILE:
                newout = checkEntitiesHostile(newpos, inputSide);
                break;
            case SENSOR_PASSIVE:
                newout = checkEntitiesPassive(newpos, inputSide);
                break;
            default:
                newout = false;
        }
        return newout;
    }

    private boolean checkBlock(BlockPos newpos, EnumFacing dir) {
        int blockCount = areaType.getBlockCount();
        for (int i = 0 ; i < blockCount ; i++) {
            boolean result = checkBlock(newpos);
            if (result && groupType == GroupType.GROUP_ONE) {
                return true;
            }
            if ((!result) && groupType == GroupType.GROUP_ALL) {
                return false;
            }
            newpos = newpos.offset(dir);
        }
        return groupType == GroupType.GROUP_ALL;
    }

    private boolean checkBlock(BlockPos newpos) {
        IBlockState state = worldObj.getBlockState(newpos);
        ItemStack matcher = inventoryHelper.getStackInSlot(0);
        Block block = state.getBlock();
        if (matcher == null) {
        	if (block instanceof BlockLiquid || block instanceof IFluidBlock) {
        		return !block.isAir(state, worldObj, newpos);
        	}
        	
            return block.isFullBlock(state);
        }
        ItemStack stack = block.getItem(worldObj, newpos, state);
        Item matcherItem = matcher.getItem();
        
        FluidStack matcherFluidStack = null;
        if (matcherItem instanceof IFluidContainerItem) {
	    	matcherFluidStack = ((IFluidContainerItem)matcherItem).getFluid(matcher);
	    	return checkFluid(block, matcherFluidStack);
        } else if (matcherItem instanceof ItemBucket || matcherItem instanceof UniversalBucket) {
	    	matcherFluidStack = new FluidBucketWrapper(matcher).getFluid();
	    	return checkFluid(block, matcherFluidStack);
	    } else if (stack != null) {
        	Item stackItem = stack.getItem();
            return matcherItem == stackItem;
        } else {
        	Item blockItem = Item.getItemFromBlock(block);
            return matcherItem == blockItem;
        }
    }

	private boolean checkFluid(Block block, FluidStack matcherFluidStack) {
	    if (matcherFluidStack == null) {
    		return false;
	    }

	    Fluid matcherFluid = matcherFluidStack.getFluid();
    	if (matcherFluid == null) {
    		return false;
    	}
	    	
		Block matcherFluidBlock = matcherFluid.getBlock();
		if (matcherFluidBlock == null) {
			return false;
		}
		
		String matcherBlockName = matcherFluidBlock.getUnlocalizedName();
		String blockName = block.getUnlocalizedName();
		return blockName.equals(matcherBlockName);
	}

    private boolean checkGrowthLevel(BlockPos newpos, EnumFacing dir) {
        int blockCount = areaType.getBlockCount();
        for (int i = 0 ; i < blockCount ; i++) {
            boolean result = checkGrowthLevel(newpos);
{'fraction_non_alphanumeric': 0.08775276611980161, 'fraction_numerical': 0.001602441816100725, 'mean_word_length': 2.5278600269179003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3229137', 'n_tokens_mistral': 3857, 'n_tokens_neox': 3757, 'n_words': 824}