Lorenzo Hogan

can you code a plugin that turns the wardens sonic boom in to an item that gets used as a fishing rod

Kodari AI
KodariOct 8 at 11:49 pm 1
SonicBoomFishingRod
A Minecraft plugin that transforms the Warden's Sonic Boom attack into a collectible item that functions as a fishing rod. When a Warden uses its Sonic Boom ability, there is a configurable chance to drop a Sonic Boom item. This item can be picked up and used like a fishing rod, but instead of casting a fishing line, it launches a powerful sonic wave in the direction the player is facing. The sonic wave damages entities in its path, applies the deafened effect, and can be used repeatedly with a cooldown system. The plugin includes configurable damage values, cooldown durations, drop chances, and range settings through a config file. It also provides visual and audio feedback when the sonic wave is fired, making it an exciting and unique gameplay mechanic that combines fishing mechanics with combat utility.
  1. Add the Paper plugin setup and /sonicrod command.

  2. Make a tagged fishing rod fire a sonic-boom beam on right-click, damaging and knocking back the nearest target.

Added the SonicRod plugin. Run /sonicrod to get a tagged fishing rod; right-clicking fires a sonic-boom beam up to 24 blocks, damaging the nearest target for 10 damage and knocking it back. The beam stops at blocks and has a 1.5-second cooldown.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 204
src/main/java/com/kodari/sonicrod/SonicRodPlugin.java
package com.kodari.sonicrod;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
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.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import org.bukkit.FluidCollisionMode;

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

public final class SonicRodPlugin extends JavaPlugin implements Listener {
    private static final double RANGE = 24.0;
    private static final double DAMAGE = 10.0;
    private static final long COOLDOWN_MILLIS = 1500L;

    private final Map<UUID, Long> cooldowns = new HashMap<>();
    private NamespacedKey rodKey;

    @Override
    public void onEnable() {
        rodKey = new NamespacedKey(this, "sonic_rod");
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("sonicrod").setExecutor((sender, command, label, args) -> {
            if (!(sender instanceof Player player)) {
                sender.sendMessage("Only players can receive the Sonic Rod.");
                return true;
            }

            ItemStack rod = createRod();
            if (rod == null) {
                player.sendMessage("Could not create the fishing rod item.");
                return true;
            }

            player.getInventory().addItem(rod).values().forEach(item ->
                    player.getWorld().dropItemNaturally(player.getLocation(), item));
            player.sendMessage("§5You received the Warden Sonic Rod.");
            return true;
        });
    }

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

        Player player = event.getPlayer();
        ItemStack item = event.getItem();
        if (!isSonicRod(item)) {
            return;
        }

        event.setCancelled(true);
        long now = System.currentTimeMillis();
        long nextUse = cooldowns.getOrDefault(player.getUniqueId(), 0L);
        if (now < nextUse) {
            return;
        }

        cooldowns.put(player.getUniqueId(), now + COOLDOWN_MILLIS);
        fireSonicBoom(player);
    }

    private ItemStack createRod() {
        ItemStack rod = XMaterial.matchXMaterial("FISHING_ROD")
                .map(XMaterial::parseItem)
                .orElse(null);
        if (rod == null) {
            return null;
        }

        ItemMeta meta = rod.getItemMeta();
        if (meta == null) {
            return null;
        }

        meta.setDisplayName("§5Warden Sonic Rod");
        meta.setLore(java.util.List.of("§7Right-click to unleash a sonic boom."));
        meta.getPersistentDataContainer().set(rodKey, PersistentDataType.BYTE, (byte) 1);
        rod.setItemMeta(meta);
        return rod;
    }

    private boolean isSonicRod(ItemStack item) {
        if (item == null || !item.hasItemMeta()) {
            return false;
        }
        return item.getItemMeta().getPersistentDataContainer().has(rodKey, PersistentDataType.BYTE);
    }

    private void fireSonicBoom(Player player) {
        Location start = player.getEyeLocation();
        Vector direction = start.getDirection().normalize();
        double beamLength = RANGE;

        RayTraceResult blockHit = player.getWorld().rayTraceBlocks(
                start, direction, RANGE, FluidCollisionMode.NEVER, true);
        if (blockHit != null && blockHit.getHitPosition() != null) {
            beamLength = start.toVector().distance(blockHit.getHitPosition());
        }

        Entity target = findTarget(player, start, direction, beamLength);
        if (target != null) {
            beamLength = start.distance(target.getLocation().add(0, target.getHeight() * 0.5, 0));
            LivingEntity livingTarget = (LivingEntity) target;
            livingTarget.damage(DAMAGE, player);
            Vector knockback = direction.clone().multiply(1.25).setY(0.3);
            livingTarget.setVelocity(livingTarget.getVelocity().add(knockback));
        }

        double effectLength = beamLength;
        XParticle.of("SONIC_BOOM").ifPresent(particle -> {
            for (double distance = 0.5; distance <= effectLength; distance += 0.5) {
                Location point = start.clone().add(direction.clone().multiply(distance));
                player.getWorld().spawnParticle(particle.get(), point, 1, 0, 0, 0, 0);
            }
        });
        XSound.matchXSound("ENTITY_WARDEN_SONIC_BOOM").ifPresent(sound -> sound.play(player));
    }

    private Entity findTarget(Player player, Location start, Vector direction, double maxDistance) {
        Entity nearest = null;
        double nearestDistance = maxDistance;

        for (Entity entity : player.getWorld().getNearbyEntities(start, RANGE, RANGE, RANGE)) {
            if (!(entity instanceof LivingEntity) || entity == player || entity.isDead()) {
                continue;
            }

            Location center = entity.getLocation().add(0, entity.getHeight() * 0.5, 0);
            Vector offset = center.toVector().subtract(start.toVector());
            double distanceAlongBeam = offset.dot(direction);
            if (distanceAlongBeam <= 0 || distanceAlongBeam > nearestDistance) {
                continue;
            }

            Vector closestPoint = start.toVector().add(direction.clone().multiply(distanceAlongBeam));
            if (center.toVector().distanceSquared(closestPoint) <= 1.0) {
                nearest = entity;
                nearestDistance = distanceAlongBeam;
            }
        }
        return nearest;
    }
}