PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
BanIpCommand.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
32use function array_shift;
33use function count;
34use function implode;
35use function inet_pton;
36
38
39 public function __construct(){
40 parent::__construct(
41 "ban-ip",
42 KnownTranslationFactory::pocketmine_command_ban_ip_description(),
43 KnownTranslationFactory::commands_banip_usage()
44 );
45 $this->setPermission(DefaultPermissionNames::COMMAND_BAN_IP);
46 }
47
48 public function execute(CommandSender $sender, string $commandLabel, array $args){
49 if(count($args) === 0){
51 }
52
53 $value = array_shift($args);
54 $reason = implode(" ", $args);
55
56 if(inet_pton($value) !== false){
57 $this->processIPBan($value, $sender, $reason);
58
59 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_banip_success($value));
60 }else{
61 if(($player = $sender->getServer()->getPlayerByPrefix($value)) instanceof Player){
62 $ip = $player->getNetworkSession()->getIp();
63 $this->processIPBan($ip, $sender, $reason);
64
65 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_banip_success_players($ip, $player->getName()));
66 }else{
67 $sender->sendMessage(KnownTranslationFactory::commands_banip_invalid());
68
69 return false;
70 }
71 }
72
73 return true;
74 }
75
76 private function processIPBan(string $ip, CommandSender $sender, string $reason) : void{
77 $sender->getServer()->getIPBans()->addBan($ip, $reason, null, $sender->getName());
78
79 foreach($sender->getServer()->getOnlinePlayers() as $player){
80 if($player->getNetworkSession()->getIp() === $ip){
81 $player->kick(KnownTranslationFactory::pocketmine_disconnect_ban($reason !== "" ? $reason : KnownTranslationFactory::pocketmine_disconnect_ban_ip()));
82 }
83 }
84
85 $sender->getServer()->getNetwork()->blockAddress($ip, -1);
86 }
87}
execute(CommandSender $sender, string $commandLabel, array $args)