An highly customisable UHC plugin for PocketMine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Server;
  14. use pocketmine\event\player\PlayerDeathEvent;
  15. use pocketmine\event\player\PlayerChatEvent;
  16. use pocketmine\event\player\PlayerQuitEvent;
  17. use pocketmine\event\player\PlayerRespawnEvent;
  18. use pocketmine\event\entity\EntityRegainHealthEvent;
  19. use pocketmine\level\Level;
  20. use pocketmine\plugin\Plugin;
  21. use pocketmine\math\Vector3;
  22. use pocketmine\utils\TextFormat as C;
  23. use pocketmine\Player;
  24. use Ad5001\UHC\UHCWorld;
  25. use Ad5001\UHC\task\StopResTask;
  26. use Ad5001\UHC\Main;
  27. use Ad5001\UHC\event\GameStartEvent;
  28. use Ad5001\UHC\event\GameStopEvent;
  29. class UHCGame implements Listener{
  30. public function __construct(Plugin $plugin, UHCWorld $world) {
  31. $this->m = $plugin;
  32. $this->world = $world;
  33. $world->getLevel()->setTime(0);
  34. $plugin->getServer()->getPluginManager()->registerEvents($this, $plugin);
  35. $event = new GameStartEvent($this, $world, $world->getLevel()->getPlayers());
  36. $this->m->getServer()->getPluginManager()->callEvent($event);
  37. $this->cancelled = false;
  38. $this->kills = [];
  39. if($event->isCancelled()) {
  40. $this->cancelled = true;
  41. } else {
  42. $radius = $world->radius;
  43. foreach($world->getLevel()->getPlayers() as $player) {
  44. $player->getInventory()->clearAll();
  45. $player->setGamemode(0);
  46. for($e = 1; $e < 24; $e++) {$player->removeEffect($e);}
  47. $x = rand($radius + $world->getLevel()->getSpawnLocation()->x, $world->getLevel()->getSpawnLocation()->x - $radius);
  48. $z = rand($radius + $world->getLevel()->getSpawnLocation()->z, $world->getLevel()->getSpawnLocation()->z - $radius);
  49. $pos = new Vector3($x, 128, $z);
  50. $player->teleport($pos);
  51. $effect = \pocketmine\entity\Effect::getEffect(11);
  52. $effect->setDuration(30*20);
  53. $effect->setAmplifier(99);
  54. $effect->setVisible(false);
  55. $player->addEffect($effect);
  56. $this->m->getServer()->getScheduler()->scheduleDelayedTask(new StopResTask($this->m, $this->world->getPlayers()), 30*20);
  57. $player->sendMessage(Main::PREFIX . C::GREEN . "Game started ! Good luck {$player->getName()} !");
  58. }
  59. }
  60. }
  61. public function onHeal(EntityRegainHealthEvent $event) {
  62. if($event->getEntity() instanceof Player and $event->getRegainReason() === EntityRegainHealthEvent::CAUSE_SATURATION and $event->getEntity()->getLevel()->getName() === $this->world->getLevel()->getName()) { // if player is playing
  63. $event->setCancelled();
  64. }
  65. }
  66. public function onGameStart(\Ad5001\UHC\event\GameStartEvent $event) {}
  67. public function onGameStop(\Ad5001\UHC\event\GameStopEvent $event) {}
  68. public function onRespawn(PlayerRespawnEvent $event) {
  69. if(isset($this->respawn[$event->getPlayer()->getName()]) and !$this->cancelled) {
  70. $event->getPlayer()->setGamemode(3);
  71. unset($this->respawn[$event->getPlayer()->getName()]);
  72. }
  73. }
  74. public function onPlayerQuit(PlayerQuitEvent $event) {
  75. if($event->getPlayer()->getLevel()->getName() === $this->world->getLevel()->getName()) {
  76. $this->m->quit[$event->getPlayer()->getName()] = "{$event->getPlayer()->x}/{$event->getPlayer()->y}/{$event->getPlayer()->z}/{$event->getPlayer()->getLevel()->getName()}/";
  77. }
  78. }
  79. public function onPlayerDeath(PlayerDeathEvent $event) {
  80. if($event->getPlayer()->getLevel()->getName() === $this->world->getName() and !$this->cancelled) {
  81. foreach($event->getPlayer()->getLevel()->getPlayers() as $p) {
  82. $p->sendMessage(Main::PREFIX . C::YELLOW . $event->getPlayer()->getName() . " died. " . (count($this->world->getLevel()->getPlayers()) - 1) . " players left !");
  83. }
  84. $this->respawn[$event->getPlayer()->getName()] = true;
  85. $pls = [];
  86. foreach($event->getPlayer()->getLevel()->getPlayers() as $pl) {
  87. array_push($pls, $pl);
  88. }
  89. $cause = $event->getEntity()->getLastDamageCause();
  90. if($cause instanceof \pocketmine\event\entity\EntityDamageByEntityEvent){
  91. $killer = $cause->getDamager();
  92. if($killer instanceof Player){
  93. if(isset($this->kills[$killer->getName()])) {
  94. $this->kills[$killer->getName()]++;
  95. } else {
  96. $this->kills[$killer->getName()] = 1;
  97. }
  98. } else {
  99. if(isset($this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"])) {
  100. $this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"]++;
  101. } else {
  102. $this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"] = 1;
  103. }
  104. }
  105. } else {
  106. if(isset($this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"])) {
  107. $this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"]++;
  108. } else {
  109. $this->kills[C::GREEN . "P" . C::BLUE . "v" . C::RED . "E"] = 1;
  110. }
  111. }
  112. if(count($pls) == 2) {
  113. foreach($pls as $p) {
  114. if($p !== $event->getPlayer()) {
  115. $this->stop($p);
  116. }
  117. }
  118. } elseif(count($pls) == 1) {
  119. $this->stop($event->getPlayer());
  120. }
  121. }
  122. }
  123. public function stop(Player $winner) {
  124. $this->m->getServer()->getPluginManager()->callEvent($ev = new GameStopEvent($this, $this->world, $winner));
  125. if(!$ev->cancelled) {
  126. foreach($winner->getLevel()->getPlayers() as $player) {
  127. $player->sendMessage(Main::PREFIX . C::YELLOW . $winner->getName() . " won the game ! Teleporting back to lobby...");
  128. $player->teleport($this->m->getServer()->getLevelByName($this->m->getConfig()->get("LobbyWorld"))->getSafeSpawn());
  129. $this->m->UHCManager->stopUHC($this->world->getLevel(), $winner);
  130. }
  131. }
  132. }
  133. public function getPlayers() {
  134. return $this->world->getPlayers();
  135. }
  136. public function onPlayerChat(PlayerChatEvent $event) {
  137. if($event->getPlayer()->getLevel()->getName() === $this->world->getLevel()->getName() and $event->getPlayer()->getGamemode() === 3) {
  138. if($event->getPlayer()->isSpectator()) {
  139. foreach($this->world->getLevel()->getPlayer() as $player) {
  140. $player->sendMessage(C::GRAY . "[SPECTATOR] {$event->getPlayer()->getName()} > " . $event->getMessage());
  141. }
  142. $event->setCancelled(true);
  143. }
  144. }
  145. }
  146. /*
  147. Will be useful for scenarios:
  148. @param player
  149. */
  150. public function getKills(Player $player) {
  151. if(isset($this->kills[$player->getName()])) {
  152. return $this->kills[$player->getName()];
  153. } else {
  154. return null;
  155. }
  156. }
  157. /*
  158. Will be useful for scenarios too:
  159. @param player
  160. */
  161. public function addKills(Player $player, int $count) {
  162. if(isset($this->kills[$player->getName()])) {
  163. $this->kills[$player->getName()] += $count;
  164. } else {
  165. $this->kills[$player->getName()] = $count;
  166. }
  167. return true;
  168. }
  169. }