RexChat API Documentation

The RexChat API allows other plugins to interact with RexChat's features programmatically. This enables you to create addons, integrations, or extend RexChat's functionality.

Version: API introduced in RexChat 1.6.0

Setup

Step 1: Add RexChat as a Dependency

First, add RexChat to your plugin's plugin.yml file:

name: YourPlugin
version: 1.0.0
main: com.example.yourplugin.YourPlugin

# Use 'depend' if your plugin requires RexChat to work
depend: [RexChat]

# Or use 'softdepend' if RexChat is optional
softdepend: [RexChat]

Step 2: Add RexChat to Your Build

If you're using Maven, you'll need to add RexChat as a dependency. Since RexChat isn't in a public repository yet, you can install it locally:

# Install RexChat to your local Maven repository
mvn install:install-file \
  -Dfile=RexChat-1.6.0.jar \
  -DgroupId=cc.rexsystems \
  -DartifactId=rexchat \
  -Dversion=1.6.0 \
  -Dpackaging=jar

Then add it to your pom.xml:

<dependencies>
    <dependency>
        <groupId>cc.rexsystems</groupId>
        <artifactId>rexchat</artifactId>
        <version>1.6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
Important: Use scope as provided since RexChat will be available on the server at runtime.

Getting the API Instance

Basic Setup

import cc.rexsystems.rexChat.api.RexChatAPI;
import org.bukkit.plugin.java.JavaPlugin;

public class YourPlugin extends JavaPlugin {
    
    private RexChatAPI rexChatAPI;
    
    @Override
    public void onEnable() {
        // Get the API instance
        rexChatAPI = RexChatAPI.getInstance(this);
        
        // Check if RexChat is available
        if (rexChatAPI == null) {
            getLogger().warning("RexChat is not installed or loaded!");
            getLogger().warning("Some features will be disabled.");
            return;
        }
        
        getLogger().info("Successfully hooked into RexChat!");
    }
    
    public RexChatAPI getRexChatAPI() {
        return rexChatAPI;
    }
}

Checking if RexChat is Available

// Method 1: Check API instance
if (rexChatAPI != null) {
    // RexChat is available
}

// Method 2: Check plugin directly
Plugin rexChat = getServer().getPluginManager().getPlugin("RexChat");
if (rexChat != null && rexChat.isEnabled()) {
    // RexChat is available
}

Chat Management

Check if Chat is Muted

boolean isMuted = rexChatAPI.isChatMuted();

if (isMuted) {
    player.sendMessage("Chat is currently muted!");
}

Mute or Unmute Chat

// Mute chat
rexChatAPI.setChatMuted(true, "YourPlugin");

// Unmute chat
rexChatAPI.setChatMuted(false, "YourPlugin");

// The second parameter is the executor name shown in announcements
// Example: "The chat has been muted by YourPlugin"

Toggle Chat Mute

// Toggle mute state
boolean currentState = rexChatAPI.isChatMuted();
rexChatAPI.setChatMuted(!currentState, "Admin");

Clear Chat

// Clear chat for all players
rexChatAPI.clearChat("YourPlugin");

// This will:
// 1. Send blank lines to all players
// 2. Show a clear announcement
// 3. Fire a ChatClearEvent

Chat Colors

Get Player's Chat Color

String colorId = rexChatAPI.getPlayerChatColor(player);

if (colorId == null) {
    player.sendMessage("You don't have a chat color set.");
} else {
    player.sendMessage("Your chat color: " + colorId);
    // Example output: "Your chat color: rainbow"
}

Set Player's Chat Color

// Set a chat color
boolean success = rexChatAPI.setPlayerChatColor(player, "rainbow");

if (success) {
    player.sendMessage("Chat color set to rainbow!");
} else {
    player.sendMessage("Failed! Color doesn't exist or you lack permission.");
}

// Available colors (from default config):
// red, gold, green, aqua, pink, rainbow, sunset, ocean

Remove Player's Chat Color

// Pass null to remove color
boolean success = rexChatAPI.setPlayerChatColor(player, null);

if (success) {
    player.sendMessage("Chat color removed!");
}

Check Color Availability

// Colors are defined in RexChat's config.yml
// Check if a color exists before setting it

String[] colors = {"red", "gold", "green", "aqua", "pink", 
                   "rainbow", "sunset", "ocean"};

for (String color : colors) {
    if (rexChatAPI.setPlayerChatColor(player, color)) {
        // Color exists and player has permission
        break;
    }
}

Message Formatting

Send Formatted Message

// Send a message with color codes
rexChatAPI.sendFormattedMessage(player, "&6Hello &b{player}&f!");

// Supports:
// - Legacy color codes: &a, &c, &6, etc.
// - HEX colors: &#FF5733
// - Gradients: <gradient:#FF0000:#00FF00>text</gradient>
// - Rainbow: <rainbow>text</rainbow>

Format Message to Component

import net.kyori.adventure.text.Component;

// Convert a string to an Adventure Component
Component component = rexChatAPI.formatMessage("&aGreen &bBlue &#FF5733HEX");

// Use the component
player.sendMessage(component);

// Or use it in other Adventure API methods
Audience audience = player;
audience.sendMessage(component);

Formatting Examples

// Legacy colors
rexChatAPI.sendFormattedMessage(player, "&aGreen &cRed &6Gold");

// HEX colors
rexChatAPI.sendFormattedMessage(player, "&#FF5733Orange �FF00Green");

// Gradients
rexChatAPI.sendFormattedMessage(player, "<gradient:#FF0000:#0000FF>Rainbow Text</gradient>");

// Rainbow effect
rexChatAPI.sendFormattedMessage(player, "<rainbow>Colorful!</rainbow>");

// Combined
rexChatAPI.sendFormattedMessage(player, 
    "&6[&cWarning&6] <gradient:#FF0000:#FFFF00>Important message!</gradient>");

Events

ChatMuteEvent

Fired when chat is muted or unmuted.

import cc.rexsystems.rexChat.api.events.ChatMuteEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class ChatListener implements Listener {
    
    @EventHandler
    public void onChatMute(ChatMuteEvent event) {
        // Check if chat was muted or unmuted
        if (event.isMuted()) {
            // Chat was muted
            String executor = event.getExecutor();
            Bukkit.getLogger().info("Chat muted by: " + executor);
            
            // Do something when chat is muted
            // Example: Pause a minigame, log to database, etc.
        } else {
            // Chat was unmuted
            Bukkit.getLogger().info("Chat unmuted!");
        }
    }
}

ChatClearEvent

Fired when chat is cleared.

import cc.rexsystems.rexChat.api.events.ChatClearEvent;

@EventHandler
public void onChatClear(ChatClearEvent event) {
    String executor = event.getExecutor();
    
    // Log who cleared the chat
    Bukkit.getLogger().info("Chat cleared by: " + executor);
    
    // Example: Send a notification to admins
    for (Player admin : Bukkit.getOnlinePlayers()) {
        if (admin.hasPermission("yourplugin.notify")) {
            admin.sendMessage("§6Chat was cleared by " + executor);
        }
    }
}

Registering Event Listeners

@Override
public void onEnable() {
    // Register your listener
    getServer().getPluginManager().registerEvents(new ChatListener(), this);
}

Complete Examples

Example 1: Anti-Spam Addon

public class AntiSpamAddon extends JavaPlugin implements Listener {
    
    private RexChatAPI rexChatAPI;
    private Map<UUID, Integer> spamCount = new HashMap<>();
    
    @Override
    public void onEnable() {
        rexChatAPI = RexChatAPI.getInstance(this);
        
        if (rexChatAPI == null) {
            getLogger().severe("RexChat not found! Disabling...");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
        
        getServer().getPluginManager().registerEvents(this, this);
    }
    
    @EventHandler
    public void onChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();
        UUID uuid = player.getUniqueId();
        
        // Track spam
        int count = spamCount.getOrDefault(uuid, 0) + 1;
        spamCount.put(uuid, count);
        
        // Auto-mute if spam detected
        if (count > 5) {
            rexChatAPI.setChatMuted(true, "AntiSpam");
            rexChatAPI.sendFormattedMessage(player, 
                "&c&lSPAM DETECTED! &7Chat has been muted.");
            
            // Reset after 30 seconds
            Bukkit.getScheduler().runTaskLater(this, () -> {
                rexChatAPI.setChatMuted(false, "AntiSpam");
                spamCount.remove(uuid);
            }, 600L);
        }
    }
}

Example 2: Chat Color Rewards

public class ColorRewards extends JavaPlugin {
    
    private RexChatAPI rexChatAPI;
    
    @Override
    public void onEnable() {
        rexChatAPI = RexChatAPI.getInstance(this);
        getCommand("rewardcolor").setExecutor(this::onRewardCommand);
    }
    
    private boolean onRewardCommand(CommandSender sender, Command cmd, 
                                     String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Players only!");
            return true;
        }
        
        Player player = (Player) sender;
        
        // Check player level (example)
        int level = getPlayerLevel(player);
        
        if (level >= 10) {
            // Unlock rainbow color
            if (rexChatAPI.setPlayerChatColor(player, "rainbow")) {
                rexChatAPI.sendFormattedMessage(player, 
                    "&6&lREWARD! <rainbow>Rainbow color unlocked!</rainbow>");
            }
        } else {
            player.sendMessage("§cYou need level 10 to unlock rainbow color!");
        }
        
        return true;
    }
    
    private int getPlayerLevel(Player player) {
        // Your level system here
        return player.getLevel();
    }
}

Example 3: Chat Logger

public class ChatLogger extends JavaPlugin implements Listener {
    
    private RexChatAPI rexChatAPI;
    private File logFile;
    
    @Override
    public void onEnable() {
        rexChatAPI = RexChatAPI.getInstance(this);
        logFile = new File(getDataFolder(), "chat.log");
        
        getServer().getPluginManager().registerEvents(this, this);
    }
    
    @EventHandler
    public void onChatMute(ChatMuteEvent event) {
        String message = String.format("[%s] Chat %s by %s",
            new Date(),
            event.isMuted() ? "MUTED" : "UNMUTED",
            event.getExecutor()
        );
        
        logToFile(message);
    }
    
    @EventHandler
    public void onChatClear(ChatClearEvent event) {
        String message = String.format("[%s] Chat CLEARED by %s",
            new Date(),
            event.getExecutor()
        );
        
        logToFile(message);
    }
    
    private void logToFile(String message) {
        try (FileWriter fw = new FileWriter(logFile, true)) {
            fw.write(message + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

API Reference Summary

Method Return Type Description
getInstance(Plugin) RexChatAPI Get API instance (static method)
isChatMuted() boolean Check if chat is muted
setChatMuted(boolean, String) void Mute/unmute chat
clearChat(String) void Clear chat for all players
getPlayerChatColor(Player) String Get player's chat color ID
setPlayerChatColor(Player, String) boolean Set player's chat color
sendFormattedMessage(Player, String) void Send formatted message
formatMessage(String) Component Format string to Adventure Component
getPlugin() RexChat Get RexChat plugin instance

Support & Links