|
24 | 24 | namespace pocketmine\command\defaults; |
25 | 25 |
|
26 | 26 | use pocketmine\command\CommandSender; |
27 | | -use pocketmine\lang\KnownTranslationFactory; |
| 27 | +use pocketmine\lang\KnownTranslationFactory as l10n; |
| 28 | +use pocketmine\lang\Translatable; |
28 | 29 | use pocketmine\permission\DefaultPermissionNames; |
29 | 30 | use pocketmine\utils\Process; |
30 | 31 | use pocketmine\utils\TextFormat; |
|
33 | 34 | use function microtime; |
34 | 35 | use function number_format; |
35 | 36 | use function round; |
| 37 | +use function strval; |
36 | 38 |
|
37 | 39 | class StatusCommand extends VanillaCommand{ |
38 | 40 |
|
39 | 41 | public function __construct(string $namespace, string $name){ |
40 | 42 | parent::__construct( |
41 | 43 | $namespace, |
42 | 44 | $name, |
43 | | - KnownTranslationFactory::pocketmine_command_status_description() |
| 45 | + l10n::pocketmine_command_status_description() |
44 | 46 | ); |
45 | 47 | $this->setPermission(DefaultPermissionNames::COMMAND_STATUS); |
46 | 48 | } |
47 | 49 |
|
| 50 | + private static function send(CommandSender $sender, Translatable $message) : void{ |
| 51 | + $sender->sendMessage($message->prefix(TextFormat::GOLD)); |
| 52 | + } |
| 53 | + |
| 54 | + private static function formatTPS(float $tps, float $usage, string $tpsColor) : Translatable{ |
| 55 | + return l10n::pocketmine_command_status_tps_stat(strval($tps), strval($usage))->prefix($tpsColor); |
| 56 | + } |
| 57 | + |
| 58 | + private static function formatBandwidth(float $bytes) : Translatable{ |
| 59 | + //TODO: this should probably be number formatted? |
| 60 | + return l10n::pocketmine_command_status_network_stat(strval(round($bytes / 1024, 2)))->prefix(TextFormat::RED); |
| 61 | + } |
| 62 | + |
| 63 | + private static function formatMemory(int $bytes) : Translatable{ |
| 64 | + return l10n::pocketmine_command_status_memory_stat(number_format(round(($bytes / 1024) / 1024, 2), 2))->prefix(TextFormat::RED); |
| 65 | + } |
| 66 | + |
48 | 67 | public function execute(CommandSender $sender, string $commandLabel, array $args){ |
49 | 68 | $mUsage = Process::getAdvancedMemoryUsage(); |
50 | 69 |
|
51 | 70 | $server = $sender->getServer(); |
52 | | - $sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::RESET . "Server status" . TextFormat::GREEN . " ----"); |
| 71 | + $sender->sendMessage(l10n::pocketmine_command_status_header()->format( |
| 72 | + TextFormat::GREEN . "---- " . TextFormat::RESET, |
| 73 | + TextFormat::GREEN . " ----" . TextFormat::RESET |
| 74 | + )); |
53 | 75 |
|
54 | 76 | $time = (int) (microtime(true) - $server->getStartTime()); |
55 | 77 |
|
56 | | - $seconds = $time % 60; |
57 | | - $minutes = null; |
58 | | - $hours = null; |
59 | | - $days = null; |
60 | | - |
| 78 | + $seconds = strval($time % 60); |
61 | 79 | if($time >= 60){ |
62 | | - $minutes = floor(($time % 3600) / 60); |
| 80 | + $minutes = strval(floor(($time % 3600) / 60)); |
63 | 81 | if($time >= 3600){ |
64 | | - $hours = floor(($time % (3600 * 24)) / 3600); |
65 | | - if($time >= 3600 * 24){ |
66 | | - $days = floor($time / (3600 * 24)); |
67 | | - } |
| 82 | + $hours = strval(floor(($time % (3600 * 24)) / 3600)); |
| 83 | + $message = $time >= 3600 * 24 ? |
| 84 | + l10n::pocketmine_command_status_uptime_days(strval(floor($time / (3600 * 24))), $hours, $minutes, $seconds) : |
| 85 | + l10n::pocketmine_command_status_uptime_hours($hours, $minutes, $seconds); |
| 86 | + }else{ |
| 87 | + $message = l10n::pocketmine_command_status_uptime_minutes($minutes, $seconds); |
68 | 88 | } |
| 89 | + }else{ |
| 90 | + $message = l10n::pocketmine_command_status_uptime_seconds($seconds); |
69 | 91 | } |
70 | 92 |
|
71 | | - $uptime = ($minutes !== null ? |
72 | | - ($hours !== null ? |
73 | | - ($days !== null ? |
74 | | - "$days days " |
75 | | - : "") . "$hours hours " |
76 | | - : "") . "$minutes minutes " |
77 | | - : "") . "$seconds seconds"; |
78 | | - |
79 | | - $sender->sendMessage(TextFormat::GOLD . "Uptime: " . TextFormat::RED . $uptime); |
| 93 | + self::send($sender, l10n::pocketmine_command_status_uptime($message->prefix(TextFormat::RED))); |
80 | 94 |
|
81 | 95 | $tpsColor = TextFormat::GREEN; |
82 | | - if($server->getTicksPerSecond() < 12){ |
| 96 | + $tps = $server->getTicksPerSecond(); |
| 97 | + if($tps < 12){ |
83 | 98 | $tpsColor = TextFormat::RED; |
84 | | - }elseif($server->getTicksPerSecond() < 17){ |
| 99 | + }elseif($tps < 17){ |
85 | 100 | $tpsColor = TextFormat::GOLD; |
86 | 101 | } |
87 | 102 |
|
88 | | - $sender->sendMessage(TextFormat::GOLD . "Current TPS: {$tpsColor}{$server->getTicksPerSecond()} ({$server->getTickUsage()}%)"); |
89 | | - $sender->sendMessage(TextFormat::GOLD . "Average TPS: {$tpsColor}{$server->getTicksPerSecondAverage()} ({$server->getTickUsageAverage()}%)"); |
| 103 | + self::send($sender, l10n::pocketmine_command_status_tps_current(self::formatTPS($tps, $server->getTickUsage(), $tpsColor))); |
| 104 | + self::send($sender, l10n::pocketmine_command_status_tps_average(self::formatTPS($server->getTicksPerSecondAverage(), $server->getTickUsageAverage(), $tpsColor))); |
90 | 105 |
|
91 | 106 | $bandwidth = $server->getNetwork()->getBandwidthTracker(); |
92 | | - $sender->sendMessage(TextFormat::GOLD . "Network upload: " . TextFormat::RED . round($bandwidth->getSend()->getAverageBytes() / 1024, 2) . " kB/s"); |
93 | | - $sender->sendMessage(TextFormat::GOLD . "Network download: " . TextFormat::RED . round($bandwidth->getReceive()->getAverageBytes() / 1024, 2) . " kB/s"); |
| 107 | + self::send($sender, l10n::pocketmine_command_status_network_upload(self::formatBandwidth($bandwidth->getSend()->getAverageBytes()))); |
| 108 | + self::send($sender, l10n::pocketmine_command_status_network_download(self::formatBandwidth($bandwidth->getReceive()->getAverageBytes()))); |
94 | 109 |
|
95 | | - $sender->sendMessage(TextFormat::GOLD . "Thread count: " . TextFormat::RED . Process::getThreadCount()); |
| 110 | + self::send($sender, l10n::pocketmine_command_status_threads(TextFormat::RED . Process::getThreadCount())); |
96 | 111 |
|
97 | | - $sender->sendMessage(TextFormat::GOLD . "Main thread memory: " . TextFormat::RED . number_format(round(($mUsage[0] / 1024) / 1024, 2), 2) . " MB."); |
98 | | - $sender->sendMessage(TextFormat::GOLD . "Total memory: " . TextFormat::RED . number_format(round(($mUsage[1] / 1024) / 1024, 2), 2) . " MB."); |
99 | | - $sender->sendMessage(TextFormat::GOLD . "Total virtual memory: " . TextFormat::RED . number_format(round(($mUsage[2] / 1024) / 1024, 2), 2) . " MB."); |
| 112 | + self::send($sender, l10n::pocketmine_command_status_memory_mainThread(self::formatMemory($mUsage[0]))); |
| 113 | + self::send($sender, l10n::pocketmine_command_status_memory_total(self::formatMemory($mUsage[1]))); |
| 114 | + self::send($sender, l10n::pocketmine_command_status_memory_virtual(self::formatMemory($mUsage[2]))); |
100 | 115 |
|
101 | 116 | $globalLimit = $server->getMemoryManager()->getGlobalMemoryLimit(); |
102 | 117 | if($globalLimit > 0){ |
103 | | - $sender->sendMessage(TextFormat::GOLD . "Maximum memory (manager): " . TextFormat::RED . number_format(round(($globalLimit / 1024) / 1024, 2), 2) . " MB."); |
| 118 | + self::send($sender, l10n::pocketmine_command_status_memory_manager(self::formatMemory($globalLimit))); |
104 | 119 | } |
105 | 120 |
|
106 | 121 | foreach($server->getWorldManager()->getWorlds() as $world){ |
107 | 122 | $worldName = $world->getFolderName() !== $world->getDisplayName() ? " (" . $world->getDisplayName() . ")" : ""; |
108 | 123 | $timeColor = $world->getTickRateTime() > 40 ? TextFormat::RED : TextFormat::YELLOW; |
109 | | - $sender->sendMessage(TextFormat::GOLD . "World \"{$world->getFolderName()}\"$worldName: " . |
110 | | - TextFormat::RED . number_format(count($world->getLoadedChunks())) . TextFormat::GREEN . " loaded chunks, " . |
111 | | - TextFormat::RED . number_format(count($world->getTickingChunks())) . TextFormat::GREEN . " ticking chunks, " . |
112 | | - TextFormat::RED . number_format(count($world->getEntities())) . TextFormat::GREEN . " entities. " . |
113 | | - "Time $timeColor" . round($world->getTickRateTime(), 2) . "ms" |
114 | | - ); |
| 124 | + self::send($sender, l10n::pocketmine_command_status_world( |
| 125 | + "\"{$world->getFolderName()}\"$worldName", |
| 126 | + TextFormat::RED . number_format(count($world->getLoadedChunks())) . TextFormat::GREEN, |
| 127 | + TextFormat::RED . number_format(count($world->getTickingChunks())) . TextFormat::GREEN, |
| 128 | + TextFormat::RED . number_format(count($world->getEntities())) . TextFormat::GREEN, |
| 129 | + l10n::pocketmine_command_status_world_timeStat(strval(round($world->getTickRateTime(), 2)))->prefix($timeColor) |
| 130 | + )); |
115 | 131 | } |
116 | 132 |
|
117 | 133 | return true; |
|
0 commit comments