Skip to content

Commit

Permalink
Added autosmelter
Browse files Browse the repository at this point in the history
  • Loading branch information
Leronus committed Feb 19, 2024
1 parent 4d910a4 commit fb5b049
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/main/java/mod/leronus/mores/mixin/BlockMixin.java
Original file line number Diff line number Diff line change
@@ -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<List<ItemStack>> cir) {
List<ItemStack> items = new ArrayList<>();
List<ItemStack> 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<SmeltingRecipe> recipe = world.getRecipeManager().listAllOfType(RecipeType.SMELTING).stream().filter((smeltingRecipe -> smeltingRecipe.getPreviewInputs().get(0).test(itemStack))).findFirst();
Optional<SmeltingRecipe> 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);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/mores.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"MoresMixin",
"SwordItemMixin"
"SwordItemMixin",
"BlockMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit fb5b049

Please sign in to comment.