Akshat Raj

Compile it

Kodari AI
KodariNov 13 at 7:39 am 1

To compile the project:

  1. On desktop, click Compile in the top bar. On mobile, tap ⋮ → Compile Project.

  2. Click Compile in the window.

  3. Once it succeeds, click Download on the build or Download JAR in the chat.

If it fails, click Auto-fix in the chat or Fix in the build window.

Kodari can make mistakes. Review important code before using.
Files
src
pom.xml
Code
4 364
src/main/java/me/orbitalstrikecannon/OrbitalStrikeCannon.java
package me.orbitalstrikecannon;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class OrbitalStrikeCannon extends JavaPlugin implements Listener, TabExecutor {

    private static final String PERMISSION = "orbitalcannon.use";

    private NamespacedKey cannonKey;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        cannonKey = new NamespacedKey(this, "orbital_cannon");

        getServer().getPluginManager().registerEvents(this, this);

        PluginCommand cmd = getCommand("orbitalcannon");
        if (cmd != null) {
            cmd.setExecutor(this);
            cmd.setTabCompleter(this);
        }
    }

    // ------------------------------------------------------------------
    // Command: /orbitalcannon [player]
    // ------------------------------------------------------------------

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission(PERMISSION)) {
            sender.sendMessage(Component.text("You don't have permission to do that.", NamedTextColor.RED));
            return true;
        }

        Player target;
        if (args.length >= 1) {
            target = Bukkit.getPlayerExact(args[0]);
            if (target == null) {
                sender.sendMessage(Component.text("Player not found: " + args[0], NamedTextColor.RED));
                return true;
            }
        } else if (sender instanceof Player self) {
            target = self;
        } else {
            sender.sendMessage(Component.text("Usage: /orbitalcannon <player>", NamedTextColor.RED));
            return true;
        }

        Map<Integer, ItemStack> leftover = target.getInventory().addItem(createCannon());
        for (ItemStack extra : leftover.values()) {
            target.getWorld().dropItemNaturally(target.getLocation(), extra);
        }

        target.sendMessage(Component.text("You received the Orbital Strike Cannon.", NamedTextColor.RED));
        if (sender != target) {
            sender.sendMessage(Component.text("Gave the Orbital Strike Cannon to " + target.getName() + ".", NamedTextColor.GREEN));
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        List<String> result = new ArrayList<>();
        if (args.length == 1 && sender.hasPermission(PERMISSION)) {
            String typed = args[0].toLowerCase();
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (p.getName().toLowerCase().startsWith(typed)) {
                    result.add(p.getName());
                }
            }
        }
        return result;
    }

    // ------------------------------------------------------------------
    // The cannon item: a fishing rod with exactly 1 durability left
    // ------------------------------------------------------------------

    private ItemStack createCannon() {
        ItemStack item = new ItemStack(Material.FISHING_ROD);
        ItemMeta meta = item.getItemMeta();

        meta.displayName(Component.text("Orbital Strike Cannon", NamedTextColor.RED, TextDecoration.BOLD)
                .decoration(TextDecoration.ITALIC, false));
        meta.lore(List.of(
                Component.text("Right-click to call down the strike.", NamedTextColor.GRAY)
                        .decoration(TextDecoration.ITALIC, false),
                Component.text("One use only.", NamedTextColor.DARK_RED)
                        .decoration(TextDecoration.ITALIC, false)
        ));
        meta.setEnchantmentGlintOverride(true);
        meta.getPersistentDataContainer().set(cannonKey, PersistentDataType.BYTE, (byte) 1);

        if (meta instanceof Damageable damageable) {
            // 1 durability left = max durability - 1 damage
            damageable.setDamage(Material.FISHING_ROD.getMaxDurability() - 1);
        }

        item.setItemMeta(meta);
        return item;
    }

    private boolean isCannon(ItemStack item) {
        if (item == null || item.getType() != Material.FISHING_ROD || !item.hasItemMeta()) {
            return false;
        }
        return item.getItemMeta().getPersistentDataContainer().has(cannonKey, PersistentDataType.BYTE);
    }

    // ------------------------------------------------------------------
    // Using the cannon
    // ------------------------------------------------------------------

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onUse(PlayerInteractEvent event) {
        Action action = event.getAction();
        if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        EquipmentSlot hand = event.getHand();
        if (hand == null) {
            return;
        }

        ItemStack item = event.getItem();
        if (!isCannon(item)) {
            return;
        }

        // Stop the rod from actually casting a hook.
        event.setCancelled(true);

        Player player = event.getPlayer();
        if (!player.hasPermission(PERMISSION)) {
            player.sendMessage(Component.text("You can't use the Orbital Strike Cannon.", NamedTextColor.RED));
            return;
        }

        // The rod breaks (its last 1 durability is used up).
        player.getInventory().setItem(hand, null);
        playBreakEffects(player);

        fire(player);
    }

    private void playBreakEffects(Player player) {
        Location eye = player.getEyeLocation();
        World world = player.getWorld();

        // Raw sound keys so this works whichever name the game version uses.
        world.playSound(eye, "minecraft:entity.item.break", 1.0f, 1.0f);
        world.playSound(eye, "minecraft:item.break", 1.0f, 1.0f);

        world.spawnParticle(Particle.ITEM,
                eye.clone().add(eye.getDirection().multiply(0.6)),
                14, 0.15, 0.15, 0.15, 0.05,
                new ItemStack(Material.FISHING_ROD));
    }

    // ------------------------------------------------------------------
    // The strike: concentric rings of primed TNT around the player
    // ------------------------------------------------------------------

    private void fire(Player player) {
        FileConfiguration cfg = getConfig();