Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hey guys,

 

I have some trouble checking the block below my custom block, I'm checking to see if the block is a stair/slab block or something else. If the block below is a stair/slab, I need it to add a true boolean property to my custom block, and false for everything else.

 

I have tried the code below, and that worked for checking if the block below was a stair, but not if it was a slab. Any help would be very much appreciated.

 

 

 

@Override
public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
IBlockState state = super.onBlockPlaced(world, pos, facing, hitX, hitY, hitZ, meta, placer);
Block block_under = world.getBlockState(pos.down()).getBlock();
if(block_under instanceof BlockSlab) //I have also tried to use BlockHalfWoodSlab, and the other variants of BlockSlab, still didn't work
{
	state = state.withProperty(SLAB_STAIR, true);
}
if(block_under instanceof BlockStairs)
{
	state = state.withProperty(SLAB_STAIR, true);
}
else
	state = state.withProperty(SLAB_STAIR, false);
return state;
}

 

 

 

Also as a little side question, is it in anyway possible to check and see if the stair/slab is the bottom variant, if so, how would I go about doing that?

  • Author

This is something that should be done in getActualState, take a look at BlockFence for an example.

 

Cool thanks. However this still doesn't make the BlockSlab work. Any idea why, or how I can make it see slabs below?

New updated code

 

 

public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
{
Block block_under = world.getBlockState(pos.down()).getBlock();
if(block_under instanceof BlockSlab)
{
	state = state.withProperty(SLAB_STAIR, true);
}
if(block_under instanceof BlockStairs)
{
	state = state.withProperty(SLAB_STAIR, true);
}
else
	state = state.withProperty(SLAB_STAIR, false);
        return state;
}

 

 

  • Author

Would need to see more of your code and a more detailed explanation of what's not working.

 

Whole class

 

 

public class ChimneyBlock extends Block
{
public static final PropertyBool SLAB_STAIR = PropertyBool.create("slab_stair");

protected static final double pixel = 1/16D;
protected static final AxisAlignedBB CHIMNEY_BLOCK_AABB = new AxisAlignedBB(2*pixel, 0.0D, 2*pixel, 14*pixel, 1.0D, 14*pixel);
protected static final AxisAlignedBB CHIMNEY_BLOCK_AABB2 = new AxisAlignedBB(2*pixel, -0.5D, 2*pixel, 14*pixel, 1.0D, 14*pixel);

public ChimneyBlock(String name)
{
	super(Material.ROCK);
	this.setUnlocalizedName(name);
	this.setRegistryName(name);
	this.setCreativeTab(CreativeTabs.DECORATIONS);
	this.setDefaultState(this.blockState.getBaseState().withProperty(SLAB_STAIR, Boolean.valueOf(false)));
}

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

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

@Override
@SideOnly(Side.CLIENT)
    public BlockRenderLayer getBlockLayer()
{
        return BlockRenderLayer.CUTOUT;
}

@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
	if ((Boolean)state.getValue(SLAB_STAIR).booleanValue() == true)
        {
		return CHIMNEY_BLOCK_AABB2;
        }
	else
        	return CHIMNEY_BLOCK_AABB;
}

@Override
@Nullable
    public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, World worldIn, BlockPos pos)
{
        return CHIMNEY_BLOCK_AABB;
}

@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
{
	for (int i = 0; i < 3; ++i)
	{
		double d0 = (double)pos.getX() + rand.nextDouble();
		double d1 = (double)pos.getY() + rand.nextDouble() * 0.5D + 1.0D;
		double d2 = (double)pos.getZ() + rand.nextDouble();
		worldIn.spawnParticle(EnumParticleTypes.SMOKE_LARGE, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
	}
}

@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
{
	Block block_under = world.getBlockState(pos.down()).getBlock();
	if(block_under instanceof BlockSlab)
	{
		state = state.withProperty(SLAB_STAIR, true);
	}
	if(block_under instanceof BlockStairs)
	{
		state = state.withProperty(SLAB_STAIR, true);
	}
	else
		state = state.withProperty(SLAB_STAIR, false);
        return state;
    }

@Override
public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
	IBlockState state = super.onBlockPlaced(world, pos, facing, hitX, hitY, hitZ, meta, placer);
	return state;
}

@Override
public int getMetaFromState(IBlockState state)
{
	int i = 0;
        
        if (((Boolean)state.getValue(SLAB_STAIR)).booleanValue())
        {
            i |= 8;
        }
        return i;
}

@Override
public IBlockState getStateFromMeta(int meta)
{
	return getDefaultState().withProperty(SLAB_STAIR, Boolean.valueOf((meta) > 0));
}

@Override
protected BlockStateContainer createBlockState()
{
	return new BlockStateContainer(this, new IProperty[] {SLAB_STAIR});
}
}

 

 

 

What's not working is, that when I have a slab below my custom block, is doesn't set it's property to true, as the stair does. And I really don't know why.

  • Author

Regarding my little side question, I was wondering if anyone here knows if this is possible, and maybe how to do it?

Also as a little side question, is it in anyway possible to check and see if the stair/slab is the bottom variant, if so, how would I go about doing that?

  • Author

if(block_under instanceof BlockSlab)

{

state = state.withProperty(SLAB_STAIR, true);

}

if(block_under instanceof BlockStairs)

{

state = state.withProperty(SLAB_STAIR, true);

}

else

state = state.withProperty(SLAB_STAIR, false);

        return state;

    }

Ok, let's walk through this assuming there is a slab below.

[*]

block_under

is a

BlockSlab

so execute the if statement: set

state

to

state.withProperty(SLAB_STAIR, true)

.

[*]

block_under

is not a

BlockStair

, so execute the else branch: set

state

to

state.withProperty(SLAB_STAIR, false)

.

End result:

SLAB_STAIR

is set to

false

.

 

I see now... I should use else if on the second if statement, or put both in the same one, right?

Also as a little side question, is it in anyway possible to check and see if the stair/slab is the bottom variant, if so, how would I go about doing that?
BlockSlab

and

BlockStairs

has a property

HALF

, which you can check.

 

I think I understand this, but how would I get the property from block_under? I mean is there a "simple" way of getting it?

 

I know I'm asking a lot here, and I want to thank you for your patience and help :)

  • Author

I see now... I should use else if on the second if statement, or put both in the same one, right?
else-if is the correct solution, yes.

 

I think I understand this, but how would I get the property from block_under? I mean is there a "simple" way of getting it?
You need to keep the
IBlockState

, not the

Block

(i.e. don't immediately call

getBlock

). Then you can access all

IBlockState

methods, like

getValue

.

 

I see, thank you

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.