SamTebbs33 Posted August 21, 2016 Posted August 21, 2016 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. Quote "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
Ernio Posted August 21, 2016 Posted August 21, 2016 Lookup HarvestDropsEvent docs. Yes - that is even fired when harvestBlock(...) happens and yes - as per design - it expects null players. Quote Quote 1.7.10 is no longer supported by forge, you are on your own.
SamTebbs33 Posted August 21, 2016 Author Posted August 21, 2016 On 8/21/2016 at 7:29 PM, Ernio said: 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. Quote "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
Animefan8888 Posted August 21, 2016 Posted August 21, 2016 On 8/21/2016 at 6:11 PM, SamTebbs33 said: 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). Quote 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.
Ernio Posted August 22, 2016 Posted August 22, 2016 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. Quote Quote 1.7.10 is no longer supported by forge, you are on your own.
imadnsn Posted August 22, 2016 Posted August 22, 2016 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 Quote
SamTebbs33 Posted August 22, 2016 Author Posted August 22, 2016 On 8/22/2016 at 5:56 AM, Ernio said: 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. Quote "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
SamTebbs33 Posted August 22, 2016 Author Posted August 22, 2016 On 8/21/2016 at 9:58 PM, Animefan8888 said: 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. Quote "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
Draco18s Posted August 22, 2016 Posted August 22, 2016 On 8/22/2016 at 11:46 AM, SamTebbs33 said: 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. Quote 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.
Ernio Posted August 22, 2016 Posted August 22, 2016 On 8/22/2016 at 11:46 AM, SamTebbs33 said: Please don't get so hostile all of a sudden. At this point I would be scared of giving you a flower - too hostile! Like seriously - what was so hostile in that/my response? There is literally no hard language there. Anyway - cheers to solution. Quote Quote 1.7.10 is no longer supported by forge, you are on your own.
Recommended Posts
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.