Jump to content

[1.11.2] Checking block above


Erfurt

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I'm using Modrinth as a launcher for a forge modpack on 1.20.1, and can't diagnose the issue on the crash log myself. Have tried repairing the Minecraft instillation as well as removing a few mods that have been problematic for me in the past to no avail. Crash log is below, if any further information is necessary let me know. Thank you! https://paste.ee/p/k6xnS
    • Hey folks. I am working on a custom "Mecha" entity (extended from LivingEntity) that the player builds up from blocks that should get modular stats depending on the used blocks. e.g. depending on what will be used for the legs, the entity will have a different jump strength. However, something unexpected is happening when trying to override a few of LivingEntity's functions and using my new own "Mecha" specific fields: instead of their actual instance-specific value, the default value is used (0f for a float, null for an object...) This is especially strange as when executing with the same entity from a point in the code specific to the mecha entity, the correct value is used. Here are some code snippets to better illustrate what I mean: /* The main Mecha class, cut down for brevity */ public class Mecha extends LivingEntity { protected float jumpMultiplier; //somewhere later during the code when spawning the entity, jumpMultiplier is set to something like 1.5f //changing the access to public didn't help @Override //Overridden from LivingEntity, this function is only used in the jumpFromGround() function, used in the aiStep() function, used in the LivingEntity tick() function protected float getJumpPower() { //something is wrong with this function //for some reason I can't correctly access the fields and methods from the instanciated entity when I am in one of those overridden protected functions. this is very annoying LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 0f return this.jumpMultiplier * super.getJumpPower(); } //The code above does not operate properly. Written as is, the entity will not jump, and adding debug logs shows that when executing the code, the value of this.jumpMultiplier is 0f //in contrast, it will be the correct value when done here: @Override public void tick() { super.tick(); //inherited LivingEntity logic //Custom logic LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 1.5f } } My actual code is slightly different, as the jumpMuliplier is stored in another object (so I am calling "this.legModule.getJumpPower()" instead of the float), but even using a simple float exactly like in the code above didn't help. When running my usual code, the object I try to use is found to be null instead, leading to a crash from a nullPointerException. Here is the stacktrace of said crash: The full code can be viewed here. I have found a workaround in the case of jump strength, but have already found the same problem for another parameter I want to do, and I do not understand why the code is behaving as such, and I would very much like to be able to override those methods as intended - they seemed to work just fine like that for vanilla mobs... Any clues as to what may be happening here?
    • Please delete post. Had not noticed the newest edition for 1.20.6 which resolves the issue.
    • https://paste.ee/p/GTgAV Here's my debug log, I'm on 1.18.2 with forge 40.2.4 and I just want to get it to work!! I cant find any mod names in the error part and I would like some help from the pros!! I have 203 mods at the moment.
  • Topics

×
×
  • Create New...

Important Information

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