console - Bukkit How do I get a argument from a string? -
i'm trying make plugin detects when people chat "#say " broadcast message arguments. need know how arguments 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); } } } }
you should use commands prefixed /
, so, /say string[args]
, , easier arguments, yet if want prefixed #
, that's story. this:
if(message.contains("#say")){ string messagetosend = message.replaceall("#say", "");//get arguments if(messagetosend.length <= 0){//make sure there's after #say e.getplayer().sendmessage("correct usage: #say <arguments>"); //the user didn't put after #say return; } else{ e.setcancelled(true);//cancel event bukkit.getserver().broadcastmessage(messagetosend);//send message comes after "#say" //you may want add chat color message make stand out more } }
so, here's event should like:
@eventhandler public void onchat(asyncplayerchatevent e){ if(e.isasynchronous()){ string message = e.getmessage(); if(message.contains("#say")){ string messagetosend = message.replaceall("#say", "");//get arguments if(messagetosend.length <= 0){//make sure there's after #say e.getplayer().sendmessage("correct usage: #say <arguments>"); //the user didn't put after #say return; } else{ e.setcancelled(true);//cancel event bukkit.getserver().broadcastmessage(messagetosend);//send message comes after "#say" //you may want add chat color message make stand out more } } } }
Comments
Post a Comment