Jump to content

fortune and silktouch enchantments when breaking blocks


Crare1

Recommended Posts

Trying to figure out a way to handle breaking multiple blocks with enchanted item.

I know how to get the enchantments from tools.

But I'm not familiar how minecraft and forge handles block breaking. Can you call a block break event to happen based on tool used on them?

Some kind of blockbreakevent-handler might be needed..

how to send block break calls though to the world with the item doing it?

I know how to break the blocks, but it doesn't register them as being done by the tool, so it won't apply enchantment modifier effects what should happen with them.(multiple drops, silk touch, experience...)

Useful Tools - Early game items for your building and farming needs.

 

Link to comment
Share on other sites

Register an event listener for the BlockBreakEvent

Inside, check the tool used to see if it has your enchantment

If so, modify the adjacent blocks

(note: if you intend to break and harvest adjacent blocks, you should either invoke the same pathways that items do, or broadcast the related events yourself, in the event that another mod or effect wants to do anything. For example, a permissions mod that prevents a player from breaking blocks in regions they don't have access to because it is owned by another player. Be aware of recursion!).

  • Like 1

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

On 3/25/2020 at 10:55 PM, Draco18s said:

Register an event listener for the BlockBreakEvent

Inside, check the tool used to see if it has your enchantment

If so, modify the adjacent blocks

(note: if you intend to break and harvest adjacent blocks, you should either invoke the same pathways that items do, or broadcast the related events yourself, in the event that another mod or effect wants to do anything. For example, a permissions mod that prevents a player from breaking blocks in regions they don't have access to because it is owned by another player. Be aware of recursion!).

Ok I don't really get how to call that block break event correctly to do it's thing right. Easy way out is doing world.destroyBlock(), but that of course would skip all the checking and doesn't handle enchantment modifiers.

 

I've tried to create block break event and post it to the forge's event bus, I only see it happening in the eventhandler that listens to it, but it doesn't seem to do much else, like breaking the block and spawning the drops:
 

BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(worldIn, blockPos, blockState, player);
MinecraftForge.EVENT_BUS.post(event);

 

Useful Tools - Early game items for your building and farming needs.

 

Link to comment
Share on other sites

3 hours ago, Crare1 said:

 


BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(worldIn, blockPos, blockState, player);
MinecraftForge.EVENT_BUS.post(event);

 

That's a good start. As long as you insure that the blockPos you're passing there is one of the adjacent blocks you're trying to break.

Second, the event has a canceled property. If the event has been canceled when it gets back to you, you need to not break the block. This is typically done by:

if(MinecraftForge.EVENT_BUS.post(event)) {
    //break the block
}

 

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

1 hour ago, loordgek said:

Yes I've seen that page, what are you trying to tell me? That's for subscribing to the events. Also that's for 1.13.x. Here's new https://mcforge.readthedocs.io/en/1.14.x/events/intro/

This is more inline what I'm trying to do, I think: https://mcforge.readthedocs.io/en/1.14.x/blocks/interaction/#player-breakdestroy

But what is the event that breaks the block and does the rest of the stuff like drops, exp, silktouch-modifiers? Or do I need to write that myself? Clearly the item calls it once when it breaks the block and does the rest, but I don't know how to initiate that same process myself.

 

Also I'm developing for 1.15.x but the docs are even more lacking at the moment...

 

16 minutes ago, Draco18s said:

That's a good start. As long as you insure that the blockPos you're passing there is one of the adjacent blocks you're trying to break.

Second, the event has a canceled property. If the event has been canceled when it gets back to you, you need to not break the block. This is typically done by:


if(MinecraftForge.EVENT_BUS.post(event)) {
    //break the block
}

 

I've been looking at this for some time. If I understand right, breaking blocks and harvesting them doesn't really do the same thing am I right? Breaking means removing the block from world/changing it to air or waterlogged fluid that's in there. And harvesting means the action that actually does the dropping of items from the block. So I would need to handle both separately. (BreakEvent + HarvestDropsEvent)

I'm still wondering how it works for the vanilla tools. how they handle it. What starts the whole process after the block is about to break. because it seems whole heck of a job to handle all the echantments, blockdrops and stuff inside the tool's class.

 

Then there's PlayerInteractionManager.tryHarvestBlock() which seems to do something similar to what I want. Can I use it somehow? At least I can study what it does and simulate that.

Sorry it's late for me I'll get back to this later..

Useful Tools - Early game items for your building and farming needs.

 

Link to comment
Share on other sites

2 hours ago, Crare1 said:

breaking blocks and harvesting them doesn't really do the same thing am I right?

Correct.

2 hours ago, Crare1 said:

HarvestDropsEvent

No longer exists. Well, the event exists, but it isn't actually used any more. See this PR.

But if you don't want to handle that yourself, find the area of the code where the existing BreakEvent is fired and drops are generated, and see what calls it and call that yourself.

 

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

Ok finally figured it out. calling this function does all the block breaking, spawning drops, exp and silk touch and fortune enchantment handling and cancels it if needed to.

 

serverPlayerEntity.interactionManager.tryHarvestBlock(blockPos);

Of course then there's checking it's not remote and only initiating that above method when hitting the center block at start for each surrounding block.

Useful Tools - Early game items for your building and farming needs.

 

Link to comment
Share on other sites

Yep, that's the spot. I haven't been in a position to open up my IDE and go poking around, I just knew what to look for.

Anyway, sounds like you're on the right track.

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

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.