Hide and seek game recreated for PocketMine-MP
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.

Game.php 25KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. # _ _ _ _ _ _____ _
  3. # | | | | (_) | | /\ | | / ____| | |
  4. # | |__| | _ __| | ___ / \ _ __ __| | | (___ ___ ___ | | __
  5. # | __ | | | / _` | / _ \ / /\ \ | '_ \ / _` | \___ \ / _ \ / _ \ | |/ /
  6. # | | | | | | | (_| | | __/ / ____ \ | | | | | (_| | ____) | | __/ | __/ | <
  7. # |_| |_| |_| \__,_| \___| /_/ \_\ |_| |_| \__,_| |_____/ \___| \___| |_|\_\
  8. #
  9. #
  10. # The original minigame, free, and better than ever !
  11. # @author Ad5001
  12. # @link ad5001.eu
  13. namespace Ad5001\HideAndSeek;
  14. use pocketmine\Server;
  15. use pocketmine\Player;
  16. use pocketmine\level\Level;
  17. use pocketmine\event\Listener;
  18. use pocketmine\math\Vector3;
  19. use pocketmine\scheduler\PluginTask;
  20. use pocketmine\command\ConsoleCommandSender;
  21. use Ad5001\HideAndSeek\Main;
  22. use Ad5001\HideAndSeek\GameManager;
  23. class Game extends PluginTask /* Allows easy game running */ implements Listener {
  24. const STEP_WAIT = "WAITING";
  25. const STEP_START = "STARTING";
  26. const STEP_HIDE = "HIDING";
  27. const STEP_SEEK = "SEEKING";
  28. const STEP_WIN = "RESTARTING";
  29. const NO_WIN = 0;
  30. const WIN_SEEKERS = 1;
  31. const WIN_HIDERS = 2;
  32. const ROLE_WAIT = 0;
  33. const ROLE_SEEK = 1;
  34. const ROLE_HIDE = 2;
  35. const ROLE_NEW_SEEK = 3;
  36. const ROLE_SPECTATE = 4;
  37. // Level based informations
  38. protected $level;
  39. protected $players = [];
  40. protected $spectators = [];
  41. // Game based informations
  42. public $step = self::STEP_WAIT;
  43. protected $win = self::NO_WIN;
  44. protected $stepTick;
  45. protected $hidersLeft;
  46. protected $seekersCount;
  47. /*
  48. Constructs the class
  49. @param $level Level
  50. */
  51. public function __construct(Level $level) {
  52. // Initialisation
  53. $this->level = $level;
  54. $this->initDB();
  55. $level->game = $this;
  56. $this->getMain()->getServer()->getPluginManager()->registerEvents($this,$this->getMain());
  57. // Registering players
  58. foreach($this->getLevel()->getPlayers() as $p) {
  59. $this->registerPlayer($p);
  60. }
  61. // Loading timer.
  62. parent::__construct($this->getMain());
  63. $this->getMain()->getServer()->getScheduler()->scheduleRepeatingTask($this, 1);
  64. }
  65. /*
  66. Function that runs every x ticks
  67. @param $tick int
  68. */
  69. public function onRun($tick) {
  70. switch($this->step) {
  71. case self::STEP_WAIT:
  72. // $this->getMain()->getLogger()->debug("Running game " . $this->getName() . " at wait step");
  73. if(count($this->getPlayers()) >= round($this->getMaxPlayers() * 0.75)) {
  74. $this->stepTick = $tick;
  75. $this->step = self::STEP_START;
  76. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  77. $p->sendMessage(Main::PREFIX . "§aGame will start in " . $this->getWaitTime() . " seconds.");
  78. }
  79. }
  80. break;
  81. case self::STEP_START:
  82. // $this->getMain()->getLogger()->debug("Running game " . $this->getName() . " at start step");
  83. $tickWaited = $tick - $this->stepTick;
  84. if($tickWaited % 20 == 0) {
  85. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  86. $p->sendMessage(Main::PREFIX . "§aGame will start in " . ($this->getWaitTime() - ($tickWaited / 20)) . " seconds.");
  87. }
  88. }
  89. if($this->getWaitTime() - ($tickWaited / 20) <= 0) {
  90. $this->start();
  91. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  92. $p->sendMessage(Main::PREFIX . "§aGame started ! There is $this->seekersCount seekers and $this->hidersLeft hiders.");
  93. if($p->HideAndSeekRole == self::ROLE_SEEK) {
  94. $p->teleport($this->getSeekersSpawn());
  95. } elseif($p->HideAndSeekRole == self::ROLE_HIDE) {
  96. $p->teleport($this->getSpawn());
  97. $p->sendPopup("§lHider: You have 1 minute to hide yourself so seekers won't find you ! Don't get caught for " . $this->getSeekTime() . " minutes to win !");
  98. }
  99. }
  100. }
  101. break;
  102. case self::STEP_HIDE:
  103. // $this->getMain()->getLogger()->debug("Running game " . $this->getName() . " at hide step");
  104. $tickWaited = $tick - $this->stepTick;
  105. if($tickWaited % (20*10) == 0) {
  106. $this->sendMessage("§aSeekers will be released in " . (60 - ($this->tickwaited / 20)) . " seconds !");
  107. }
  108. if($tickWaited >= 20*60) { // One minute has past !
  109. $this->step = self::STEP_SEEK;
  110. $this->stepTick = $tick;
  111. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  112. $p->sendMessage(Main::PREFIX . "§aSeekers released !");
  113. if($p->HideAndSeekRole == self::ROLE_SEEK) {
  114. $p->teleport($this->getSpawn());
  115. $p->sendMessage("§lSeeker: Seek the hiders ! Catch them all to win in " . $this->getSeekTime() . " minutes to win !");
  116. }
  117. }
  118. }
  119. break;
  120. case self::STEP_SEEK:
  121. // $this->getMain()->getLogger()->debug("Running game " . $this->getName() . " at seek step");
  122. $tickWaited = $tick - $this->stepTick;
  123. if($tickWaited % (20*60) == 0) {
  124. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  125. $p->sendMessage(Main::PREFIX . "§aGame ends in " . ($this->getSeekTime() - ($this->tickWaited / 20 / 60)) . " minutes.");
  126. }
  127. }
  128. if($tickWaited >= 20*60*$this->getSeekTime()) { // Seek time has past
  129. $this->win = self::WIN_HIDERS;
  130. $this->step = self::STEP_WIN;
  131. }
  132. break;
  133. case self::STEP_WIN:
  134. // $this->getMain()->getLogger()->debug("Running game " . $this->getName() . " at win step");
  135. foreach(array_merge($this->getPlayers(), $this->getSpectators()) as $p) {
  136. if($this->win == self::WIN_SEEKERS) {
  137. $p->sendMessage(Main::PREFIX . "§aThe last hider got caught ! Seekers won !");
  138. $p->sendTip("§a§lSeekers won !");
  139. swicth($p->HideAndSeekRole) {
  140. case self::ROLE_HIDE:
  141. $this->getMain()->getServer()->dispatchCommand(new ConsoleCommandSencer(), $this->getMain()->getConfig()->get("Losers command"));
  142. break;
  143. case self::ROLE_NEW_SEEK:
  144. $this->getMain()->getServer()->dispatchCommand(new ConsoleCommandSencer(), $this->getMain()->getConfig()->get("Semi winners command"));
  145. break;
  146. case self::ROLE_SEEK:
  147. $this->getMain()->getServer()->dispatchCommand(new ConsoleCommandSencer(), $this->getMain()->getConfig()->get("Winners command"));
  148. break;
  149. }
  150. } elseif($this->win == self::WIN_HIDERS) {
  151. $p->sendMessage(Main::PREFIX . "§aTimes up ! Hiders won !");
  152. $p->sendTip("§a§lHiders won !");
  153. swicth($p->HideAndSeekRole) {
  154. case self::ROLE_SEEK:
  155. case self::ROLE_NEW_SEEK:
  156. $this->getMain()->getServer()->dispatchCommand(new ConsoleCommandSencer(), $this->getMain()->getConfig()->get("Losers command"));
  157. break;
  158. case self::ROLE_HIDE:
  159. $this->getMain()->getServer()->dispatchCommand(new ConsoleCommandSencer(), $this->getMain()->getConfig()->get("Winners command"));
  160. break;
  161. }
  162. } else {
  163. $p->sendMessage(Main::PREFIX . "§aGame cancelled !");
  164. }
  165. $p->HideAndSeekRole = self::ROLE_WAIT;
  166. $p->teleport($this->getMain()->getLobbyWorld()->getSafeSpawn());
  167. $p->setGamemode($this->getMain()->getServer()->getDefaultGamemode());
  168. }
  169. $this->players = [];
  170. $this->step = self::STEP_WAIT;
  171. break;
  172. }
  173. }
  174. /*
  175. Inits the database for the game.
  176. */
  177. protected function initDB() {
  178. $qry = $this->getMain()->getDatabase()->get("*", ["table" => "Games", "name" => $this->getName()]);
  179. if($qry instanceof \SQLite3Result) {
  180. if($qry->num_rows == 0) { // Game not initiated in the db.
  181. $id = $this->getMain()->getDatabase()->get("*", ["table" => "Games"]);
  182. $v3 = $this->getLevel()->getSafeSpawn();
  183. $v3Ser = $v3->x . "," . $v3->y . "," . $v3->z; // V32String
  184. $this->getMain()->getDatabase()->insert("Games", [$this->getName(), $v3Ser, $v3Ser, $this->getMain()->getMaxPlayers(), $this->getMain()->getWaitTime(), $this->getMain()->getSeekTime(), $this->getMain()->getSeekersPercentage(), $id->num_rows+1]); // Inserting the db with new queries
  185. }
  186. } else {
  187. throw new \Exception("Could not contact database.");
  188. }
  189. }
  190. # _____ _____
  191. # /\ | __ \ |_ _|
  192. # / \ | |__) | | |
  193. # / /\ \ | ___/ | |
  194. # / ____ \ | | _| |_
  195. # /_/ \_\ |_| |_____|
  196. #
  197. #
  198. /*
  199. Starts the game
  200. */
  201. public function start() {
  202. $this->stepTick = $this->getMain()->getServer()->getTick();
  203. $count = count($this->players);
  204. $this->step = self::STEP_HIDE;
  205. $this->seekersCount = ceil($count * ($this->getSeekersPercentage() / 100)); // Minimum $this->getSeekersPercentage() percents of the players (inimum because if there are less than $this->getSeekersPercentage(), then there could be no seeker)
  206. $this->hidersLeft = count($this->players) - $this->seekersCount;
  207. shuffle($this->players);
  208. $i = 0;
  209. foreach($this->players as $p) {
  210. if($i < $this->seekersCount) {
  211. $this->players[$i]->HideAndSeekRole = self::ROLE_SEEK;
  212. $this->players[$i]->sendTip("§4You're a seeker.");
  213. } else {
  214. $this->players[$i]->HideAndSeekRole = self::ROLE_HIDE;
  215. $this->players[$i]->sendTip("§4You're an hider.");
  216. }
  217. $i++;
  218. }
  219. }
  220. /*
  221. Returns game's spawn (waiting + for players) spawn
  222. @return \pocketmine\math\Vector3
  223. */
  224. public function getSpawn() : Vector3 {
  225. $data = $this->getMain()->getDatabase()->get("spawnpoint", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  226. $data = explode(",", $data);
  227. if(!isset($data[1])) return $this->getLevel()->getSafeSpawn();
  228. return new Vector3($data[0], $data[1], $data[2]);
  229. }
  230. /*
  231. Returns seekers spawn (waiting hiders to hide + startup)
  232. @return \pocketmine\math\Vector3
  233. */
  234. public function getSeekersSpawn() : Vector3 {
  235. $data = $this->getMain()->getDatabase()->get("seekerspawn", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  236. $data = explode(",", $data);
  237. if(!isset($data[1])) return $this->getLevel()->getSafeSpawn();
  238. return new Vector3($data[0], $data[1], $data[2]);
  239. }
  240. /*
  241. Returns the game level.
  242. @return \pocketmine\level\Level
  243. */
  244. public function getLevel() : Level {
  245. return $this->level;
  246. }
  247. /*
  248. Returns the game's name.
  249. @return string
  250. */
  251. public function getName() : string {
  252. return $this->level->getName();
  253. }
  254. /*
  255. Returns the game's players.
  256. @return array
  257. */
  258. public function getPlayers() : array {
  259. return $this->players;
  260. }
  261. /*
  262. Returns the game's spectators.
  263. @return array
  264. */
  265. public function getSpectators() : array {
  266. return $this->spectators;
  267. }
  268. /*
  269. Returns a player by it's name.
  270. @param $name string
  271. @return Player|null
  272. */
  273. public function getPlayer(string $name) {
  274. return isset($this->players[$name]) ? $this->players[$name] : null;
  275. }
  276. /*
  277. Returns the main class
  278. */
  279. public function getMain() {
  280. return Server::getInstance()->getPluginManager()->getPlugin("HideAndSeek");
  281. }
  282. /*
  283. Returns the game id
  284. */
  285. public function getId() {
  286. return $this->getGameManager()->getGameId($this);
  287. }
  288. /*
  289. Returns the max players in a game.
  290. @return int
  291. */
  292. public function getMaxPlayers() : int {
  293. return (int) $this->getMain()->getDatabase()->get("max_players", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  294. }
  295. /*
  296. Returns the time to wait between 75% of max players joined and the game start (ending filling if needed)
  297. @return int
  298. */
  299. public function getWaitTime() : int {
  300. return (int) $this->getMain()->getDatabase()->get("waiting_time", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  301. }
  302. /*
  303. Returns the time that the seekers have to find all hiders (used to balance with the max players)
  304. @return int
  305. */
  306. public function getSeekTime() : int {
  307. return (int) $this->getMain()->getDatabase()->get("seek_time", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  308. }
  309. /*
  310. Returns the percentage of the players that should be hunters.
  311. @return int
  312. */
  313. public function getSeekersPercentage() : int {
  314. return (int) $this->getMain()->getDatabase()->get("seekers_percentage", ["table" => "Games", "name" => $this->getName()])->fetchArray()[0];
  315. }
  316. /*
  317. Returns the current number of seekers
  318. @return int
  319. */
  320. public function getSeekersCount() : int {
  321. return $this->seekersCount;
  322. }
  323. /*
  324. Returns the current number of seekers
  325. @return int
  326. */
  327. public function getHidersLeft() : int {
  328. return $this->hidersLeft;
  329. }
  330. // SET
  331. /*
  332. Sets the spawn of the game
  333. @param $v3 Vector3
  334. */
  335. public function setSpawn(Vector3 $v3) {
  336. $str = $v3->x . "," . $v3->y . "," . $v3->z;
  337. return $this->getMain()->getDatabase()->set("spawnpoint", $str, ["table" => "Games", "name" => $this->getName()]);
  338. }
  339. /*
  340. Sets the seeker spawn. Check get for more details.
  341. @param $v3 Vector3
  342. */
  343. public function setSeekersSpawn(Vector3 $v3) {
  344. $str = $v3->x . "," . $v3->y . "," . $v3->z;
  345. return $this->getMain()->getDatabase()->set("seekerspawn", $str, ["table" => "Games", "name" => $this->getName()]);
  346. }
  347. /*
  348. Changes the level of the game
  349. @param $level Level
  350. */
  351. public function setLevel(Level $level) {
  352. $this->level = $level;
  353. return $this->getMain()->getDatabase()->set("name", $this->getName(), ["table" => "Games", "name" => $this->getName()]);
  354. }
  355. /*
  356. Sets a maximum number of players
  357. @param $int int
  358. */
  359. public function setMaxPlayers(int $int) {
  360. return $this->getMain()->getDatabase()->set("max_players", $int, ["table" => "Games", "name" => $this->getName()]);
  361. }
  362. /*
  363. Sets the waiting time in the lobby
  364. @param $int int
  365. */
  366. public function setWaitTime(int $int) {
  367. return $this->getMain()->getDatabase()->set("waiting_time", $int, ["table" => "Games", "name" => $this->getName()]);
  368. }
  369. /*
  370. Sets the seeking time
  371. @param $int int
  372. */
  373. public function setSeekTime(int $int) {
  374. return $this->getMain()->getDatabase()->set("seek_time", $int, ["table" => "Games", "name" => $this->getName()]);
  375. }
  376. /*
  377. Sets a the percentage of the seekers
  378. @param $int int
  379. */
  380. public function setSeekersPercentage(int $int) {
  381. return $this->getMain()->getDatabase()->set("seekers_percentage", $int, ["table" => "Games", "name" => $this->getName()]);
  382. }
  383. /*
  384. Registers a player
  385. @param $player \pocketmine\Player
  386. */
  387. public function registerPlayer(Player $player) {
  388. if($player->isSpectator() ||
  389. (!is_null($this->getMain()->getServer()->getPluginManager()->getPlugin("SpectatorPlus")) &&
  390. $this->getMain()->getServer()->getPluginManager()->getPlugin("SpectatorPlus")->isSpectator($player))) { // Support for spectator Plus
  391. $this->spectators[$player->getName()] = $player;
  392. $player->HideAndSeekRole = self::ROLE_SPECTATE;
  393. } elseif($this->step == self::STEP_WAIT || $this->step == self::STEP_START) {$player->hideAndSeekGame = $this;
  394. // API inside player's class (easilier to get data)
  395. $player->HideAndSeekRole = self::ROLE_WAIT;
  396. $player->playsHideAndSeek = true;
  397. $this->players[$player->getName()] = $player;
  398. $player->setGamemode(2); // Set it to adventure so player cannot break blocks.
  399. $this->sendMessage("§a" . $player->getName() . " joined (" . count($this->players) . "/" . $this->getMaxPlayers() . "). " . (round($this->getMaxPlayers() * 0.75) - count($this->players)) . " players left before starting");
  400. } else {
  401. $this->spectators[$player->getName()] = $player;
  402. $player->HideAndSeekRole = self::ROLE_SPECTATE;
  403. $player->setGamemode(3);
  404. }
  405. }
  406. /*
  407. Unregisters a player
  408. @param $player \pocketmine\Player
  409. */
  410. public function unregisterPlayer(Player $player) {
  411. switch(isset($player->HideAndSeekRole) ? $player->HideAndSeekRole : -1) {
  412. case self::ROLE_SEEK:
  413. $this->seekersCount--;
  414. $this->sendMessage($player->getName() . " left the game. $this->seekersCount seekers left.");
  415. unset($this->players[$player->getName()]);
  416. unset($player->hideAndSeekGame);
  417. unset($player->HideAndSeekRole);
  418. unset($player->playsHideAndSeek);
  419. if($this->seekersCount == 0) {
  420. $this->step = self::STEP_WIN;
  421. $this->win = self::WIN_HIDERS;
  422. }
  423. break;
  424. case self::ROLE_HIDE:
  425. $this->hidersLeft--;
  426. $this->sendMessage($player->getName() . " left the game. $this->hidersLeft hiders left.");
  427. unset($this->players[$player->getName()]);
  428. unset($player->hideAndSeekGame);
  429. unset($player->HideAndSeekRole);
  430. unset($player->playsHideAndSeek);
  431. if($this->hidersLeft == 0) {
  432. $this->step = self::STEP_WIN;
  433. $this->win = self::WIN_SEEKERS;
  434. }
  435. break;
  436. case self::ROLE_WAIT:
  437. unset($this->players[$player->getName()]);
  438. unset($player->hideAndSeekGame);
  439. unset($player->playsHideAndSeek);
  440. break;
  441. case self::ROLE_SPECTATE:
  442. unset($this->spectators[$player->getName()]);
  443. break;
  444. }
  445. }
  446. /*
  447. Sends a message to all players and spectators in the game.
  448. @param $message string
  449. */
  450. public function sendMessage(string $message) {
  451. foreach($this->getLevel()->getPlayers() as $p) {
  452. $p->sendMessage(Main::PREFIX . $message);
  453. }
  454. }
  455. # ______ _
  456. # | ____| | |
  457. # | |__ __ __ ___ _ __ | |_ ___
  458. # | __| \ \ / / / _ \ | '_ \ | __| / __|
  459. # | |____ \ V / | __/ | | | | | |_ \__ \
  460. # |______| \_/ \___| |_| |_| \__| |___/
  461. #
  462. #
  463. /*
  464. Checks when an entity changes level to add a player to the list/remove one.
  465. @param $event \pocketmine\event\entity\EntityLevelChangeEvent
  466. */
  467. public function onEntityLevelChange(\pocketmine\event\entity\EntityLevelChangeEvent $event) {
  468. if($event->getTarget()->getName() == $this->getName() && $event->getEntity() instanceof Player) {
  469. if(count($this->players) >= $this->getMaxPlayers()) {
  470. $event->setCancelled();
  471. $event->getEntity()->sendMessage(Main::PREFIX . "§cThe maximum number of players in this game has been reached.");
  472. }
  473. $this->registerPlayer($event->getEntity());
  474. } elseif($event->getOrigin()->getName() == $this->getName() && $event->getEntity() instanceof Player) {
  475. $this->unregisterPlayer($event->getEntity());
  476. }
  477. }
  478. /*
  479. Checks when a seeker moves when he shouldn't.
  480. @param $event \pocketmine\event\player\PlayerMoveEvent
  481. */
  482. public function onPlayerMove(\pocketmine\event\player\PlayerMoveEvent $event) {
  483. if($event->getPlayer()->getLevel()->getName() == $this->getName()) {
  484. if($event->getPlayer()->HideAndSeekRole == self::ROLE_SEEK && $this->step == self::STEP_HIDE && ($event->getTo()->x !== $event->getPlayer()->x || $event->getTo()->y !== $event->getPlayer()->y || $event->getTo()->z !== $event->getPlayer()->z)) {
  485. $event->setCancelled();
  486. }
  487. }
  488. }
  489. /*
  490. Checks when a block breaks to prevent it.
  491. @param $event \pocketmine\event\block\BlockBreakEvent
  492. */
  493. public function onBlockBreak(\pocketmine\event\block\BlockBreakEvent $event) {
  494. if($event->getBlock()->getLevel()->getName() == $this->getName()) {
  495. $event->setCancelled();
  496. }
  497. }
  498. /*
  499. Checks when a block places to prevent it.
  500. @param $event \pocketmine\event\block\BlockPlaceEvent
  501. */
  502. public function onBlockPlace(\pocketmine\event\block\BlockPlaceEvent $event) {
  503. if($event->getBlock()->getLevel()->getName() == $this->getName()) {
  504. $event->setCancelled();
  505. }
  506. }
  507. /*
  508. Checks when an entity get hurts (used to tag.)
  509. @param $event \pocketmine\event\entity\EntityDamageEvent
  510. */
  511. public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $event) {
  512. if($event->getEntity()->getLevel()->getName() == $this->getName()) {
  513. $this->getMain()->getLogger()->debug("Cancelling game hit: " . $this->getName());
  514. if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent
  515. && $event->getEntity() instanceof Player
  516. && $event->getDamager() instanceof Player
  517. && ($event->getDamager()->HideAndSeekRole : -1) == self::ROLE_SEEK || $event->getDamager()->HideAndSeekRole : -1) == self::ROLE_NEW_SEEK)
  518. && $event->getEntity()->HideAndSeekRole : -1) == self::ROLE_HIDE) { // Tagging
  519. $this->hidersLeft--;
  520. $event->getEntity()->HideAndSeekRole = self::ROLE_SEEK;
  521. $event->getEntity()->teleport($this->getSpawn());
  522. $event->getEntity()->sendMessage(Main::PREFIX . "§aYou got caught !");
  523. if($this->hidersLeft == 0) {
  524. $this->step = self::STEP_WIN;
  525. $this->win = self::WIN_SEEKERS;
  526. } else {
  527. $event->getEntity()->HideAndSeekRole = self::ROLE_NEW_SEEK;
  528. $event->getEntity()->sendMessage(Main::PREFIX . "§aYou're now a seeker!");
  529. $event->getEntity()->sendMessage("§lSeeker: Seek the hiders ! Catch them all to win in " . $this->getSeekTime() . " minutes to win !");
  530. }
  531. }
  532. $event->setCancelled();
  533. }
  534. }
  535. /*
  536. Checks when a player dies to prevent it.
  537. @param $event \pocketmine\event\player\PlayerDeathEvent
  538. */
  539. public function onPlayerDeath(\pocketmine\event\player\PlayerDeathEvent $event) {
  540. if($event->getPlayer()->getLevel()->getName() == $this->getName()) {
  541. $event->setCancelled();
  542. }
  543. }
  544. /*
  545. Checks when a player joins in the world to make him rejoin automaticly
  546. @param $event \pocketmine\event\player\PlayerJoinEvent
  547. */
  548. public function onPlayerJoin(\pocketmine\event\player\PlayerJoinEvent $event) {
  549. if($event->getPlayer()->getLevel()->getName() == $this->getName()) {
  550. $this->registerPlayer($event->getPlayer());
  551. }
  552. }
  553. /*
  554. Checks when a player leaves in the world to make him die.
  555. @param $event \pocketmine\event\player\PlayerQuitEvent
  556. */
  557. public function onPlayerQuit(\pocketmine\event\player\PlayerQuitEvent $event) {
  558. if($event->getPlayer()->getLevel()->getName() == $this->getName()) {
  559. $this->unregisterPlayer($event->getPlayer());
  560. }
  561. }
  562. }