Jump to content

MC 1.10+ How to set different hardness for different block type


Recommended Posts

Posted

I have a block called BlockFeliron.

It contains 2 types : ORE and BLOCK

i want a different hardness, resistance, soundtype for those both.

How to i do this?

i tried this:

 

 

public class BlockFeliron extends Block implements IMetaBlockName{

public static final PropertyEnum TYPE = PropertyEnum.create("type", BlockFeliron.EnumType.class);
public BlockFeliron() {
	super(Material.ROCK);
        
        this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, EnumType.ORE));
        if (this.getDefaultState() == this.getDefaultState().withProperty(TYPE, EnumType.ORE)){
        	setHardness(7.0F);
        	setResistance(10.0F);
        	setSoundType(SoundType.STONE);
        }
        if (this.getDefaultState() == this.getDefaultState().withProperty(TYPE, EnumType.BLOCK)){
        	setHardness(15.0F);
        	setResistance(20.0F);
        	setSoundType(SoundType.METAL);
        }
        setUnlocalizedName(References.temBlocks.FELIRON.getUnlocalizedName());
	setRegistryName(References.temBlocks.FELIRON.getRegistryName());
	setCreativeTab(Tem.blockstab);
        
}

 

It always takes the first settings, perhaps because it is the defaultstate

Posted

A

Block

can only have one default state. Any setter methods in

Block

will set that value to be used for every state of the block.

 

Instead of calling the setter methods in the constructor, you need to override the getter methods to return the appropriate value based on the state they receive as an argument.

 

The methods you need to override are

Block#getBlockHardness

,

Block#getExplosionResistance(World, BlockPos, Entity, Explosion)

and

Block#getSoundType(IBlockState, World, BlockPos, Entity)

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I have succeeded in applying the hardnes and resistance but not the soundtype:

 

@Override
public float getBlockHardness(IBlockState state, World worldIn, BlockPos pos){
	float hardness=0F;
	if(state == state.withProperty(TYPE, EnumType.ORE)){
		hardness=7.5F;
	}
	if(state == state.withProperty(TYPE, EnumType.BLOCK)){
		hardness=15.0F;
	}
	return hardness;
}
@Override
public float getExplosionResistance(World world, BlockPos pos, Entity entity, Explosion explosion){
	float resistance=0F;
	IBlockState blockstate = world.getBlockState(pos);
	if (blockstate== blockstate.withProperty(TYPE, EnumType.ORE)){
		resistance = 10.0F;
	}
	if (blockstate== blockstate.withProperty(TYPE, EnumType.BLOCK)){
		resistance = 20.0F;
	}
	return resistance;
}
@Override
public SoundType getSoundType(){
	SoundType sound=null;
	IBlockState blockstate = (IBlockState) this.getBlockState();
	if (blockstate == blockstate.withProperty(TYPE, EnumType.ORE)){
		sound = SoundType.GROUND;
	}
	if (blockstate == blockstate.withProperty(TYPE, EnumType.BLOCK)){
		sound = SoundType.STONE;
	}

	return sound ;

}

 

doing this getSoundType(IBlockState state, World world, BlockPos pos, Entity entity)

gives me an error:

The method getSoundType(IBlockState, World, BlockPos, Entity) of type BlockFeliron must override or implement a supertype method

 

in the Block class from minecraft i find it like this getSoundType(), so i did this instead

Posted

If you get a blockstate from the world, check that it is still your block before you attempt to get your properties from it. Vanilla blocks don't have variable hardness etc, so Mojang doesn't necessarily make those methods safe for modders who do (and there have indeed been times past when deleting such a block could crash the game because getHardness was called on a block after it had been turned into air in the world).

 

You will need to decide what your methods should return when they detect a blockstate mismatches.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted
Vanilla blocks don't have variable hardness etc, so Mojang doesn't necessarily make those methods safe for modders who do

 

Something something, this is why I have to try-catch a chunk of my code, because I specifically want to know what the hardness of a block (and its harvest tool/level) is without having a valid world position to look at (so I pass null for the world and catch the null pointer exception).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

The

Block#getSoundType

overload I mentioned was added in Forge 1.10.2-12.18.1.2031. Update Forge to the latest or recommend version.

 

Use

IBlockState#getValue

to get the value of a property.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.