Send command to server?

Discussion in 'Plugin Development' started by Mike724, Aug 31, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Mike724

    I want my plugin to send a give command to the server, how can I do this?
     
  2. Offline

    Jogy34

    There are a few ways that you can go about this:
    1. you can use a playerPickUpItemEvent and set the item to whatever you want to give.
    2. you could search the players inventory for an open space and put an item there
    3. you could change whatever the player is holding to the item(I wouldn't suggest this it could be dangerous however it is the easiest way in my opinion)
     
  3. Offline

    Mike724

    Hm, I want it so the item will drop, so the player will get the item even if they dont have a free inventory space
    (which the only way to do this I know of is give).
     
  4. Offline

    Jogy34

    You could try to use a ServerCommandEvent.(I have no clue how to use it, all i know is that it sends a command through the console.) The format is:
    Code:
    ServerCommandEvent(ConloleCommandSender console, String message)
    
     
  5. Offline

    Mike724

    I was thinking about using this...
    Code:
    plugin.getServer().dispatchCommand(sender, "give blah 1 64");
    
    But the variable sender needs to be a CommandSender (which I don't know how to use/make).
     
  6. Offline

    Jogy34

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(cmd.getName().equalsIgnoreCase("give")){\\or whatever you want the command to be
    plugin.getServer().dispatchCommand(sender, "give blah 1 64");
    }
    
    This is the best way i know how
     
  7. Offline

    Mike724

    Aha I figured out how to do it myself!

    Here's the code for anyone else wanting to know (it works flawlessly)...
    Code:
    		CommandSender cs = new ConsoleCommandSender(this.getServer());
    		this.getServer().dispatchCommand(cs, "give blah 1 64");
    
    Obviously you can add any other command in there if you want too. <3 Bukkit

    EDIT: In 1.8 you have to use this code
    Code:
    this.getServer().dispatchCommand(this.getServer().getConsoleSender(), "command here");
     
  8. You might be able to cast a player to sender also; haven't tried this though.
     
  9. Offline

    Mike724

    You can, but then if the player doesn't have the correct permissions, it wont work. That's why I had to use ConsoleCommandSender because it has all permissions (or that's what I think...).
     
Thread Status:
Not open for further replies.

Share This Page