Jump to content

[1.12] Is there a way to get the hardness of a block in a non-deprecated way?


ctbe

Recommended Posts

I do it like this and it works:

 

Blocks.BRICK_BLOCK.getBlockState().getBaseState().getBlockHardness(null, null);

 

Because Blocks.BRICK_BLOCK.getBlockHardness(...) was marked as deprecated.

 

Yet, I noticed that getBlockHardness(null, null) is not using the parameters for anything. It is simply calling another function that in the end, calls the deprecated getBlockHardness function and said function just returns the value of the variable blockHardness of the block in question. It does nothing with the parameters. To be more specific it calls the getBlockHardness of BlockStateContainer which in turn just calls the deprecated getBlockHardness of the Block class which in turn does nothing with the parameters it gets passed. That's why it works with null being passed as parameters.

 

I was thinking that maybe using getBlockState().getBaseState().withProperty(...) could help, but after a while looking through the code, I could find no such a property for hardness. It is all hardcoded in the block registering method registerBlocks() of the Block class.

 

Is there an alternative to using this method that all it does is simply call a deprecated method or is this the only way?

Link to comment
Share on other sites

You should be getting the hardness from the IBlockState. If you don't have one, use Block#getDefaultState()

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

Thanks. I changed it to use the IBlockState returned from the getDefaultState() method.

 

Though it still goes through the same path. It calls the deprecated method from the non deprecated one in the BlockStateContainer class. Unless I am missing something else.

 

After looking at the classes implementations it feels like there is no other way to achieve it since I see no other method that returns the value of the protected blockHardness other than the deprecated one which is used by the non-deprecated method.

Link to comment
Share on other sites

Yes, but all of the IBlockState methods call the deprecated Block methods. But that doesn't mean you shouldn't be using them.

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

If you're dealing with a block type that has variants, then (in theory), each variant could have a different hardness. Therefore, if you want the hardness for a block in the world, you should use pos to fetch the block-state from the world (and if you want the hardness for a block that has already been broken from the world, then you need the prior blockstate from before it was broken).

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

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.