Overview

Namespaces

  • aliuly
    • common
      • selectors
    • loader
  • xPaw

Classes

  • aliuly\common\ArmorItems
  • aliuly\common\BasicCli
  • aliuly\common\BasicHelp
  • aliuly\common\BasicPlugin
  • aliuly\common\ChatSession
  • aliuly\common\Cmd
  • aliuly\common\CmdSelector
  • aliuly\common\ExpandVars
  • aliuly\common\FastTransfer
  • aliuly\common\FileUtils
  • aliuly\common\FreezeSession
  • aliuly\common\GetMotd
  • aliuly\common\GetMotdAsyncTask
  • aliuly\common\InvisibleSession
  • aliuly\common\InvUtils
  • aliuly\common\ItemName
  • aliuly\common\mc
  • aliuly\common\mc2
  • aliuly\common\MoneyAPI
  • aliuly\common\MPMU
  • aliuly\common\Npc
  • aliuly\common\PermUtils
  • aliuly\common\PluginAsyncTask
  • aliuly\common\PluginCallbackTask
  • aliuly\common\PMScript
  • aliuly\common\QueryAsyncTask
  • aliuly\common\Rcon
  • aliuly\common\RconTask
  • aliuly\common\selectors\All
  • aliuly\common\selectors\AllEntity
  • aliuly\common\selectors\BaseSelector
  • aliuly\common\selectors\Random
  • aliuly\common\Session
  • aliuly\common\ShieldSession
  • aliuly\common\ShoppingCart
  • aliuly\common\SignUtils
  • aliuly\common\SkinUtils
  • aliuly\common\SpySession
  • aliuly\common\SubCommandMap
  • aliuly\common\TPUtils
  • aliuly\loader\Main
  • xPaw\MinecraftQuery

Exceptions

  • xPaw\MinecraftQueryException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace aliuly\common;
  3: //= api-features
  4: //: - Config shortcuts and multi-module|feature management
  5: 
  6: use pocketmine\plugin\PluginBase;
  7: use pocketmine\command\CommandSender;
  8: use pocketmine\command\Command;
  9: use pocketmine\command\CommandExecutor;
 10: use pocketmine\utils\TextFormat;
 11: use pocketmine\utils\Config;
 12: 
 13: use aliuly\common\mc;
 14: use aliuly\common\BasicHelp;
 15: use aliuly\common\Session;
 16: use aliuly\common\SubCommandMap;
 17: 
 18: /**
 19:  * Simple extension to the PocketMine PluginBase class
 20:  */
 21: abstract class BasicPlugin extends PluginBase {
 22:     protected $modules = [];
 23:     protected $scmdMap = null;
 24:     protected $session;
 25: 
 26:     /**
 27:      * Given some defaults, this will load optional features
 28:      *
 29:      * @param str $ns - namespace used to search for classes to load
 30:      * @param array $mods - optional module definition
 31:      * @param array $defaults - default options to use for config.yml
 32:      * @param str $xhlp - optional help format.
 33:      * @return array
 34:      */
 35:     protected function modConfig($ns,$mods,$defaults,$xhlp="") {
 36:         if (!isset($defaults["features"])) $defaults["features"] = [];
 37:         foreach ($mods as $i => $j) {
 38:             $defaults["features"][$i] = $j[1];
 39:         }
 40:         $cfg=(new Config($this->getDataFolder()."config.yml",
 41:                                       Config::YAML,$defaults))->getAll();
 42:         $this->modules = [];
 43:         foreach ($cfg["features"] as $i=>$j) {
 44:             if (!isset($mods[$i])) {
 45:                 $this->getLogger()->info(mc::_("Unknown feature \"%1%\" ignored.",$i));
 46:                 continue;
 47:             }
 48:             if (!$j) continue;
 49:             $class = $mods[$i][0];
 50:             if (is_array($class)) {
 51:                 while (count($class) > 1) {
 52:                     // All classes before the last one are dependencies...
 53:                     $classname = $dep = array_shift($class);
 54:                     if(strpos($classname,"\\") === false) $classname = $ns."\\".$classname;
 55:                     if (isset($this->modules[$dep])) continue; // Dependancy already loaded
 56:                     if(isset($cfg[strtolower($dep)])) {
 57:                         $this->modules[$dep] = new $classname($this,$cfg[strtolower($dep)]);
 58:                     } else {
 59:                         $this->modules[$dep] = new $classname($this);
 60:                     }
 61:                 }
 62:                 // The last class in the array implements the actual feature
 63:                 $class = array_shift($class);
 64:             }
 65:             if(strpos($class,"\\") === false) $class = $ns."\\".$class;
 66:             if (isset($cfg[$i]))
 67:                 $this->modules[$i] = new $class($this,$cfg[$i]);
 68:             else
 69:                 $this->modules[$i] = new $class($this);
 70:         }
 71:         $c = count($this->modules);
 72:         if ($c == 0) {
 73:             $this->getLogger()->info(mc::_("NO features enabled"));
 74:             return;
 75:         }
 76:         $this->session = null;
 77:         $this->getLogger()->info(mc::n(mc::_("Enabled one feature"),
 78:                                                      mc::_("Enabled %1% features",$c),
 79:                                                      $c));
 80:         if ($this->scmdMap !== null && $this->scmdMap->getCommandCount() > 0) {
 81:             $this->modules[] = new BasicHelp($this,$xhlp);
 82:         }
 83:         return $cfg;
 84:     }
 85:   /**
 86:      * Get module
 87:      * @param str $module - module to retrieve
 88:      * @return mixed|null
 89:      */
 90:     public function getModule($str) {
 91:         if (isset($this->modules[$str])) return $this->modules[$str];
 92:         return null;
 93:     }
 94:     /**
 95:      * Get Modules array
 96:      * @return array
 97:      */
 98:     public function getModules() {
 99:         return $this->modules;
100:     }
101:     /**
102:      * Save a config section to the plugins' config.yml
103:      *
104:      * @param str $key - section to save
105:      * @param mixed $settings - settings to save
106:      */
107:     public function cfgSave($key,$settings) {
108:         $cfg=new Config($this->getDataFolder()."config.yml",Config::YAML);
109:         $dat = $cfg->getAll();
110:         $dat[$key] = $settings;
111:         $cfg->setAll($dat);
112:         $cfg->save();
113:     }
114:     /**
115:      * Dispatch commands using sub command table
116:      */
117:     protected function dispatchSCmd(CommandSender $sender,Command $cmd,array $args,$data=null) {
118:         if ($this->scmdMap === null) {
119:             $sender->sendMessage(mc::_("No sub-commands available"));
120:             return false;
121:         }
122:         return $this->scmdMap->dispatchSCmd($sender,$cmd,$args,$data);
123:     }
124:     /** Look-up sub command map
125:      * @returns SubCommandMap
126:      */
127:     public function getSCmdMap() {
128:         return $this->scmdMap;
129:     }
130:     /**
131:      * Register a sub command
132:      * @param str $cmd - sub command
133:      * @param callable $callable - callable to execute
134:      * @param array $opts - additional options
135:      */
136:     public function registerSCmd($cmd,$callable,$opts) {
137:         if ($this->scmdMap === null) {
138:             $this->scmdMap = new SubCommandMap();
139:         }
140:         $this->scmdMap->registerSCmd($cmd,$callable,$opts);
141:     }
142:     /**
143:      * Get a player state for the desired module/$label.
144:      *
145:      * @param str $label - state variable to get
146:      * @param Player|str $player - Player instance or name
147:      * @param mixed $default - default value to return is no state found
148:      * @return mixed
149:      */
150:     public function getState($label,$player,$default) {
151:         if ($this->session === null) return $default;
152:         return $this->session->getState($label,$player,$default);
153:     }
154:     /**
155:      * Set a player related state
156:      *
157:      * @param str $label - state variable to set
158:      * @param Player|str $player - player instance or their name
159:      * @param mixed $val - value to set
160:      * @return mixed
161:      */
162:     public function setState($label,$player,$val) {
163:         if ($this->session === null) $this->session = new Session($this);
164:         return $this->session->setState($label,$player,$val);
165:     }
166:     /**
167:      * Clears a player related state
168:      *
169:      * @param str $label - state variable to clear
170:      * @param Player|str $player - intance of Player or their name
171:      */
172:     public function unsetState($label,$player) {
173:         if ($this->session === null) return;
174:         $this->session->unsetState($label,$player);
175:     }
176: 
177:     /**
178:      * Gets the contents of an embedded resource on the plugin file.
179:      *
180:      * @param string $filename
181:      * @return string|null
182:      */
183:     public function getResourceContents($filename){
184:         $fp = $this->getResource($filename);
185:         if($fp === null){
186:             return null;
187:         }
188:         $contents = stream_get_contents($fp);
189:         fclose($fp);
190:         return $contents;
191:     }
192: }
193: 
API documentation generated by ApiGen