Tristan Butterworth

can you make a plugin that when you die you lose a heart and when you die it says in red You Have -1 Heart! and when you kill someone it says You Have Gained +1 Heart! in green and the max you can have is 20 hearts and if you are at 20 hearts it will say in chat You Are At The Maximum Hearts! in red and if you kill someone on 20 they drop a heart that is red coloured called Heart and when you right click it on say anything below 20 it gives you a heart and if you die to natural causes you lose 1 heart and it drops on the floor and the minimum hearts you can be on is 3 when you have died enough to be on 3 and you can do /withdraw to withdraw a heart but when you are on 3 you cant withdraw and it says You Are On The Minimum Hearts!

Kodari AI
KodariOct 16 at 4:49 pm 1
HeartSystem
A Minecraft plugin that introduces a heart-based life system. Players start with a default health pool and lose one heart upon death, with a red chat message "You Have -1 Heart!" displayed. Killing another player grants a heart, shown in green as "You Have Gained +1 Heart!". The maximum heart count is 20, and attempting to exceed it triggers a red "You Are At The Maximum Hearts!" message. When at max, killing a player drops a red item named "Heart" that can be right-clicked to restore a heart if below max. Natural deaths also drop a heart on the ground. The minimum heart count is 3, preventing further loss. A /withdraw command allows players to remove a heart, but is blocked at minimum with a "You Are On The Minimum Hearts!" message.
  • Create the Java plugin setup and metadata.

  • Persist each player’s heart count, defaulting to 10 with a 3–20 range.

  • Handle death losses/drops, kill rewards, usable Heart items, and /withdraw.

Created HeartSteal with persisted heart counts (starting at 10, minimum 3, maximum 20). Deaths remove a heart and drop a red Heart item; kills grant the killer a heart, or drop one if they’re already at the maximum. Right-clicking a Heart uses it, and /withdraw trades one heart for an item—unless you’re at the minimum.

Tristan Butterworth

can you add a command /lifesteal set_heart player 1-20

Kodari AI
KodariOct 16 at 4:52 pm 1

Added /lifesteal set_heart <player> <1-20>. It sets an online player’s hearts exactly, and the value persists across restarts. The command is restricted to operators by default (heartsteal.admin).

Tristan Butterworth

can you make the heart a wither star

Kodari AI
KodariOct 16 at 4:57 pm 1

Heart items now use a Wither Star as their icon.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 303
src/main/java/com/kodari/heartsteal/HeartStealPlugin.java
package com.kodari.heartsteal;

import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public final class HeartStealPlugin extends JavaPlugin implements Listener {
    private static final int DEFAULT_HEARTS = 10;
    private static final int MIN_HEARTS = 3;
    private static final int MAX_HEARTS = 20;

    private final Map<UUID, Integer> hearts = new HashMap<>();
    private File heartsFile;
    private NamespacedKey heartItemKey;

    @Override
    public void onEnable() {
        heartsFile = new File(getDataFolder(), "hearts.yml");
        heartItemKey = new NamespacedKey(this, "heart_item");
        loadHearts();
        getServer().getPluginManager().registerEvents(this, this);

        PluginCommand withdrawCommand = getCommand("withdraw");
        if (withdrawCommand != null) {
            withdrawCommand.setExecutor(this);
        }
    }

    @Override
    public void onDisable() {
        saveHearts();
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (hearts.putIfAbsent(player.getUniqueId(), DEFAULT_HEARTS) == null) {
            saveHearts();
        }
        applyMaxHealth(player);
    }

    @EventHandler
    public void onRespawn(PlayerRespawnEvent event) {
        applyMaxHealth(event.getPlayer());
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player victim = event.getEntity();
        int victimHearts = getHearts(victim);

        if (victimHearts > MIN_HEARTS) {
            setHearts(victim, victimHearts - 1);
            event.getDrops().add(createHeartItem());
            victim.sendMessage(ChatColor.RED + "You Have -1 Heart!");
        }

        Player killer = victim.getKiller();
        if (killer == null || killer.getUniqueId().equals(victim.getUniqueId())) {
            return;
        }

        int killerHearts = getHearts(killer);
        if (killerHearts >= MAX_HEARTS) {
            killer.sendMessage(ChatColor.RED + "You Are At The Maximum Hearts!");
            killer.getWorld().dropItemNaturally(killer.getLocation(), createHeartItem());
            return;
        }

        setHearts(killer, killerHearts + 1);
        killer.sendMessage(ChatColor.GREEN + "You Have Gained +1 Heart!");
    }

    @EventHandler
    public void onHeartUse(PlayerInteractEvent event) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            return;
        }
        if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        ItemStack heldItem = event.getItem();
        if (!isHeartItem(heldItem)) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        int playerHearts = getHearts(player);
        if (playerHearts >= MAX_HEARTS) {
            player.sendMessage(ChatColor.RED + "You Are At The Maximum Hearts!");
            return;
        }

        ItemStack remainingItem = heldItem.getAmount() > 1 ? heldItem.clone() : null;
        if (remainingItem != null) {
            remainingItem.setAmount(heldItem.getAmount() - 1);
        }
        player.getInventory().setItemInMainHand(remainingItem);
        setHearts(player, playerHearts + 1);
        player.sendMessage(ChatColor.GREEN + "You Have Gained +1 Heart!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("lifesteal")) {
            if (args.length != 3 || !args[0].equalsIgnoreCase("set_heart")) {
                sender.sendMessage(ChatColor.RED + "Usage: /lifesteal set_heart <player> <1-20>");
                return true;
            }

            Player target = getServer().getPlayerExact(args[1]);
            if (target == null) {
                sender.sendMessage(ChatColor.RED + "That player is not online.");
                return true;
            }

            int heartCount;
            try {
                heartCount = Integer.parseInt(args[2]);
            } catch (NumberFormatException exception) {
                sender.sendMessage(ChatColor.RED + "Heart count must be a number from 1 to 20.");
                return true;
            }
            if (heartCount < 1 || heartCount > MAX_HEARTS) {
                sender.sendMessage(ChatColor.RED + "Heart count must be a number from 1 to 20.");
                return true;
            }

            setHearts(target, heartCount);
            sender.sendMessage(ChatColor.GREEN + "Set " + target.getName() + " to " + heartCount + " hearts.");
            return true;
        }

        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can use this command.");
            return true;
        }

        int playerHearts = getHearts(player);
        if (playerHearts <= MIN_HEARTS) {
            player.sendMessage(ChatColor.RED + "You Are On The Minimum Hearts!");
            return true;
        }

        setHearts(player, playerHearts - 1);
        ItemStack heartItem = createHeartItem();
        Map<Integer, ItemStack> leftovers = player.getInventory().addItem(heartItem);
        leftovers.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
        return true;
    }

    private int getHearts(Player player) {
        return hearts.getOrDefault(player.getUniqueId(), DEFAULT_HEARTS);
    }

    private void setHearts(Player player, int heartCount) {
        int boundedHeartCount = Math.max(1, Math.min(MAX_HEARTS, heartCount));
        hearts.put(player.getUniqueId(), boundedHeartCount);
        applyMaxHealth(player);
        saveHearts();
    }

    private void applyMaxHealth(Player player) {
        XAttribute.of("max_health").ifPresent(attribute -> {
            AttributeInstance maxHealth = player.getAttribute(attribute.get());
            if (maxHealth == null) {
                return;
            }

            double healthLimit = getHearts(player) * 2.0;
            maxHealth.setBaseValue(healthLimit);
            if (player.getHealth() > healthLimit) {