-
Notifications
You must be signed in to change notification settings - Fork 0
F | Creating Custom Commands
Sartaj Singh edited this page May 31, 2025
·
2 revisions
- 1. Overview
- 2. Command System Architecture
- 3. Command Registration
- 4. Command Classes: Implementing CommandExecutor
- 5. Command Options and Features
- 6. Example Custom Commands
- 7. Troubleshooting & FAQ
- 8. See Also
FXItems supports the creation and dynamic registration of custom commands using the Bukkit/Spigot API, but with a modern registry-centric pattern. You can register commands at runtime, modularize your command logic, and integrate commands with custom items, foods, or advanced abilities.
- Commands are registered through the FXItems Registry API.
- Each command is implemented as a Java class that implements
CommandExecutor. - The registration system allows for dynamic, on-the-fly registration and unregistration—no plugin reload required.
- Commands can interact with FXItems systems, including item/food registries, utility APIs, cooldowns, and more.
To register a command, call the registry in your plugin's onEnable() or initialization logic:
Registry registry = new RegistryAPI();
registry.registerCommand(
this, // Plugin instance
"giveitem", // Command name (no slash)
GiveItemCommand.class // Your class (implements CommandExecutor)
);Parameters:
-
plugin: Your main plugin instance (typicallythis). -
name: Command name, no slash, all lowercase recommended. -
cmdClass: The class of your command (must implementCommandExecutorand have a public no-argument constructor).
If you have many commands, you can use a pattern similar to the auto-registration of items/behaviors:
- Place all command classes in a specific package.
- Write a reflection-based registrar (or use one if provided by FXItems) to auto-register all classes implementing
CommandExecutor.
Your command class must implement CommandExecutor and have a public no-argument constructor:
public class GiveItemCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("§cThis command is player-only.");
return true;
}
if (args.length < 1) {
player.sendMessage("§eUsage: /giveitem <itemid>");
return true;
}
String id = args[0].toLowerCase();
FXItemDefinition def = ItemRegistry.getItemDefinition(id);
if (def == null) {
player.sendMessage("§cUnknown item: " + id);
return true;
}
player.getInventory().addItem(ItemRegistry.getCustomItem(id));
player.sendMessage("§aYou received: " + def.getDisplayName());
return true;
}
}-
Tab Completion: Implement
TabCompleterfor suggestions. -
Subcommands: Parse
args[]for multi-level commands (e.g.,/fxitems reload). -
Permission Checks: Use
sender.hasPermission("yourplugin.cmd"). - Integration: Use FXItems APIs within commands (give items/foods, trigger effects, modify mana, etc.).
- Feedback: Use color codes and clear error messages for users.
- Dynamic Registration: Commands are available as soon as they're registered (no server reload needed).
- Multiple Commands: Register as many as you want; each must have a unique name.
- Integration: Commands can interact with FXItems content (give items, spawn foods, debug info, etc.).
- Advanced Usage: Register event listeners or schedule tasks from within command logic.
public class GetFoodCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("§cThis command is player-only.");
return true;
}
if (args.length < 1) {
player.sendMessage("§eUsage: /getfood <foodid>");
return true;
}
String id = args[0].toLowerCase();
FXFoodDefinition def = FoodRegistry.getFoodDefinition(id);
if (def == null) {
player.sendMessage("§cUnknown food: " + id);
return true;
}
player.getInventory().addItem(FoodRegistry.getCustomFood(id));
player.sendMessage("§aYou received: " + def.getDisplayName());
return true;
}
}public class ItemInfoCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length < 1) {
sender.sendMessage("§eUsage: /iteminfo <itemid>");
return true;
}
String id = args[0].toLowerCase();
FXItemDefinition def = ItemRegistry.getItemDefinition(id);
if (def == null) {
sender.sendMessage("§cUnknown item: " + id);
return true;
}
sender.sendMessage("§bItem: " + def.getDisplayName());
sender.sendMessage("§7Type: " + def.getMaterial().name());
sender.sendMessage("§7Rarity: " + (def.getRarity() != null ? def.getRarity().name() : "None"));
// Add more fields as needed
return true;
}
}-
“Command not found”:
- Confirm you registered it in
onEnable()and used the correct name. - Class must implement
CommandExecutorand have a public no-arg constructor.
- Confirm you registered it in
-
“Command does nothing”:
- Check for exceptions in the server log.
- Make sure your
onCommandreturnstrueand sends feedback to the user.
-
Tab completion not working:
- Implement
TabCompleterand register with Bukkit/Spigot if needed.
- Implement
-
Can I reload commands at runtime?
- Yes! Simply re-register with a new class instance.
- B | The Registry System: Items, Foods, Commands, Events
- C | Creating Custom Items
- D | Creating Custom Foods
- Q | FXItems API & Making Addons
- Home
- A | Getting Started & Plugin Overview
- B | The Registry System: Items, Foods, Commands, Events
- C | Creating Custom Items
- D | Creating Custom Foods
- E | Advanced: Custom Effects in FXItems
- F | Creating Custom Commands
- G | Creating Custom Events & Behaviors
- H | Utility: CooldownUtils
- I | Utility: CustomItemUtils
- J | Utility: EffectUtils
- K | Utility: ManaUtils
- L | Utility: OneTimeCraftUtils
- M | Utility: TeleportUtils
- N | Utility: ProjectileUtils
- O | Utility: GammaUtils
- P | Utility: EntitySizeUtils
- Q | FXItems API (com.noctify.API) & Making Addons
- R | Advanced Techniques, Gotchas & FAQ
- S | Auto-Registration System
- T | Migration Guide (v1.1 → v1.2+)
- U | Changelog