bai09gd

make a spear for a minecraft server, that counts 3 stages (3 seconds) of charging and then sending a fiery ray send at the entity you are aiming at, make it have a name "Ember Spear" and make the counter 1 2 3, and then have 3 stages of charging up when holding right click, 1st stage does 2 hearts of damage, 2nd does 4, and 3rd does 8. make the charging ability have a 20 second cooldown and make a permanent bar above the players hotbar that says how many seconds are left, make it cool.

Kodari AI
KodariNov 8 at 12:39 pm 1
EmberSpear
A Minecraft plugin that adds a custom "Ember Spear" item. When right-clicked and held, the spear charges through 3 visual stages (1, 2, 3) over 3 seconds. Upon release, it fires a fiery ray at the targeted entity, dealing 2, 4, or 8 hearts of damage based on charge stage. The ability has a 20-second cooldown displayed via a permanent boss bar above the player's hotbar, showing remaining seconds. Includes custom item crafting, particle effects, and sound cues for an immersive charging experience.
  • Add the Paper plugin entry point and build metadata.

  • Implement the named spear, staged charging, aimed fire ray, and cooldown.

  • Keep a live action-bar status bar for spear holders.

Added the Ember Spear plugin. Use /emberspear to get the spear. Right-click to charge; right-click again to release early for 2, 4, or 8 hearts of damage, or let it auto-fire at full charge after 3 seconds. It has a 20-second cooldown, with a live charge/cooldown bar above the hotbar.

Minecraft’s plugin events don’t expose when a mouse button is released, so a second right-click releases early; holding the initial click charges to full automatically.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 271
src/main/java/com/kodari/emberspear/EmberSpearPlugin.java
package com.kodari.emberspear;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
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.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;

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

public final class EmberSpearPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final String SPEAR_NAME = "§6§lEmber Spear";
    private static final long CHARGE_DURATION_MILLIS = 3_000L;
    private static final long COOLDOWN_MILLIS = 20_000L;
    private static final double RAY_RANGE = 40.0;

    private final Map<UUID, Charge> charges = new HashMap<>();
    private final Map<UUID, Long> cooldowns = new HashMap<>();

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);
        getCommand("emberspear").setExecutor(this);
        Bukkit.getScheduler().runTaskTimer(this, this::updateStatusBars, 0L, 2L);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can receive an Ember Spear.");
            return true;
        }

        ItemStack spear = createSpear();
        if (spear == null) {
            player.sendMessage("§cAn Ember Spear cannot be created on this server version.");
            return true;
        }

        Map<Integer, ItemStack> overflow = player.getInventory().addItem(spear);
        overflow.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
        player.sendMessage("§6You received an Ember Spear!");
        return true;
    }

    @EventHandler
    public void onSpearUse(PlayerInteractEvent event) {
        if (event.getHand() != EquipmentSlot.HAND
                || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)
                || !isSpear(event.getItem())) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        UUID playerId = player.getUniqueId();
        Charge activeCharge = charges.get(playerId);
        if (activeCharge != null) {
            if (activeCharge.stage > 0) {
                charges.remove(playerId);
                launchRay(player, activeCharge.stage);
            }
            return;
        }

        long now = System.currentTimeMillis();
        long cooldownEnd = cooldowns.getOrDefault(playerId, 0L);
        if (cooldownEnd > now) {
            return;
        }

        charges.put(playerId, new Charge(now));
        XSound.matchXSound("ENTITY_BLAZE_AMBIENT").ifPresent(sound -> sound.play(player));
    }

    private void updateStatusBars() {
        long now = System.currentTimeMillis();

        Iterator<Map.Entry<UUID, Charge>> chargeIterator = charges.entrySet().iterator();
        while (chargeIterator.hasNext()) {
            Map.Entry<UUID, Charge> entry = chargeIterator.next();
            Player player = Bukkit.getPlayer(entry.getKey());
            if (player == null) {
                chargeIterator.remove();
                continue;
            }

            Charge charge = entry.getValue();
            long elapsed = now - charge.startedAt;
            charge.stage = Math.min(3, (int) (elapsed / 1_000L));
            if (elapsed >= CHARGE_DURATION_MILLIS) {
                chargeIterator.remove();
                launchRay(player, 3);
            }
        }

        cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);

        for (Player player : Bukkit.getOnlinePlayers()) {
            UUID playerId = player.getUniqueId();
            Charge charge = charges.get(playerId);
            Long cooldownEnd = cooldowns.get(playerId);
            if (charge == null && cooldownEnd == null && !hasSpear(player)) {
                continue;
            }

            String stageOne = charge != null && charge.stage >= 1 ? "§6" : "§8";
            String stageTwo = charge != null && charge.stage >= 2 ? "§6" : "§8";
            String stageThree = charge != null && charge.stage >= 3 ? "§6" : "§8";
            String status;
            if (charge != null) {
                int secondsLeft = Math.max(0, (int) Math.ceil((CHARGE_DURATION_MILLIS - (now - charge.startedAt)) / 1_000.0));
                status = "§eCharging §f" + charge.stage + "/3 §8• §f" + secondsLeft + "s";
            } else if (cooldownEnd != null) {
                int secondsLeft = (int) Math.ceil((cooldownEnd - now) / 1_000.0);
                status = "§cCooldown §f" + secondsLeft + "s";
            } else {
                status = "§aReady";
            }

            String bar = "§6EMBER SPEAR §8[" + stageOne + "1 " + stageTwo + "2 " + stageThree + "3§8] §8» " + status;
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(bar));
        }
    }

    private void launchRay(Player player, int stage) {
        int hearts = switch (stage) {
            case 1 -> 2;
            case 2 -> 4;
            default -> 8;
        };

        Location eye = player.getEyeLocation();
        Vector direction = eye.getDirection().normalize();
        RayTraceResult result = player.getWorld().rayTraceEntities(
                eye,
                direction,
                RAY_RANGE,
                0.35,
                entity -> entity instanceof LivingEntity && !entity.getUniqueId().equals(player.getUniqueId())
        );
        double rayLength = result == null ? RAY_RANGE : eye.toVector().distance(result.getHitPosition());
        renderFireRay(eye, direction, rayLength);
        XSound.matchXSound("ENTITY_BLAZE_SHOOT").ifPresent(sound -> sound.play(player));

        if (result != null && result.getHitEntity() instanceof LivingEntity target) {
            target.damage(hearts * 2.0, player);
            target.setFireTicks(Math.max(target.getFireTicks(), 40));
            XSound.matchXSound("ENTITY_BLAZE_HURT").ifPresent(sound -> sound.play(target));
        }

        cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + COOLDOWN_MILLIS);
    }

    private void renderFireRay(Location eye, Vector direction, double length) {
        XParticle.of("FLAME").ifPresent(particle -> {
            for (double distance = 0.4; distance <= length; distance += 0.35) {
                Location point = eye.clone().add(direction.clone().multiply(distance));
                eye.getWorld().spawnParticle(particle.get(), point, 1, 0.02, 0.02, 0.02, 0.0);
            }
        });
    }

    private boolean hasSpear(Player player) {
        for (ItemStack item : player.getInventory().getContents()) {
            if (isSpear(item)) {
                return true;
            }
        }
        return false;
    }

    private boolean isSpear(ItemStack item) {
        if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
            return false;
        }
        ItemMeta meta = item.getItemMeta();
        return meta != null && SPEAR_NAME.equals(meta.getDisplayName());