Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Running Array of commands

Comments in 'Plugin Development' started by SpiderPig, Mar 10, 2014.

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

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
    Hi,
    Is it possible to run a few commands from one command e.g the player runs /starter and then the console runs /give (a few times) and /givemoney once??
  2. SpiderPig
    Offline

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
  3. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    You could use a for loop like:
    PHP:
    for(0NUM_OF_TIMES_TO_RUNi++){
    $this->api->console->run("command here");
    }
  4. SpiderPig
    Offline

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
    @Falk Could I use ??
    PHP:
    $this->api->console->run("array("command here,command here also")");
  5. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    I don't think so.
  6. SpiderPig
    Offline

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
    @Falk it says Parse error, unexpected '=', expecting ';' on the top line
  7. Topic
    Offline

    Topic New Member Plugin Developer

    Joined:
    Aug 29, 2013
    Posts:
    47
    Plugins:
    3
    Minecraft User:
    topic
    You can use like this:
    PHP:
    case "command":
    $this->api->console->run("first command");
    $this->api->console->run("second command");
    $this->api->console->run("third command");
    break;
  8. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    If you wanted to use an array to do it you could try:
    PHP:
    $array = array("command1","command1","command2");
    foreach(
    $array as $cmd){
    $this->api->console->run($cmd);
    }
  9. SpiderPig
    Offline

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
    @Falk What is the problem with this? It says
    "Parse error: syntax error, unexpected 'case' (T_CASE) in C:\Users\Public\Documents\PocketMine-MP\plugins\test.php on line 26"

    PHP:
     public function init(){
         
    $this->api->console->register("sta"," testing", array($this"cmd"));
       }

       public function 
    cmd($cmd$params$issuer){
                
    $username $issuer->username;
    /*Line  26*/ case "sta":
                        
    $array = array("say hi","give $username 6 5","give $username 2 64 ");
                        foreach(
    $array as $cmd){
                        
    $this->api->console->run($cmd);
                        }
                        break;
                    default:
                        
    $output "Usage: /sta";
                        break;
                }
                return 
    $output;
            }
  10. zgeorge2222
    Offline

    zgeorge2222 New Member

    Joined:
    Mar 3, 2014
    Posts:
    9
    Minecraft User:
    zgeorge22
    BTW I am not @Falk, but I might be able to help.
    I am not sure if I am right but before the case "sta" you need a switch. So it would be
    PHP:
     public function init(){
         
    $this->api->console->register("sta"," testing", array($this"cmd"));
       }

       public function 
    cmd($cmd$params$issuer){
                
    $username $issuer->username;
    /*Added the Switch here*/ switch($cmd){
    /*Line  26*/ case "sta":
                        
    $array = array("say hi","give $username 6 5","give $username 2 64 ");
                        foreach(
    $array as $cmd){
                        
    $this->api->console->run($cmd);
                        }
                        break;
                    default:
                        
    $output "Usage: /sta";
                        break;
                }
                return 
    $output;
            }
    You also had an extra } so when I added the switch it matched up.
    Last edited: Mar 12, 2014
  11. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    That should work great :)
    Except variables inside strings need to be written "like" . $var . "this"
  12. SpiderPig
    Offline

    SpiderPig Active Member

    Joined:
    Sep 21, 2013
    Posts:
    128
    Minecraft User:
    SpiderPig
  13. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Locked.
Thread Status:
Not open for further replies.

Share This Page

Advertisement