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 12KB

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