Build a Fabric 1.21.11 Minecraft Java server-side Lifesteal mod, compatible with other mods. Implement all features below in this project, preserving the generated project structure and using Fabric-compatible APIs for the exact target version. Requirements: default 10 permanent hearts (20 max health); min 6 hearts (12 max health); max 20 hearts (40 max health). Track persistent player permanent-heart counts (including offline/respawn persistence) reliably, independent of temporary/current health, and apply max-health attribute appropriately on join/respawn/dimension changes as needed without disrupting other mods' attribute modifiers. On player-vs-player kill: if victim has >6 permanent hearts and killer <20, victim loses exactly 1 permanent heart and killer gains exactly 1. If victim is at minimum, do not take/give hearts and tell killer the victim has no hearts to steal. If killer is already at max, do not take/give hearts and send killer an appropriate error. Resolve both conditions without changing either player's permanent hearts. Add a server command /withdraw <positive integer> for players: withdraw that many permanent hearts into exactly that many stackable heart items only when the player would remain at or above 6; otherwise withdraw none and show a private error. Handle invalid/zero quantities and permissions safely. Register a clearly named heart item. Right-click/use by an eligible player consumes exactly one item and permanently adds one heart; if already at 20, do not consume item and send private error. Heart items must be transferable and remain normal dropped inventory items on death (no keep-inventory special handling). Add a server-side recipe for one heart item from exactly 1 enchanted golden apple, 4 diamonds, and 4 netherite scraps, using a shaped recipe; crafted item must behave same as withdrawn item. Ensure heart count data survives server restart and death; initialize existing/new player defaults to 10. Keep implementation robust and avoid replacing shared entity max-health attributes in a way that conflicts with other mods. Add useful user-facing messages. Code only the feature (no tests).

Kodari AI
KodariOct 27 at 4:49 pm 5
LifestealHearts
Server-side Fabric mod adding lifesteal mechanics. Tracks persistent permanent heart counts (default 10, min 6, max 20) independently of temporary health. On PvP kill, transfers one permanent heart between players when conditions allow. Adds /withdraw command converting hearts to stackable heart items, a consumable heart item granting one permanent heart, and a shaped recipe from enchanted golden apple, diamonds, and netherite scraps. Data persists across restarts and deaths, using attribute modifiers that avoid conflicts with other mods.
  1. Implement persistent heart storage, health modifiers, PvP stealing, /withdraw, and heart-item use.

  2. Add the shaped recipe and item assets.

  3. Create the Fabric 1.21.11 project metadata and entrypoint.

Implemented the Fabric 1.21.11 Lifesteal mod with persistent 6–20 permanent hearts, defaulting players to 10. PvP kills transfer one heart when both players are eligible; health changes use a mod-specific attribute modifier so other modifiers are preserved.

Players can use /withdraw <amount> to turn hearts into stackable heart items, provided they keep at least 6. Using a heart item permanently adds one heart; at 20 hearts, the item is not consumed.

Survival access: craft one heart from 1 enchanted golden apple, 4 diamonds, and 4 netherite scraps, or withdraw hearts with the command. Heart items are also added to the Ingredients creative tab.

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

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.serialization.Codec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.datafixer.DataFixTypes;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.world.PersistentState;
import net.minecraft.world.PersistentStateManager;
import net.minecraft.world.PersistentStateType;
import net.minecraft.world.World;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class LifestealMod implements ModInitializer {
    public static final String MOD_ID = "lifesteal";

    private static final int DEFAULT_HEARTS = 10;
    private static final int MIN_HEARTS = 6;
    private static final int MAX_HEARTS = 20;
    private static final Identifier HEART_MODIFIER_ID = Identifier.of(MOD_ID, "permanent_hearts");
    private static final RegistryKey<Item> HEART_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MOD_ID, "heart"));
    private static final RegistryKey<ItemGroup> INGREDIENTS_GROUP = RegistryKey.of(
            RegistryKeys.ITEM_GROUP, Identifier.of("minecraft", "ingredients")
    );
    public static final Item HEART = Registry.register(
            Registries.ITEM, HEART_KEY, new HeartItem(new Item.Settings().registryKey(HEART_KEY))
    );

    private static final Codec<Map<String, Integer>> HEARTS_CODEC = Codec.unboundedMap(Codec.STRING, Codec.INT);
    private static final PersistentStateType<HeartState> HEART_STATE_TYPE = new PersistentStateType<>(
            MOD_ID + "_hearts",
            HeartState::new,
            HEARTS_CODEC.xmap(HeartState::new, state -> state.hearts),
            DataFixTypes.SAVED_DATA_COMMAND_STORAGE
    );

    @Override
    public void onInitialize() {
        ItemGroupEvents.modifyEntriesEvent(INGREDIENTS_GROUP).register(entries -> entries.prepend(HEART));

        ServerPlayerEvents.JOIN.register(LifestealMod::refreshPlayer);
        ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> refreshPlayer(newPlayer));
        ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register(
                (player, origin, destination) -> refreshPlayer(player)
        );
        ServerLivingEntityEvents.AFTER_DEATH.register(LifestealMod::handlePlayerDeath);
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
                dispatcher.register(CommandManager.literal("withdraw")
                        .then(CommandManager.argument("amount", IntegerArgumentType.integer())
                                .executes(context -> withdraw(context.getSource(),
                                        IntegerArgumentType.getInteger(context, "amount"))))));
    }

    private static void refreshPlayer(ServerPlayerEntity player) {
        HeartState state = stateFor(player);
        applyHeartAttribute(player, state.getHearts(player.getUuid()));
    }

    private static void handlePlayerDeath(LivingEntity entity, DamageSource damageSource) {
        if (!(entity instanceof ServerPlayerEntity victim)
                || !(damageSource.getAttacker() instanceof ServerPlayerEntity killer)
                || victim == killer) {
            return;
        }

        HeartState state = stateFor(killer);
        int victimHearts = state.getHearts(victim.getUuid());
        int killerHearts = state.getHearts(killer.getUuid());
        boolean victimAtMinimum = victimHearts <= MIN_HEARTS;
        boolean killerAtMaximum = killerHearts >= MAX_HEARTS;

        if (victimAtMinimum) {
            killer.sendMessage(Text.literal("This player has no hearts left to steal."));
        }
        if (killerAtMaximum) {
            killer.sendMessage(Text.literal("You already have the maximum of 20 permanent hearts."));
        }
        if (victimAtMinimum || killerAtMaximum) {
            return;
        }

        state.setHearts(victim.getUuid(), victimHearts - 1);
        state.setHearts(killer.getUuid(), killerHearts + 1);
        applyHeartAttribute(victim, victimHearts - 1);
        applyHeartAttribute(killer, killerHearts + 1);
        victim.sendMessage(Text.literal("You lost 1 permanent heart to " + killer.getName().getString() + "."));
        killer.sendMessage(Text.literal("You stole 1 permanent heart. You now have " + (killerHearts + 1) + "."));
    }

    private static int withdraw(ServerCommandSource source, int amount) {
        ServerPlayerEntity player = source.getPlayer();
        if (player == null) {
            source.sendFeedback(() -> Text.literal("Only players can withdraw hearts."), false);
            return 0;
        }
        if (amount <= 0) {
            player.sendMessage(Text.literal("The amount must be a positive whole number."));
            return 0;
        }

        HeartState state = stateFor(player);
        int hearts = state.getHearts(player.getUuid());
        if (hearts - amount < MIN_HEARTS) {
            player.sendMessage(Text.literal("You must keep at least 6 permanent hearts."));
            return 0;
        }

        state.setHearts(player.getUuid(), hearts - amount);
        applyHeartAttribute(player, hearts - amount);

        int remaining = amount;
        while (remaining > 0) {
            int stackSize = Math.min(64, remaining);
            player.getInventory().offerOrDrop(new ItemStack(HEART, stackSize));
            remaining -= stackSize;
        }
        player.sendMessage(Text.literal("Withdrew " + amount + " permanent heart(s) as heart items."));
        return 1;
    }

    private static void applyHeartAttribute(ServerPlayerEntity player, int hearts) {
        EntityAttributeInstance maxHealth = player.getAttributeInstance(EntityAttributes.MAX_HEALTH);
        if (maxHealth == null) {
            return;
        }

        maxHealth.overwritePersistentModifier(new EntityAttributeModifier(
                HEART_MODIFIER_ID,
                (hearts - DEFAULT_HEARTS) * 2.0,
                EntityAttributeModifier.Operation.ADD_VALUE
        ));
        player.setHealth(Math.min(player.getHealth(), player.getMaxHealth()));
    }

    private static HeartState stateFor(ServerPlayerEntity player) {
        ServerWorld world = (ServerWorld) player.getEntityWorld();
        PersistentStateManager manager = world.getServer().getOverworld().getPersistentStateManager();
        return manager.getOrCreate(HEART_STATE_TYPE);
    }

    private static final class HeartItem extends Item {
        private HeartItem(Settings settings) {
            super(settings);
        }

        @Override
        public ActionResult use(World world, PlayerEntity user, Hand hand) {
            if (world.isClient()) {
                return ActionResult.SUCCESS;
            }
            if (!(user instanceof ServerPlayerEntity player)) {
                return ActionResult.PASS;
            }

            HeartState state = stateFor(player);
            int hearts = state.getHearts(player.getUuid());
            if (hearts >= MAX_HEARTS) {
                player.sendMessage(Text.literal("You already have the maximum of 20 permanent hearts."));
                return ActionResult.FAIL;
            }

            state.setHearts(player.getUuid(), hearts + 1);
            applyHeartAttribute(player, hearts + 1);
            player.getStackInHand(hand).decrement(1);
            player.sendMessage(Text.literal("You gained 1 permanent heart. You now have " + (hearts + 1) + "."));
            return ActionResult.SUCCESS;
        }
    }

    private static final class HeartState extends PersistentState {
        private final Map<String, Integer> hearts;