From ae92bc056d1b4121b2a3b1d1874376fb46092b88 Mon Sep 17 00:00:00 2001 From: Alejandro Liu Date: Tue, 8 Nov 2016 06:42:04 +0100 Subject: [PATCH] 2.0.3 --- README.md | 6 + plugin.yml | 2 +- src/aliuly/worldprotect/Main.php | 4 + src/aliuly/worldprotect/WpMotdMgr.php | 6 + .../worldprotect/common/BasicPlugin.php | 17 ++- src/aliuly/worldprotect/common/MPMU.php | 120 +++++++++++++++++- 6 files changed, 150 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0dd1109..bea7cd3 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ If `true` the feature is enabled. if `false` the feature is disabled. #### motd * ticks: line delay when showing multi-line motd texts. +* auto-motd: Automatically shows motd when entering world ### Permission Nodes @@ -279,6 +280,11 @@ for sample files. # Changes +* 2.0.3: Minor bug fix + * Fixed bug: Configuration is not applied when reloading +* 2.0.2: Feature request + * Feature Request(@Nifo2000): Option to control if MOTD is shown + automatically * 2.0.1: critical bug fix * Fixed a crash * Can now add to auth list when players are off-line diff --git a/plugin.yml b/plugin.yml index 6ae7102..8dff533 100644 --- a/plugin.yml +++ b/plugin.yml @@ -4,7 +4,7 @@ load: STARTUP name: WorldProtect description: protect worlds from griefers, pvp, limits and borders -version: 2.0.1 +version: 2.0.3 author: aliuly commands: diff --git a/src/aliuly/worldprotect/Main.php b/src/aliuly/worldprotect/Main.php index 169b387..5a21dfb 100644 --- a/src/aliuly/worldprotect/Main.php +++ b/src/aliuly/worldprotect/Main.php @@ -50,6 +50,10 @@ public function onEnable() { "motd" => WpMotdMgr::defaults(), ],mc::_("/%s [world] %s %s")); $this->modules[] = new WpList($this); + // Make sure that loaded worlds are inded loaded... + foreach ($this->getServer()->getLevels() as $lv) { + $this->loadCfg($lv); + } } ////////////////////////////////////////////////////////////////////// diff --git a/src/aliuly/worldprotect/WpMotdMgr.php b/src/aliuly/worldprotect/WpMotdMgr.php index 80953f2..eecf3f6 100644 --- a/src/aliuly/worldprotect/WpMotdMgr.php +++ b/src/aliuly/worldprotect/WpMotdMgr.php @@ -43,17 +43,21 @@ class WpMotdMgr extends BaseWp implements Listener, CommandExecutor { protected $ticks; + protected $auto; static public function defaults() { return [ "# ticks" => "line delay when showing multi-line motd texts.", "ticks" => 15, + "# auto-motd" => "Automatically shows motd when entering world", + "auto-motd" => true, ]; } public function __construct(Plugin $plugin,$cfg) { parent::__construct($plugin); $this->owner->getServer()->getPluginManager()->registerEvents($this, $this->owner); $this->ticks = $cfg["ticks"]; + $this->auto = $cfg["auto-motd"]; $this->enableSCmd("motd",["usage" => mc::_("[text]"), "help" => mc::_("Edits world motd text"), "permission" => "wp.cmd.wpmotd"]); @@ -124,10 +128,12 @@ private function showMotd($c,$world) { } public function onJoin(PlayerJoinEvent $ev) { + if (!$this->auto) return; $pl = $ev->getPlayer(); $this->showMotd($pl,$pl->getLevel()->getName()); } public function onLevelChange(EntityLevelChangeEvent $ev) { + if (!$this->auto) return; $pl = $ev->getEntity(); if (!($pl instanceof Player)) return; $level = $ev->getTarget()->getName(); diff --git a/src/aliuly/worldprotect/common/BasicPlugin.php b/src/aliuly/worldprotect/common/BasicPlugin.php index ba4d011..3eea30c 100644 --- a/src/aliuly/worldprotect/common/BasicPlugin.php +++ b/src/aliuly/worldprotect/common/BasicPlugin.php @@ -147,5 +147,20 @@ public function unsetState($label,$player) { unset($this->state[$player][$label]); } - + /** + * Gets the contents of an embedded resource on the plugin file. + * + * @param string $filename + * + * @return string, or null + */ + public function getResourceContents($filename){ + $fp = $this->getResource($filename); + if($fp === null){ + return null; + } + $contents = stream_get_contents($fp); + fclose($fp); + return $contents; + } } diff --git a/src/aliuly/worldprotect/common/MPMU.php b/src/aliuly/worldprotect/common/MPMU.php index e5388b0..5158aa7 100644 --- a/src/aliuly/worldprotect/common/MPMU.php +++ b/src/aliuly/worldprotect/common/MPMU.php @@ -6,19 +6,47 @@ use pocketmine\command\CommandSender; use pocketmine\Player; +/** + * My PocketMine Utils class + */ abstract class MPMU { - // My PocketMine Utils static protected $items = []; - const VERSION = "0.0.1"; + const VERSION = "0.0.2"; + /** + * libcommon library version. If a version is provided it will check + * the version using apiCheck. + * + * @param str version Version to check + * + * @return str|bool + */ static public function version($version = "") { if ($version == "") return self::VERSION; return self::apiCheck(self::VERSION,$version); } + /** + * Used to check the PocketMine API version + * + * @param str version Version to check + * + * @return str|bool + */ static public function apiVersion($version = "") { if ($version == "") return \pocketmine\API_VERSION; return self::apiCheck(\pocketmine\API_VERSION,$version); } + /** + * Checks API compatibility from $api against $version. $version is a + * string containing the version. It can contain the following operators: + * + * >=, <=, <> or !=, =, !|~, <, > + * + * @param str api Installed API version + * @param str version API version to compare against + * + * @return bool + */ static public function apiCheck($api,$version) { switch (substr($version,0,2)) { case ">=": @@ -43,6 +71,13 @@ static public function apiCheck($api,$version) { if (intval($api) != intval($version)) return 0; return version_compare($api,$version) >= 0; } + /** + * Given an pocketmine\item\Item object, it returns a friendly name + * for it. + * + * @param Item item + * @return str + */ static public function itemName(Item $item) { $n = $item->getName(); if ($n != "Unknown") return $n; @@ -58,6 +93,12 @@ static public function itemName(Item $item) { return self::$items[$item->getId()]; return $n; } + /** + * Returns a localized string for the gamemode + * + * @param int mode + * @return str + */ static public function gamemodeStr($mode) { if (class_exists(__NAMESPACE__."\\mc",false)) { switch ($mode) { @@ -76,13 +117,27 @@ static public function gamemodeStr($mode) { } return "$mode-mode"; } - + /** + * Check's player or sender's permissions and shows a message if appropriate + * + * @param CommandSender $sender + * @param str $permission + * @param bool $msg If false, no message is shown + * @return bool + */ static public function access(CommandSender $sender, $permission,$msg=true) { if($sender->hasPermission($permission)) return true; if ($msg) $sender->sendMessage(mc::_("You do not have permission to do that.")); return false; } + /** + * Check's if $sender is a player in game + * + * @param CommandSender $sender + * @param bool $msg If false, no message is shown + * @return bool + */ static public function inGame(CommandSender $sender,$msg = true) { if (!($sender instanceof Player)) { if ($msg) $sender->sendMessage(mc::_("You can only do this in-game")); @@ -96,4 +151,63 @@ static public function iName($player) { } return $player; } + static public function getResourceContents($plugin,$filename) { + $fp = $plugin->getResource($filename); + if($fp === null){ + return null; + } + $contents = stream_get_contents($fp); + fclose($fp); + return $contents; + } + + static public function callPlugin($server,$plug,$method,$args,$default = null) { + if (($plugin = $server->getPluginManager()->getPlugin($plug)) !== null + && $plugin->isEnabled()) { + $fn = [ $plugin, $method ]; + return $fn(...$args); + } + return $default; + } + static public function addCommand($plugin, $executor, $cmd, $yaml) { + $newCmd = new PluginCommand($cmd,$plugin); + if (isset($yaml["description"])) + $newCmd->setDescription($yaml["description"]); + if (isset($yaml["usage"])) + $newCmd->setUsage($yaml["usage"]); + if(isset($yaml["aliases"]) and is_array($yaml["aliases"])) { + $aliasList = []; + foreach($yaml["aliases"] as $alias) { + if(strpos($alias,":")!== false) { + $this->owner->getLogger()->info("Unable to load alias $alias"); + continue; + } + $aliasList[] = $alias; + } + $newCmd->setAliases($aliasList); + } + if(isset($yaml["permission"])) + $newCmd->setPermission($yaml["permission"]); + if(isset($yaml["permission-message"])) + $newCmd->setPermissionMessage($yaml["permission-message"]); + $newCmd->setExecutor($executor); + $cmdMap = $plugin->getServer()->getCommandMap(); + $cmdMap->register($plugin->getDescription()->getName(),$newCmd); + } + + static public function sendPopup($player,$msg) { + $pm = $player->getServer()->getPluginManager(); + if (($sa = $pm->getPlugin("SimpleAuth")) !== null) { + // SimpleAuth also has a HUD when not logged in... + if ($sa->isEnabled() && !$sa->isPlayerAuthenticated($player)) return; + } + if (($hud = $pm->getPlugin("BasicHUD")) !== null) { + // Send pop-ups through BasicHUD + $hud->sendPopup($player,$msg); + return; + } + $player->sendPopup($msg); + } + + }