From 04394d469370dc7999d3d69c7a0c2485c033800b Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 21 Feb 2024 16:51:29 -0500 Subject: [PATCH] Post process block place event (#84) * backport fix for #68 * during place event * Post process place event * fix injection point & add deprecation javadoc --- .../porting_lib/event/common/BlockEvents.java | 16 ++++++++++++++++ .../porting_lib/mixin/common/BlockItemMixin.java | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java b/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java index baa974a46..82335a354 100644 --- a/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java +++ b/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java @@ -62,7 +62,9 @@ public interface BeforePlace { /** * Invoked after a block is placed, from {@link BlockItem#useOn(UseOnContext)}. Called on both client and server. + * @deprecated Use {@link BlockEvents#POST_PROCESS_PLACE} instead. */ + @Deprecated public static final Event AFTER_PLACE = EventFactory.createArrayBacked(AfterPlace.class, callbacks -> context -> { for (AfterPlace callback : callbacks) callback.afterPlace(context); @@ -72,6 +74,20 @@ public interface AfterPlace { void afterPlace(BlockPlaceContext ctx); } + /** + * Invoked after a block is placed, from the TAIL of {@link BlockItem#place(BlockPlaceContext)}. + * Called on both client and server. + * Provides the block's Position and BlockState as well. + */ + public static final Event POST_PROCESS_PLACE = EventFactory.createArrayBacked(PostProcessPlace.class, callbacks -> (context, blockPos, blockState) -> { + for (PostProcessPlace callback : callbacks) + callback.postProcessPlace(context, blockPos, blockState); + }); + + public interface PostProcessPlace { + void postProcessPlace(BlockPlaceContext ctx, BlockPos blockPos, BlockState blockState); + } + private final LevelAccessor world; private final BlockPos pos; private final BlockState state; diff --git a/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java b/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java index 7bb94241e..21c6ee2ef 100644 --- a/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java +++ b/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java @@ -2,8 +2,14 @@ import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; + import io.github.fabricators_of_create.porting_lib.event.common.BlockEvents; +import net.minecraft.core.BlockPos; + +import net.minecraft.world.level.block.state.BlockState; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -36,4 +42,9 @@ public abstract class BlockItemMixin implements BlockItemExtensions { BlockEvents.AFTER_PLACE.invoker().afterPlace(new BlockPlaceContext(context)); return placeResult; } + + @Inject(method = "place", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/InteractionResult;sidedSuccess(Z)Lnet/minecraft/world/InteractionResult;")) + private void port_lib$postProcessPlace(BlockPlaceContext context, CallbackInfoReturnable cir, @Local BlockPos blockPos, @Local BlockState blockState) { + BlockEvents.POST_PROCESS_PLACE.invoker().postProcessPlace(context, blockPos, blockState); + } }