Trying to send command arguments to another plugin

Discussion in 'Plugin Development' started by Hpiz, May 18, 2011.

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

    Hpiz

    I'm trying to make a plugin for my server that needs to send the /region define command to worldguard. Not exactly sure how to do this, here is what I've tried as proof of concept.


    Code:
     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            Plugin wg = new com.sk89q.worldguard.bukkit.WorldGuardPlugin();
            if (sender instanceof Player) {
                Player player = (Player) sender;
                player.sendMessage(args[0] + "  ");
                 player.sendMessage(args[1]);
    
              if (args.length > 0) {
                    if (commandLabel.equalsIgnoreCase("region")) {
                        player.sendMessage("Recognized Region");
                        if (args[0].equalsIgnoreCase("define")) {
                            player.sendMessage("Recognized define");
    wg.onCommand(sender, cmd, commandLabel, args);
         }
        }
      }
    }
    }
    I don't have this build anymore and don't remember if/what the error message was but I know it didn't work and gave a null pointed to the wg.onCommand(sender, cmd, commandLabel, args) line.
     
  2. Offline

    Evenprime

    @Hpiz :

    Try this:

    Code:
    Bukkit.getServer().dispatchCommand(commandSender, "region define whatever parameters that command takes");
    
    You can do that from everywhere in your plugin and don't need to know which plugin actually handles the "/region" command. It will simply be treated as if it was entered into the console.

    commandSender can be anything that implements the CommandSender interface.
     
  3. Offline

    Hpiz

    I also need to have my mod catch the first call of the command and after logic throw it to worldguard, wouldnt' doing what you suggested be recaught by my plugin

    I'll try and let you know

    I'm new to writing Bukkit Plugins

    As I thought unfortunately,
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            commandLabel=commandLabel+ " " + args [0];
            for (int i=1 ;i<args.length; i++){
                commandLabel=commandLabel + " " + args[i];
            }
            server.dispatchCommand(sender, commandLabel);
            return true;
    org.bukkit.command.CommandException Triggered Unhandled exception executing region in my plugin

    it just looped a couple 100,000 times before it stopped

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  4. Offline

    GardenGnomer

    I also wonder how to do this.
    Especially, can you send admin commands though bukkit with no admin on the server?
     
  5. Offline

    Hpiz

    Getting around permissions can be a challenge depending on how the other plugins author implemented permission checks, you would have to create a new CommandSender object that has an admins name
     
  6. Offline

    SpaceManiac

    If this is the case, you need to use PLAYER_COMMAND_PREPROCESS; using dispatchCommand the way you are...

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            commandLabel=commandLabel+ " " + args [0];
            for (int i=1 ;i<args.length; i++){
                commandLabel=commandLabel + " " + args[i];
            }
            server.dispatchCommand(sender, commandLabel);
            return true;
    ... you end up dispatching your own command in an infinite recursion. (someone says /cmd abc, your logic will just dispatch /cmd abc again)
     
  7. Offline

    GardenGnomer

    Thankyou, interesting...
     
Thread Status:
Not open for further replies.

Share This Page