Skip to content

Custom Handling System

Traqueur edited this page Feb 27, 2025 · 4 revisions

Introduction

The MessageHandler interface in CommandsAPI allows you to define custom messages for various command-related events, such as lacking permissions, missing arguments, and unrecognized arguments. This page will guide you through the process of creating your own message handling system by overriding the default messages.

Thanks to Robotv2 for the idea.

MessageHandler Interface

The MessageHandler interface provides methods to retrieve messages for different scenarios:

package fr.traqueur.commands.api.lang;

public interface MessageHandler {

    String getNoPermissionMessage();

    String getOnlyInGameMessage();

    String getArgNotRecognized();

    String getRequirementMessage();
}

Creating a Custom MessageHandler

To override the default messages, you need to implement the MessageHandler interface and provide your own versions of the message methods.

Step-by-Step Guide

  1. Create a new class that implements MessageHandler:
public class CustomMessagehandler extends MessageHandler {

    @Override
    public String getNoPermissionMessage() {
        return /* your implementation */;
    }

    @Override
    public String getOnlyInGameMessage() {
        return /* your implementation */;
    }

    @Override
    public String getArgNotRecognized() {
        return /* your implementation */;
    }
    
    @Override
    public String getRequirementMessage() {
        return /* your implementation */
    }
}
  1. Register the new class that implements MessageHandler:
public class MyPlugin extends JavaPlugin {

    private final CommandManager<MyPlugin> commandManager;

    @Override
    public void onEnable() {
        commandManager = new CommandManager<>(this);
        commandManager.setMessageHandler(new MyMessageHandler());
    }
}

Clone this wiki locally