noahlyles0818
📎 1 file 2,103 chars
📄
pasted-code-1776495948291.txt
txt 74 lines
▶
Create a Minecraft plugin with the following specifications:
Plugin Name: CommandItem
Minecraft Version: 1.21.11 (Paper/Spigot)
Java Version: Java 21
Core Functionality
The plugin allows players to convert the item in their hand into an executable item that runs a command when clicked.
Command

/ci    [message]
Argument Details

The command to execute when the item is used
Must be wrapped in angle brackets
Example: 
Should run as console by default

rightclick → Executes on right-click
leftclick → Executes on left-click

disappear → Item is removed after use
keep → Item stays after use
[message] (optional)
Message sent only to the player when used
Example: You claimed your reward!
Example Usage

/ci  rightclick disappear You claimed your reward!
Behavior Requirements
The command converts the item in the player's main hand into a Command Item
Store data using PersistentDataContainer (NBT):
command
click type
consume type
message
Replace %player% with the player's name when executing
Interaction Logic
Detect:
PlayerInteractEvent
If item has stored data:
Check click type matches
Execute command from console
Send optional message (only to player)
Handle item:
disappear → remove 1 item
keep → do nothing
Additional Requirements
Prevent usage if player is in cooldown (optional but recommended)
Cancel event to prevent default behavior
Add colored feedback when creating item:
"Command item created successfully!"
Permissions

commanditem.use
commanditem.create
Code Requirements
Use modern Paper API (1.21.11)
Use Java 21 features where appropriate
Clean structure:
Main class
Command handler
Listener class
Utility class for item data
No deprecated methods
Optional Enhancements (if included)
Cooldown per item
Support MiniMessage formatting
Support multiple commands separated by ;
Output
Provide:
Full source code
plugin.yml
Clear package structure
Goal:
A lightweight, efficient plugin that allows creating clickable command-executing items with flexible behavior. :::
Kodari AI
KodariApr 18 at 7:06 am 6,320 (v2)
CommandItem
A lightweight Paper plugin that converts held items into executable command items via /ci. Players can bind commands to items with configurable click types (left/right), consume behavior (disappear/keep), and optional player messages. Commands run as console with %player% placeholder support. Data is stored using PersistentDataContainer. Includes per-item cooldown protection and MiniMessage formatting support. Permissions: commanditem.create and commanditem.use.
  • Main class + command handler + listener + PDC utility + cooldown manager + ColorUtil

  • Command parsing extracts <command> from angle brackets then parses remaining args

  • MiniMessage for all formatting since user explicitly requested it and project is Paper 1.21

Here’s what was built:

  • CommandItemPlugin — main class, wires together CooldownManager, the /ci command, and the interact listener

  • CreateCommand — parses /ci <cmd> clicktype consumetype [message], extracts the command from angle brackets, validates args, writes PDC data to the held item

  • ItemListener — handles PlayerInteractEvent, checks click type match, runs commands via console (supports multiple commands split by ;), sends the optional message, handles item removal, and enforces a 3-second cooldown

  • ItemDataUtil — all PDC read/write logic using a record CommandItemData(...) for clean data passing

  • CooldownManager — per-player per-item cooldown using a Map<String, Long>

  • ColorUtil — MiniMessage deserialization + plain-text stripping for logger output

noahlyles0818

Add /ci reload and /ci help and also /ci remove - removes the command action from the held item

Kodari AI
KodariApr 18 at 7:11 am 5,040 (v2)
  • CreateCommand handles all routing — check args[0] for help, reload, remove before falling through to create logic

  • ItemDataUtil needs a removeData method to strip all PDC keys from an item

  • plugin.yml needs the commanditem.reload permission added

Three subcommands added:

  • /ci help — formatted help menu showing all usage, placeholders, and multi-command syntax; also shown when /ci is run with too few args

  • /ci reload — reloads config, requires commanditem.reload (op by default)

  • /ci remove — strips all PDC keys from the held item via the new ItemDataUtil.removeData(), requires commanditem.create; errors if the item isn’t a command item

  • Tab completion on args[0] suggests help, reload, remove

noahlyles0818

Add item.yml where shows all item which got commands and more things. We can edit things from there too.

Kodari AI
KodariApr 18 at 7:14 am 12,626 (v2)

Let me read all existing files before planning anything.

Now I have a complete picture. Here’s my plan:

  • New ItemEntry (Lombok @Data) — data class holding all item config fields

  • New ItemsManager — loads/saves items.yml, holds a Map<String, ItemEntry> keyed by UUID item_id

  • Patch ItemDataUtil — store ci_id in PDC on applyData; add getItemId() helper

  • Patch CommandItemPlugin — add ItemsManager field + @Getter

  • Patch CreateCommand — generate UUID on create → register in ItemsManager; add /ci list; /ci remove also unregisters; /ci reload also reloads items.yml

  • Patch ItemListener — look up live data from ItemsManager by item_id (so edits in items.yml take effect after reload), fallback to PDC if not found

Here’s what was added and how items.yml works end-to-end:


items.yml — structure

Every time you run /ci <command> ... on a held item, a UUID entry is written:

YAML
items:
  a3f1bc7d-...:
    item-type: DIAMOND_SWORD
    item-name: Sword of Giving
    command: give %player% diamond 1
    click-type: rightclick
    consume-type: keep
    message: You claimed your reward!
    cooldown: 3
    created-by: Steve
    created-at: 2024-06-01 14:32:00

You can freely edit any field (command, click-type, consume-type, message, cooldown, etc.) and run /ci reload — the listener will immediately read the updated values on next use, without touching the physical item.


New subcommand: /ci list

Shows all registered items with index, item type/name, short ID, full config, and a reminder to edit items.yml and reload.

Updated /ci reload

Now reloads both config.yml and items.yml in one call.

Updated /ci remove

Strips PDC data and removes the entry from items.yml + saves the file.

noahlyles0818

The command is only runnable by op?

Kodari AI
KodariApr 18 at 7:15 am 3,995 (v2)

The default: op in plugin.yml is what restricts everything to ops. The fix is straightforward — change defaults so normal players can create/use, while reload stays op-only.

Also remove the redundant permission check at the top of onCommand since plugin.yml already gates the /ci command itself with commanditem.create:

What changed:

  • commanditem.create — op → true (all players can create/remove command items)

  • commanditem.list — op → true (all players can use /ci list)

  • commanditem.reload — stays op only

  • Removed the now-redundant manual hasPermission("commanditem.create") check in the create flow (Bukkit already blocks the command if the player lacks it, per plugin.yml)

noahlyles0818

Add hex and minecraft colour code login like. &a &b &l &#FFFFFF

Kodari AI
KodariApr 18 at 7:27 am 100 (v2)

The generation was stopped before any content could be produced.

noahlyles0818

Add hex and minecraft colour code logic like. &a &b &l &#FFFFFF

Kodari AI
KodariApr 18 at 7:27 am 1,775 (v2)

Simple fix — translate &a, &l, &#FFFFFF etc. into MiniMessage tags before passing to MiniMessage.deserialize().

[Generation stopped]

noahlyles0818

Add hex and minecraft colour code logic like. &a &b &l &#FFFFFF. Also add a sound effect on rightclick or left click. A more likely claim sound of minecraft.

Kodari AI
KodariApr 18 at 7:29 am 12,378 (v2)
  • ColorUtil needs a pre-processor that converts &a/&#FFFFFF etc. into MiniMessage tags before MINI.deserialize()

  • ItemListener plays a XSound claim sound on successful use

  • Sound config key added to ItemEntry + ItemsManager so it can be set per-item in items.yml

Now find where ItemEntry is constructed in CreateCommand and add the default sound field:

Now I have everything. Here’s the plan:

  • ColorUtil — add a pre-processor that converts &a→<green>, &#RRGGBB→<#RRGGBB>, &l→<bold>, etc. before MiniMessage deserializes

  • ItemEntry + ItemsManager — add a sound field so each item can have its own claim sound in items.yml

  • CreateCommand — pass default sound into ItemEntry

  • ItemListener — play the item’s sound via XSound on successful use

What changed:

  • ColorUtil.translate() — runs two passes before MiniMessage: &#RRGGBB → <#RRGGBB> via regex, then all &0–&f/&l/&o/&n/&m/&k/&r (upper and lower case) → their MiniMessage equivalents. Now any string passed to ColorUtil.colorize() supports both formats alongside native MiniMessage tags

  • ItemEntry + ItemsManager — new sound field (defaults to UI_TOAST_CHALLENGE_COMPLETE) stored and read from items.yml under sound: — change it per-item and /ci reload

  • ItemListener — plays the resolved XSound on the player after a successful command execution

noahlyles0818

Don't give everyone the colour code books only op. But once only. Never do again

Kodari AI
KodariApr 18 at 7:39 am 9,662 (v2)

Plan:

  • Create BookGiftManager — tracks which op UUIDs already got the book, persisted in data/given-book.yml

  • Create PlayerJoinListener — on join, if player is op + hasn’t received book → give written book with full color code reference → mark as given

  • Register both in CommandItemPlugin

What was added:

  • BookGiftManager — persists op UUIDs who have already received the book in plugins/CommandItem/data/given-book.yml. Once written, that UUID is never gifted again, even across restarts

  • PlayerJoinListener — fires on join, checks isOp() + hasReceived() — both must pass. Opens a 3-page Adventure Book directly (no item given, no inventory clutter) with full color code reference

  • Page 1 — &0–&9 color codes

  • Page 2 — &a–&f colors + &l &o &n &m &k &r formatting codes

  • Page 3 — hex &#RRGGBB explanation with 6 live hex examples, each rendered in its own color

noahlyles0818

Make it /ci <command> rightclick/leftclick disappear/keep sound-true/false message

Kodari AI
KodariApr 18 at 11:47 am 9,837 (v2)
  • Add soundEnabled boolean to ItemEntry + ItemsManager (save/load sound-enabled)

  • Parse sound-true/sound-false as the 4th positional arg in CreateCommand

  • ItemListener only plays sound when soundEnabled is true

  • Update tab completion and help text

Also need to fix the duplicate sound block in ItemListener — the first patch already removed the old block, but let me verify the file only has one sound section now. The two patches together replace both the old single block that had the duplicate logic. Done.

What changed:

  • Command syntax is now /ci <command> rightclick|leftclick disappear|keep sound-true|sound-false [message]

  • sound-true plays UI_TOAST_CHALLENGE_COMPLETE on use; sound-false silences it entirely

  • ItemEntry + ItemsManager persist sound-enabled: true/false in items.yml so it survives reloads

  • Help text, usage, and confirmation message all reflect the new argument

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
12 961
src/main/java/com/commanditem/CommandItemPlugin.java
package com.commanditem;

import com.commanditem.command.CreateCommand;
import com.commanditem.listener.ItemListener;
import com.commanditem.listener.PlayerJoinListener;
import com.commanditem.util.BookGiftManager;
import com.commanditem.util.CooldownManager;
import com.commanditem.util.ColorUtil;
import com.commanditem.util.ItemsManager;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;

public class CommandItemPlugin extends JavaPlugin {

    @Getter
    private CooldownManager cooldownManager;

    @Getter
    private ItemsManager itemsManager;

    @Getter
    private BookGiftManager bookGiftManager;

    @Override
    public void onEnable() {
        this.cooldownManager = new CooldownManager();
        this.itemsManager = new ItemsManager(this);
        this.bookGiftManager = new BookGiftManager(this);

        CreateCommand createCommand = new CreateCommand(this);
        var cmd = getCommand("ci");
        cmd.setExecutor(createCommand);
        cmd.setTabCompleter(createCommand);
        getServer().getPluginManager().registerEvents(new ItemListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);

        getLogger().info(ColorUtil.strip("<green>CommandItem enabled successfully."));
    }

    @Override
    public void onDisable() {
        getLogger().info(ColorUtil.strip("<red>CommandItem disabled."));
    }
}