PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
WhitelistCommand.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
34use function count;
35use function implode;
36use function sort;
37use function strtolower;
38use const SORT_STRING;
39
41
42 public function __construct(){
43 parent::__construct(
44 "whitelist",
45 KnownTranslationFactory::pocketmine_command_whitelist_description(),
46 KnownTranslationFactory::commands_whitelist_usage()
47 );
48 $this->setPermissions([
49 DefaultPermissionNames::COMMAND_WHITELIST_RELOAD,
50 DefaultPermissionNames::COMMAND_WHITELIST_ENABLE,
51 DefaultPermissionNames::COMMAND_WHITELIST_DISABLE,
52 DefaultPermissionNames::COMMAND_WHITELIST_LIST,
53 DefaultPermissionNames::COMMAND_WHITELIST_ADD,
54 DefaultPermissionNames::COMMAND_WHITELIST_REMOVE
55 ]);
56 }
57
58 public function execute(CommandSender $sender, string $commandLabel, array $args){
59 if(count($args) === 1){
60 switch(strtolower($args[0])){
61 case "reload":
62 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_RELOAD)){
63 $server = $sender->getServer();
64 $server->getWhitelisted()->reload();
65 if($server->hasWhitelist()){
66 $this->kickNonWhitelistedPlayers($server);
67 }
68 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_reloaded());
69 }
70
71 return true;
72 case "on":
73 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_ENABLE)){
74 $server = $sender->getServer();
75 $server->getConfigGroup()->setConfigBool(ServerProperties::WHITELIST, true);
76 $this->kickNonWhitelistedPlayers($server);
77 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_enabled());
78 }
79
80 return true;
81 case "off":
82 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_DISABLE)){
83 $sender->getServer()->getConfigGroup()->setConfigBool(ServerProperties::WHITELIST, false);
84 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_disabled());
85 }
86
87 return true;
88 case "list":
89 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_LIST)){
90 $entries = $sender->getServer()->getWhitelisted()->getAll(true);
91 sort($entries, SORT_STRING);
92 $result = implode(", ", $entries);
93 $count = (string) count($entries);
94
95 $sender->sendMessage(KnownTranslationFactory::commands_whitelist_list($count, $count));
96 $sender->sendMessage($result);
97 }
98
99 return true;
100
101 case "add":
102 $sender->sendMessage(KnownTranslationFactory::commands_generic_usage(KnownTranslationFactory::commands_whitelist_add_usage()));
103 return true;
104
105 case "remove":
106 $sender->sendMessage(KnownTranslationFactory::commands_generic_usage(KnownTranslationFactory::commands_whitelist_remove_usage()));
107 return true;
108 }
109 }elseif(count($args) === 2){
110 if(!Player::isValidUserName($args[1])){
112 }
113 switch(strtolower($args[0])){
114 case "add":
115 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_ADD)){
116 $sender->getServer()->addWhitelist($args[1]);
117 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_add_success($args[1]));
118 }
119
120 return true;
121 case "remove":
122 if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_REMOVE)){
123 $server = $sender->getServer();
124 $server->removeWhitelist($args[1]);
125 if(!$server->isWhitelisted($args[1])){
126 $server->getPlayerExact($args[1])?->kick(KnownTranslationFactory::pocketmine_disconnect_kick(KnownTranslationFactory::pocketmine_disconnect_whitelisted()));
127 }
128 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_remove_success($args[1]));
129 }
130
131 return true;
132 }
133 }
134
136 }
137
138 private function kickNonWhitelistedPlayers(Server $server) : void{
139 $message = KnownTranslationFactory::pocketmine_disconnect_kick(KnownTranslationFactory::pocketmine_disconnect_whitelisted());
140 foreach($server->getOnlinePlayers() as $player){
141 if(!$server->isWhitelisted($player->getName())){
142 $player->kick($message);
143 }
144 }
145 }
146}
setPermissions(array $permissions)
Definition Command.php:103
execute(CommandSender $sender, string $commandLabel, array $args)