Jump to content

Recommended Posts

Posted

Hey guys,

 

So a quick question. I'm trying to check the block above my custom block to see if it's not an air block/material, I would like it to be compatible with mods that adds gasses and other stuff, that uses Material.Air. But for some reason it doesn't work.

This is what I'm trying.

@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
{
	IBlockState block_above = world.getBlockState(pos.up());
	if(!(block_above.getMaterial() == Material.AIR))
	{
		state = state.withProperty(BLOCK_ABOVE, true);
	}
	else
		state = state.withProperty(BLOCK_ABOVE, false);
	if(block_above.getProperties().containsValue(BlockSlab.EnumBlockHalf.TOP) || block_above.getProperties().containsValue(BlockStairs.EnumHalf.TOP))
	{
		state = state.withProperty(SLAB_STAIR_ABOVE, true);
	}
	else
		state = state.withProperty(SLAB_STAIR_ABOVE, false);
	return state;
}

So the first if statement is the one that doesn't work. The second one works fine.

 

Any idea what I'm doing wrong?

Posted
15 minutes ago, Jay Avery said:

What do you mean by "doesn't work"? What result do you get and what do you expect?

What I mean, is that no matter what's above my block, I get the same false result on my BLOCK_ABOVE property. What I expect is when there's any kind of block above it, the property should return true and not false, unless it's an air block/material.

Posted (edited)
1 hour ago, diesieben07 said:

Show where you try to access the property.

Might as well show you the whole class

Spoiler

My BlockClass


public class ChimneyBlock extends Block implements IMetaBlockName
{
	public static final PropertyBool SLAB_STAIR_UNDER = PropertyBool.create("slab_stair_under");
	public static final PropertyBool SLAB_STAIR_ABOVE = PropertyBool.create("slab_stair_above");
	public static final PropertyBool BLOCK_ABOVE = PropertyBool.create("block_above");
	public static final PropertyEnum TYPE = PropertyEnum.create("type", DecorationTypes.class);
	
	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);

	public ChimneyBlock()
	{
		super(Material.ROCK);
		setHardness(1.0F);
		setSoundType(SoundType.STONE);
		isToolEffective("pickaxe", getDefaultState());
		setCreativeTab(CreativeTabs.DECORATIONS);
		setDefaultState(blockState.getBaseState().withProperty(SLAB_STAIR_UNDER, Boolean.valueOf(false)).withProperty(SLAB_STAIR_ABOVE, Boolean.valueOf(false)).withProperty(BLOCK_ABOVE, Boolean.valueOf(false)).withProperty(TYPE, DecorationTypes.STONEBRICK));
	}
	
	@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)
	{
        return CHIMNEY_BLOCK_AABB;
	}
	
	@Override
	@Nullable
	public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos)
	{
        return CHIMNEY_BLOCK_AABB;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
	{
		if(this.getActualState(stateIn, worldIn, pos).getValue(BLOCK_ABOVE) == false)
			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)
	{
		IBlockState block_under = world.getBlockState(pos.down());
		IBlockState block_above = world.getBlockState(pos.up());
		if(block_under.getProperties().containsValue(BlockSlab.EnumBlockHalf.BOTTOM) || block_under.getProperties().containsValue(BlockStairs.EnumHalf.BOTTOM))
		{
			state = state.withProperty(SLAB_STAIR_UNDER, true);
		}
		else
			state = state.withProperty(SLAB_STAIR_UNDER, false);
		if(!(block_above.getMaterial() == Material.AIR))
		{
			state = state.withProperty(BLOCK_ABOVE, true);
		}
		else
			state = state.withProperty(BLOCK_ABOVE, false);
		if(block_above.getProperties().containsValue(BlockSlab.EnumBlockHalf.TOP) || block_above.getProperties().containsValue(BlockStairs.EnumHalf.TOP))
		{
			state = state.withProperty(SLAB_STAIR_ABOVE, true).withProperty(BLOCK_ABOVE, true);
		}
		else
			state = state.withProperty(SLAB_STAIR_ABOVE, false).withProperty(BLOCK_ABOVE, false);
		return state;
	}
	
	@Override
	public int damageDropped(IBlockState state)
    {
        return ((DecorationTypes)state.getValue(TYPE)).getMeta();
    }
	
	@Override
	public int getMetaFromState(IBlockState state)
	{
		DecorationTypes type = (DecorationTypes) state.getValue(TYPE);
        return type.getMeta();
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta)
	{
		return this.getDefaultState().withProperty(TYPE, DecorationTypes.values()[meta]);
	}
	
	@Override
	protected BlockStateContainer createBlockState()
	{
		return new BlockStateContainer(this, new IProperty[] { SLAB_STAIR_UNDER, SLAB_STAIR_ABOVE, BLOCK_ABOVE, TYPE });
	}
	
	@Override
	public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list)
	{
		for(int i = 0; i < DecorationTypes.values().length; i++)
		{
			list.add(new ItemStack(itemIn, 1, i));
		}
	}

	@Override
	public String getSpecialName(ItemStack stack)
	{
		return DecorationTypes.values()[stack.getItemDamage()].getName();
	}
}

 

 

I have been testing if the property BLOCK_ABOVE is working, by adding it to one of the other working if statements. Where it does work. So I can conclude that it has something to do with this if statement.

if(!(block_above.getMaterial() == Material.AIR))
{
	state = state.withProperty(BLOCK_ABOVE, true);
}
else
	state = state.withProperty(BLOCK_ABOVE, false);

 

I just can't see what's wrong with it, and as I mentioned in an earlier post, I was expecting this to return true when there's a block above and false when it's block using the material air, at least that's my end goal.

Edited by Erfurt
Posted (edited)
8 minutes ago, diesieben07 said:

Ok, first of all, your boolean logic and formatting is all over the place.

 

First of all, why on earth does the if block have braces but the else block not? Also, != is a thing. Or, much much cleaner:

state = state.withProperty(BLOCK_ABOVE, block_above.getMaterial != Material.AIR)

 

This happens multiple times all over the whole class.

 

And what is this?

 

Why not if (!this.getActualState(...)) ? Why does this if statement not have braces? That is very confusing here!

Mostly because I was writing this code while half asleep, but my code should and does still work, other than that one if statement.

 

EDIT: Maybe I need there to be an if statement, because I would need to put in some other code, when I had it working.

Edited by Erfurt

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.