From fb5b0493ec405375efbb02b2dcf57d486cc1c504 Mon Sep 17 00:00:00 2001 From: Leronus Date: Sun, 18 Feb 2024 21:08:45 -0800 Subject: [PATCH] Added autosmelter --- .../mod/leronus/mores/mixin/BlockMixin.java | 51 +++++++++++++++++++ src/main/resources/mores.mixins.json | 3 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mod/leronus/mores/mixin/BlockMixin.java diff --git a/src/main/java/mod/leronus/mores/mixin/BlockMixin.java b/src/main/java/mod/leronus/mores/mixin/BlockMixin.java new file mode 100644 index 0000000..305b67a --- /dev/null +++ b/src/main/java/mod/leronus/mores/mixin/BlockMixin.java @@ -0,0 +1,51 @@ +package mod.leronus.mores.mixin; + +import mod.leronus.mores.item.ModItems; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.Entity; +import net.minecraft.inventory.SimpleInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.recipe.RecipeType; +import net.minecraft.recipe.SmeltingRecipe; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; +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.CallbackInfoReturnable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Mixin(Block.class) +public class BlockMixin { + + @Inject( + method = "getDroppedStacks(Lnet/minecraft/block/BlockState;Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/entity/BlockEntity;Lnet/minecraft/entity/Entity;Lnet/minecraft/item/ItemStack;)Ljava/util/List;", + at = @At("RETURN"), + cancellable = true) + private static void getDroppedStacks(BlockState state, ServerWorld world, BlockPos pos, BlockEntity blockEntity, Entity entity, ItemStack stack, CallbackInfoReturnable> cir) { + List items = new ArrayList<>(); + List returnValue = cir.getReturnValue(); + if (stack.getItem() != ModItems.RUBY_AXE && stack.getItem() != ModItems.RUBY_PICKAXE && stack.getItem() != ModItems.RUBY_SHOVEL && stack.getItem() != ModItems.RUBY_HOE) { + cir.setReturnValue(returnValue); + return; + } + for (ItemStack itemStack : returnValue) { +// Optional recipe = world.getRecipeManager().listAllOfType(RecipeType.SMELTING).stream().filter((smeltingRecipe -> smeltingRecipe.getPreviewInputs().get(0).test(itemStack))).findFirst(); + Optional recipe = world.getRecipeManager().getFirstMatch(RecipeType.SMELTING, new SimpleInventory(itemStack), world); + + if (recipe.isPresent()) { + ItemStack smelted = recipe.get().getOutput(world.getRegistryManager()).copy(); + smelted.setCount(itemStack.getCount()); + items.add(smelted); + } else { + items.add(itemStack); + } + } + cir.setReturnValue(items); + } +} \ No newline at end of file diff --git a/src/main/resources/mores.mixins.json b/src/main/resources/mores.mixins.json index e7bc350..8df17e5 100644 --- a/src/main/resources/mores.mixins.json +++ b/src/main/resources/mores.mixins.json @@ -4,7 +4,8 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "MoresMixin", - "SwordItemMixin" + "SwordItemMixin", + "BlockMixin" ], "injectors": { "defaultRequire": 1