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