Jump to content

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


winnetrie

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Try refreshing or rebuilding your project in Eclipse.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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

    • Hello All! Started a MC Eternal 1.6.2.2 server on Shockbyte hosting. The only other mod I added was betterfarmland v0.0.8BETA. Server is 16GB and Shockbyte wont tell me how many CPU cores i have.  We are having problems now when players log in it seems to crash the server. At other times it seems fine and we can have 3 people playing for hours at a time. Usually always when it does crash it is when someone logs in. Crash Reports Below. To the person who can post the fix I will reward $100 via Paypal.   ---- Minecraft Crash Report ---- // This is a token for 1 free hug. Redeem at your nearest Mojangsta: [~~HUG~~] Time: 2024-09-19 21:04:58 UTC Description: Exception in server tick loop java.lang.StackOverflowError     at net.minecraft.advancements.PlayerAdvancements.hasCompletedChildrenOrSelf(PlayerAdvancements.java:451)     at net.minecraft.advancements.PlayerAdvancements.shouldBeVisible(PlayerAdvancements.java:419)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:385)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:406)     at net.minecraft.advancements.PlayerAdvancements.ensureVisibility(PlayerAdvancements.java:411)     at net.minecraft.advancements.P  
    • DUTA89:Link Slot Gacor Hari ini Paling Mudah Menang dan viral se Indonesia. DUTA89 adalah Game slot gacor hari ini paling mudah menang dan viral se Indonesia serta resmi dan terbukti memberikan kegacoran kepada banyak orang. Link daftar : https://heylink.me/DUTA89OFFICIAL/ #slotduta89 #slotgacor #maxwin #slotterbaik #duta89 #duta89mantap      
    • DUTA89:Link Slot Gacor Hari ini Paling Mudah Menang dan viral se Indonesia. DUTA89 adalah Game slot gacor hari ini paling mudah menang dan viral se Indonesia serta resmi dan terbukti memberikan kegacoran kepada banyak orang. Link daftar : https://heylink.me/DUTA89OFFICIAL/ #slotduta89 #slotgacor #maxwin #slotterbaik #duta89 #duta89mantap      
    • DUTA89:Link Slot Gacor Hari ini Paling Mudah Menang dan viral se Indonesia. DUTA89 adalah Game slot gacor hari ini paling mudah menang dan viral se Indonesia serta resmi dan terbukti memberikan kegacoran kepada banyak orang. Link daftar : https://heylink.me/DUTA89OFFICIAL/ #slotduta89 #slotgacor #maxwin #slotterbaik #duta89 #duta89mantap      
    • DUTA89:Link Slot Gacor Hari ini Paling Mudah Menang dan viral se Indonesia. DUTA89 adalah Game slot gacor hari ini paling mudah menang dan viral se Indonesia serta resmi dan terbukti memberikan kegacoran kepada banyak orang. Link daftar : https://heylink.me/DUTA89OFFICIAL/ #slotduta89 #slotgacor #maxwin #slotterbaik #duta89 #duta89mantap      
  • Topics

×
×
  • Create New...

Important Information

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