PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
FormattedCommandAlias.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\command;
23 
27 
29  private $formatStrings = [];
30 
35  public function __construct($alias, array $formatStrings){
36  parent::__construct($alias);
37  $this->formatStrings = $formatStrings;
38  }
39 
40  public function execute(CommandSender $sender, $commandLabel, array $args){
41 
42  $commands = [];
43  $result = false;
44 
45  foreach($this->formatStrings as $formatString){
46  try{
47  $commands[] = $this->buildCommand($formatString, $args);
48  }catch(\Exception $e){
49  if($e instanceof \InvalidArgumentException){
50  $sender->sendMessage(TextFormat::RED . $e->getMessage());
51  }else{
52  $sender->sendMessage(TextFormat::RED . "An internal error occurred while attempting to perform this command");
53  if(($logger = $sender->getServer()->getLogger()) instanceof MainLogger){
54  $logger->logException($e);
55  }
56  }
57 
58  return false;
59  }
60  }
61 
62  foreach($commands as $command){
63  $result |= Server::getInstance()->dispatchCommand($sender, $command);
64  }
65 
66  return (bool) $result;
67  }
68 
76  private function buildCommand($formatString, array $args){
77  $index = strpos($formatString, '$');
78  while($index !== false){
79  $start = $index;
80  if($index > 0 and $formatString{$start - 1} === "\\"){
81  $formatString = substr($formatString, 0, $start - 1) . substr($formatString, $start);
82  $index = strpos($formatString, '$', $index);
83  continue;
84  }
85 
86  $required = false;
87  if($formatString{$index + 1} == '$'){
88  $required = true;
89 
90  ++$index;
91  }
92 
93  ++$index;
94 
95  $argStart = $index;
96 
97  while($index < strlen($formatString) and self::inRange($formatString{$index} - 48, 0, 9)){
98  ++$index;
99  }
100 
101  if($argStart === $index){
102  throw new \InvalidArgumentException("Invalid replacement token");
103  }
104 
105  $position = intval(substr($formatString, $argStart, $index));
106 
107  if($position === 0){
108  throw new \InvalidArgumentException("Invalid replacement token");
109  }
110 
111  --$position;
112 
113  $rest = false;
114 
115  if($index < strlen($formatString) and $formatString{$index} === "-"){
116  $rest = true;
117  ++$index;
118  }
119 
120  $end = $index;
121 
122  if($required and $position >= count($args)){
123  throw new \InvalidArgumentException("Missing required argument " . ($position + 1));
124  }
125 
126  $replacement = "";
127  if($rest and $position < count($args)){
128  for($i = $position; $i < count($args); ++$i){
129  if($i !== $position){
130  $replacement .= " ";
131  }
132 
133  $replacement .= $args[$i];
134  }
135  }elseif($position < count($args)){
136  $replacement .= $args[$position];
137  }
138 
139  $formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
140 
141  $index = $start + strlen($replacement);
142 
143  $index = strpos($formatString, '$', $index);
144  }
145 
146  return $formatString;
147  }
148 
156  private static function inRange($i, $j, $k){
157  return $i >= $j and $i <= $k;
158  }
159 
160 }
static getInstance()
Definition: Server.php:1444