pocketmine\Server::getDifficultyFromString PHP Method

getDifficultyFromString() public static method

public static getDifficultyFromString ( string $str ) : integer
$str string
return integer
    public static function getDifficultyFromString($str)
    {
        switch (strtolower(trim($str))) {
            case "0":
            case "peaceful":
            case "p":
                return 0;
            case "1":
            case "easy":
            case "e":
                return 1;
            case "2":
            case "normal":
            case "n":
                return 2;
            case "3":
            case "hard":
            case "h":
                return 3;
        }
        return -1;
    }

Usage Example

コード例 #1
0
 public function execute(CommandSender $sender, $currentAlias, array $args)
 {
     if (!$this->testPermission($sender)) {
         return true;
     }
     if (count($args) !== 1) {
         $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
         return false;
     }
     $difficulty = Server::getDifficultyFromString($args[0]);
     if ($sender->getServer()->isHardcore()) {
         $difficulty = 3;
     }
     if ($difficulty !== -1) {
         $sender->getServer()->setConfigInt("difficulty", $difficulty);
         $pk = new SetDifficultyPacket();
         $pk->difficulty = $sender->getServer()->getDifficulty();
         Server::broadcastPacket($sender->getServer()->getOnlinePlayers(), $pk);
         Command::broadcastCommandMessage($sender, new TranslationContainer("commands.difficulty.success", [$difficulty]));
     } else {
         $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
         return false;
     }
     return true;
 }
All Usage Examples Of pocketmine\Server::getDifficultyFromString
Server