Question

I'm trying to make a plugin that detects when people chat "#say " it will broadcast a message with those arguments. What I need to know is how to get arguments from a string. Please help.

Main:

package com.gong.say;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin{
    String sayMessage = ChatColor.GREEN + "Your message has been said!";
    public void onEnable()
    {
        Bukkit.getLogger().info("[BukkitAPIEnhancer] Plugin started!");
    Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
    }
    public void onDisable()
    {
        Bukkit.getLogger().info("[BukkitAPIEnhancer] Plugin disabled!");
    }

}

ChatListener:

package com.gong.say;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public class ChatListener implements Listener {
    Main plugin;
    public ChatListener(Main plugin)
    {
        this.plugin = plugin;
    }
    @EventHandler
    public void onChat(AsyncPlayerChatEvent e)
    {
        if(e.isAsynchronous())
        {
                String message = e.getMessage();
            if(message.contains("#say"))
            {
                //String[] args = Arguments after #say
                //Bukkit.broadcastMessage(args);
            }
        }
    }
}
Was it helpful?

Solution

You should usually use commands prefixed by /, so, normally you would do /say String[args], and It would be easier to get the arguments, yet if you want it to be prefixed by #, then that's another story. You could do something like this:

if(message.contains("#say")){
    String messageToSend = message.replaceAll("#say", "");//get the arguments
    if(messageToSend.length <= 0){//make sure there's something after #say
        e.getPlayer().sendMessage("Correct usage: #say <arguments>"); //the user didn't put anything after #say
        return;
    }
    else{
        e.setCancelled(true);//cancel the event
        Bukkit.getServer().broadcastMessage(messageToSend);//send the message that comes after "#say"
        //you may want to add a chat color to the message to make it stand out more
    }
}

So, here's what your event should look like:

@EventHandler
public void onChat(AsyncPlayerChatEvent e){
    if(e.isAsynchronous()){
        String message = e.getMessage();
        if(message.contains("#say")){
            String messageToSend = message.replaceAll("#say", "");//get the arguments
            if(messageToSend.length <= 0){//make sure there's something after #say
                e.getPlayer().sendMessage("Correct usage: #say <arguments>"); //the user didn't put anything after #say
                return;
            }
            else{
                e.setCancelled(true);//cancel the event
                Bukkit.getServer().broadcastMessage(messageToSend);//send the message that comes after "#say"
                //you may want to add a chat color to the message to make it stand out more
            }
        }
    }
}

OTHER TIPS

@EventHandler
public void onChat2(AsyncPlayerChatEvent e) {
    if(e.isAsynchronous()) {
        String msg = e.getMessage();
        /** Verify if message starts with #say **/
        if(msg.startsWith("#say")) {
            /** Split message for get the args **/
            String[] args = e.getMessage().split(" ");
            /** Verify if have something after #say **/
            if(args.length > 1) {
                /** Cancel message and broadcast **/
                e.setCancelled(true);

                StringBuilder sb = new StringBuilder();
                for(int i = 1; i <args.length; i++) {
                    sb.append(args[i] + " ");
                }
                /** Add color to broadcast */
                String broadcast = ChatColor.translateAlternateColorCodes('&', sb.toString());
                /** Broadcast prefix **/
                String prefix = "§c[Broadcast] §r";
                /** Broadcast **/
                Bukkit.broadcastMessage(prefix + broadcast);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top