Bartek
📎 1 file 1,024 chars
📄
pasted-code-1793716444659.txt
txt 24 lines
▶
Create a Minecraft plugin for **Paper 1.21.4**.

Plugin requirements:

1. When a player joins the server, automatically give them a Compass.
2. Place the Compass in the **middle hotbar slot (slot 5)**.
3. The Compass must be completely unbreakable and cannot be dropped, moved, swapped, or removed by players.
4. Right-clicking the Compass opens a custom GUI menu.
5. The GUI must contain exactly **ONE game mode**.
6. The game mode must use `RED_DYE` as its icon.
7. The game mode name must be `AnarchiaFFA`.
8. When a player clicks the RED_DYE, execute `/server anarchiaffa`.
9. The Compass must be restored after death and respawning.
10. Prevent players from moving or stealing items from the GUI.

**Configuration (config.yml):**
- Only ONE game mode.
- Game mode icon: `RED_DYE`.
- Game mode name: `AnarchiaFFA`.
- Command: `server anarchiaffa`.

Make the plugin fully compatible with Paper 1.21.4 and Java 21.

Keep the plugin lightweight, clean and optimized. Do not add any other game modes or unnecessary features.
Kodari AI
KodariNov 3 at 2:35 pm 1
AnarchiaFFACompass
A lightweight Paper 1.21.4 plugin that grants players an unbreakable, immovable Compass in hotbar slot 5 on join. Right-clicking opens a GUI menu with a single RED_DYE game mode icon labeled AnarchiaFFA. Clicking executes /server anarchiaffa. The Compass is restored on respawn, and GUI interactions are fully locked to prevent item theft or movement. Config-driven with MiniMessage support for modern text formatting.

I’ll create a minimal Paper plugin with the protected slot-5 compass, one config-driven menu entry, and protections for inventory, drop, hand-swap, and death/respawn handling.

Implemented the Paper plugin with a protected, unbreakable compass in the fifth hotbar position (inventory index 4). Right-click opens a menu containing only the configured RED_DYE entry, AnarchiaFFA, which runs server anarchiaffa.

The compass is protected against dropping, inventory movement, hand swaps, and death drops, and is restored on join and respawn. Configuration is in src/main/resources/config.yml.

Bartek

Fix the command execution in my Minecraft Paper 1.21.4 CompassMiniGames plugin.

IMPORTANT:

  • When a player clicks the RED_DYE in the GUI, execute /server anarchiaffa.
  • Do NOT execute the command as the player.
  • Instead, dispatch the command through the server console using Bukkit.dispatchCommand().
  • Execute: server anarchiaffa
  • Do not include the / at the beginning when dispatching the command.
  • Close the GUI after clicking the item.
  • Keep the Compass in hotbar slot 5.
  • Keep all existing GUI features and protection.
  • Do not change the RED_DYE icon or add any extra game modes.

Make sure the command is executed correctly and handle any errors.

Kodari AI
KodariNov 3 at 2:48 pm 1

Plan:

  • Dispatch the configured command as the server console, without a leading slash.

  • Close the menu on the RED_DYE click and report failed/exceptional dispatches.

  • Leave the compass placement, icon, GUI contents, and protections unchanged.

Updated AnarchiaFFAPlugin: clicking RED_DYE now closes the GUI and dispatches server anarchiaffa through Bukkit.getConsoleSender() without a leading slash. Empty commands, failed dispatches, and runtime errors are logged and reported to the player. The compass slot, menu item, and protections are unchanged.

Kodari can make mistakes. Review important code before using.
Files
src
Code
3 230
src/main/java/com/kodari/anarchiaffa/AnarchiaFFAPlugin.java
package com.kodari.anarchiaffa;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
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.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Level;

public final class AnarchiaFFAPlugin extends JavaPlugin implements Listener {
    private static final int COMPASS_SLOT = 4;
    private static final int GAME_MODE_SLOT = 4;

    private NamespacedKey compassKey;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        compassKey = new NamespacedKey(this, "protected_compass");
        Bukkit.getPluginManager().registerEvents(this, this);
        for (Player player : Bukkit.getOnlinePlayers()) {
            ensureCompass(player);
        }
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        ensureCompass(event.getPlayer());
    }

    @EventHandler
    public void onRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        Bukkit.getScheduler().runTask(this, () -> ensureCompass(player));
    }

    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
        event.getDrops().removeIf(this::isProtectedCompass);
    }

    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
        Action action = event.getAction();
        if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
                && isProtectedCompass(event.getItem())) {
            event.setCancelled(true);
            openGameModeMenu(event.getPlayer());
        }
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!(event.getWhoClicked() instanceof Player player)) {
            return;
        }

        if (event.getView().getTopInventory().getHolder() instanceof GameModeMenuHolder) {
            event.setCancelled(true);
            if (event.getClickedInventory() == event.getView().getTopInventory()
                    && event.getSlot() == GAME_MODE_SLOT) {
                String command = getConfig().getString("game-mode.command", "server anarchiaffa");
                if (command != null && command.startsWith("/")) {
                    command = command.substring(1);
                }
                player.closeInventory();
                if (command == null || command.isBlank()) {
                    getLogger().severe("The configured game-mode command is empty.");
                    player.sendMessage(ChatColor.RED + "Unable to join the game mode: command is not configured.");
                    return;
                }
                try {
                    if (!Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)) {
                        getLogger().warning("The game-mode command could not be dispatched: " + command);
                        player.sendMessage(ChatColor.RED + "Unable to join the game mode.");
                    }
                } catch (RuntimeException exception) {
                    getLogger().log(Level.SEVERE, "Failed to dispatch the game-mode command: " + command, exception);
                    player.sendMessage(ChatColor.RED + "Unable to join the game mode.");
                }
            }
            return;
        }

        PlayerInventory inventory = player.getInventory();
        boolean protectedSlotClicked = event.getClickedInventory() == inventory
                && event.getSlot() == COMPASS_SLOT;
        boolean protectedHotbarSwap = event.getHotbarButton() == COMPASS_SLOT;
        boolean protectedItemMoved = isProtectedCompass(event.getCurrentItem())
                || isProtectedCompass(event.getCursor());
        boolean protectedItemCollected = event.getAction() == InventoryAction.COLLECT_TO_CURSOR
                && hasProtectedCompass(inventory);

        if (protectedSlotClicked || protectedHotbarSwap || protectedItemMoved || protectedItemCollected) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onInventoryDrag(InventoryDragEvent event) {
        if (!(event.getWhoClicked() instanceof Player player)) {
            return;
        }
        if (event.getView().getTopInventory().getHolder() instanceof GameModeMenuHolder) {
            event.setCancelled(true);
            return;
        }

        if (isProtectedCompass(event.getOldCursor())) {
            event.setCancelled(true);
            return;
        }

        for (int rawSlot : event.getRawSlots()) {
            if (event.getView().getInventory(rawSlot) == player.getInventory()
                    && event.getView().convertSlot(rawSlot) == COMPASS_SLOT) {
                event.setCancelled(true);
                return;
            }
        }
    }

    @EventHandler
    public void onDrop(PlayerDropItemEvent event) {
        if (isProtectedCompass(event.getItemDrop().getItemStack())) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onSwapHands(PlayerSwapHandItemsEvent event) {
        if (isProtectedCompass(event.getMainHandItem()) || isProtectedCompass(event.getOffHandItem())) {
            event.setCancelled(true);
        }
    }

    private void ensureCompass(Player player) {
        PlayerInventory inventory = player.getInventory();
        for (int slot = 0; slot < inventory.getSize(); slot++) {
            if (isProtectedCompass(inventory.getItem(slot))) {
                inventory.setItem(slot, null);
            }
        }
        inventory.setItem(COMPASS_SLOT, createCompass());
    }

    private ItemStack createCompass() {
        ItemStack compass = XMaterial.matchXMaterial("COMPASS")
                .map(XMaterial::parseItem)
                .orElseThrow(() -> new IllegalStateException("COMPASS is unavailable"));
        ItemMeta meta = compass.getItemMeta();
        meta.setUnbreakable(true);
        meta.getPersistentDataContainer().set(compassKey, PersistentDataType.BYTE, (byte) 1);
        compass.setItemMeta(meta);
        return compass;
    }

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

    private boolean hasProtectedCompass(PlayerInventory inventory) {
        for (ItemStack item : inventory.getContents()) {
            if (isProtectedCompass(item)) {
                return true;
            }
        }
        return false;
    }

    private void openGameModeMenu(Player player) {
        GameModeMenuHolder holder = new GameModeMenuHolder();
        String title = ChatColor.translateAlternateColorCodes('&', getConfig().getString("menu-title", "&8Game Modes"));
        Inventory menu = Bukkit.createInventory(holder, 9, title);
        holder.inventory = menu;

        String iconName = getConfig().getString("game-mode.icon", "RED_DYE");