Send Command from Console (from within a plugin)

Discussion in 'Plugin Development' started by stargateanubis14, Jan 13, 2012.

Thread Status:
Not open for further replies.
  1. I'm looking for a way to send a command, as tho the console was saying it.
    Some people a while back, had me using player.chat("/command"); ...but i was later advised against that.
    Then I was advised the proper way is to use Bukkit.getServer().dispatchCommand();

    In general, this is what i have inside the onCommand
    Code:
    CommandSender console=Bukkit.getConsoleSender();
    
    Bukkit.getServer().dispatchCommand(console,"/command arg0 arg1");
    
    That, according to everyone, SHOULD send a command from console...but it doesn't do anything. No errors, no console messages, nothing to resemble that anything is happening.
    just to play around, i tried this too:

    Code:
    ((Player) console).chat("/mycommand");
    However, that had the same problem.


    So, it brought me to think that something is wrong with the Bukkit.getConsoleSender(); part... i know that the chat works, for a normal player (but that player has to be online for it to work).
    I need this to work, even if noone is online. The command it is executing has nothing to do with the people online. Which is why i was going for console.


    Any ideas?
     
  2. Offline

    tomjw64

    I don't have too much experience doing this, but I have two suggestions.
    1. Remove the / in the dispatch command version that you tried and see if it works
    2. If it still doesn't work, go with the PluginCommand way.
    Example (note: May not be completely correct)
    Code:java
    1. PluginCommand cmd=getServer().getPluginCommand("commandName");
    2. if(cmd!=null)
    3. {
    4. String[] arguments={"firstArgument","secondArgument"};
    5. cmd.execute(getServer().getConsoleSender(),"commandName",arguments);
    6. }


    Edit: I am sure that in this method of doing this that no forward slashes(/) are supposed to be added. Also replace commandName with the first part of the command (directly after the '/' in chat, and the firstArgument and secondArgument with the arguments or parameters to that command.

    But yeah, I'm fairly sure that removing the '/' should work though. :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  3. Offline

    hammale

    well maybe we can find another solution to ur problem then this. what exactly is the command and why does the console have to run it?
     
  4. @tomjw64
    Well, first off, i did try the command without the / . it still did the same thing.

    @hammale
    the reason i need the console to run it, is because i need a few different commands to be executed on prompt, even if noone is online. (that prompt is from a web server).
    I have thought of any other ways to do it that would be any easier.

    Btw... i have the current source/files up at pastebin
    http://pastebin.com/u/empirebattles

    I guess a better way to phrase the question, is how to send a command AS console...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  5. Offline

    tomjw64

    You did try the alternate method that I proposed, right? That would run the command as the console. If it doesn't work, just tag me again, and I will try to do something more thorough.
     
Thread Status:
Not open for further replies.

Share This Page