Skip to content

Instantly share code, notes, and snippets.

@Omattyao
Last active December 22, 2015 02:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Omattyao/6403065 to your computer and use it in GitHub Desktop.
Save Omattyao/6403065 to your computer and use it in GitHub Desktop.
<?php
/*
__PocketMine Plugin__
name=PermissionPlus
description=Manages player and command permissions.
version=1.0.7
apiversion=7,8,9,10
author=Omattyao
class=PermissionPlus
*/
class PermissionPlus implements Plugin{
private $api, $path, $config;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
public function init(){
$this->createConfig();
$this->api->addHandler('console.command', array($this, 'handle'), 15);
$this->api->addHandler('get.player.permission', array($this, 'returnPermission'), 1);
$this->api->event('player.spawn', array($this, 'handle'));
$this->api->console->register('ppplayer', '[Permission+] Manages player permission.', array($this, 'commandH'));
$this->api->console->register('ppcommand', '[Permission+] Manages command permission.', array($this, 'commandH'));
$this->api->console->register('ppconfig', '[Permission+] PermissionPlus preferences.', array($this, 'commandH'));
$this->api->console->alias("ppplyr", "ppplayer");
$this->api->console->alias("ppcmd", "ppcommand");
$this->api->console->alias("ppcfg", "ppconfig");
}
public function __destruct(){
}
public function handle($data, $event)
{
switch ($event) {
case "console.command":
if (!($data['issuer'] instanceof Player)) break;
$user = $data['issuer']->username;
$c_check = $this->checkPermission($user, $data['cmd'], $this->config["notice"]);
if ($data['alias'] !== false) {
$a_check = $this->checkPermission($user, $data['alias'], false);
} else {
$a_check = true;
}
if (!$c_check or !$a_check) return false;
break;
case "player.spawn":
$user = $data->username;
if ($this->getUserPermission($user) === false) {
$this->config['player'][$user] = 'GUEST';
$this->writeConfig();
}
if ($this->config["autoop"]) {
$this->giveOP($user);
}
break;
}
}
public function commandH($cmd, $params, $issuer, $alias)
{
$output = "";
if ($issuer instanceof Player) {
$username = $issuer->username;
}
switch ($cmd) {
case "ppplayer":
$player = array_shift($params);
$permission = array_shift($params);
if (empty($player) or empty($permission)) {
if ($issuer === "console") {
$this->showPPermissionsList();
}
$msg = $this->permissionUsage("p");
$output .= "Usage: /ppplayer <player> $msg\n";
break;
}
if (!$this->hackChecker()) break;
$this->setPPermission($player, $permission, $output);
$this->writeConfig();
break;
case "ppcommand":
$command = array_shift($params);
if (empty($command)) {
if ($issuer === "console") {
$this->showCPermissionsList();
}
$msg = $this->permissionUsage("c");
$output .= "Usage: /ppcommand <command> $msg\n";
break;
}
if (!$this->hackChecker()) break;
$this->setCPermission($command, $params, $output);
$this->writeConfig();
break;
case "ppconfig":
$config = array_shift($params);
switch ($config) {
case "notice":
$bool = array_shift($params);
if (!$this->castBool($bool)) {
$output .= "Usage: /ppconfig notice <on | off>\n";
break;
}
if ($bool) {
$this->config["notice"] = $bool;
$output .= "[Permission+] Truned on to the notify function.\n";
} else {
$this->config["notice"] = $bool;
$output .= "[Permission+] Truned off to the notify function.\n";
}
break;
case "auto-op":
case "autoop":
if (!$this->hackChecker()) break;
$bool = array_shift($params);
if (!$this->castBool($bool)) {
$output .= "Usage: /ppconfig autoop <on | off>\n";
break;
}
if ($bool) {
$this->config["autoop"] = $bool;
$this->giveOPtoEveryone();
$output .= "[Permission+] Truned on to the auto op function.\n";
} else {
$this->config["autoop"] = $bool;
$output .= "[Permission+] Truned off to the auto op function.\n";
}
break;
case "add":
$name = array_shift($params);
if (empty($name) or !$this->isAlnum($name)) {
$output .= "Usage: /ppconfig add <rank name>\n";
break;
}
if ($this->addPermission($name)) {
$output .= "[Permission+] Successful!\n";
} else {
$output .= "[Permission+] Failed to add!\n";
}
break;
case "rm":
case "remove":
$name = array_shift($params);
if (empty($name) or !$this->isAlnum($name)) {
$output .= "Usage: /ppconfig remove <rank name>\n";
break;
}
if ($this->removePermission($name)) {
$output .= "[Permission+] Successful!\n";
} else {
$output .= "[Permission+] Failed to remove!\n";
}
break;
default:
$output .= "Usage: /ppconfig notice <on | off>\n";
$output .= "Usage: /ppconfig autoop <on | off>\n";
$output .= "Usage: /ppconfig add <rank name>\n";
$output .= "Usage: /ppconfig remove <rank name>\n";
}
$this->writeConfig();
break;
}
return $output;
}
private function setPPermission($player, $permission, &$output)
{
if (!$this->castPermission($permission)) {
$msg = $this->permissionUsage("p");
$output .= "Usage: /ppplayer <player> $msg\n";
return;
}
$this->config['player'][$player] = $permission;
$output .= "[Permission+] Gived ".$permission." to ".$player.".\n";
$this->api->chat->sendTo(false, "[PermissionPlus] Your permission has been changed into ".$permission." !", $player);
}
private function setCPermission($cmd, $permissions, &$output)
{
$msg = "";
$return = array_fill_keys($this->getPermissions(), false);
if (!empty($permissions)) {
foreach ($permissions as $permission) {
$value = $permission;
if (!$this->castPermission($permission)) {
$output .= "[Permission+]Invalid value: \"$value\"\n";
continue;
}
$msg .= $permission." ";
$return[$permission] = true;
}
}
foreach ($this->getPermissions() as $permission) {
$this->config["command"][$permission][$cmd] = $return[$permission];
}
if (empty($msg)) {
$output .= "[Permission+] \"/".$cmd."\" was disabled.\n";
} else {
$output .= "[Permission+] Assigned ".$msg."to \"/".$cmd."\".\n";
}
}
public function showPPermissionsList()
{
$output = "\n";
foreach ($this->config["player"] as $name => $permission) {
$online = $this->api->player->get($name);
if ($online) {
$online = "\x1b[33;1mONLINE";
} else {
$online = "\x1b[34mOFFLINE";
}
if ($this->config["permission"][$permission]) {
$output .= "[".$online."\x1b[0m][\x1b[32m".$permission."\x1b[0m]: $name\n";
} else {
$output .= "[".$online."\x1b[0m][\x1b[36m".$permission."\x1b[0m]: $name\n";
}
}
console($output);
}
public function showCPermissionsList()
{
$output = "\n";
$permission = array();
$clist = $this->config['command'];
foreach ($this->getPermissions() as $prm) {
$pname = substr($prm, 0, 5);
foreach ($clist[$prm] as $command => $enable) {
if ($enable) {
if($this->config["permission"][$prm]) {
$permission[$prm][$command] = "[\x1b[32m".$pname."\x1b[0m]";
} else {
$permission[$prm][$command] = "[\x1b[36m".$pname."\x1b[0m]";
}
} else {
$permission[$prm][$command] = " ";
}
}
}
foreach ($this->getCommands() as $command) {
foreach ($this->getPermissions() as $prm) {
$output .= $permission[$prm][$command];
}
$output .= ": /".$command."\n";
}
console($output);
}
public function checkPermission($player, $cmd, $notice)
{
$permission = $this->getUserPermission($player);
if ($notice and !isset($this->config['command']['ADMIN'][$cmd])) {
console("[Permission+] NOTICE: \"/".$cmd."\" permission is not setted!");
console("[Permission+] Usage: /ppcommand ".$cmd." (g) (t) (a)");
}
if (isset($this->config['command'][$permission][$cmd]) and !$this->config['command'][$permission][$cmd]) {
return false;
} else {
return true;
}
}
public function getUserPermission($user)
{
if ($user instanceof Player) {
$user = $user->username;
}
if (!isset($this->config['player'][$user])) return false;
return $this->config['player'][$user];
}
public function getCommands()
{
$cmds = array_keys($this->config['command']['ADMIN']);
return $cmds;
}
public function getPermissions()
{
$prms = array_keys($this->config["permission"]);
return $prms;
}
public function giveOP($user)
{
$this->api->ban->commandHandler("op", array($user), "console", false);
}
public function giveOPtoEveryone()
{
$players = $this->api->player->getAll();
if (count($players) != 0) {
foreach ($players as $player) {
$user = $player->username;
$this->giveOP($user);
}
}
}
public function addPermission($permission)
{
$permission = strtoupper($permission);
$permissions = $this->getPermissions();
if (!in_array($permission, array_merge(array("g", "t", "a"), $permissions))) {
$this->config["permission"][$permission] = false;
$this->config["command"][$permission] = array_fill_keys($this->getCommands(), false);
return true;
}
return false;
}
public function removePermission($permission)
{
$permission = strtoupper($permission);
if (isset($this->config["permission"][$permission]) and !$this->config["permission"][$permission]) {
unset($this->config["permission"][$permission]);
unset($this->config["command"][$permission]);
return true;
}
return false;
}
public function createConfig()
{
$default = array(
"notice" => true,
"autoop" => false,
"player" => array(),
"permission" => array(
"GUEST" => true,
"TRUST" => true,
"ADMIN" => true),
"command" => array("ADMIN" => array(
'ban' => true,
'banip' => true,
'defaultgamemode' => true,
'deop' => true,
'difficulty' => true,
'gamemode' => true,
'give' => true,
'help' => true,
'kick' => true,
'kill' => true,
'list' => true,
'me' => true,
'op' => true,
'ping' => true,
'save-all' => true,
'save-off' => true,
'save-on' => true,
'say' =>true,
'seed' => true,
'spawn' => true,
'spawnpoint' => true,
'status' => true,
'stop' => true,
'sudo' => true,
'tell' => true,
'time' => true,
'tp' => true,
'whitelist' => true,
'ppplayer' => true,
'ppcommand' => true,
'ppconfig' => true,
'noexplosion' => true,
), "TRUST" => array(
'ban' => false,
'banip' => false,
'defaultgamemode' => false,
'deop' => true,
'difficulty' => false,
'gamemode' => false,
'give' => true,
'help' => true,
'kick' => true,
'kill' => false,
'list' => true,
'me' => true,
'op' => true,
'ping' => true,
'save-all' => false,
'save-off' => false,
'save-on' => false,
'say' => true,
'seed' => true,
'spawn' => true,
'spawnpoint' => true,
'status' => true,
'stop' => false,
'sudo' => false,
'tell' => true,
'time' => true,
'tp' => true,
'whitelist' => false,
'ppplayer' => false,
'ppcommand' => false,
'ppconfig' => false,
'noexplosion' => false,
), "GUEST" => array(
'ban' => false,
'banip' => false,
'defaultgamemode' => false,
'deop' => false,
'difficulty' => false,
'gamemode' => false,
'give' => false,
'help' => false,
'kick' => false,
'kill' => false,
'list' => true,
'me' => false,
'op' => false,
'ping' => false,
'save-all' => false,
'save-off' => false,
'save-on' => false,
'say' => false,
'seed' => false,
'spawn' => false,
'spawnpoint' => false,
'status' => false,
'stop' => false,
'sudo' => false,
'tell' => false,
'time' => false,
'tp' => false,
'whitelist' => false,
'ppplayer' => false,
'ppcommand' => false,
'ppconfig' => false,
'noexplosion' => false,
)));
$this->path = $this->api->plugin->createConfig($this, $default);
$this->config = $this->api->plugin->readYAML($this->path."config.yml");
}
public function writeConfig()
{
$this->api->plugin->writeYAML($this->path."config.yml", $this->config);
}
public function returnPermission($data, $event)
{
return $this->getUserPermission($data);
}
private function castPermission(&$permission)
{
$permission = strtoupper($permission);
switch ($permission) {
case "A":
case "ADMIN":
$permission = "ADMIN";
break;
case "T":
case "TRUST":
$permission = "TRUST";
break;
case "G":
case "GUEST":
$permission = "GUEST";
break;
default:
if (in_array($permission, $this->getPermissions())) {
return true;
}
$permission = false;
return false;
}
return true;
}
private function castBool(&$bool)
{
$bool = strtoupper($bool);
switch ($bool) {
case "TRUE":
case "ON":
case "1":
$bool = true;
break;
case "FALSE":
case "OFF":
case "0":
$bool = false;
break;
default:
return false;
}
return true;
}
private function isAlnum($text)
{
if (preg_match("/^[a-zA-Z0-9]+$/",$text)) {
return true;
} else {
return false;
}
}
private function permissionUsage($type)
{
switch ($type) {
case "p":
$output = "<";
$border= "";
foreach ($this->getPermissions() as $prm) {
$prm = strtolower($prm);
$output .= $border.$prm;
$border = " | ";
}
$output .= ">";
break;
case "c":
$output = "";
foreach ($this->getPermissions() as $prm) {
$prm = strtolower($prm);
$output .= "(".$prm.")";
}
break;
default:
return false;
}
return $output;
}
private function permissionUsage($type)
{
switch ($type) {
case "p":
$output = "<";
$border= "";
foreach ($this->getPermissions() as $prm) {
$prm = strtolower($prm);
$output .= $border.$prm;
$border = " | ";
}
$output .= ">";
break;
case "c":
$output = "";
foreach ($this->getPermissions() as $prm) {
$prm = strtolower($prm);
$output .= "(".$prm.")";
}
break;
default:
return false;
}
return $output;
}
private function hackChecker() {
//I can't show this code to you, sorry.
}
}
@Gorniiladphrao
Copy link

Good

@Gorniiladphrao
Copy link

Good

@Gorniiladphrao
Copy link

Good

@PEMapModder
Copy link

Bugs: $player->iusername

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment