fabric icon indicating copy to clipboard operation
fabric copied to clipboard

[Suggestion] Block place event

Open raoulvdberge opened this issue 1 year ago • 8 comments

There is UseBlockCallback, but I'm not sure if that is what I need.

raoulvdberge avatar Apr 04 '24 11:04 raoulvdberge

Nevermind, misread this.

~~We already have this. Override Block#onPlaced(). Most modern editors, such as Eclipse or IntelliJ should recommend this using Intellisense.~~

kaleidoscopikatt avatar Apr 05 '24 10:04 kaleidoscopikatt

Is this a serious comment?

raoulvdberge avatar Apr 05 '24 14:04 raoulvdberge

Keep in mind player Events have to listen for methods in ClientPlayerInteractionManager and ServerPlayerIneractionManager (both seperate to the Fabric API) in their Mixins for hooking in events, and LivingEntites have to listen for methods in ServerPlayerEntity and PlayerEntity (which are also separate to the Fabric API) in their Mixins for hooking in events - and there isn't a method that focuses on the onPlaced method - in fact the onPlaced method is never accessed to any classes accesible to the Fabric API so this is pretty much impossible to implement without a very hacky solution or attempting to modify the Minecraft packages. For now I'd suggest possibly just creating classes that extend Block and use those instead until something like this could be solved.

kaleidoscopikatt avatar Apr 05 '24 15:04 kaleidoscopikatt

Keep in mind player Events have to listen for methods in ClientPlayerInteractionManager and ServerPlayerIneractionManager (both seperate to the Fabric API) in their Mixins for hooking in events, and LivingEntites have to listen for methods in ServerPlayerEntity and PlayerEntity (which are also separate to the Fabric API) in their Mixins for hooking in events - and there isn't a method that focuses on the onPlaced method - in fact the onPlaced method is never accessed to any classes accesible to the Fabric API so this is pretty much impossible to implement without a very hacky solution or attempting to modify the Minecraft packages. For now I'd suggest possibly just creating classes that extend Block and use those instead until something like this could be solved.

I don't understand why you can't just hook into the interaction manager like you said. What is hacky, and why do we need to modify the packages?

sylv256 avatar Nov 03 '25 15:11 sylv256

This is a bit old, and I haven't used Fabric in a long while, so I very well could be wrong. Judging by what I said, hooking into the InteractionManager is possible, but they don't have any methods for placing blocks to listen for. This might have changed in later versions. This is where my point of these files being separate from the FabricAPI comes from - you can't just look & modify the code, or at least as far as I know. I don't know what hacky method I used to know I was referencing.

kaleidoscopikatt avatar Nov 03 '25 15:11 kaleidoscopikatt

Yeah, looking back on it this doesn't really seem very... possible. Not with the Player's interaction. ServerPlayerInteractionManager doesn't have any method for placing blocks, and there are not any methods exposed to the Fabric API you can inject into for this. This is how ServerPlayerInteractionManagerMixin works, injecting in code that call the specific fabric events.

For example, onBlockBroken injects into the tryBreakBlock method in the base file (ServerPlayerInteractionManager)

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onBroken(Lnet/minecraft/world/WorldAccess;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V"), method = "tryBreakBlock", locals = LocalCapture.CAPTURE_FAILHARD)

Leaving this code in whenever the method is called, which then calls all the methods onto the AFTER event in PlayerBlockBreakEvents:

PlayerBlockBreakEvents.AFTER.invoker().afterBlockBreak(this.world, this.player, pos, state, entity);

This is only possible because the ServerPlayerInteractionManager has the method tryBreakBlock, but it has no other method to do with placing a block, so we can't inject any event code into the class from the mixin at all. The only block-related interaction that is exposed to the Fabric API is interactBlock

I'd recommend trying to create a BlockItem mixin and inject in code for the place method?

kaleidoscopikatt avatar Nov 03 '25 16:11 kaleidoscopikatt

I say we find where Block#onPlace or whatever is called, then we go from there. Perhaps I'll do some investigating in a few days.

BlockItem could have something, though.

sylv256 avatar Nov 03 '25 19:11 sylv256

Potential changes to some mixin toolchain stuff could make it viable to port the neo event. Currently it would be a little jank

cputnam-a11y avatar Nov 03 '25 19:11 cputnam-a11y