An highly customisable UHC plugin for PocketMine
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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. # _ _ _ _ _____
  3. # | | | | | | |/ ____|
  4. # | | | | |__| | |
  5. # | | | | __ | |
  6. # | |__| | | | | |____
  7. # \____/|_| |_|\_____|
  8. # The most customisable UHC plugin for Minecraft PE!
  9. namespace Ad5001\UHC ;
  10. use pocketmine\command\CommandSender;
  11. use pocketmine\command\Command;
  12. use pocketmine\event\Listener;
  13. use pocketmine\event\level\LevelLoadEvent;
  14. use pocketmine\event\player\PlayerJoinEvent;
  15. use pocketmine\event\player\PlayerDeathEvent;
  16. use pocketmine\event\player\PlayerChatEvent;
  17. use pocketmine\event\player\PlayerQuitEvent;
  18. use pocketmine\event\player\PlayerRespawnEvent;
  19. use pocketmine\event\entity\EntityRegainHealthEvent;
  20. use pocketmine\item\Item;
  21. use pocketmine\block\Block;
  22. use pocketmine\plugin\PluginBase;
  23. use pocketmine\Server;
  24. use pocketmine\Player;
  25. use pocketmine\event\entity\EntityLevelChangeEvent;
  26. use pocketmine\utils\TextFormat as C;
  27. use Ad5001\UHC\UHCWorld;
  28. use Ad5001\UHC\UHCGame;
  29. use Ad5001\UHC\task\FetchPlayersTask;
  30. use Ad5001\UHC\task\StartGameTask;
  31. use Ad5001\UHC\event\GameStartEvent;
  32. use Ad5001\UHC\event\GameFinishEvent;
  33. class Main extends PluginBase implements Listener{
  34. const PREFIX = C::GOLD . "[" . C::DARK_RED . "UHC" . C::GOLD . "] ". C::RESET . C::WHITE;
  35. public function onLevelChange(EntityLevelChangeEvent $event) {
  36. foreach($this->UHCManager->getLevels() as $world) {
  37. if($event->getTarget()->getName() == $world->getName() and !isset($this->UHCManager->getStartedUHCs()[$world->getName()])) {
  38. if(count($world->getLevel()->getPlayers()) > $world->maxplayers) {
  39. $event->setCancelled();
  40. }
  41. } elseif($event->getTarget()->getName() == $world->getName() and isset($this->UHCManager->getStartedUHCs()[$world->getName()]) and !isset($this->quit[$event->getEntity()->getName()])) {
  42. $event->getEntity()->setGamemode(3);
  43. } elseif($event->getTarget()->getName() == $world->getName() and isset($this->UHCManager->getStartedUHCs()[$world->getName()]) and isset($this->quit[$event->getEntity()->getName()])) {
  44. $this->quit = explode("/", $this->quit[$event->getEntity()->getName()]);
  45. if($this->quit[3] === $world->getName()) {
  46. $event->getEntity()->teleport(new \pocketmine\level\Location($this->quit[0], $this->quit[1], $this->quit[2], $event->getTarget()));
  47. foreach($world->getLevel()->getPlayers() as $player) {
  48. $player->sendMessage(self::PREFIX . C::GREEN . "{$event->getPlayer()->getName()} back to game !");
  49. }
  50. $event->setCancelled();
  51. } else {
  52. $event->getEntity()->setGamemode(3);
  53. }
  54. }
  55. }
  56. }
  57. public function onEnable(){
  58. $this->saveDefaultConfig();
  59. @mkdir($this->getDataFolder() . "scenarios");
  60. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  61. $this->getServer()->getPluginManager()->registerEvent("Ad5001\\UHC\\event\\GameStartEvent", $this, \pocketmine\event\EventPriority::NORMAL, new \pocketmine\plugin\MethodEventExecutor("onGameStart"), $this, true);
  62. $this->getServer()->getPluginManager()->registerEvent("Ad5001\\UHC\\event\\GameStopEvent", $this, \pocketmine\event\EventPriority::NORMAL, new \pocketmine\plugin\MethodEventExecutor("onGameStop"), $this, false);
  63. $this->UHCManager = new UHCManager($this);
  64. $this->quit = [];
  65. }
  66. public function onLevelLoad(\pocketmine\event\level\LevelLoadEvent $event) {
  67. if(isset($this->getConfig()->get("worlds")[$event->getLevel()->getName()])) {
  68. $this->UHCManager->registerLevel($event->getLevel());
  69. }
  70. }
  71. public function getClasses(string $file) {
  72. $tokens = token_get_all($file);
  73. $class_token = false;
  74. foreach ($tokens as $token) {
  75. if (is_array($token)) {
  76. if ($token[0] == T_CLASS) {
  77. $class_token = true;
  78. } else if ($class_token && $token[0] == T_STRING) {
  79. return $token[1];
  80. }
  81. }
  82. }
  83. }
  84. public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
  85. switch($cmd->getName()){
  86. case "uhc":
  87. if(isset($args[0]) and $sender instanceof Player) {
  88. switch($args[0]) {
  89. case "start":
  90. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
  91. $this->getLogger()->debug("Starting game {$sender->getLevel()->getName()}");
  92. foreach($sender->getLevel()->getPlayers() as $player) {
  93. $player->sendMessage(self::PREFIX . "Starting game...");
  94. }
  95. $this->UHCManager->startUHC($sender->getLevel());
  96. } else {
  97. $sender->sendMessage("You are not in a UHC world or UHC is already started");
  98. }
  99. return true;
  100. break;
  101. case "stop":
  102. if(isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
  103. $this->getLogger()->debug("Starting game {$sender->getLevel()->getName()}");
  104. foreach($sender->getLevel()->getPlayers() as $player) {
  105. $player->sendMessage(self::PREFIX . "Starting game...");
  106. }
  107. $this->UHCManager->stopUHC($sender->getLevel());
  108. } else {
  109. $sender->sendMessage("You are not in a UHC world or UHC is already started");
  110. }
  111. return true;
  112. break;
  113. case "howtoplay":
  114. case "?":
  115. case "help":
  116. if(!isset($args[1])) {
  117. $sender->sendMessage(self::PREFIX . "Welcome to UHC's help ! WHat do you want to know about UHC?\n" . self::PREFIX . "- The game (/uhc howtoplay game)\n" . self::PREFIX . "- The commands (/uhc howtoplay commands)\n" . self::PREFIX . "- The scenarios (/uhc howtoplay scenarios)\n");
  118. } else {
  119. switch(strtolower($args[1])) {
  120. case "game":
  121. $sender->sendMessage(self::PREFIX . "UHC (aka Ultra HardCord) is a new difficulty where it's hardcord but with no health regen exept for golden apples and potions and only 1 life. The most popular 'UHC' Game is a PvP (Player versus Player) game where you're in this mode and you need to kill other players.\n" . self::PREFIX . "What do you want to know next?\n" . self::PREFIX . "- The commands (/uhc howtoplay commands)\n" . self::PREFIX . "- The scenarios (/uhc howtoplay scenarios)\n");
  122. break;
  123. case "commands":
  124. $sender->sendMessage(self::PREFIX . "This UHC Plugin have special commands. For players: /scenarios will give you the list of the current enabled scenarios. " . ($sender->isOp() ? "For OP: '/uhc start' start the uhc. '/uhc stop' finish it. '/scenarios add <scenario>' adds a scenario. '/scenario rm <scenario>' remove a scenario. '/scenarios list' gives you the list of installed scenarios.\n" : "\n") . self::PREFIX . "What do you want to know next?\n" . self::PREFIX . "- The game (/uhc howtoplay game)\n" . self::PREFIX . "- The scenarios (/uhc howtoplay scenarios)");
  125. break;
  126. case "scenarios":
  127. $sender->sendMessage(self::PREFIX . "Scenarios are 'addons' to the UHC that modify some mechanics or some parts of the game. You can download them or create them yourself. Look at ccommands if you want to know how to add a scenario to a game and look at https://github.com/Ad5001/UHC/wiki/What-are-Scenarios%3F. If you want to know more about a scenario, use /uhc howtoplay <scenario name>.\n" . self::PREFIX . "What do you want to know next?\n" . self::PREFIX . "- The game (/uhc howtoplay game)\n" . self::PREFIX . "- The commands (/uhc howtoplay commands)\n");
  128. break;
  129. default:
  130. if(in_array($args[1], $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getScenarios())) {
  131. $sender->sendMessage(self::PREFIX . "Help for $args[1]. " . $args[1]::help());
  132. } else {
  133. $sender->sendMessage(self::PREFIX . "No help for $args[1]");
  134. }
  135. break;
  136. }
  137. }
  138. return true;
  139. break;
  140. }
  141. }
  142. break;
  143. case "scenarios":
  144. if(isset($args[0]) and $sender instanceof Player and $sender->hasPermission("uhc.scenarios.modify")) {
  145. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
  146. switch($args[0]) {
  147. case "add":
  148. if(isset($args[1])) {
  149. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getScenarios()[$args[1]])) {
  150. if(!isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()[$args[1]])) {
  151. $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->addScenario($args[1]);
  152. foreach($sender->getLevel()->getPlayers() as $p) {
  153. $p->sendTip(C::GOLD . C::BOLD . "Scenario added !" . PHP_EOL . C::RESET . C::GREEN . C::ITALIC . "+ " . $args[1]);
  154. }
  155. $sender->sendMessage(self::PREFIX . C::GREEN . " Succefully added scenario $args[1] !");
  156. } else {
  157. $sender->sendMessage(self::PREFIX . C::DARK_RED . "Scenario $args[1] has already been added !");
  158. }
  159. } else {
  160. $sender->sendMessage(self::PREFIX . C::DARK_RED . "Scenario $args[1] does not exists !");
  161. }
  162. } else {
  163. $sender->sendMessage(self::PREFIX . C::DARK_RED . "Usage: /scenarios add <scenario>");
  164. }
  165. break;
  166. case "remove":
  167. case "rm":
  168. case "delete":
  169. case "del":
  170. if(isset($args[1])) {
  171. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()[$args[1]])) {
  172. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()[$args[1]])) {
  173. $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->rmScenario($args[1]);
  174. foreach($sender->getLevel()->getPlayers() as $p) {
  175. $p->sendTip(C::GOLD . C::BOLD . "Scenario added !" . PHP_EOL . C::RESET . C::GREEN . C::ITALIC . "+ " . $args[1]);
  176. }
  177. $sender->sendMessage(self::PREFIX . C::GREEN . " Succefully removed scenario $args[1] !");
  178. } else {
  179. $sender->sendMessage(slef::PREFIX . C::DARK_RED . "Scenario $args[1] hasn't been added yet !");
  180. }
  181. } else {
  182. $sender->sendMessage(self::PREFIX . C::DARK_RED . "Scenario $args[1] does not exists !");
  183. }
  184. } else {
  185. $sender->sendMessage(self::PREFIX . C::DARK_RED . "Usage: /scenarios rm <scenario>");
  186. }
  187. break;
  188. case "list":
  189. $sender->sendMessage(self::PREFIX . "Current server's scenarios: " . implode(", ", $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getScenarios()));
  190. break;
  191. }
  192. } else {
  193. $sender->sendMessage(self::PREFIX . "You're not in a UHC world !");
  194. }
  195. } else {
  196. if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
  197. $scs = $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios();
  198. $sender->sendMessage(self::PREFIX . "Current enabled scenarios : " . implode(", ", $scs));
  199. }
  200. }
  201. return true;
  202. break;
  203. }
  204. return false;
  205. }
  206. # Event Listener !
  207. public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {
  208. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  209. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  210. $sc->onInteract($event);
  211. }
  212. }
  213. }
  214. public function onEntityLevelChange(EntityLevelChangeEvent $event) {
  215. if(isset($this->UHCManager->getLevels()[$event->getOrigin()->getName()]) and $event->getEntity() instanceof Player) {
  216. $this->quit[$event->getEntity()->getName()] = $event->getEntity()->x . "/" . $event->getEntity()->y . "/" . $event->getEntity()->z. "/" . $event->getOrigin()->getName();
  217. foreach($this->UHCManager->getLevels()[$event->getOrigin()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  218. $sc->onQuit($event->getEntity());
  219. }
  220. }
  221. if(isset($this->UHCManager->getLevels()[$event->getTarget()->getName()]) and $event->getEntity() instanceof Player) {
  222. foreach($this->UHCManager->getLevels()[$event->getTarget()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  223. $sc->onJoin($event->getEntity());
  224. }
  225. }
  226. }
  227. public function onPlayerChat(\pocketmine\event\player\PlayerChatEvent $event) {
  228. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  229. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  230. $sc->onPlayerChat($event);
  231. $sc->onChat($event);
  232. }
  233. }
  234. }
  235. public function onPlayerCommandPreprocess(\pocketmine\event\player\PlayerCommandPreprocessEvent $event) {
  236. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  237. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  238. $sc->onPlayerCommand($event);
  239. }
  240. }
  241. }
  242. public function onPlayerDeath(\pocketmine\event\player\PlayerDeathEvent $event) {
  243. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  244. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  245. $sc->onPlayerDeath($event);
  246. $sc->onDeath($event);
  247. }
  248. }
  249. }
  250. public function onPlayerDropItem(\pocketmine\event\player\PlayerDropItemEvent $event) {
  251. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  252. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  253. $sc->onPlayerDropItem($event);
  254. $sc->onPlayerDropItem($event);
  255. }
  256. }
  257. }
  258. public function onPlayerMove(\pocketmine\event\player\PlayerMoveEvent $event) {
  259. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  260. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  261. $sc->onPlayerMove($event);
  262. $sc->onMove($event);
  263. }
  264. }
  265. }
  266. public function onPlayerItemConsume(\pocketmine\event\player\PlayerItemConsumeEvent $event) {
  267. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  268. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  269. $sc->onPlayerItemConsume($event);
  270. $sc->onItemConsume($event);
  271. }
  272. }
  273. }
  274. public function onPlayerItemHeld(\pocketmine\event\player\PlayerItemHeldEvent $event) {
  275. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  276. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  277. $sc->onPlayerItemHeld($event);
  278. $sc->onItemHeld($event);
  279. }
  280. }
  281. }
  282. public function onBlockBreak(\pocketmine\event\block\BlockBreakEvent $event) {
  283. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  284. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  285. $sc->onBlockBreak($event);
  286. $sc->onBreak($event);
  287. }
  288. }
  289. }
  290. public function onBlockPlace(\pocketmine\event\block\BlockPlaceEvent $event) {
  291. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  292. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  293. $sc->onBlockPlace($event);
  294. $sc->onPlace($event);
  295. }
  296. }
  297. }
  298. public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $event) {
  299. if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
  300. foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  301. $sc->onEntityDamage($event);
  302. }
  303. }
  304. }
  305. public function onProjectileLaunch(\pocketmine\event\entity\ProjectileLaunchEvent $event) {
  306. if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
  307. foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  308. $sc->onProjectileLaunch($event);
  309. }
  310. }
  311. }
  312. public function onProjectileHit(\pocketmine\event\entity\ProjectileHitEvent $event) {
  313. if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
  314. foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  315. $sc->onProjectileHit($event);
  316. }
  317. }
  318. }
  319. public function onDataPacketReceive(\pocketmine\event\server\DataPacketReceiveEvent $event) {
  320. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  321. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  322. $sc->onDataPacketReceive($event);
  323. }
  324. }
  325. }
  326. public function onDataPacketSend(\pocketmine\event\server\DataPacketSendEvent $event) {
  327. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  328. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  329. $sc->onDataPacketSend($event);
  330. }
  331. }
  332. }
  333. public function onServerCommand(\pocketmine\event\server\ServerCommandEvent $event) {
  334. foreach($this->UHCManager->getLevels() as $lvl => $world) {
  335. foreach($world->scenarioManager->getUsedScenarios() as $sc) {
  336. $sc->onServerCommand($event);
  337. }
  338. }
  339. }
  340. public function onPlayerJoin(\pocketmine\event\player\PlayerJoinEvent $event) {
  341. if(!isset($this->ft)) {
  342. $this->ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new FetchPlayersTask($this, $this->UHCManager->getStartedUHCs()), 10);
  343. }
  344. if(isset($this->quit[$event->getPlayer()->getName()]) and $event->getPlayer()->getLevel()->getName() == $this->quit[3]) {
  345. $this->quit = explode("/", $this->quit[$event->getPlayer()->getName()]);
  346. $event->getPlayer()->teleport($this->getServer()->getLevelByName($this->quit[3]));
  347. $event->getPlayer()->teleport(new Vector3($this->quit[0], $this->quit[1], $this->quit[2]));
  348. foreach($this->getServer()->getLevelByName($this->quit[3])->getLevel()->getPlayers() as $player) {
  349. $player->sendMessage(self::PREFIX . C::GREEN . "{$event->getPlayer()->getName()} back to game !");
  350. }
  351. }
  352. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  353. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  354. $sc->onJoin($event->getPlayer());
  355. }
  356. }
  357. }
  358. public function onPlayerQuit(\pocketmine\event\player\PlayerQuitEvent $event) {
  359. if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
  360. $this->quit[$event->getPlayer()->getName()] = $event->getPlayer()->x . "/" . $event->getPlayer()->y . "/" . $event->getPlayer()->z. "/" . $event->getPlayer()->getLevel()->getName();
  361. foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
  362. $sc->onQuit($event->getPlayer());
  363. }
  364. }
  365. }
  366. public function onGameStart(\Ad5001\UHC\event\GameStartEvent $event) {}
  367. public function onGameStop(\Ad5001\UHC\event\GameStopEvent $event) {}
  368. }