tim1438746

Create a mod for a new enchantment: pyk on a pickaxe: when a player has mined gold, iron, or copper, he will automatically smelt these ores when breaking them.

Kodari AI
KodariAug 22 at 1:55 pm 8

Created the Fabric 1.21.11 PyK mod.

  • Adds the PyK enchantment to all vanilla pickaxes through enchanting tables and villager trades.

  • Mining gold, iron, or copper ore with a PyK-enchanted pickaxe automatically converts its raw drops into ingots.

  • Supports normal and deepslate ore variants.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 212
src/main/java/com/kodari/pyk/PykMod.java
package com.kodari.pyk;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Box;

public class PykMod implements ModInitializer {
    public static final String MOD_ID = "pyk";
    public static final RegistryKey<Enchantment> PYK_ENCHANTMENT = RegistryKey.of(
            RegistryKeys.ENCHANTMENT,
            Identifier.of(MOD_ID, "pyk")
    );

    @Override
    public void onInitialize() {
        PlayerBlockBreakEvents.AFTER.register((world, player, pos, state, blockEntity) -> {
            if (!hasPykEnchantment(player.getMainHandStack(), world.getRegistryManager().getOrThrow(RegistryKeys.ENCHANTMENT).getEntry(PYK_ENCHANTMENT).orElse(null))) {
                return;
            }

            Item smeltedItem = getSmeltedItem(state);
            if (smeltedItem == null) {
                return;
            }

            Box dropArea = Box.of(pos.toCenterPos()).expand(1.0D);
            for (ItemEntity itemEntity : world.getEntitiesByClass(ItemEntity.class, dropArea, entity -> true)) {
                ItemStack stack = itemEntity.getStack();
                if (isRawOreFor(state, stack)) {
                    itemEntity.setStack(new ItemStack(smeltedItem, stack.getCount()));
                }
            }
        });
    }

    private static boolean hasPykEnchantment(ItemStack stack, RegistryEntry<Enchantment> enchantment) {
        return enchantment != null && EnchantmentHelper.getLevel(enchantment, stack) > 0;
    }

    private static Item getSmeltedItem(BlockState state) {
        Block block = state.getBlock();
        if (block == Blocks.GOLD_ORE || block == Blocks.DEEPSLATE_GOLD_ORE) {
            return Items.GOLD_INGOT;
        }
        if (block == Blocks.IRON_ORE || block == Blocks.DEEPSLATE_IRON_ORE) {
            return Items.IRON_INGOT;
        }
        if (block == Blocks.COPPER_ORE || block == Blocks.DEEPSLATE_COPPER_ORE) {
            return Items.COPPER_INGOT;
        }
        return null;
    }

    private static boolean isRawOreFor(BlockState state, ItemStack stack) {
        Block block = state.getBlock();
        return (block == Blocks.GOLD_ORE || block == Blocks.DEEPSLATE_GOLD_ORE) && stack.isOf(Items.RAW_GOLD)
                || (block == Blocks.IRON_ORE || block == Blocks.DEEPSLATE_IRON_ORE) && stack.isOf(Items.RAW_IRON)
                || (block == Blocks.COPPER_ORE || block == Blocks.DEEPSLATE_COPPER_ORE) && stack.isOf(Items.RAW_COPPER);
    }
}