Created
August 21, 2015 16:18
-
-
Save Humerus/16437ac3267a3340340c to your computer and use it in GitHub Desktop.
Bans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @name Bans | |
* @main Humerus\Bans\Main | |
* @version 0.0.1 | |
* @api 1.12.0 | |
* @descriptionBans plugin | |
* @author Humerus | |
*/ | |
namespace Humerus\Bans{ | |
use pocketmine\event\Listener; | |
use pocketmine\event\player\PlayerJoinEvent; | |
use pocketmine\event\player\PlayerCommandPreprocessEvent; | |
use pocketmine\event\server\ServerCommandEvent; | |
use pocketmine\plugin\PluginBase; | |
use pocketmine\utils\TextFormat; | |
use pocketmine\scheduler\PluginTask; | |
use pocketmine\Player; | |
class Main extends PluginBase implements Listener{ | |
public $config = [ | |
"host" => "127.0.0.1", | |
"user" => "root", | |
"password" => "toor", | |
"database" => "bans", | |
"port" => 3306 | |
]; | |
public $ready = false; | |
public function onEnable(){ | |
$this->getLogger()->info(TextFormat::GREEN . "Enabled ban linker"); | |
$this->getServer()->getPluginManager()->registerEvents($this, $this); | |
$this->linktask = new LinkTask($this); | |
$this->getServer()->getScheduler()->scheduleRepeatingTask($this->linktask, 20 * 5); | |
$this->cleantask = new CleanTask($this); | |
$this->initServer(); | |
} | |
public function initServer() { | |
$this->getLogger()->info(TextFormat::YELLOW . "Connecting to MySQL"); | |
$this->database = new \mysqli($this->config["host"], $this->config["user"], $this->config["password"], $this->config["database"], isset($this->config["port"]) ? $this->config["port"] : 3306); | |
if($this->database->connect_error){ | |
$this->getLogger()->critical("Couldn't connect to MySQL: ". $this->database->connect_error); | |
$this->getServer()->getPluginManager()->disablePlugin($this); | |
return; | |
} | |
$this->ready = true; | |
$this->getLogger()->info(TextFormat::AQUA . "Connected to MySQL!"); | |
$query = $this->database->query("CREATE TABLE IF NOT EXISTS bans (name VARCHAR(16) PRIMARY KEY);"); | |
} | |
public function getBans() { | |
if($this->ready) { | |
$query = $this->database->query("SELECT * FROM bans;"); | |
if($query) { | |
return $query->fetch_all(\MYSQLI_ASSOC); | |
} | |
} | |
return false; | |
} | |
public function getBan($user) { | |
if($this->ready) { | |
$query = $this->database->query("SELECT * FROM bans WHERE name = " . $user . ";"); | |
if($query) { | |
return $query->fetch_all(\MYSQLI_ASSOC)[0]; | |
} | |
} | |
return false; | |
} | |
public function banName($name) { | |
if($this->ready) { | |
$query = $this->database->query("INSERT INTO bans (name) VALUES ('" . $name . "');"); | |
return $query; | |
} | |
return false; | |
} | |
public function removeBan($name) { | |
if($this->ready) { | |
$query = $this->database->query("DELETE FROM bans WHERE name = '" . $name . "';"); | |
return $query; | |
} | |
return false; | |
} | |
public function updateBans() { | |
$lbans = $this->getLocalBans(); | |
$this->database->query("DELETE FROM bans;"); | |
foreach($lbans as $user) { | |
$this->banName($user); | |
} | |
$rbans = $this->getRemoteBans(); | |
foreach($rbans as $user) { | |
$this->getServer()->getNameBans()->addBan($user); | |
} | |
} | |
public function getLocalBans() { | |
$barray = array(); | |
$bans = $this->getServer()->getNameBans(); | |
$bans = $bans->getEntries(); | |
foreach ($bans as $ban) { | |
$barray[] = $ban->getName(); | |
} | |
return $barray; | |
} | |
public function getRemoteBans() { | |
$barray = array(); | |
$bans = $this->getBans(); | |
foreach ($bans as $ban) { | |
$barray[] = $ban["name"]; | |
} | |
return $barray; | |
} | |
public function onDisable() { | |
$this->getLogger()->info(TextFormat::RED . "Disabled ban linker"); | |
} | |
} | |
class LinkTask extends PluginTask { | |
public function onRun($tick) { | |
$this->getOwner()->updateBans(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all in 1 file.