mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
Added /ban, /ban-ip, /pardon, /pardon-ip
This commit is contained in:
parent
1ff381ebf4
commit
befe1c606f
@ -608,8 +608,12 @@ class Server{
|
||||
if(file_exists($this->dataPath . "banned.txt") and !file_exists($this->dataPath . "banned-players.txt")){
|
||||
@rename($this->dataPath . "banned.txt", $this->dataPath . "banned-players.txt");
|
||||
}
|
||||
@touch($this->dataPath . "banned-players.txt");
|
||||
$this->banByName = new BanList($this->dataPath . "banned-players.txt");
|
||||
$this->banByName->load();
|
||||
@touch($this->dataPath . "banned-ips.txt");
|
||||
$this->banByIP = new BanList($this->dataPath . "banned-ips.txt");
|
||||
$this->banByIP->load();
|
||||
|
||||
$this->tickScheduler = new TickScheduler(20);
|
||||
$this->scheduler = new ServerScheduler();
|
||||
|
@ -21,14 +21,20 @@
|
||||
|
||||
namespace PocketMine\Command;
|
||||
|
||||
use PocketMine\Command\Defaults\BanCommand;
|
||||
use PocketMine\Command\Defaults\BanIpCommand;
|
||||
use PocketMine\Command\Defaults\BanListCommand;
|
||||
use PocketMine\Command\Defaults\DefaultGamemodeCommand;
|
||||
use PocketMine\Command\Defaults\HelpCommand;
|
||||
use PocketMine\Command\Defaults\PardonCommand;
|
||||
use PocketMine\Command\Defaults\PardonIpCommand;
|
||||
use PocketMine\Command\Defaults\PluginsCommand;
|
||||
use PocketMine\Command\Defaults\SeedCommand;
|
||||
use PocketMine\Command\Defaults\StopCommand;
|
||||
use PocketMine\Command\Defaults\TellCommand;
|
||||
use PocketMine\Command\Defaults\VanillaCommand;
|
||||
use PocketMine\Command\Defaults\VersionCommand;
|
||||
use PocketMine\Permission\BanList;
|
||||
use PocketMine\Server;
|
||||
|
||||
class SimpleCommandMap implements CommandMap{
|
||||
@ -55,6 +61,11 @@ class SimpleCommandMap implements CommandMap{
|
||||
$this->register("pocketmine", new StopCommand("stop"));
|
||||
$this->register("pocketmine", new TellCommand("tell"));
|
||||
$this->register("pocketmine", new DefaultGamemodeCommand("defaultgamemode"));
|
||||
$this->register("pocketmine", new BanCommand("ban"));
|
||||
$this->register("pocketmine", new BanIpCommand("ban-ip"));
|
||||
$this->register("pocketmine", new BanListCommand("banlist"));
|
||||
$this->register("pocketmine", new PardonCommand("pardon"));
|
||||
$this->register("pocketmine", new PardonIpCommand("pardon-ip"));
|
||||
}
|
||||
|
||||
|
||||
|
63
src/PocketMine/command/defaults/BanCommand.php
Normal file
63
src/PocketMine/command/defaults/BanCommand.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace PocketMine\Command\Defaults;
|
||||
|
||||
use PocketMine\Command\Command;
|
||||
use PocketMine\Command\CommandSender;
|
||||
use PocketMine\Player;
|
||||
use PocketMine\Server;
|
||||
use PocketMine\Utils\TextFormat;
|
||||
|
||||
class BanCommand extends VanillaCommand{
|
||||
|
||||
public function __construct($name){
|
||||
parent::__construct(
|
||||
$name,
|
||||
"Prevents the specified player from using this server",
|
||||
"/ban <player> [reason...]"
|
||||
);
|
||||
$this->setPermission("pocketmine.command.ban.player");
|
||||
}
|
||||
|
||||
public function execute(CommandSender $sender, $currentAlias, array $args){
|
||||
if(!$this->testPermission($sender)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(count($args) === 0){
|
||||
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
$name = array_shift($args);
|
||||
$reason = implode(" ", $args);
|
||||
|
||||
Server::getInstance()->getNameBans()->addBan($name, $reason, null, $sender->getName());
|
||||
|
||||
if(($player = Player::get($name, true)) instanceof Player){
|
||||
$player->kick("Banned by admin.");
|
||||
}
|
||||
|
||||
Command::broadcastCommandMessage($sender, "Banned player ". $name);
|
||||
return true;
|
||||
}
|
||||
}
|
80
src/PocketMine/command/defaults/BanIpCommand.php
Normal file
80
src/PocketMine/command/defaults/BanIpCommand.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace PocketMine\Command\Defaults;
|
||||
|
||||
use PocketMine\Command\Command;
|
||||
use PocketMine\Command\CommandSender;
|
||||
use PocketMine\Player;
|
||||
use PocketMine\Server;
|
||||
use PocketMine\Utils\TextFormat;
|
||||
|
||||
class BanIpCommand extends VanillaCommand{
|
||||
|
||||
public function __construct($name){
|
||||
parent::__construct(
|
||||
$name,
|
||||
"Prevents the specified IP address from using this server",
|
||||
"/ban <address|player> [reason...]"
|
||||
);
|
||||
$this->setPermission("pocketmine.command.ban.ip");
|
||||
}
|
||||
|
||||
public function execute(CommandSender $sender, $currentAlias, array $args){
|
||||
if(!$this->testPermission($sender)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(count($args) === 0){
|
||||
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = array_shift($args);
|
||||
$reason = implode(" ", $args);
|
||||
|
||||
if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $value)){
|
||||
$this->processIPBan($value, $sender, $reason);
|
||||
}else{
|
||||
if(($player = Player::get($name, true)) instanceof Player){
|
||||
$this->processIPBan($player->getIP(), $sender, $reason);
|
||||
}else{
|
||||
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Command::broadcastCommandMessage($sender, "Banned player ". $name);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function processIPBan($ip, CommandSender $sender, $reason){
|
||||
Server::getInstance()->getIPBans()->addBan($ip, $reason, null, $sender->getName());
|
||||
|
||||
foreach(Player::getAll() as $player){
|
||||
if($player->getIP() === $ip){
|
||||
$player->kick("You have been IP banned.");
|
||||
}
|
||||
}
|
||||
|
||||
Command::broadcastCommandMessage($sender, "Banned IP Address ". $ip);
|
||||
}
|
||||
}
|
65
src/PocketMine/command/defaults/BanListCommand.php
Normal file
65
src/PocketMine/command/defaults/BanListCommand.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace PocketMine\Command\Defaults;
|
||||
|
||||
use PocketMine\Command\Command;
|
||||
use PocketMine\Command\CommandSender;
|
||||
use PocketMine\Player;
|
||||
use PocketMine\Server;
|
||||
use PocketMine\Utils\TextFormat;
|
||||
|
||||
class BanListCommand extends VanillaCommand{
|
||||
|
||||
public function __construct($name){
|
||||
parent::__construct(
|
||||
$name,
|
||||
"View all players banned from this server",
|
||||
"/banlist [ips|players]"
|
||||
);
|
||||
$this->setPermission("pocketmine.command.ban.list");
|
||||
}
|
||||
|
||||
public function execute(CommandSender $sender, $currentAlias, array $args){
|
||||
if(!$this->testPermission($sender)){
|
||||
return true;
|
||||
}
|
||||
$list = Server::getInstance()->getNameBans();
|
||||
if(isset($args[0])){
|
||||
$args[0] = strtolower($args[0]);
|
||||
if($args[0] === "ips"){
|
||||
$list = Server::getInstance()->getIPBans();
|
||||
}elseif($args[0] === "players"){
|
||||
$list = Server::getInstance()->getNameBans();
|
||||
}
|
||||
}
|
||||
|
||||
$message = "";
|
||||
$list = $list->getEntries();
|
||||
foreach($list as $entry){
|
||||
$message .= $entry->getName() . ", ";
|
||||
}
|
||||
|
||||
$sender->sendMessage("There are ". count($list)." total banned players:");
|
||||
$sender->sendMessage(substr($message, 0, -2));
|
||||
return true;
|
||||
}
|
||||
}
|
56
src/PocketMine/command/defaults/PardonCommand.php
Normal file
56
src/PocketMine/command/defaults/PardonCommand.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace PocketMine\Command\Defaults;
|
||||
|
||||
use PocketMine\Command\Command;
|
||||
use PocketMine\Command\CommandSender;
|
||||
use PocketMine\Player;
|
||||
use PocketMine\Server;
|
||||
use PocketMine\Utils\TextFormat;
|
||||
|
||||
class PardonCommand extends VanillaCommand{
|
||||
|
||||
public function __construct($name){
|
||||
parent::__construct(
|
||||
$name,
|
||||
"Allows the specified player to use this server",
|
||||
"/pardon <player>"
|
||||
);
|
||||
$this->setPermission("pocketmine.command.unban.player");
|
||||
}
|
||||
|
||||
public function execute(CommandSender $sender, $currentAlias, array $args){
|
||||
if(!$this->testPermission($sender)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(count($args) !== 1){
|
||||
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
Server::getInstance()->getNameBans()->remove($args[0]);
|
||||
|
||||
Command::broadcastCommandMessage($sender, "Pardoned ". $name);
|
||||
return true;
|
||||
}
|
||||
}
|
60
src/PocketMine/command/defaults/PardonIpCommand.php
Normal file
60
src/PocketMine/command/defaults/PardonIpCommand.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace PocketMine\Command\Defaults;
|
||||
|
||||
use PocketMine\Command\Command;
|
||||
use PocketMine\Command\CommandSender;
|
||||
use PocketMine\Player;
|
||||
use PocketMine\Server;
|
||||
use PocketMine\Utils\TextFormat;
|
||||
|
||||
class PardonIpCommand extends VanillaCommand{
|
||||
|
||||
public function __construct($name){
|
||||
parent::__construct(
|
||||
$name,
|
||||
"Allows the specified IP address to use this server",
|
||||
"/pardon-ip <address>"
|
||||
);
|
||||
$this->setPermission("pocketmine.command.unban.ip");
|
||||
}
|
||||
|
||||
public function execute(CommandSender $sender, $currentAlias, array $args){
|
||||
if(!$this->testPermission($sender)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(count($args) !== 1){
|
||||
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $args[0])){
|
||||
Server::getInstance()->getIPBans()->remove($args[0]);
|
||||
Command::broadcastCommandMessage($sender, "Pardoned IP ". $name);
|
||||
}else{
|
||||
$sender->sendMessage("Invalid IP");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -61,7 +61,10 @@ class BanEntry{
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
public function setExpires(\DateTime $date){
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
*/
|
||||
public function setExpires($date){
|
||||
$this->expirationDate = $date;
|
||||
}
|
||||
|
||||
@ -89,7 +92,6 @@ class BanEntry{
|
||||
$str .= $this->getExpires() === null ? "Forever" : $this->getExpires()->format(self::$format);
|
||||
$str .= "|";
|
||||
$str .= $this->getReason();
|
||||
$str .= "|";
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ use PocketMine\Server;
|
||||
|
||||
class BanList{
|
||||
|
||||
/** @var string[] */
|
||||
/** @var BanEntry[] */
|
||||
private $list = array();
|
||||
|
||||
/** @var string */
|
||||
@ -56,7 +56,7 @@ class BanList{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @return BanEntry[]
|
||||
*/
|
||||
public function getEntries(){
|
||||
$this->removeExpired();
|
||||
@ -86,6 +86,25 @@ class BanList{
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $target
|
||||
* @param string $reason
|
||||
* @param \DateTime $expires
|
||||
* @param string $source
|
||||
*
|
||||
* @return BanEntry
|
||||
*/
|
||||
public function addBan($target, $reason = null, $expires = null, $source = null){
|
||||
$entry = new BanEntry($target);
|
||||
$entry->setSource($source != null ? $source : $entry->getSource());
|
||||
$entry->setExpires($expires);
|
||||
$entry->setReason($reason != null ? $reason : $entry->getReason());
|
||||
|
||||
$this->list[$entry->getName()] = $entry;
|
||||
$this->save();
|
||||
return $entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user