From 3faeaa93c40b53cc05c9d74ae6abe0e98ce72aa3 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 21 Feb 2024 16:53:08 -0500 Subject: [PATCH] Post process block place event 1.20.1 (#86) * The star of the show, MutableContainerItemContext! * during 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/base/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java b/base/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java index b985db7b6..f8b70c018 100644 --- a/base/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java +++ b/base/src/main/java/io/github/fabricators_of_create/porting_lib/event/common/BlockEvents.java @@ -50,7 +50,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); @@ -60,6 +62,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 level; private final BlockPos pos; private final BlockState state; diff --git a/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java b/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java index d4f74fefe..23bbd92d0 100644 --- a/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/BlockItemMixin.java +++ b/base/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); + } }