Jump to content

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


Recommended Posts

Posted

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?

Posted

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.

Posted

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.

Posted

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.

Posted

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.

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

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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