From 642833e0a64d9dd93d7c78065944cc731af92f29 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Sun, 18 Feb 2024 15:17:08 -0500 Subject: [PATCH 1/3] The star of the show, MutableContainerItemContext! --- .../transfer/MutableContainerItemContext.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 transfer/src/main/java/io/github/fabricators_of_create/porting_lib/transfer/MutableContainerItemContext.java diff --git a/transfer/src/main/java/io/github/fabricators_of_create/porting_lib/transfer/MutableContainerItemContext.java b/transfer/src/main/java/io/github/fabricators_of_create/porting_lib/transfer/MutableContainerItemContext.java new file mode 100644 index 000000000..b7004b466 --- /dev/null +++ b/transfer/src/main/java/io/github/fabricators_of_create/porting_lib/transfer/MutableContainerItemContext.java @@ -0,0 +1,49 @@ +package io.github.fabricators_of_create.porting_lib.transfer; + +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.item.base.SingleItemStorage; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; + +import net.minecraft.world.item.ItemStack; + +import org.jetbrains.annotations.UnmodifiableView; + +import java.util.List; + +public class MutableContainerItemContext implements ContainerItemContext { + private final Slot slot; + + public MutableContainerItemContext(ItemStack initial) { + this.slot = new Slot(initial); + } + + @Override + public SingleSlotStorage getMainSlot() { + return this.slot; + } + + @Override + public long insertOverflow(ItemVariant itemVariant, long maxAmount, TransactionContext transactionContext) { + return 0; + } + + @Override + @UnmodifiableView + public List> getAdditionalSlots() { + return List.of(); + } + + private static class Slot extends SingleItemStorage { + public Slot(ItemStack initial) { + this.variant = ItemVariant.of(initial); + this.amount = initial.getCount(); + } + + @Override + protected long getCapacity(ItemVariant variant) { + return variant.getItem().getMaxStackSize(); + } + } +} From e7336e88bd4c63cdfc798c5907d5a0ae1bf0d982 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Tue, 20 Feb 2024 20:08:08 -0500 Subject: [PATCH 2/3] during place event --- .../porting_lib/event/common/BlockEvents.java | 15 +++++++++++++++ .../porting_lib/mixin/common/BlockItemMixin.java | 11 +++++++++++ 2 files changed, 26 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..67ec4e207 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 @@ -51,6 +51,7 @@ public interface BeforePlace { /** * Invoked after a block is placed, from {@link BlockItem#useOn(UseOnContext)}. Called on both client and server. */ + @Deprecated public static final Event AFTER_PLACE = EventFactory.createArrayBacked(AfterPlace.class, callbacks -> context -> { for (AfterPlace callback : callbacks) callback.afterPlace(context); @@ -60,6 +61,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..fc842ebcd 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 = "TAIL")) + private void port_lib$postProcessPlace(BlockPlaceContext context, CallbackInfoReturnable cir, @Local BlockPos blockPos, @Local BlockState blockState) { + BlockEvents.POST_PROCESS_PLACE.invoker().postProcessPlace(context, blockPos, blockState); + } } From 15b74c4e6d6da7bdb90a01b48aeaf9dfdef67bf5 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 21 Feb 2024 16:47:55 -0500 Subject: [PATCH 3/3] fix injection point & add deprecation javadoc --- .../porting_lib/event/common/BlockEvents.java | 1 + .../porting_lib/mixin/common/BlockItemMixin.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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 67ec4e207..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,6 +50,7 @@ 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 -> { 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 fc842ebcd..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 @@ -43,7 +43,7 @@ public abstract class BlockItemMixin implements BlockItemExtensions { return placeResult; } - @Inject(method = "place", at = @At(value = "TAIL")) + @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); }