1: <?php
2: namespace aliuly\common;
3:
4:
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: 20:
21: abstract class BasicPlugin extends PluginBase {
22: protected $modules = [];
23: protected $scmdMap = null;
24: protected $session;
25:
26: 27: 28: 29: 30: 31: 32: 33: 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:
53: $classname = $dep = array_shift($class);
54: if(strpos($classname,"\\") === false) $classname = $ns."\\".$classname;
55: if (isset($this->modules[$dep])) continue;
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:
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: 87: 88: 89:
90: public function getModule($str) {
91: if (isset($this->modules[$str])) return $this->modules[$str];
92: return null;
93: }
94: 95: 96: 97:
98: public function getModules() {
99: return $this->modules;
100: }
101: 102: 103: 104: 105: 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: 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: 125: 126:
127: public function getSCmdMap() {
128: return $this->scmdMap;
129: }
130: 131: 132: 133: 134: 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: 144: 145: 146: 147: 148: 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: 156: 157: 158: 159: 160: 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: 168: 169: 170: 171:
172: public function unsetState($label,$player) {
173: if ($this->session === null) return;
174: $this->session->unsetState($label,$player);
175: }
176:
177: 178: 179: 180: 181: 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: