Jump to content

[1.10] Check if block can be harvested and then harvest it without player object


SamTebbs33

Recommended Posts

I am writing some code that will harvest a number of blocks around it (all of which implement IGrowable) and to stop abuse and breaking the mechanics of other mods I will need to check if the block is harvest-able with canHarvestBlock(...) and then harvest it with harvestBlock(...) if it is. These methods require an EntityPlayer object, which isn't applicable since the code will be run from within a tile entity. I could pass null as the player value but I never like doing that due to the risk of running into an NPE. Is there some accepted or established method of checking if a block is harvest-able and then harvesting it without using an EntityPlayer object? I'm open to using libraries.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

Lookup HarvestDropsEvent docs.

Yes - that is even fired when harvestBlock(...) happens and yes - as per design - it expects null players.

 

Will I need to fire the HarvestDropsEvent in order to break the block and spawn the drops? I tried calling canHarvestBlock() with a null player object and as expected I got an NPE.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

I am writing some code that will harvest a number of blocks around it (all of which implement IGrowable) and to stop abuse and breaking the mechanics of other mods I will need to check if the block is harvest-able with canHarvestBlock(...) and then harvest it with harvestBlock(...) if it is. These methods require an EntityPlayer object, which isn't applicable since the code will be run from within a tile entity. I could pass null as the player value but I never like doing that due to the risk of running into an NPE. Is there some accepted or established method of checking if a block is harvest-able and then harvesting it without using an EntityPlayer object? I'm open to using libraries.

I have never seen anything that requires a tool to be used to break an IGrowable object, but you could create a fake player.

From BlockEvent.BreakEvent

/** Reference to the Player who broke the block. If no player is available, use a EntityFakePlayer */

Or even store the players UUID and Name when they place it, then check if they are on use the player if so, if not create one(recommend the first more).

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

harvestBlock(...) - ALLOWS null player because harvesting can occur without player (e.g explosion).

canHarvestBlock(...) - DOESN'T allow, because it is player-specific check. Why the hell would you even use this check if player is NOT harvesting given block? Just don't use it. If you really need some safety check - do it yourself, write helper method similar to canHarvestBlock. What is so difficult?

 

And no - you don't fire the event, I just mentioned it as a docs reference.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Block#canHarvestBlock

is used to check whether the player has the tool to break the block or fire a

PlayerEvent.HarvestCheck

if he doesn't have the required tool to check whether he can. other mods may have other implementations but that's the forge implementation in the Block class

Link to comment
Share on other sites

canHarvestBlock(...) - DOESN'T allow, because it is player-specific check. Why the hell would you even use this check if player is NOT harvesting given block? Just don't use it. If you really need some safety check - do it yourself, write helper method similar to canHarvestBlock. What is so difficult?

 

I just thought that canHarvestBlock() was the general (not player-specific) way of checking if a block is harvestable and thought I should be calling it in order to not harvest blocks that shouldn't be harvestable by a non-player entity/machine. Please don't get so hostile all of a sudden.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

I have never seen anything that requires a tool to be used to break an IGrowable object, but you could create a fake player.

 

Thanks! I'll go with this solution.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

I just thought that canHarvestBlock() was the general (not player-specific) way of checking if a block is harvestable

 

"Harvestable" requires a tool to be involved.

Tools can only be used by players.

Ergo a player object is needed.

 

Q.E.D.

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

Please don't get so hostile all of a sudden.

 

At this point I would be scared of giving you a flower - too hostile! :o

Like seriously - what was so hostile in that/my response? There is literally no hard language there.

 

Anyway - cheers to solution.

1.7.10 is no longer supported by forge, you are on your own.

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.



×
×
  • Create New...

Important Information

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