Make a website for your PocketMine server fully free !
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace Ad5001\Online;
  3. use pocketmine\command\CommandSender;
  4. use pocketmine\command\Command;
  5. use pocketmine\event\Listener;
  6. use pocketmine\plugin\PluginBase;
  7. use pocketmine\network\upnp\UPnP;
  8. use pocketmine\Server;
  9. use pocketmine\Player;
  10. class Main extends PluginBase{
  11. protected $handlers;
  12. protected $arguments;
  13. public function onEnable(){
  14. $this->saveDefaultConfig();
  15. $this->handlers = [];
  16. $this->arguments = [];
  17. $this->getServer()->getScheduler()->scheduleRepeatingTask($task = new ArgFillTask($this), 20*10);
  18. @mkdir($this->getDataFolder() . "tmp");
  19. $task->onRun(0);
  20. if(!file_exists($this->getDataFolder() . "tmp/router.php")) {
  21. file_put_contents($this->getDataFolder() . "tmp/router.php", $this->getResource("handler.php"));
  22. }
  23. foreach($this->getConfig()->get("Domains") as $d) {
  24. @mkdir($this->getDataFolder() . $d);
  25. if(!file_exists($this->getDataFolder() . $d . "/index.html") and !file_exists($this->getDataFolder() . $d . "/index.php")) {
  26. file_put_contents($this->getDataFolder() .$d. "/index.html", $this->getResource("index.html"));
  27. }
  28. if(!file_exists($this->getDataFolder() .$d. "/404.html")) {
  29. file_put_contents($this->getDataFolder() . $d . "/404.html", $this->getResource("404.html"));
  30. }
  31. if(!file_exists($this->getDataFolder() . $d . "/403.html")) {
  32. file_put_contents($this->getDataFolder() .$d . "/403.html", $this->getResource("403.html"));
  33. }
  34. }
  35. set_time_limit(0);
  36. $this->port = $this->getConfig()->get("port");
  37. if(!UPnP::PortForward($this->port)) {// Beta for Windows
  38. $this->getLogger()->info("Not able to port forward!");
  39. }
  40. $this->getServer()->getScheduler()->scheduleAsyncTask(new execTask($this->getServer()->getFilePath()));
  41. }
  42. public function onDisable() {
  43. if($this->getConfig()->get("KillOnShutdown") !== "false") {
  44. $this->getLogger()->info("Shutdowning.....");
  45. switch(true) {
  46. case stristr(PHP_OS, "WIN"):
  47. shell_exec('FOR /F "tokens=5" %P IN (\'netstat -a -n -o ^| findstr 0.0.0.0:'. $this->port .'\') DO TaskKill.exe /F /PID %P');
  48. $this->getLogger()->info("Shutdowned on Windows !");
  49. break;
  50. case stristr(PHP_OS, "DAR") or stristr(PHP_OS, "LINUX"):
  51. shell_exec("kill -kill `lsof -t -i tcp:$this->port`");
  52. $this->getLogger()->info("Shutdowned on Linux or MAC !");
  53. break;
  54. }
  55. }
  56. }
  57. /*
  58. Adds a PHP handler
  59. @param $path string
  60. @return bool
  61. */
  62. public function addHandler(string $path) : bool {
  63. if(file_exists($path)) {
  64. $this->handlers[$path] = $path;
  65. return true;
  66. }
  67. return false;
  68. }
  69. /*
  70. Return an array with all handlers
  71. @return array
  72. */
  73. public function getHandlers() : array {
  74. return $this->handlers;
  75. }
  76. /*
  77. Sets a plugin argument
  78. @param $plugin \pocketmine\plugin\Plugin
  79. @param $id string
  80. @param $value mixed
  81. @return bool
  82. */
  83. public function setArgument(\pocketmine\plugin\Plugin $plugin, string $id, $value) : bool {
  84. if(!isset($this->arguments[$plugin->getName()])) $this->arguments[$plugin->getName()] = [];
  85. $this->arguments[$plugin->getName()][$id] = $value;
  86. return true;
  87. }
  88. /*
  89. Gets a plugin arguments
  90. @param $plugin \pocketmine\plugin\Plugin
  91. @return array
  92. */
  93. public function getArguments(\pocketmine\plugin\Plugin $plugin) : array {
  94. return isset($this->arguments[$plugin->getName()]) ? $this->arguments[$plugin->getName()] : [];
  95. }
  96. }
  97. /**
  98. * Setting rguments for the pocketine infos
  99. */
  100. class ArgFillTask extends \pocketmine\scheduler\PluginTask {
  101. /*
  102. Constructs the class
  103. @param $main Main
  104. */
  105. public function __construct(Main $main) {
  106. parent::__construct($main);
  107. $this->main = $main;
  108. $this->server = $main->getServer();
  109. }
  110. /*
  111. Called when the task runs
  112. @param $tick mixed
  113. */
  114. public function onRun($tick) {
  115. $server = $this->server;
  116. $pmargs = [];
  117. $pluginsargs = [];
  118. $players = [];
  119. $levels = [];
  120. // Filling the arguments
  121. // 1. Pocketmine
  122. $pmargs["software_name"] = $server->getName();
  123. $pmargs["software_codename"] = $server->getCodename();
  124. $pmargs["software_version"] = $server->getPocketmineVersion();
  125. $pmargs["mcpe_version"] = $server->getVersion();
  126. $pmargs["API"] = $server->getApiVersion();
  127. $pmargs["software_datapath"] = $server->getDataPath();
  128. $pmargs["software_pluginpath"] = $server->getPluginPath();
  129. $pmargs["max_players"] = $server->getMaxPlayers();
  130. $pmargs["port"] = $server->getPort();
  131. $pmargs["view_distance"] = $server->getViewDistance();
  132. $pmargs["ip"] = $server->getIp();
  133. $pmargs["server_unique_id"] = $server->getServerUniqueId();
  134. $pmargs["auto_save"] = $server->getAutoSave();
  135. $pmargs["default_gamemode"] = $server->getGamemode();
  136. $pmargs["force_gamemode"] = $server->getForceGamemode();
  137. $pmargs["difficulty"] = $server->getDifficulty();
  138. $pmargs["spawn_radius"] = $server->getSpawnRadius();
  139. $pmargs["allow_flight"] = $server->getAllowFlight();
  140. $pmargs["ultra_hardcore"] = $server->isHardcore();
  141. $pmargs["motd"] = $server->getMotd();
  142. $pmargs["tps"] = $server->getTicksPerSecond();
  143. $pmargs["lang_name"] = $server->getLanguage()->getName();
  144. $pmargs["lang_code"] = $server->getLanguage()->getLang();
  145. // 2. Plugins
  146. foreach($server->getPluginManager()->getPlugins() as $plugin) {
  147. $pl = [];
  148. $pl["name"] = $plugin->getName();
  149. $pl["is_enabled"] = $plugin->isEnabled();
  150. $pl["data_folder"] = $plugin->getDataFolder();
  151. $pl["apis"] = $plugin->getDescription()->getCompatibleApis();
  152. $pl["authors"] = $plugin->getDescription()->getAuthors();
  153. $pl["prefix"] = $plugin->getDescription()->getPrefix();
  154. $pl["commands"] = $plugin->getDescription()->getCommands();
  155. $pl["dependencies"] = $plugin->getDescription()->getDepend();
  156. $pl["description"] = $plugin->getDescription()->getDescription();
  157. $pl["load_before"] = $plugin->getDescription()->getLoadBefore();
  158. $pl["main"] = $plugin->getDescription()->getMain();
  159. $pl["order"] = $plugin->getDescription()->getOrder();
  160. $pl["soft_depend"] = $plugin->getDescription()->getSoftDepend();
  161. $pl["version"] = $plugin->getDescription()->getVersion();
  162. $pl["website"] = $plugin->getDescription()->getWebsite();
  163. foreach($this->main->getArguments($plugin) as $name => $arg) {
  164. if(!isset($pl[$name])) {
  165. $pl[$name] = $arg;
  166. }
  167. }
  168. $pluginsargs[$plugin->getName()] = $pl;
  169. }
  170. // 3. Players
  171. foreach ($server->getOnlinePlayers() as $player) {
  172. $pl = [];
  173. $pl["client_secret"] = $player->getClientSecret();
  174. $pl["banned"] = $player->isBanned(); // I don't know if tis could be to false but we never know xD
  175. $pl["whitelisted"] = $player->isWhitelisted();
  176. $pl["client_secret"] = $player->getClientSecret();
  177. $pl["first_played"] = $player->getFirstPlayed();
  178. $pl["last_played"] = $player->getLastPlayed();
  179. $pl["played_before"] = $player->hasPlayedBefore();
  180. $pl["allow_flight"] = $player->getAllowFlight();
  181. $pl["flying"] = $player->isFlying();
  182. $pl["auto_jump"] = $player->hasAutoJump();
  183. $pl["op"] = $player->isOp();
  184. $pl["connected"] = $player->isConnected();
  185. $pl["display_name"] = $player->getDisplayName();
  186. $pl["ip"] = $player->getAddress();
  187. $pl["port"] = $player->getPort();
  188. $pl["is_sleeping"] = $player->isSleeping();
  189. $pl["ticks_in_air"] = $player->getInAirTicks();
  190. $pl["gamemode"] = $player->getGamemode();
  191. $pl["name"] = $player->getName();
  192. $pl["loader_id"] = $player->getLoaderId();
  193. $players[$player->getName()] = $pl;
  194. }
  195. // 4.Levels
  196. foreach ($server->getLevels() as $level) {
  197. $lvl = [];
  198. $lvl["tick_rate"] = $level->getTickRate();
  199. $lvl["id"] = $level->getId();
  200. $lvl["auto_save"] = $level->getAutoSave();
  201. $lvl["tick_rate"] = $level->getTickRate();
  202. $lvl["players"] = [];
  203. foreach ($level->getPlayers() as $pl) {
  204. $lvl["players"][$pl->getName()] = $players[$pl->getName()];
  205. }
  206. $lvl["time"] = $level->getTime();
  207. $lvl["name"] = $level->getName();
  208. $lvl["folder_name"] = $level->getFolderName();
  209. $lvl["seed"] = $level->getSeed();
  210. $levels[$lvl["name"]] = $lvl;
  211. }
  212. file_put_contents($this->main->getDataFolder() . "tmp/args", json_encode(["POCKETMINE" => $pmargs, "PLUGINS" => $pluginsargs, "PLAYERS" => $players, "LEVELS" => $levels, "HANDLERS" => $this->main->getHandlers()]));
  213. }
  214. }
  215. class execTask extends \pocketmine\scheduler\AsyncTask {
  216. public function __construct(string $path) {
  217. $this->path = $path;
  218. }
  219. public function onRun() {
  220. $address = '0.0.0.0';
  221. $port = yaml_parse(file_get_contents("plugins\\Online\\config.yml"))["port"];
  222. switch(true) {
  223. case stristr(PHP_OS, "WIN"):
  224. // echo '"%CD%\\bin\\php\\php.exe -t %CD%\\plugins\\Online -n -d include_path=\'%CD%\\plugins\\Online\\\' -S ' . $address . ":" . $port . ' -f %CD%\\plugins\\Online\\router.php"';
  225. shell_exec('start /MIN cmd /c "%CD%\\bin\\php\\php.exe -t %CD%\\plugins\\Online -n -d include_path=\'%CD%\\plugins\\Online\\\' -d extension=\'%CD%\\bin\\php\\ext\\php_yaml.dll\' -S ' . $address . ":" . $port . ' tmp/router.php"');
  226. break;
  227. case stristr(PHP_OS, "DAR"):
  228. shell_exec('open -a Terminal php -t ' . $this->path . "plugins\\Online -n -d include_path=\'" . $this->path . "plugins\\Online\\\' -d extension=\'" . $this->path . "bin\\php\\ext\\php_yaml.dll\' -S " . $address . ":" . $port . ' tmp/router.php"');
  229. break;
  230. case stristr(PHP_OS, "LINUX"):
  231. shell_exec('gnome-terminal -e php -t ' . $this->path . "plugins\\Online -n -d include_path=\'" . $this->path . "plugins\\Online\\\' -d extension=\'" . $this->path . "bin\\php\\ext\\php_yaml.dll\' -S " . $address . ":" . $port . ' tmp/router.php"');
  232. break;
  233. }
  234. }
  235. }