Skip to content

Commit

Permalink
Add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 1, 2023
1 parent ec82f9a commit bfc31a7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 13 deletions.
10 changes: 7 additions & 3 deletions resources/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"message.cache.running": "§eTask is running! Please wait until it is finished",
"message.cache.failed": "§cCould not get Poggit plugins list from Poggit: {%reason}",
"message.cache.successfully": "§a({%count}) Poggit plugins has been cached successfully"
"text.cache.running": "§eTask is running! Please wait until it is finished",
"text.cache.failed": "§cCould not get Poggit plugins list from Poggit: {%reason}",
"text.cache.successfully": "§a({%count}) Poggit plugins has been cached successfully",

"command.version.api_version": "§aPocketMine-MP API v{{version}}",
"command.version.php_version": "§aPHP v{{version}}",
"command.version.plugin_version": "§aoh-my-pmmp v{{version}}"
}
12 changes: 5 additions & 7 deletions src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use pocketmine\plugin\PluginBase;
use pocketmine\utils\SingletonTrait;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\omp\commands\OMPCommand;
use thebigcrafter\omp\lang\OMPLanguage;
use thebigcrafter\omp\trait\Language;
use thebigcrafter\Sulfur\Path;
use function is_dir;
use function is_file;
Expand All @@ -23,8 +25,7 @@
final class OhMyPMMP extends PluginBase
{
use SingletonTrait;

private OMPLanguage $language;
use Language;

public function onLoad() : void
{
Expand All @@ -35,6 +36,7 @@ public function onEnable() : void
{
$this->saveDefaultConfig();
$this->loadLanguage();
$this->getServer()->getCommandMap()->register("OhMyPMMP", new OMPCommand($this, "ohmypmmp", "Oh My PMMP", ["omp", "oh-my-pmmp"]));

Fluorine::run();
}
Expand All @@ -58,10 +60,6 @@ public function loadLanguage() : void
}

OMPLanguage::loadLanguageFile($selectedLanguage, $selectedLanguagePath);
$this->language = new OMPLanguage($selectedLanguage);
}

public function getLanguage() : OMPLanguage {
return $this->language;
self::setLanguage(new OMPLanguage($selectedLanguage));
}
}
32 changes: 31 additions & 1 deletion src/thebigcrafter/omp/commands/OMPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,34 @@

namespace thebigcrafter\omp\commands;

class OMPCommand {}
use CortexPE\Commando\BaseCommand;
use pocketmine\command\CommandSender;
use thebigcrafter\omp\commands\subcommands\VersionCommand;
use thebigcrafter\omp\OhMyPMMP;

class OMPCommand extends BaseCommand {
private string $permission = "oh-my-pmmp.cmds";

protected function prepare() : void {
$this->setPermission($this->permission);

$subcommands = [
new VersionCommand(OhMyPMMP::getInstance(),"version", "Get plugin version", ["v", "-v", "--version"]),
];

foreach ($subcommands as $subcommand) {
$this->registerSubcommand($subcommand);
}
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void {
$this->sendUsage();
}

public function getPermission() : string {
return $this->permission;
}
}
37 changes: 37 additions & 0 deletions src/thebigcrafter/omp/commands/subcommands/VersionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\commands\subcommands;

use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\Server;
use thebigcrafter\omp\OhMyPMMP;
use function phpversion;

class VersionCommand extends BaseSubCommand {
protected function prepare() : void {
$this->setPermission("oh-my-pmmp.version");
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender,string $aliasUsed,array $args) : void {
$apiVersion = Server::getInstance()->getApiVersion();
$phpVersion = phpversion();
$pluginVersion = OhMyPMMP::getInstance()->getDescription()->getVersion();

$sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.version.api_version", ["version" => $apiVersion]));
$sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.version.php_version", ["version" => $phpVersion]));
$sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.version.plugin_version", ["version" => $pluginVersion]));
}
}
14 changes: 12 additions & 2 deletions src/thebigcrafter/omp/lang/OMPLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function array_key_exists;
use function file_exists;
use function json_decode;
use function str_replace;

class OMPLanguage
{
Expand Down Expand Up @@ -50,12 +51,21 @@ public function setDefaultLanguage(string $name) : void
$this->default = $name;
}

public function get(string $key) : string
/**
* @param array<string, string> $placeholders
*/
public function translate(string $key, array $placeholders = []) : string
{
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */
throw new Exception("$key not found");
}

return self::$languages[$this->default][$key]; /** @phpstan-ignore-line */
$translation = self::$languages[$this->default][$key]; /** @phpstan-ignore-line */

foreach ($placeholders as $i => $v) {
$translation = str_replace("{{" . $i . "}}", (string) $v, $translation);
}

return $translation;
}
}
26 changes: 26 additions & 0 deletions src/thebigcrafter/omp/trait/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\trait;

use thebigcrafter\omp\lang\OMPLanguage;

trait Language {
private static OMPLanguage $language;

public static function setLanguage(OMPLanguage $language) : void {
self::$language = $language;
}

public static function getLanguage() : OMPLanguage {
return self::$language;
}
}

0 comments on commit bfc31a7

Please sign in to comment.