From 97f6a3255716d3af9aa7970f946ba997d05b0829 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 4 Jul 2017 13:44:47 +0100 Subject: [PATCH] Changed usage message displaying to exceptions to reduce boilerplate code Someday this won't need to be done by commands themselves, it'll be done by the parser. --- src/pocketmine/command/PluginCommand.php | 4 +-- src/pocketmine/command/SimpleCommandMap.php | 5 ++++ .../command/defaults/BanCommand.php | 5 ++-- .../command/defaults/BanIpCommand.php | 5 ++-- .../command/defaults/BanListCommand.php | 5 ++-- .../defaults/DefaultGamemodeCommand.php | 5 ++-- .../command/defaults/DeopCommand.php | 5 ++-- .../command/defaults/DifficultyCommand.php | 9 ++---- .../command/defaults/EffectCommand.php | 5 ++-- .../command/defaults/EnchantCommand.php | 5 ++-- .../command/defaults/GamemodeCommand.php | 9 ++---- .../command/defaults/GiveCommand.php | 5 ++-- .../command/defaults/KickCommand.php | 5 ++-- .../command/defaults/KillCommand.php | 9 ++---- src/pocketmine/command/defaults/MeCommand.php | 5 ++-- src/pocketmine/command/defaults/OpCommand.php | 5 ++-- .../command/defaults/PardonCommand.php | 5 ++-- .../command/defaults/PardonIpCommand.php | 5 ++-- .../command/defaults/ParticleCommand.php | 5 ++-- .../command/defaults/SayCommand.php | 5 ++-- .../command/defaults/SetWorldSpawnCommand.php | 5 ++-- .../command/defaults/SpawnpointCommand.php | 5 ++-- .../command/defaults/TeleportCommand.php | 9 ++---- .../command/defaults/TellCommand.php | 5 ++-- .../command/defaults/TimeCommand.php | 11 +++----- .../command/defaults/TimingsCommand.php | 5 ++-- .../command/defaults/TitleCommand.php | 19 +++++-------- .../defaults/TransferServerCommand.php | 6 ++-- .../command/defaults/WhitelistCommand.php | 4 +-- .../command/utils/CommandException.php | 28 +++++++++++++++++++ .../utils/InvalidCommandSyntaxException.php | 28 +++++++++++++++++++ 31 files changed, 128 insertions(+), 108 deletions(-) create mode 100644 src/pocketmine/command/utils/CommandException.php create mode 100644 src/pocketmine/command/utils/InvalidCommandSyntaxException.php diff --git a/src/pocketmine/command/PluginCommand.php b/src/pocketmine/command/PluginCommand.php index 44813e9b4..b90bd2a14 100644 --- a/src/pocketmine/command/PluginCommand.php +++ b/src/pocketmine/command/PluginCommand.php @@ -23,7 +23,7 @@ declare(strict_types=1); namespace pocketmine\command; -use pocketmine\event\TranslationContainer; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\plugin\Plugin; class PluginCommand extends Command implements PluginIdentifiableCommand{ @@ -58,7 +58,7 @@ class PluginCommand extends Command implements PluginIdentifiableCommand{ $success = $this->executor->onCommand($sender, $this, $commandLabel, $args); if(!$success and $this->usageMessage !== ""){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); + throw new InvalidCommandSyntaxException(); } return $success; diff --git a/src/pocketmine/command/SimpleCommandMap.php b/src/pocketmine/command/SimpleCommandMap.php index ffc46cf10..99bd90393 100644 --- a/src/pocketmine/command/SimpleCommandMap.php +++ b/src/pocketmine/command/SimpleCommandMap.php @@ -64,6 +64,7 @@ use pocketmine\command\defaults\TransferServerCommand; use pocketmine\command\defaults\VanillaCommand; use pocketmine\command\defaults\VersionCommand; use pocketmine\command\defaults\WhitelistCommand; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Server; use pocketmine\utils\TextFormat; @@ -216,13 +217,17 @@ class SimpleCommandMap implements CommandMap{ } $target->timings->startTiming(); + try{ $target->execute($sender, $sentCommandLabel, $args); + }catch(InvalidCommandSyntaxException $e){ + $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$target->getUsage()])); }catch(\Throwable $e){ $sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.exception")); $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.command.exception", [$commandLine, (string) $target, $e->getMessage()])); $sender->getServer()->getLogger()->logException($e); } + $target->timings->stopTiming(); return true; diff --git a/src/pocketmine/command/defaults/BanCommand.php b/src/pocketmine/command/defaults/BanCommand.php index 090d5113e..4188379cf 100644 --- a/src/pocketmine/command/defaults/BanCommand.php +++ b/src/pocketmine/command/defaults/BanCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; @@ -45,9 +46,7 @@ class BanCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $name = array_shift($args); diff --git a/src/pocketmine/command/defaults/BanIpCommand.php b/src/pocketmine/command/defaults/BanIpCommand.php index ffdad06d3..f86a9e26e 100644 --- a/src/pocketmine/command/defaults/BanIpCommand.php +++ b/src/pocketmine/command/defaults/BanIpCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; @@ -45,9 +46,7 @@ class BanIpCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $value = array_shift($args); diff --git a/src/pocketmine/command/defaults/BanListCommand.php b/src/pocketmine/command/defaults/BanListCommand.php index 846e7734f..09000b798 100644 --- a/src/pocketmine/command/defaults/BanListCommand.php +++ b/src/pocketmine/command/defaults/BanListCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\permission\BanEntry; @@ -50,9 +51,7 @@ class BanListCommand extends VanillaCommand{ }elseif($args[0] === "players"){ $list = $sender->getServer()->getNameBans(); }else{ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } }else{ $list = $sender->getServer()->getNameBans(); diff --git a/src/pocketmine/command/defaults/DefaultGamemodeCommand.php b/src/pocketmine/command/defaults/DefaultGamemodeCommand.php index cd2b6aa6f..63dde0d92 100644 --- a/src/pocketmine/command/defaults/DefaultGamemodeCommand.php +++ b/src/pocketmine/command/defaults/DefaultGamemodeCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Server; @@ -44,9 +45,7 @@ class DefaultGamemodeCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $gameMode = Server::getGamemodeFromString($args[0]); diff --git a/src/pocketmine/command/defaults/DeopCommand.php b/src/pocketmine/command/defaults/DeopCommand.php index 6e9cc162c..d0c1dc7ac 100644 --- a/src/pocketmine/command/defaults/DeopCommand.php +++ b/src/pocketmine/command/defaults/DeopCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -46,9 +47,7 @@ class DeopCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $name = array_shift($args); diff --git a/src/pocketmine/command/defaults/DifficultyCommand.php b/src/pocketmine/command/defaults/DifficultyCommand.php index c3921ddf5..4a6d3335a 100644 --- a/src/pocketmine/command/defaults/DifficultyCommand.php +++ b/src/pocketmine/command/defaults/DifficultyCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\network\mcpe\protocol\SetDifficultyPacket; use pocketmine\Server; @@ -46,9 +47,7 @@ class DifficultyCommand extends VanillaCommand{ } if(count($args) !== 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $difficulty = Server::getDifficultyFromString($args[0]); @@ -66,9 +65,7 @@ class DifficultyCommand extends VanillaCommand{ Command::broadcastCommandMessage($sender, new TranslationContainer("commands.difficulty.success", [$difficulty])); }else{ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } return true; diff --git a/src/pocketmine/command/defaults/EffectCommand.php b/src/pocketmine/command/defaults/EffectCommand.php index f011e67e0..db0a15ced 100644 --- a/src/pocketmine/command/defaults/EffectCommand.php +++ b/src/pocketmine/command/defaults/EffectCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\entity\Effect; use pocketmine\event\TranslationContainer; use pocketmine\utils\TextFormat; @@ -45,9 +46,7 @@ class EffectCommand extends VanillaCommand{ } if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $player = $sender->getServer()->getPlayer($args[0]); diff --git a/src/pocketmine/command/defaults/EnchantCommand.php b/src/pocketmine/command/defaults/EnchantCommand.php index 42ad15fdf..b4244754d 100644 --- a/src/pocketmine/command/defaults/EnchantCommand.php +++ b/src/pocketmine/command/defaults/EnchantCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\item\enchantment\Enchantment; use pocketmine\utils\TextFormat; @@ -45,9 +46,7 @@ class EnchantCommand extends VanillaCommand{ } if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $player = $sender->getServer()->getPlayer($args[0]); diff --git a/src/pocketmine/command/defaults/GamemodeCommand.php b/src/pocketmine/command/defaults/GamemodeCommand.php index 90ceb4a2f..c297cece0 100644 --- a/src/pocketmine/command/defaults/GamemodeCommand.php +++ b/src/pocketmine/command/defaults/GamemodeCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\Server; @@ -47,9 +48,7 @@ class GamemodeCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $gameMode = Server::getGamemodeFromString($args[0]); @@ -69,9 +68,7 @@ class GamemodeCommand extends VanillaCommand{ return true; } }elseif(!($sender instanceof Player)){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $target->setGamemode($gameMode); diff --git a/src/pocketmine/command/defaults/GiveCommand.php b/src/pocketmine/command/defaults/GiveCommand.php index 710478d20..3b5219b61 100644 --- a/src/pocketmine/command/defaults/GiveCommand.php +++ b/src/pocketmine/command/defaults/GiveCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\item\Item; use pocketmine\nbt\JsonNBTParser; @@ -49,9 +50,7 @@ class GiveCommand extends VanillaCommand{ } if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $player = $sender->getServer()->getPlayer($args[0]); diff --git a/src/pocketmine/command/defaults/KickCommand.php b/src/pocketmine/command/defaults/KickCommand.php index e5b603dd8..c5e57f7a6 100644 --- a/src/pocketmine/command/defaults/KickCommand.php +++ b/src/pocketmine/command/defaults/KickCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -46,9 +47,7 @@ class KickCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $name = array_shift($args); diff --git a/src/pocketmine/command/defaults/KillCommand.php b/src/pocketmine/command/defaults/KillCommand.php index d161c47a6..9934683cb 100644 --- a/src/pocketmine/command/defaults/KillCommand.php +++ b/src/pocketmine/command/defaults/KillCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\TranslationContainer; use pocketmine\Player; @@ -48,9 +49,7 @@ class KillCommand extends VanillaCommand{ } if(count($args) >= 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } if(count($args) === 1){ @@ -97,9 +96,7 @@ class KillCommand extends VanillaCommand{ $sender->setHealth(0); $sender->sendMessage(new TranslationContainer("commands.kill.successful", [$sender->getName()])); }else{ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } return true; diff --git a/src/pocketmine/command/defaults/MeCommand.php b/src/pocketmine/command/defaults/MeCommand.php index af5e5af3f..0fdb4b79b 100644 --- a/src/pocketmine/command/defaults/MeCommand.php +++ b/src/pocketmine/command/defaults/MeCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -45,9 +46,7 @@ class MeCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $sender->getServer()->broadcastMessage(new TranslationContainer("chat.type.emote", [$sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::WHITE . implode(" ", $args)])); diff --git a/src/pocketmine/command/defaults/OpCommand.php b/src/pocketmine/command/defaults/OpCommand.php index ac6f9e9b9..2e6104268 100644 --- a/src/pocketmine/command/defaults/OpCommand.php +++ b/src/pocketmine/command/defaults/OpCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -46,9 +47,7 @@ class OpCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $name = array_shift($args); diff --git a/src/pocketmine/command/defaults/PardonCommand.php b/src/pocketmine/command/defaults/PardonCommand.php index a927613db..878171815 100644 --- a/src/pocketmine/command/defaults/PardonCommand.php +++ b/src/pocketmine/command/defaults/PardonCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; class PardonCommand extends VanillaCommand{ @@ -44,9 +45,7 @@ class PardonCommand extends VanillaCommand{ } if(count($args) !== 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $sender->getServer()->getNameBans()->remove($args[0]); diff --git a/src/pocketmine/command/defaults/PardonIpCommand.php b/src/pocketmine/command/defaults/PardonIpCommand.php index 4fd60df6d..e92be4f97 100644 --- a/src/pocketmine/command/defaults/PardonIpCommand.php +++ b/src/pocketmine/command/defaults/PardonIpCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; class PardonIpCommand extends VanillaCommand{ @@ -44,9 +45,7 @@ class PardonIpCommand extends VanillaCommand{ } if(count($args) !== 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $args[0])){ diff --git a/src/pocketmine/command/defaults/ParticleCommand.php b/src/pocketmine/command/defaults/ParticleCommand.php index ba51cf0ed..ce7c0c062 100644 --- a/src/pocketmine/command/defaults/ParticleCommand.php +++ b/src/pocketmine/command/defaults/ParticleCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\block\Block; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\item\Item; use pocketmine\level\particle\AngryVillagerParticle; @@ -77,9 +78,7 @@ class ParticleCommand extends VanillaCommand{ } if(count($args) < 7){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } if($sender instanceof Player){ diff --git a/src/pocketmine/command/defaults/SayCommand.php b/src/pocketmine/command/defaults/SayCommand.php index d5c5a0de3..a7ed1d4b1 100644 --- a/src/pocketmine/command/defaults/SayCommand.php +++ b/src/pocketmine/command/defaults/SayCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; use pocketmine\command\ConsoleCommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -46,9 +47,7 @@ class SayCommand extends VanillaCommand{ } if(count($args) === 0){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $sender->getServer()->broadcastMessage(new TranslationContainer(TextFormat::LIGHT_PURPLE . "%chat.type.announcement", [$sender instanceof Player ? $sender->getDisplayName() : ($sender instanceof ConsoleCommandSender ? "Server" : $sender->getName()), TextFormat::LIGHT_PURPLE . implode(" ", $args)])); diff --git a/src/pocketmine/command/defaults/SetWorldSpawnCommand.php b/src/pocketmine/command/defaults/SetWorldSpawnCommand.php index 089b9c363..0ea724408 100644 --- a/src/pocketmine/command/defaults/SetWorldSpawnCommand.php +++ b/src/pocketmine/command/defaults/SetWorldSpawnCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\math\Vector3; use pocketmine\Player; @@ -59,9 +60,7 @@ class SetWorldSpawnCommand extends VanillaCommand{ $level = $sender->getServer()->getDefaultLevel(); $pos = new Vector3($this->getInteger($sender, $args[0]), $this->getInteger($sender, $args[1]), $this->getInteger($sender, $args[2])); }else{ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $level->setSpawnLocation($pos); diff --git a/src/pocketmine/command/defaults/SpawnpointCommand.php b/src/pocketmine/command/defaults/SpawnpointCommand.php index 05791f250..b0e3b194b 100644 --- a/src/pocketmine/command/defaults/SpawnpointCommand.php +++ b/src/pocketmine/command/defaults/SpawnpointCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\level\Level; use pocketmine\level\Position; @@ -94,8 +95,6 @@ class SpawnpointCommand extends VanillaCommand{ } } - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } } diff --git a/src/pocketmine/command/defaults/TeleportCommand.php b/src/pocketmine/command/defaults/TeleportCommand.php index d43a4bcce..874995287 100644 --- a/src/pocketmine/command/defaults/TeleportCommand.php +++ b/src/pocketmine/command/defaults/TeleportCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\math\Vector3; use pocketmine\Player; @@ -50,9 +51,7 @@ class TeleportCommand extends VanillaCommand{ return strlen($arg) > 0; }); if(count($args) < 1 or count($args) > 6){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $target = null; @@ -121,8 +120,6 @@ class TeleportCommand extends VanillaCommand{ return true; } - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } } diff --git a/src/pocketmine/command/defaults/TellCommand.php b/src/pocketmine/command/defaults/TellCommand.php index 5ef903ee7..3b51993e6 100644 --- a/src/pocketmine/command/defaults/TellCommand.php +++ b/src/pocketmine/command/defaults/TellCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -46,9 +47,7 @@ class TellCommand extends VanillaCommand{ } if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } $name = strtolower(array_shift($args)); diff --git a/src/pocketmine/command/defaults/TimeCommand.php b/src/pocketmine/command/defaults/TimeCommand.php index bb6124389..5c559734a 100644 --- a/src/pocketmine/command/defaults/TimeCommand.php +++ b/src/pocketmine/command/defaults/TimeCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\level\Level; use pocketmine\Player; @@ -43,9 +44,7 @@ class TimeCommand extends VanillaCommand{ public function execute(CommandSender $sender, $currentAlias, array $args){ if(count($args) < 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } if($args[0] === "start"){ @@ -91,9 +90,7 @@ class TimeCommand extends VanillaCommand{ if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); } if($args[0] === "set"){ @@ -132,7 +129,7 @@ class TimeCommand extends VanillaCommand{ } Command::broadcastCommandMessage($sender, new TranslationContainer("commands.time.added", [$value])); }else{ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); + throw new InvalidCommandSyntaxException(); } return true; diff --git a/src/pocketmine/command/defaults/TimingsCommand.php b/src/pocketmine/command/defaults/TimingsCommand.php index 0ea2bcd65..142bc9788 100644 --- a/src/pocketmine/command/defaults/TimingsCommand.php +++ b/src/pocketmine/command/defaults/TimingsCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TimingsHandler; use pocketmine\event\TranslationContainer; use pocketmine\Player; @@ -49,9 +50,7 @@ class TimingsCommand extends VanillaCommand{ } if(count($args) !== 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return true; + throw new InvalidCommandSyntaxException(); } $mode = strtolower($args[0]); diff --git a/src/pocketmine/command/defaults/TitleCommand.php b/src/pocketmine/command/defaults/TitleCommand.php index dc432ce51..a2038518d 100644 --- a/src/pocketmine/command/defaults/TitleCommand.php +++ b/src/pocketmine/command/defaults/TitleCommand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; class TitleCommand extends VanillaCommand{ @@ -43,8 +44,7 @@ class TitleCommand extends VanillaCommand{ } if(count($args) < 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return true; + throw new InvalidCommandSyntaxException(); } $player = $sender->getServer()->getPlayer($args[0]); @@ -62,39 +62,34 @@ class TitleCommand extends VanillaCommand{ break; case "title": if(count($args) < 3){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return false; + throw new InvalidCommandSyntaxException(); } $player->addTitle(implode(" ", array_slice($args, 2))); break; case "subtitle": if(count($args) < 3){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return false; + throw new InvalidCommandSyntaxException(); } $player->addSubTitle(implode(" ", array_slice($args, 2))); break; case "actionbar": if(count($args) < 3){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return false; + throw new InvalidCommandSyntaxException(); } $player->addActionBarMessage(implode(" ", array_slice($args, 2))); break; case "times": if(count($args) < 4){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return false; + throw new InvalidCommandSyntaxException(); } $player->setTitleDuration($this->getInteger($sender, $args[2]), $this->getInteger($sender, $args[3]), $this->getInteger($sender, $args[4])); break; default: - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return false; + throw new InvalidCommandSyntaxException(); } $sender->sendMessage(new TranslationContainer("commands.title.success")); diff --git a/src/pocketmine/command/defaults/TransferServerCommand.php b/src/pocketmine/command/defaults/TransferServerCommand.php index 78f4b0f4b..5925a96dc 100644 --- a/src/pocketmine/command/defaults/TransferServerCommand.php +++ b/src/pocketmine/command/defaults/TransferServerCommand.php @@ -26,7 +26,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; -use pocketmine\event\TranslationContainer; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\Player; class TransferServerCommand extends VanillaCommand{ @@ -42,9 +42,7 @@ class TransferServerCommand extends VanillaCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ if(count($args) < 1){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - - return false; + throw new InvalidCommandSyntaxException(); }elseif(!($sender instanceof Player)){ $sender->sendMessage("This command must be executed as a player"); diff --git a/src/pocketmine/command/defaults/WhitelistCommand.php b/src/pocketmine/command/defaults/WhitelistCommand.php index 05583b211..8bb8e95e5 100644 --- a/src/pocketmine/command/defaults/WhitelistCommand.php +++ b/src/pocketmine/command/defaults/WhitelistCommand.php @@ -25,6 +25,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\event\TranslationContainer; use pocketmine\utils\TextFormat; @@ -45,8 +46,7 @@ class WhitelistCommand extends VanillaCommand{ } if(count($args) === 0 or count($args) > 2){ - $sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage])); - return true; + throw new InvalidCommandSyntaxException(); } if(count($args) === 1){ diff --git a/src/pocketmine/command/utils/CommandException.php b/src/pocketmine/command/utils/CommandException.php new file mode 100644 index 000000000..192aa9b0d --- /dev/null +++ b/src/pocketmine/command/utils/CommandException.php @@ -0,0 +1,28 @@ +