<commit_before>package net.teamio.taam.content.piping;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.teamio.taam.content.BaseBlock;
import net.teamio.taam.content.MaterialMachinesTransparent;

public class BlockPipe extends BaseBlock implements ITileEntityProvider {

	private static final float pipeWidth = 0.25f;
	private static final float fromBorder = (1f-pipeWidth) / 2;
	
	private static AxisAlignedBB bbCenter = new AxisAlignedBB(fromBorder, fromBorder, fromBorder, 1-fromBorder, 1-fromBorder, 1-fromBorder);
	private static final AxisAlignedBB[] bbFaces = new AxisAlignedBB[6];
	
	static {
		System.out.println(fromBorder);
		bbFaces[EnumFacing.EAST.ordinal()]	= new AxisAlignedBB(1-fromBorder,	fromBorder,		fromBorder,
																1,				1-fromBorder,	1-fromBorder);
		bbFaces[EnumFacing.WEST.ordinal()]	= new AxisAlignedBB(0,				fromBorder,		fromBorder,
																fromBorder,		1-fromBorder,	1-fromBorder);
		bbFaces[EnumFacing.SOUTH.ordinal()]	= new AxisAlignedBB(fromBorder,		fromBorder,		1-fromBorder,
																1-fromBorder,	1-fromBorder,	1);
		bbFaces[EnumFacing.NORTH.ordinal()]	= new AxisAlignedBB(fromBorder,		fromBorder,		0,
																1-fromBorder,	1-fromBorder,	fromBorder);
		bbFaces[EnumFacing.UP.ordinal()]	= new AxisAlignedBB(fromBorder,		1-fromBorder,	fromBorder,
																1-fromBorder,	1,				1-fromBorder);
		bbFaces[EnumFacing.DOWN.ordinal()]	= new AxisAlignedBB(fromBorder,		0,				fromBorder,
																1-fromBorder,	fromBorder,		1-fromBorder);
	}
	
	public BlockPipe() {
		super(MaterialMachinesTransparent.INSTANCE);
		this.fullBlock = false;
	}

	@Override
	public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state) {
		return true;
	}

	private AxisAlignedBB closestBB;
	
	@Override
	public MovingObjectPosition collisionRayTrace(World world, BlockPos pos, Vec3 start, Vec3 end) {
		start = start.subtract(pos.getX(), pos.getY(), pos.getZ());
		end = end.subtract(pos.getX(), pos.getY(), pos.getZ());

		List<AxisAlignedBB> boxes = getBoxes(world, pos, world.getBlockState(pos));

		MovingObjectPosition closestHit = null;
		double dist = Double.MAX_VALUE;
		closestBB = null;
		
		for (AxisAlignedBB box : boxes) {

			MovingObjectPosition newHit = box.calculateIntercept(start, end);
			if (newHit != null) {
				double newDist = newHit.hitVec.distanceTo(start);
				if (newDist < dist) {
					closestHit = newHit;
					dist = newDist;
					closestBB = box;
				}
			}
		}
		if (closestHit == null) {
			return null;
		} else {
			return new MovingObjectPosition(closestHit.hitVec, closestHit.sideHit, pos);
		}
	}
 
	/**
	 * Get all boxes in block-space coordinates
	 * 
	 * @param world
	 * @param pos
	 * @param state
	 * @return
	 */
	public List<AxisAlignedBB> getBoxes(IBlockAccess world, BlockPos pos, IBlockState state) {
		List<AxisAlignedBB> collisionBoxes = new ArrayList<AxisAlignedBB>(7);

		TileEntity te = world.getTileEntity(pos);

		// Fallback
		if (!(te instanceof TileEntityPipe)) {
			collisionBoxes.add(bbCenter);
			return collisionBoxes;
		}

		// Check pipe connections
		TileEntityPipe pipe = (TileEntityPipe) te;

		collisionBoxes.add(bbCenter);

		for (EnumFacing side : EnumFacing.VALUES) {
			if (pipe.isSideConnected(side)) {
				collisionBoxes.add(bbFaces[side.ordinal()]);
			}
		}

		return collisionBoxes;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public AxisAlignedBB getSelectedBoundingBox(World world, BlockPos pos) {
		// Get player position + look vector
		EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
		Vec3 eyes = player.getPositionEyes(0);
		Vec3 look = player.getLook(0);
		float reach = Minecraft.getMinecraft().playerController.getBlockReachDistance();
		Vec3 dest = eyes.addVector(look.xCoord * reach, look.yCoord * reach, look.zCoord * reach);
		
		// in that method, we update the closestBB
		collisionRayTrace(world, pos, eyes, dest);
		
		// Return the box that is hovered, or the default if nothing could be determined (edge cases)
		if(closestBB == null) {
			return bbCenter.offset(pos.getX(), pos.getY(), pos.getZ());
		} else {
			return closestBB.offset(pos.getX(), pos.getY(), pos.getZ());
		}
	}

	@Override
	public void addCollisionBoxesToList(World world, BlockPos pos, IBlockState state, AxisAlignedBB mask,
			List<AxisAlignedBB> list, Entity par7Entity) {
		if (!isCollidable()) {
			return;
		} else {
			for (AxisAlignedBB bb : getBoxes(world, pos, state)) {
				bb = bb.offset(pos.getX(), pos.getY(), pos.getZ());
				if (mask.intersectsWith(bb)) {
					list.add(bb);
				}
			}
		}<commit_msg>Made Pipes placeable if player is halfway into the block<commit_after>package net.teamio.taam.content.piping;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.teamio.taam.content.BaseBlock;
import net.teamio.taam.content.MaterialMachinesTransparent;

public class BlockPipe extends BaseBlock implements ITileEntityProvider {

	private static final float pipeWidth = 0.25f;
	private static final float fromBorder = (1f-pipeWidth) / 2;
	
	private static AxisAlignedBB bbCenter = new AxisAlignedBB(fromBorder, fromBorder, fromBorder, 1-fromBorder, 1-fromBorder, 1-fromBorder);
	private static final AxisAlignedBB[] bbFaces = new AxisAlignedBB[6];
	private AxisAlignedBB closestBB;
	
	static {
		System.out.println(fromBorder);
		bbFaces[EnumFacing.EAST.ordinal()]	= new AxisAlignedBB(1-fromBorder,	fromBorder,		fromBorder,
																1,				1-fromBorder,	1-fromBorder);
		bbFaces[EnumFacing.WEST.ordinal()]	= new AxisAlignedBB(0,				fromBorder,		fromBorder,
																fromBorder,		1-fromBorder,	1-fromBorder);
		bbFaces[EnumFacing.SOUTH.ordinal()]	= new AxisAlignedBB(fromBorder,		fromBorder,		1-fromBorder,
																1-fromBorder,	1-fromBorder,	1);
		bbFaces[EnumFacing.NORTH.ordinal()]	= new AxisAlignedBB(fromBorder,		fromBorder,		0,
																1-fromBorder,	1-fromBorder,	fromBorder);
		bbFaces[EnumFacing.UP.ordinal()]	= new AxisAlignedBB(fromBorder,		1-fromBorder,	fromBorder,
																1-fromBorder,	1,				1-fromBorder);
		bbFaces[EnumFacing.DOWN.ordinal()]	= new AxisAlignedBB(fromBorder,		0,				fromBorder,
																1-fromBorder,	fromBorder,		1-fromBorder);
	}
	
	public BlockPipe() {
		super(MaterialMachinesTransparent.INSTANCE);
		this.fullBlock = false;
	}

	@Override
	public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state) {
		return true;
	}

	
	@Override
	public MovingObjectPosition collisionRayTrace(World world, BlockPos pos, Vec3 start, Vec3 end) {
		start = start.subtract(pos.getX(), pos.getY(), pos.getZ());
		end = end.subtract(pos.getX(), pos.getY(), pos.getZ());

		List<AxisAlignedBB> boxes = getBoxes(world, pos, world.getBlockState(pos));

		MovingObjectPosition closestHit = null;
		double dist = Double.MAX_VALUE;
		closestBB = null;
		
		for (AxisAlignedBB box : boxes) {

			MovingObjectPosition newHit = box.calculateIntercept(start, end);
			if (newHit != null) {
				double newDist = newHit.hitVec.distanceTo(start);
				if (newDist < dist) {
					closestHit = newHit;
					dist = newDist;
					closestBB = box;
				}
			}
		}
		if (closestHit == null) {
			return null;
		} else {
			return new MovingObjectPosition(closestHit.hitVec, closestHit.sideHit, pos);
		}
	}
 
	/**
	 * Get all boxes in block-space coordinates
	 * 
	 * @param world
	 * @param pos
	 * @param state
	 * @return
	 */
	public List<AxisAlignedBB> getBoxes(IBlockAccess world, BlockPos pos, IBlockState state) {
		List<AxisAlignedBB> collisionBoxes = new ArrayList<AxisAlignedBB>(7);

		TileEntity te = world.getTileEntity(pos);

		// Fallback
		if (!(te instanceof TileEntityPipe)) {
			collisionBoxes.add(bbCenter);
			return collisionBoxes;
		}

		// Check pipe connections
		TileEntityPipe pipe = (TileEntityPipe) te;

		collisionBoxes.add(bbCenter);

		for (EnumFacing side : EnumFacing.VALUES) {
			if (pipe.isSideConnected(side)) {
				collisionBoxes.add(bbFaces[side.ordinal()]);
			}
		}

		return collisionBoxes;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public AxisAlignedBB getSelectedBoundingBox(World world, BlockPos pos) {
		// Get player position + look vector
		EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
		Vec3 eyes = player.getPositionEyes(0);
		Vec3 look = player.getLook(0);
		float reach = Minecraft.getMinecraft().playerController.getBlockReachDistance();
		Vec3 dest = eyes.addVector(look.xCoord * reach, look.yCoord * reach, look.zCoord * reach);
		
		// in that method, we update the closestBB
		collisionRayTrace(world, pos, eyes, dest);
		
		// Return the box that is hovered, or the default if nothing could be determined (edge cases)
		if(closestBB == null) {
			return bbCenter.offset(pos.getX(), pos.getY(), pos.getZ());
		} else {
			return closestBB.offset(pos.getX(), pos.getY(), pos.getZ());
		}
	}
	
	@Override
	public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state) {
		// For this block, this method is only used when placing a new block in the world.
		// Return only the center to allow placig if the player is half way into the block
		return bbCenter.offset(pos.getX(), pos.getY(), pos.getZ());
	}

	@Override
	public void addCollisionBoxesToList(World world, BlockPos pos, IBlockState state, AxisAlignedBB mask,
			List<AxisAlignedBB> list, Entity par7Entity) {
		if (!isCollidable()) {
			return;
		} else {
			for (AxisAlignedBB bb : getBoxes(world, pos, state)) {
				bb = bb.offset(pos.getX(), pos.getY(), pos.getZ());
				if (mask.intersectsWith(bb)) {
					list.add(bb);
				}
			}
		}
{'fraction_non_alphanumeric': 0.11365108745950948, 'fraction_numerical': 0.007403979639055993, 'mean_word_length': 4.15307582260372, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '5320182', 'n_tokens_mistral': 4155, 'n_tokens_neox': 3659, 'n_words': 792}