Jump to content

Recommended Posts

Posted

Hey guys,

 

I'm working on some hedges, and I have noticed that mobs try to jump over them, even when the hedges is 1½ block high, just as fences and walls are. I have looked at the blockWall class, but that didn't really help me much, as I can't see a hole lot of difference between my hedge code and the code in blockWall. Maybe it's a method I'm missing(have tried the isPassable method didn't help with my problem :) ), or maybe I have to add my hedge class/blocks to a barrier variable somewhere. Any help would be appreciated, as I'm pretty much stuck with this.

 

Here's my code

 

 

Hedge Class

public class Hedge extends Block
{
public static final PropertyBool NORTH = PropertyBool.create("north");
public static final PropertyBool EAST = PropertyBool.create("east");
public static final PropertyBool SOUTH = PropertyBool.create("south");
public static final PropertyBool WEST = PropertyBool.create("west");

protected static final AxisAlignedBB[] BOUNDING_BOX = new AxisAlignedBB[] { new AxisAlignedBB(0.1875, 0.0, 0.1875, 0.8125, 1.0, 0.8125), new AxisAlignedBB(0.1875, 0.0, 0.1875, 0.8125, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.1875, 0.8125, 1.0, 0.8125), new AxisAlignedBB(0.0, 0.0, 0.1875, 0.8125, 1.0, 1.0), new AxisAlignedBB(0.1875, 0.0, 0.0, 0.8125, 1.0, 0.8125), new AxisAlignedBB(0.1875, 0.0, 0.0, 0.8125, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.0, 0.8125, 1.0, 0.8125), new AxisAlignedBB(0.0, 0.0, 0.0, 0.8125, 1.0, 1.0), new AxisAlignedBB(0.1875, 0.0, 0.1875, 1.0, 1.0, 0.8125), new AxisAlignedBB(0.1875, 0.0, 0.1875, 1.0, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.1875, 1.0, 1.0, 0.8125), new AxisAlignedBB(0.0, 0.0, 0.1875, 1.0, 1.0, 1.0), new AxisAlignedBB(0.1875, 0.0, 0.0, 1.0, 1.0, 0.8125), new AxisAlignedBB(0.1875, 0.0, 0.0, 1.0, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.0, 1.0, 1.0, 0.8125), new AxisAlignedBB(0.0, 0.0, 0.0, 1.0, 1.0, 1.0) };
    protected static final AxisAlignedBB[] CLIP_BOUNDING_BOX = new AxisAlignedBB[] {BOUNDING_BOX[0].setMaxY(1.5D), BOUNDING_BOX[1].setMaxY(1.5D), BOUNDING_BOX[2].setMaxY(1.5D), BOUNDING_BOX[3].setMaxY(1.5D), BOUNDING_BOX[4].setMaxY(1.5D), BOUNDING_BOX[5].setMaxY(1.5D), BOUNDING_BOX[6].setMaxY(1.5D), BOUNDING_BOX[7].setMaxY(1.5D), BOUNDING_BOX[8].setMaxY(1.5D), BOUNDING_BOX[9].setMaxY(1.5D), BOUNDING_BOX[10].setMaxY(1.5D), BOUNDING_BOX[11].setMaxY(1.5D), BOUNDING_BOX[12].setMaxY(1.5D), BOUNDING_BOX[13].setMaxY(1.5D), BOUNDING_BOX[14].setMaxY(1.5D), BOUNDING_BOX[15].setMaxY(1.5D)};
    
private static final AxisAlignedBB COLLISION_BOX_CENTER = new AxisAlignedBB(0.1875, 0.0, 0.1875, 0.8125, 1.5, 0.8125);
private static final AxisAlignedBB COLLISION_BOX_NORTH = CollisionHelper.getBlockBounds(EnumFacing.NORTH, 0.8125, 0.0, 0.1875, 1.0, 1.5, 0.8125);
private static final AxisAlignedBB COLLISION_BOX_EAST = CollisionHelper.getBlockBounds(EnumFacing.EAST, 0.8125, 0.0, 0.1875, 1.0, 1.5, 0.8125);
private static final AxisAlignedBB COLLISION_BOX_SOUTH = CollisionHelper.getBlockBounds(EnumFacing.SOUTH, 0.8125, 0.0, 0.1875, 1.0, 1.5, 0.8125);
private static final AxisAlignedBB COLLISION_BOX_WEST = CollisionHelper.getBlockBounds(EnumFacing.WEST, 0.8125, 0.0, 0.1875, 1.0, 1.5, 0.8125);

public Hedge(String name)
{
	super(Material.LEAVES);
	setUnlocalizedName(name);
	setRegistryName(name);
	setSoundType(SoundType.PLANT);
	setHardness(1.0F);
	setDefaultState(this.blockState.getBaseState().withProperty(NORTH, Boolean.valueOf(false)).withProperty(EAST, Boolean.valueOf(false)).withProperty(SOUTH, Boolean.valueOf(false)).withProperty(WEST, Boolean.valueOf(false)));
	Blocks.FIRE.setFireInfo(this, 30, 60);
	setCreativeTab(CreativeTabs.DECORATIONS);
}

@Override
public boolean isOpaqueCube(IBlockState state)
    {
	return false;
    }

@Override
public boolean isNormalCube(IBlockState state)
{
	return false;
}

@Override
public boolean isFullCube(IBlockState state)
{
	return false;
}

public BlockRenderLayer getBlockLayer()
{
	return Blocks.LEAVES.getBlockLayer();
}
    
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) 
{
	state = this.getActualState(state, source, pos);
	return BOUNDING_BOX[getBoundingBoxId(state)];
}

@Nullable
    public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, World worldIn, BlockPos pos)
    {
        blockState = this.getActualState(blockState, worldIn, pos);
        return CLIP_BOUNDING_BOX[getBoundingBoxId(blockState)];
    }

private static int getBoundingBoxId(IBlockState state)
    {
        int i = 0;

        if (((Boolean)state.getValue(NORTH)).booleanValue())
        {
            i |= 1 << EnumFacing.NORTH.getHorizontalIndex();
        }

        if (((Boolean)state.getValue(EAST)).booleanValue())
        {
            i |= 1 << EnumFacing.EAST.getHorizontalIndex();
        }

        if (((Boolean)state.getValue(SOUTH)).booleanValue())
        {
            i |= 1 << EnumFacing.SOUTH.getHorizontalIndex();
        }

        if (((Boolean)state.getValue(WEST)).booleanValue())
        {
            i |= 1 << EnumFacing.WEST.getHorizontalIndex();
        }

        return i;
    }

@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB axisAligned, List<AxisAlignedBB> axisAlignedList, Entity collidingEntity) 
{
	if (state.getValue(NORTH))
        {
            super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_NORTH);
        }

        if (state.getValue(EAST))
        {
        	super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_EAST);
        }

        if (state.getValue(SOUTH))
        {
        	super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_SOUTH);
        }

        if (state.getValue(WEST))
        {
        	super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_WEST);
        }
        
        super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_CENTER);
}

@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
{
	return state.withProperty(NORTH, Boolean.valueOf(isHedge(world, pos.north()))).withProperty(EAST, Boolean.valueOf(isHedge(world, pos.east()))).withProperty(SOUTH, Boolean.valueOf(isHedge(world, pos.south()))).withProperty(WEST, Boolean.valueOf(isHedge(world, pos.west())));
}

@Override
public int getMetaFromState(IBlockState state)
{
	return 0;
}

@Override
protected BlockStateContainer createBlockState()
{
	return new BlockStateContainer(this, new IProperty[] { NORTH, EAST, SOUTH, WEST });
}

public boolean isHedge(IBlockAccess world, BlockPos pos)
{
	return world.getBlockState(pos).getBlock() instanceof Hedge || world.getBlockState(pos).getBlock() instanceof BlockFenceGate || world.getBlockState(pos).getBlock().isNormalCube(world.getBlockState(pos));
}
}

CollisionHelper Class (not sure if you guys need to see this or not)

public class CollisionHelper
{
public static AxisAlignedBB getBlockBounds(EnumFacing facing, double x1, double y1, double z1, double x2, double y2, double z2)
{
	double[] bounds = fixRotation(facing, x1, z1, x2, z2);
	return new AxisAlignedBB(bounds[0], y1, bounds[1], bounds[2], y2, bounds[3]);
}

private static double[] fixRotation(EnumFacing facing, double var1, double var2, double var3, double var4)
{
	switch (facing)
	{
	case WEST:
		double var_temp_1 = var1;
		var1 = 1.0F - var3;
		double var_temp_2 = var2;
		var2 = 1.0F - var4;
		var3 = 1.0F - var_temp_1;
		var4 = 1.0F - var_temp_2;
		break;
	case NORTH:
		double var_temp_3 = var1;
		var1 = var2;
		var2 = 1.0F - var3;
		var3 = var4;
		var4 = 1.0F - var_temp_3;
		break;
	case SOUTH:
		double var_temp_4 = var1;
		var1 = 1.0F - var4;
		double var_temp_5 = var2;
		var2 = var_temp_4;
		double var_temp_6 = var3;
		var3 = 1.0F - var_temp_5;
		var4 = var_temp_6;
		break;
	default:
		break;
	}
	return new double[] { var1, var2, var3, var4 };
}
}

 

 

Posted

Ok, taking a look at the pathfinding code (which decompiles horribly by the way, see below...) the only way to trigger PathNodeType.FENCE is extending

BlockFence

,

BlockWall

or

BlockFenceGate

.

Extending them is not always viable, so I suggest you submit a pull request to forge to allow a block to specify it's own

PathNodeType

.

 

And now for some code horror:

 

Before

[spoiler=After]JWke5pO.png

 

 

What a mess :P

 

I'll make that suggestion, in the mean time I have been able to extend BlockFence to make my custom Hedges, but as you said that isn't always viable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.