Hello, my question is, how can I make that my plugin sends a command via the player if there is no # in the config before it example: command: "say Hello" But when a server owner adds a # in front of it, example: command: "#broadcast Hello" it executes via the console. I already know how to do them seperate but I want to add this feature because it's easy for plugin owners. Thanks in advance!
for player, player.chat("/command"); for console, getServer().dispatchCommand(getServer().getConsoleSender(), "command");
That's not what I mean, sorry, I'm bad at explaining in English. What I mean is: In my plugin I have the option that they can add a command on an event. But when they add a # it needs to run as a player. Example: (In the config) command: "say hi" --> Needs to run as a player (player.chat("/command"); command "#say hi" --> Needs to run as console (getServer().dispatchCommand(getServer().getConsoleSender(), "command); I hope you understand
Do you mean when the server owner types #broadcast hello in the chat, it will try and run the broadcast command from the console? If so you can make an AsyncPlayerChatEvent to detect the message the owner sent. If the player is opped, and the message contains the string #broadcast, convert the string into a String[] by splitting it with space as the split string. Finally, replace the # and you should be good to go from there. After that is done, you can then use dispatchCommand() to grab the elements from the String[] and make a command out of it. If you don't know how to grab elements out of a String[]: String[] foo; (getServer().dispatchCommand(getServer().getConsoleSender(), "" + foo.get[0] + " " + foo.get[1]); If this were working code, foo.get[0] would be the string broadcast and foo.get[1] would be the string hello. Obviously the String[] above needs to be initialized, but you should be able to see what i'm getting at. If you don't understand, take some time to learn about them, they can be very useful. Hope this helped at least a bit, and if I misunderstood your question, let me know!
It's in the good way but what I mean is that you have in the config a string named: command So, example, you have a string command in your config. When you enter a command in there, example: command: "say hi" It will be executed via the player. When they change the string in the config to #say hi, example: command: "#say hi" it will be executed via the console. So in case one the output will be [brooky1010]: hi and in the second one [Server]: Hi Thanks!
Code (Text): String s = plugin.getConfig().getString("string"); if (s.contains("#") { s = s.replace("#", ""); // send command } else { //send command }
In that case, you would only have the command itself in the config. You would not include the hi string because nobody would be able to modify what the command /say does. And like Vexentric said, get the string from the config, test if it has a # and then go from there. If it does not have a #, use the dispatchCommand() but get the player who sent the message in game instead of the console.
I usually use Code (Text): p.chat("/command"); Instead of Code (Text): p.performCommand("command"); As many plugins use PlayerCommandPreProcessEvent instead of doing a normal command.
Doesn't a String have a startsWith method? You could check it with that. In case my memory is terrible and it doesn't exist, you could call substring on the command and see if the first character is a #.
Yes, yes it does. #charAt would be more efficient than substring for accessing one character, though.