From 23de9f78903bc0a60c833ce2695edaeab5810f1f Mon Sep 17 00:00:00 2001 From: KygekDev <68368066+KygekDev@users.noreply.github.com> Date: Tue, 8 Feb 2022 21:48:05 +0800 Subject: [PATCH] Now using ktpmpl-cfs virion, code rewrite and added ClearConsole::clear() API --- .poggit.yml | 7 +++- README.md | 3 +- plugin.yml | 11 ----- .../KygekClearConsole/ClearConsole.php | 41 ++++++++----------- 4 files changed, 24 insertions(+), 38 deletions(-) diff --git a/.poggit.yml b/.poggit.yml index b30e693..d14bb1c 100644 --- a/.poggit.yml +++ b/.poggit.yml @@ -1,11 +1,14 @@ --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/KygekTeam/KygekClearConsole build-by-default: true branches: - - main - - pm4 +- main +- pm4 projects: KygekClearConsole: path: "" + libs: + - src: KygekTeam/ktpmpl-cfs/ktpmpl-cfs + version: ^4.0.0 lint: directStdout: false ... diff --git a/README.md b/README.md index 5c870f9..32c7b12 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,14 @@ This plugin is not planned to be released on Poggit, because it may break Rule A ## ❔ How Does This Plugin Works -This plugin clears the console when the `clearconsole` command (Alias: `cc`) gets executed. This plugin prints `\e[H\e[J` character to console to clear the console. This plugin ensures the command will only show and work on console by modifying the `AvailableCommandsPacket` packet and by sending unknown command message whenever a player tries to execute the command. +This plugin clears the console when the `clearconsole` command (Alias: `cc`) gets executed. This plugin prints `\e[H\e[J` character to console to clear the console. This plugin ensures the command will only show and work on console by using `CommandEvent` to capture the command and cancelling the event to prevent unexpected outputs (e.g. Unknown command) if the sender is from the console. ## ✅ Features - Only works in console - Command gets hidden in command suggestions and help page (Player) - Sends unknown command message whenever a player tries to execute the command +- API to clear the console (`ClearConsole::clear()`) ## 🔧 Installation diff --git a/plugin.yml b/plugin.yml index 424c4b5..54be765 100644 --- a/plugin.yml +++ b/plugin.yml @@ -14,15 +14,4 @@ author: KygekTeam website: "https://github.com/KygekTeam/KygekClearConsole" api: 4.0.0 main: KygekTeam\KygekClearConsole\ClearConsole - -commands: - clearconsole: - description: Clears server console - permission: kygekclearconsole.cmd - aliases: [cc] - -# Dummy permission -permissions: - kygekclearconsole.cmd: - default: true ... \ No newline at end of file diff --git a/src/KygekTeam/KygekClearConsole/ClearConsole.php b/src/KygekTeam/KygekClearConsole/ClearConsole.php index e5e4400..fe5a0b9 100644 --- a/src/KygekTeam/KygekClearConsole/ClearConsole.php +++ b/src/KygekTeam/KygekClearConsole/ClearConsole.php @@ -14,13 +14,10 @@ namespace KygekTeam\KygekClearConsole; -use pocketmine\command\Command; -use pocketmine\command\CommandSender; +use KygekTeam\KtpmplCfs\KtpmplCfs; use pocketmine\console\ConsoleCommandSender; use pocketmine\event\Listener; -use pocketmine\event\server\DataPacketSendEvent; -use pocketmine\network\mcpe\protocol\AvailableCommandsPacket; -use pocketmine\network\mcpe\protocol\types\command\CommandData; +use pocketmine\event\server\CommandEvent; use pocketmine\plugin\PluginBase; use pocketmine\utils\TextFormat as TF; @@ -28,40 +25,36 @@ class ClearConsole extends PluginBase implements Listener { private const IS_DEV = false; - private const COMMAND = "clearconsole"; + private const COMMAND = ["clearconsole", "cc"]; private const CLEAR_CONSOLE_STRING = "\e[H\e[J"; protected function onEnable() : void { /** @phpstan-ignore-next-line */ if (self::IS_DEV) { - $this->getLogger()->warning("This plugin is running on a development version. There might be some major bugs. If you found one, please submit an issue in https://github.com/KygekTeam/KygekClearConsole/issues."); + (new KtpmplCfs($this))->warnDevelopmentVersion(); } $this->getServer()->getPluginManager()->registerEvents($this, $this); } - public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool { - if ($command->getName() === self::COMMAND) { - if (!$sender instanceof ConsoleCommandSender) { - $sender->sendMessage($this->getServer()->getLanguage()->translateString(TF::RED . "%commands.generic.notFound")); - return true; + /** + * @priority HIGHEST + */ + public function onCommandEvent(CommandEvent $event) { + if (in_array(mb_strtolower($event->getCommand()), self::COMMAND)) { + if ($event->getSender() instanceof ConsoleCommandSender) { + self::clear(); + // Prevent unexpected outputs + $event->cancel(); } - echo self::CLEAR_CONSOLE_STRING; } - return true; } /** - * @priority HIGHEST + * Clears the server console by printing a special string + * @return void */ - public function onPacketSend(DataPacketSendEvent $event) { - $pks = $event->getPackets(); - foreach ($pks as $pk) { - if ($pk instanceof AvailableCommandsPacket) { - $pk->commandData = array_filter($pk->commandData, function (CommandData $data) : bool { - return $data->getName() !== self::COMMAND; - }); - } - } + public static function clear() { + echo self::CLEAR_CONSOLE_STRING; } } \ No newline at end of file