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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\PlayerChatEvent;
  10. use pocketmine\event\block\BlockBreakEvent;
  11. use pocketmine\event\block\BlockPlaceEvent;
  12. use pocketmine\event\entity\EntityDamageEvent;
  13. use pocketmine\item\Item;
  14. use pocketmine\math\Vector3;
  15. use pocketmine\nbt\NBT;
  16. use pocketmine\Server;
  17. use pocketmine\Player;
  18. class Main extends PluginBase implements Listener{
  19. public function onEnable(){
  20. $this->reloadConfig();
  21. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  22. $this->getServer()->getScheduler()->scheduleRepeatingTask(new setGamemodeTask($this), 5);
  23. $this->players = [];
  24. $this->lastPlayer = null;
  25. }
  26. public function onLoad(){
  27. $this->saveDefaultConfig();
  28. }
  29. public function test(Player $p, int $id) {
  30. $this->getLogger()->info("Item : " . $id . ". Is spectator : " . $this->isSpectator($p));
  31. if($this->isSpectator($p) and $id == 345) {
  32. // if($event->getPacket() instanceof \pocketmine\network\protocol\UseItemPacket) {
  33. $founds = [];
  34. foreach($p->getLevel()->getPlayers() as $player) {
  35. if(!$this->isSpectator($player)) {
  36. array_push($founds, $player);
  37. if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) {
  38. array_push($this->players[$p->getName()], $player);
  39. $found = true;
  40. $event->getPlayer()->teleport(new Vector3($player->x, $player->y, $player->z));
  41. $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage")))));
  42. }
  43. }
  44. }
  45. if(!isset($found)) {
  46. $this->players[$p->getName()] = [];
  47. }
  48. foreach($p->getLevel()->getPlayers() as $player) {
  49. if(!$this->isSpectator($player)) {
  50. array_push($founds, $player);
  51. if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) {
  52. array_push($this->players[$p->getName()], $player);
  53. $found = true;
  54. $p->teleport(new Vector3($player->x, $player->y, $player->z));
  55. $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage")))));
  56. }
  57. }
  58. }
  59. // }
  60. } elseif($this->isSpectator($event->getPlayer()) and $id == 355) {
  61. // if($event->getPacket() instanceof \pocketmine\network\protocol\UseItemPacket) {
  62. $p->getInventory()->clearAll();
  63. $p->teleport($this->getServer()->getLevelByName($this->getConfig()->get("LobbyWorld"))->getDefaultSpawn());
  64. $p->sendTip(str_ireplace("{lobby}", $this->getConfig()->get("LobbyWorld"), str_ireplace("{player}", $p->getName(), $this->getConfig()->get("LobbyMessage"))));
  65. // }
  66. }
  67. }
  68. public function onInteract(PlayerInteractEvent $event) {
  69. $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId());
  70. }
  71. public function onBlockPlace(BlockPlaceEvent $event) {
  72. $this->test($event->getPlayer(), $event->getBlock()->getId());
  73. }
  74. public function onBlockBreak(BlockBreakEvent $event) {
  75. $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId());
  76. }
  77. public function onEntityDamage(EntityDamageEvent $event) {
  78. if($event->getEntity() instanceof Player) {
  79. if($this->isSpectator($event->getEntity())) {
  80. $event->setCancelled();
  81. }
  82. }
  83. if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
  84. if($event->getDamager() instanceof Player) {
  85. if($this->isSpectator($event->getDamager())) {
  86. $event->setCancelled();
  87. $this->test($event->getDamager(), $event->getDamager()->getInventory()->getItemInHand()->getId());
  88. }
  89. }
  90. }
  91. }
  92. public function onPlayerChat(PlayerChatEvent $event) {
  93. if($this->getConfig()->get("PrivateSpecChat") == "true") {
  94. foreach($event->getPlayer()->getLevel()->getPlayers() as $p) {
  95. if($this->isSpectator($p)) {
  96. $p->sendMessage
  97. }
  98. }
  99. $event->setCancelled();
  100. }
  101. }
  102. public function onPlayerGameModeChange(PlayerGameModeChangeEvent $event) {
  103. if($event->getNewGamemode() == 3) { // Testing if spectator.
  104. $player = $event->getPlayer();
  105. $this->players[$player->getName()] = [];
  106. $this->lastPlayer = $player;
  107. } elseif($this->lastPlayer !== $player and isset($this->players[$event->getPlayer()->getName()])) {
  108. unset($this->players[$event->getPlayer()->getName()]);
  109. $event->getPlayer()->setAllowFlight(false);
  110. }
  111. }
  112. public function isSpectator(Player $player) {
  113. return isset($this->players[$player->getName()]);
  114. }
  115. public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
  116. switch($cmd->getName()){
  117. case "default":
  118. break;
  119. }
  120. return false;
  121. }
  122. }
  123. class setGamemodeTask extends \pocketmine\scheduler\PluginTask {
  124. public function __construct(Main $main) {
  125. parent::__construct($main);
  126. $this->main = $main;
  127. }
  128. public function onRun($tick) {
  129. foreach($this->main->players as $playername => $tped) {
  130. $player = $this->main->getServer()->getPlayer($playername);
  131. // $this->main->getLogger()->info($player->getGamemode() . "/" . $player->isSpectator());
  132. if($player->isSpectator()) {
  133. $player->setGamemode(0);
  134. $compass = Item::get(345, 0, 1);
  135. $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("TPCompassName") . '"}}'));
  136. $player->getInventory()->addItem($compass);
  137. $compass = Item::get(Item::BED, 0, 1);
  138. $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("BedBackName") . '"}}'));
  139. $player->getInventory()->addItem($compass);
  140. foreach($player->getLevel()->getPlayers() as $p) {
  141. if(!$this->main->isSpectator($p)) {
  142. $p->hidePlayer($player);
  143. }
  144. }
  145. $player->setAllowFlight(true);
  146. }
  147. }
  148. }
  149. }