Jump to content

2 questions about destroying blocks [Forge 1.11.2]


SnakeBlock

Recommended Posts

I know the title doesn't describe much, but I'm gonna try to explain.

 

1. How would I erase a block? AKA turn it into air, like /setblock ~ ~ ~ air? Apparently you need 'blockstates', but I'm gonna need a bit more of an explanation to that that caters to what I'm trying to do specifically here. Anyway I'd just appreciate it if somebody could tell me how to turn blocks into other blocks at all.

 

2. Completely optional question, because I'm not even sure if it's possible - and even if it is it's probably really complicated and I'm not sure if I want to undertake something TOO complicated, as what I'm going for is literally just a visual effect and will not affect gameplay in any way. Is it... possible to show the block breaking animation on a block that isn't being broken? To fake it being actually broken and all. I guess I can handle the particle effects myself.

 

Thank you for answering all my prior questions perfectly and being such a competent/patient community! :) I'm starting to get used to forge.

Link to comment
Share on other sites

13 minutes ago, SnakeBlock said:

1. How would I erase a block? AKA turn it into air, like /setblock ~ ~ ~ air? Apparently you need 'blockstates', but I'm gonna need a bit more of an explanation to that that caters to what I'm trying to do specifically here. Anyway I'd just appreciate it if somebody could tell me how to turn blocks into other blocks at all.

 

Use World#setBlockState to replace the IBlockState at the specified BlockPos with another. Use World#setBlockToAir to replace the IBlockState at the specified BlockPos with air (this just calls World#setBlockState).

 

Use Block#getDefaultState to get the default IBlockState of a Block and IBlockState#withProperty to get an IBlockState with the IProperty set to the specified value.

 

Forge has an introduction to block states here.

 

 

13 minutes ago, SnakeBlock said:

2. Completely optional question, because I'm not even sure if it's possible - and even if it is it's probably really complicated and I'm not sure if I want to undertake something TOO complicated, as what I'm going for is literally just a visual effect and will not affect gameplay in any way. Is it... possible to show the block breaking animation on a block that isn't being broken? To fake it being actually broken and all. I guess I can handle the particle effects myself.

 

You can call World#sendBlockBreakProgress to render the block breaking cracks on a block.

 

If called from the logical client, it will update the break progress for that position. If called from the logical server, it will send a packet to all players within 32 blocks of the position (except the one whose entity ID was passed as the first argument) that calls the same method on their client.

  • Like 2

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

2 hours ago, Choonster said:

 

Use World#setBlockState to replace the IBlockState at the specified BlockPos with another. Use World#setBlockToAir to replace the IBlockState at the specified BlockPos with air (this just calls World#setBlockState).

 

Use Block#getDefaultState to get the default IBlockState of a Block and IBlockState#withProperty to get an IBlockState with the IProperty set to the specified value.

 

Forge has an introduction to block states here.

 

 

 

You can call World#sendBlockBreakProgress to render the block breaking cracks on a block.

 

If called from the logical client, it will update the break progress for that position. If called from the logical server, it will send a packet to all players within 32 blocks of the position (except the one whose entity ID was passed as the first argument) that calls the same method on their client.

Thank you!

 

Also, a small question about onBlockDestroyed: I'm guessing that "ItemStack stack" in its params basically is the tool/whatever that the block is destroyed by? Correct me if I'm wrong. Because I overrid onBlockDestroyed in a non-tool, non-pickaxe item (since I assumed that it'd do something , and it's not even doing anything.

 

Sorry if this is a weird question, I'm a bit confused about this.

Link to comment
Share on other sites

7 hours ago, SnakeBlock said:

Thank you!

 

Also, a small question about onBlockDestroyed: I'm guessing that "ItemStack stack" in its params basically is the tool/whatever that the block is destroyed by? Correct me if I'm wrong. Because I overrid onBlockDestroyed in a non-tool, non-pickaxe item (since I assumed that it'd do something , and it's not even doing anything.

 

Sorry if this is a weird question, I'm a bit confused about this.

 

Item#onBlockDestroyed is called when a player destroys a block while holding an ItemStack of the Item in their main hand (regardless of what that Item is). The ItemStack argument is the ItemStack in the player's main hand.

 

It's not called in creative mode.

  • Like 1

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

9 hours ago, Choonster said:

 

Item#onBlockDestroyed is called when a player destroys a block while holding an ItemStack of the Item in their main hand (regardless of what that Item is). The ItemStack argument is the ItemStack in the player's main hand.

 

It's not called in creative mode.

Well, I'm not sure if I completely understand but seems like the ItemStack of the Item is the Item that has the code that has the overridden method? If so, how would I affect pickaxes and pickaxes _only_, even if the overridden method is in a non-pickaxe item? Basically like some kind of buff to pickaxes as a whole, which is what I'm going for.

Link to comment
Share on other sites

6 minutes ago, SnakeBlock said:

Well, I'm not sure if I completely understand but seems like the ItemStack of the Item is the Item that has the code that has the overridden method? If so, how would I affect pickaxes and pickaxes _only_, even if the overridden method is in a non-pickaxe item? Basically like some kind of buff to pickaxes as a whole, which is what I'm going for.

 

The method is only called on the Item that the player is holding. This is a general principle in Minecraft: methods of classes like Item and Block are only called on the object involved in the interaction (e.g. breaking a block calls a method on the Block that was broken and the Item being held by the player that broke it).

 

To change the behaviour of Blocks and Items added by Minecraft and other mods, you need to use events.

  • Like 1

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

Just now, Choonster said:

 

The method is only called on the Item that the player is holding. This is a general principle in Minecraft: methods of classes like Item and Block are only called on the object involved in the interaction (e.g. breaking a block calls a method on the Block that was broken and the Item being held by the player that broke it).

 

To change the behaviour of Blocks and Items added by Minecraft and other mods, you need to use events.

Ahh, thank you. I'll research events. Any specific good place you know that has basic information on events?

Link to comment
Share on other sites

31 minutes ago, SnakeBlock said:

Ahh, thank you. I'll research events. Any specific good place you know that has basic information on events?

 

Forge's documentation has a page on events here.

  • Like 1

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

56 minutes ago, Choonster said:

 

Forge's documentation has a page on events here.

Sorry for the bombarding of dumb questions, I really want to figure this out. But I can't seem to find an event for a player breaking a block - just starting to break one, apparently. Is there an event that calls after the player breaks a block?

Link to comment
Share on other sites

9 minutes ago, SnakeBlock said:

Sorry for the bombarding of dumb questions, I really want to figure this out. But I can't seem to find an event for a player breaking a block - just starting to break one, apparently. Is there an event that calls after the player breaks a block?

 

BlockEvent.BreakEvent.

  • Like 1

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

1 hour ago, Choonster said:

 

BlockEvent.BreakEvent.

Thank you so so so much!

 

Final question: Is there any way to see a block's breaking progress? Like which animation state it is in. Want to use the sendBlockBreakProgress method on Stone Blocks around the block the player is mining (yes, I'm doing a multi-break system and it worked because of the help xD, this is just aesthetic), so I really just need the progress of the specific block the player is mining. Can't exactly see any methods or anything on my BlockState that gets the progress, though.

Link to comment
Share on other sites

Just now, SnakeBlock said:

Thank you so so so much!

 

Final question: Is there any way to see a block's breaking progress? Like which animation state it is in. Want to use the sendBlockBreakProgress method on Stone Blocks around the block the player is mining (yes, I'm doing a multi-break system and it worked because of the help xD, this is just aesthetic), so I really just need the progress of the specific block the player is mining. Can't exactly see any methods or anything on my BlockState that gets the progress, though.

 

If you look at the usages of World#sendBlockBreakProgress, you'll see that it's called from various places within PlayerInteractionManager; this is the server-side class that manages block interactions (e.g. breaking) for the player that the instance is attached to.

 

When the player is in the process of breaking a block, PlayerInteractionManager#durabilityRemainingOnBlock stores the current break progress; this is passed to World#sendBlockBreakProgress whenever it changes.

  • Like 1

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

7 minutes ago, Choonster said:

 

If you look at the usages of World#sendBlockBreakProgress, you'll see that it's called from various places within PlayerInteractionManager; this is the server-side class that manages block interactions (e.g. breaking) for the player that the instance is attached to.

 

When the player is in the process of breaking a block, PlayerInteractionManager#durabilityRemainingOnBlock stores the current break progress; this is passed to World#sendBlockBreakProgress whenever it changes.

Looks like 'durabilityRemainingOnBlock' is a private int. How would I access it?

w.sendBlockBreakProgress(p.getEntityId(), newpos, (PlayerInteractionManager.durabilityRemainingOnBlock));

As you can probably see from the code, I'm confused about PlayerInteractionManager as a whole. Could use some help here! :)

Link to comment
Share on other sites

Reflection.

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

3 minutes ago, SnakeBlock said:

Looks like 'durabilityRemainingOnBlock' is a private int. How would I access it?


w.sendBlockBreakProgress(p.getEntityId(), newpos, (PlayerInteractionManager.durabilityRemainingOnBlock));

As you can probably see from the code, I'm confused about PlayerInteractionManager as a whole. Could use some help here! :)

 

As Draco said, you need to use reflection to access the field.

 

PlayerInteractionManager#durabilityRemainingOnBlock is a non-static field, so it needs to be accessed from an instance rather than the class. This is a basic Java concept.

 

Each player's PlayerInteractionManager instance is stored in the EntityPlayerMP#interactionManager field. Every EntityPlayer on the logical server (when World#isRemote is false) is an instance of EntityPlayerMP.

  • Like 1

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

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.