Zaiden Fontain

make me a 1.21.11 spigot plugin that adds an item called Phantom and its a phantom membrane that has a cooldown of 20 minutes and when you right click "Phantom" it puts the person in specatator for 20 seconds

Kodari AI
KodariSep 5 at 1:23 am 1
PhantomCloak
A Spigot 1.21.11 plugin that adds a custom item called "Phantom" crafted from a phantom membrane. When a player right-clicks the Phantom, they enter spectator mode for 20 seconds. The item has a 20-minute cooldown per player, tracked with persistent metadata. The plugin includes custom item registration, cooldown management with visual feedback, and automatic restoration to survival mode after the effect ends.

Created the Spigot plugin with:

  • Phantom item using a Phantom Membrane

  • Right-click activation

  • 20-minute cooldown

  • 20 seconds of spectator mode

  • Automatic restoration of the player’s previous game mode

  • /phantom command to receive the item

  • phantom.give permission, defaulting to operators

  • Item identification via persistent data, preventing renamed membranes from working as Phantom items

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 201
src/main/java/com/kodari/phantom/PhantomPlugin.java
package com.kodari.phantom;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

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

public final class PhantomPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final long COOLDOWN_MILLIS = 20L * 60L * 1000L;
    private static final long SPECTATOR_TICKS = 20L * 20L;

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

    @Override
    public void onEnable() {
        phantomKey = new NamespacedKey(this, "phantom_item");
        Bukkit.getPluginManager().registerEvents(this, this);

        if (getCommand("phantom") != null) {
            getCommand("phantom").setExecutor(this);
        }
    }

    @Override
    public void onDisable() {
        for (Map.Entry<UUID, GameMode> entry : activeSpectatorModes.entrySet()) {
            Player player = Bukkit.getPlayer(entry.getKey());
            if (player != null && player.isOnline() && player.getGameMode() == GameMode.SPECTATOR) {
                player.setGameMode(entry.getValue());
            }
        }
        activeSpectatorModes.clear();
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (!event.getAction().name().startsWith("RIGHT_CLICK") || !isPhantom(event.getItem())) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        UUID uniqueId = player.getUniqueId();

        if (activeSpectatorModes.containsKey(uniqueId)) {
            player.sendMessage(ChatColor.RED + "You are already using Phantom.");
            return;
        }

        if (player.getGameMode() == GameMode.SPECTATOR) {
            player.sendMessage(ChatColor.RED + "You are already in spectator mode.");
            return;
        }

        long remaining = cooldowns.getOrDefault(uniqueId, 0L) - System.currentTimeMillis();
        if (remaining > 0) {
            player.sendMessage(ChatColor.RED + "Phantom is on cooldown for " + formatDuration(remaining) + ".");
            return;
        }

        cooldowns.put(uniqueId, System.currentTimeMillis() + COOLDOWN_MILLIS);
        activeSpectatorModes.put(uniqueId, player.getGameMode());
        player.setGameMode(GameMode.SPECTATOR);
        player.sendMessage(ChatColor.LIGHT_PURPLE + "Phantom activated. You are invisible for 20 seconds.");

        Bukkit.getScheduler().runTaskLater(this, () -> endSpectator(uniqueId), SPECTATOR_TICKS);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        restorePlayer(event.getPlayer());
    }

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

        Map<Integer, ItemStack> leftovers = player.getInventory().addItem(createPhantom());
        for (ItemStack leftover : leftovers.values()) {
            player.getWorld().dropItemNaturally(player.getLocation(), leftover);
        }
        player.sendMessage(ChatColor.LIGHT_PURPLE + "You received a Phantom.");
        return true;
    }

    private ItemStack createPhantom() {
        Material membrane = Material.matchMaterial("PHANTOM_MEMBRANE");
        if (membrane == null) {
            throw new IllegalStateException("PHANTOM_MEMBRANE is not available on this server.");
        }

        ItemStack item = new ItemStack(membrane);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Phantom");
        meta.getPersistentDataContainer().set(phantomKey, PersistentDataType.BYTE, (byte) 1);
        item.setItemMeta(meta);
        return item;
    }

    private boolean isPhantom(ItemStack item) {
        if (item == null || item.getType() != Material.matchMaterial("PHANTOM_MEMBRANE") || !item.hasItemMeta()) {
            return false;
        }
        return item.getItemMeta().getPersistentDataContainer().has(phantomKey, PersistentDataType.BYTE);
    }

    private void endSpectator(UUID uniqueId) {
        if (!activeSpectatorModes.containsKey(uniqueId)) {
            return;
        }

        Player player = Bukkit.getPlayer(uniqueId);
        if (player != null && player.isOnline()) {
            restorePlayer(player);
            player.sendMessage(ChatColor.LIGHT_PURPLE + "Phantom has worn off.");
        } else {
            activeSpectatorModes.remove(uniqueId);
        }
    }

    private void restorePlayer(Player player) {
        GameMode originalMode = activeSpectatorModes.remove(player.getUniqueId());
        if (originalMode != null && player.getGameMode() == GameMode.SPECTATOR) {
            player.setGameMode(originalMode);
        }
    }

    private String formatDuration(long milliseconds) {
        long totalSeconds = (milliseconds + 999L) / 1000L;
        long minutes = totalSeconds / 60L;
        long seconds = totalSeconds % 60L;
        return minutes + "m " + seconds + "s";
    }
}