From 1db1cc1f63345eaaeb60486e067d39106c39fa6d Mon Sep 17 00:00:00 2001 From: modmuss Date: Sun, 11 Aug 2024 14:52:04 +0100 Subject: [PATCH] Crafter support for the transfer api. (#4013) * Crafter support for the transfer api. Closes #4004 * Review feedback --- .../mixin/transfer/CrafterBlockMixin.java | 72 +++++++++++++++++++ .../fabric-transfer-api-v1.mixins.json | 1 + 2 files changed, 73 insertions(+) create mode 100644 fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/mixin/transfer/CrafterBlockMixin.java diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/mixin/transfer/CrafterBlockMixin.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/mixin/transfer/CrafterBlockMixin.java new file mode 100644 index 000000000..fc410dba1 --- /dev/null +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/mixin/transfer/CrafterBlockMixin.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.transfer; + +import com.llamalad7.mixinextras.sugar.Local; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.block.BlockState; +import net.minecraft.block.CrafterBlock; +import net.minecraft.block.entity.CrafterBlockEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.recipe.CraftingRecipe; +import net.minecraft.recipe.RecipeEntry; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; + +import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; +import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; + +@Mixin(CrafterBlock.class) +public class CrafterBlockMixin { + // Inject after vanilla's attempts to insert the stack into an inventory. + @Inject(method = "transferOrSpawnStack", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", shift = At.Shift.BEFORE)) + private void transferOrSpawnStack(ServerWorld world, BlockPos pos, CrafterBlockEntity blockEntity, ItemStack inputStack, BlockState state, RecipeEntry recipe, CallbackInfo ci, @Local Direction direction, @Local Inventory inventory, @Local(ordinal = 1) ItemStack itemStack) { + if (inventory != null) { + // Vanilla already found and tested an inventory, nothing else to do even if it failed to insert. + return; + } + + if (itemStack.isEmpty()) { + // Nothing left to do, in theory should never get here. + return; + } + + final Storage target = ItemStorage.SIDED.find(world, pos.offset(direction), direction.getOpposite()); + + if (target != null) { + // Attempt to move the entire stack, and decrement the size of success moves. + try (Transaction transaction = Transaction.openOuter()) { + long moved = target.insert(ItemVariant.of(itemStack), inputStack.getCount(), transaction); + + if (moved > 0) { + itemStack.decrement((int) moved); + transaction.commit(); + } + } + } + + // Any remaining will be dropped in the world by vanilla logic + } +} diff --git a/fabric-transfer-api-v1/src/main/resources/fabric-transfer-api-v1.mixins.json b/fabric-transfer-api-v1/src/main/resources/fabric-transfer-api-v1.mixins.json index 591b01df7..4952c8429 100644 --- a/fabric-transfer-api-v1/src/main/resources/fabric-transfer-api-v1.mixins.json +++ b/fabric-transfer-api-v1/src/main/resources/fabric-transfer-api-v1.mixins.json @@ -7,6 +7,7 @@ "BucketItemAccessor", "BucketItemMixin", "ChiseledBookshelfBlockEntityMixin", + "CrafterBlockMixin", "DoubleInventoryAccessor", "DropperBlockMixin", "FluidMixin",