Make player specator at the next level on 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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Ad5001\SpectatorPlus;
  3. use pocketmine\command\CommandSender;
  4. use pocketmine\command\Command;
  5. use pocketmine\event\Listener;
  6. use pocketmine\plugin\PluginBase;
  7. use pocketmine\event\player\PlayerGameModeChangeEvent;
  8. use pocketmine\event\player\PlayerInteractEvent;
  9. use pocketmine\event\player\PlayerQuitEvent;
  10. use pocketmine\event\player\PlayerJoinEvent;
  11. use pocketmine\event\player\PlayerChatEvent;
  12. use pocketmine\event\block\BlockBreakEvent;
  13. use pocketmine\event\block\BlockPlaceEvent;
  14. use pocketmine\event\entity\EntityDamageEvent;
  15. use pocketmine\item\Item;
  16. use pocketmine\math\Vector3;
  17. use pocketmine\utils\Utils;
  18. use pocketmine\entity\Entity;
  19. use pocketmine\nbt\NBT;
  20. use pocketmine\Server;
  21. use pocketmine\Player;
  22. class Main extends PluginBase implements Listener{
  23. public function onEnable(){
  24. $this->saveDefaultConfig();
  25. $this->reloadConfig();
  26. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  27. $this->getServer()->getScheduler()->scheduleRepeatingTask(new setGamemodeTask($this), 15);
  28. $this->getServer()->getScheduler()->scheduleRepeatingTask($this->teleportTask = new teleportTask($this), 0.5);
  29. $this->getLogger()->info("Enabled ! Thanks for choosing SpectatorPlus ! Gloabaly, " . Utils::getURL("http://mc-pe.ga/tracking/index.php?serverId=" . $this->getServer()->getServerUniqueId() . "&plugin=SpectatorPlus&count=SpectatorPlus", 40) . " servers are running SpectatorPlus !");
  30. $this->players = [];
  31. $this->quitedplayers = [];
  32. $this->lastPlayer = null;
  33. }
  34. public function test(Player $p, int $id) {
  35. if($this->isSpectator($p) and $id == 345) {
  36. $founds = [];
  37. foreach($p->getLevel()->getPlayers() as $player) {
  38. if(!$this->isSpectator($player)) {
  39. array_push($founds, $player);
  40. if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) {
  41. array_push($this->players[$p->getName()], $player);
  42. $found = true;
  43. $event->getPlayer()->teleport(new Vector3($player->x, $player->y, $player->z));
  44. $this->teleportTask->remove($p);
  45. $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage")))));
  46. }
  47. }
  48. }
  49. if(!isset($found)) {
  50. $this->players[$p->getName()] = [];
  51. }
  52. foreach($p->getLevel()->getPlayers() as $player) {
  53. if(!$this->isSpectator($player)) {
  54. array_push($founds, $player);
  55. if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) {
  56. array_push($this->players[$p->getName()], $player);
  57. $found = true;
  58. $p->teleport(new Vector3($player->x, $player->y, $player->z));
  59. $this->teleportTask->remove($p);
  60. $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage")))));
  61. }
  62. }
  63. }
  64. } elseif($this->isSpectator($p) and $id == 355) {
  65. $p->getInventory()->clearAll();
  66. $p->setGamemode(2);
  67. $p->setGamemode(0);
  68. $p->teleport($this->getServer()->getLevelByName($this->getConfig()->get("LobbyWorld"))->getSpawnLocation());
  69. $p->sendTip(str_ireplace("{lobby}", $this->getConfig()->get("LobbyWorld"), str_ireplace("{player}", $p->getName(), $this->getConfig()->get("LobbyMessage"))));
  70. $this->teleportTask->remove($p);
  71. } elseif($this->isSpectator($p) and $id == Item::FEATHER) {
  72. $this->teleportTask->remove($p);
  73. $item = Item::get(Item::FEATHER, 0, 1);
  74. $item->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->getConfig()->get("EscapeViewName") . '"}}'));
  75. $p->getInventory()->remove($item);
  76. }
  77. }
  78. public function onInteract(PlayerInteractEvent $event) {
  79. $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId());
  80. }
  81. public function onBlockPlace(BlockPlaceEvent $event) {
  82. $this->test($event->getPlayer(), $event->getBlock()->getId());
  83. if($this->isSpectator($event->getPlayer())) {
  84. $event->setCancelled();
  85. }
  86. }
  87. public function onBlockBreak(BlockBreakEvent $event) {
  88. $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId());
  89. if($this->isSpectator($event->getPlayer())) {
  90. $event->setCancelled();
  91. }
  92. }
  93. public function onEntityDamage(EntityDamageEvent $event) {
  94. if($event->getEntity() instanceof Player) {
  95. if($this->isSpectator($event->getEntity())) {
  96. $event->setCancelled();
  97. }
  98. }
  99. if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
  100. if($event->getDamager() instanceof Player) {
  101. if($this->isSpectator($event->getDamager())) {
  102. $event->setCancelled();
  103. $this->test($event->getDamager(), $event->getDamager()->getInventory()->getItemInHand()->getId());
  104. }
  105. if($this->isSpectator($event->getDamager()) and $event->getDamager()->getInventory()->getItemInHand()->getId() == 0) {
  106. $this->teleportTask->add($event->getDamager(), $event->getEntity());
  107. }
  108. }
  109. }
  110. }
  111. public function onPlayerChat(PlayerChatEvent $event) {
  112. if($this->getConfig()->get("PrivateSpecChat") == "true") {
  113. foreach($event->getPlayer()->getLevel()->getPlayers() as $p) {
  114. if($this->isSpectator($p)) {
  115. $p->sendMessage(\pocketmine\utils\TextFormat::GRAY . "[SPEC] " . $event->getPlayer()->getName() . " > " . $event->getMessage());
  116. }
  117. }
  118. $event->setCancelled();
  119. }
  120. }
  121. public function onPlayerGameModeChange(PlayerGameModeChangeEvent $event) {
  122. if($event->getNewGamemode() == 3) { // Testing if spectator.
  123. $player = $event->getPlayer();
  124. $this->players[$player->getName()] = [];
  125. $this->lastPlayer = $player->getName();
  126. $player->setDisplayName(\pocketmine\utils\TextFormat::GRAY . "[SPEC] " . $event->getPlayer()->getName());
  127. } elseif($this->lastPlayer !== $event->getPlayer()->getName() and isset($this->players[$event->getPlayer()->getName()])) {
  128. // $this->getLogger()->info("Removed {$event->getPlayer()->getName()}");
  129. unset($this->players[$event->getPlayer()->getName()]);
  130. $event->getPlayer()->setAllowFlight(false);
  131. $event->getPlayer()->getInventory()->clearAll();
  132. $this->teleportTask->remove($event->getPlayer());
  133. $event->getPlayer()->setDisplayName($event->getPlayer()->getName());
  134. $event->getPlayer()->getInventory()->clearAll();
  135. } elseif($this->lastPlayer == $event->getPlayer()->getName()) {
  136. $this->lastPlayer = null;
  137. }
  138. }
  139. public function isSpectator(Player $player) {
  140. return isset($this->players[$player->getName()]);
  141. }
  142. public function onPlayerQuit(PlayerQuitEvent $event) {
  143. if($this->isSpectator($event->getPlayer())) {
  144. $this->quitedplayers[$event->getPlayer()->getName()] = true;
  145. unset($this->players[$event->getPlayer()->getName()]);
  146. }
  147. }
  148. public function onPlayerJoin(PlayerJoinEvent $event) {
  149. if(isset($this->quitedplayers[$event->getPlayer()->getName()])) {
  150. $this->players[$event->getPlayer()->getName()] = [];
  151. unset($this->quitedplayers[$event->getPlayer()->getName()]);
  152. }
  153. }
  154. public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
  155. switch($cmd->getName()){
  156. case "default":
  157. break;
  158. }
  159. return false;
  160. }
  161. }
  162. class setGamemodeTask extends \pocketmine\scheduler\PluginTask {
  163. public function __construct(Main $main) {
  164. parent::__construct($main);
  165. $this->main = $main;
  166. }
  167. public function onRun($tick) {
  168. foreach($this->main->players as $playername => $tped) {
  169. $player = $this->main->getServer()->getPlayer($playername);
  170. if($player->isSpectator()) {
  171. $player->setGamemode(0);
  172. $compass = Item::get(345, 0, 1);
  173. $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("TPCompassName") . '"}}'));
  174. $player->getInventory()->addItem($compass);
  175. $compass = Item::get(Item::BED, 0, 1);
  176. $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("BedBackName") . '"}}'));
  177. $player->getInventory()->addItem($compass);
  178. foreach($player->getLevel()->getPlayers() as $p) {
  179. if(!$this->main->isSpectator($p)) {
  180. $p->hidePlayer($player);
  181. }
  182. }
  183. $player->setAllowFlight(true);
  184. }
  185. }
  186. }
  187. }
  188. class teleportTask extends \pocketmine\scheduler\PluginTask {
  189. public function __construct(Main $main) {
  190. parent::__construct($main);
  191. $this->main = $main;
  192. $this->players = [];
  193. }
  194. public function onRun($tick) {
  195. foreach($this->players as $pname => $tpname) {
  196. $p = $this->main->getServer()->getPlayer($pname);
  197. $tp = $p->getLevel()->getEntity($tpname);
  198. if($p instanceof Player) {
  199. $tp = $p->getLevel()->getEntity($tpname);
  200. if($tp instanceof Entity) {
  201. $p->teleport(new Vector3($tp->x, $tp->y + 0.5, $tp->z), $tp->yaw, $tp->pitch);
  202. }
  203. }
  204. }
  205. }
  206. public function add(Player $player, Entity $to) {
  207. $this->players[$player->getName()] = $to->getId();
  208. $item = Item::get(Item::FEATHER, 0, 1);
  209. $item->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("EscapeViewName") . '"}}'));
  210. $player->getInventory()->addItem($item);
  211. $e = \pocketmine\entity\Effect::getEffectByName("INVISIBILITY");
  212. $e->setAmbient(true);
  213. $e->setVisible(false);
  214. $player->addEffect($e);
  215. }
  216. public function remove(Player $player) {
  217. if(isset($this->players[$player->getName()])) {
  218. unset($this->players[$player->getName()]);
  219. $player->removeEffect(14);
  220. $item = Item::get(Item::FEATHER, 0, 1);
  221. $item->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("EscapeViewName") . '"}}'));
  222. $player->getInventory()->removeItem($item);
  223. return true;
  224. }
  225. return false;
  226. }
  227. }