Skip to content

Commit

Permalink
Now using ktpmpl-cfs virion, code rewrite and added ClearConsole::cle…
Browse files Browse the repository at this point in the history
…ar() API
  • Loading branch information
KygekDev committed Feb 8, 2022
1 parent 69ed22e commit 23de9f7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
7 changes: 5 additions & 2 deletions .poggit.yml
Original file line number Diff line number Diff line change
@@ -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
...
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 0 additions & 11 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
41 changes: 17 additions & 24 deletions src/KygekTeam/KygekClearConsole/ClearConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,47 @@

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;

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;
}

}

0 comments on commit 23de9f7

Please sign in to comment.