|
| 1 | +<?php |
| 2 | +namespace aliuly\floatchat; |
| 3 | + |
| 4 | +use pocketmine\plugin\PluginBase; |
| 5 | +use pocketmine\Player; |
| 6 | +use pocketmine\event\Listener; |
| 7 | +use pocketmine\event\player\PlayerChatEvent; |
| 8 | +use pocketmine\event\player\PlayerQuitEvent; |
| 9 | +use pocketmine\event\player\PlayerMoveEvent; |
| 10 | +use pocketmine\event\entity\EntityTeleportEvent; |
| 11 | +use pocketmine\utils\Config; |
| 12 | +use pocketmine\utils\TextFormat; |
| 13 | +use pocketmine\command\CommandSender; |
| 14 | + |
| 15 | +use pocketmine\level\particle\FloatingTextParticle; |
| 16 | +use pocketmine\math\Vector3; |
| 17 | + |
| 18 | +use pocketmine\scheduler\PluginTask; |
| 19 | + |
| 20 | +class cleanUpTask extends PluginTask { |
| 21 | + public function onRun($currentTick){ |
| 22 | + if ($this->owner->isDisabled()) return; |
| 23 | + $this->owner->timerTick(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class Main extends PluginBase implements Listener { |
| 29 | + protected $particles; |
| 30 | + protected $timeout = 5; |
| 31 | + |
| 32 | + protected static function iName($player) { |
| 33 | + return strtolower($player->getName()); |
| 34 | + } |
| 35 | + |
| 36 | + // Access and other permission related checks |
| 37 | + private function access(CommandSender $sender, $permission) { |
| 38 | + if($sender->hasPermission($permission)) return true; |
| 39 | + $sender->sendMessage("You do not have permission to do that."); |
| 40 | + return false; |
| 41 | + } |
| 42 | + ////////////////////////////////////////////////////////////////////// |
| 43 | + // |
| 44 | + // Standard call-backs |
| 45 | + // |
| 46 | + ////////////////////////////////////////////////////////////////////// |
| 47 | + public function onEnable(){ |
| 48 | + //if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); |
| 49 | + $this->getServer()->getPluginManager()->registerEvents($this, $this); |
| 50 | + $this->particles = []; |
| 51 | + $this->getServer()->getScheduler()->scheduleRepeatingTask(new cleanUpTask($this),20); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + protected function deSpawn($particle,$level) { |
| 56 | + if ($level === null) return; |
| 57 | + $particle->setInvisible(); |
| 58 | + $level->addParticle($particle); |
| 59 | + } |
| 60 | + |
| 61 | + ////////////////////////////////////////////////////////////////////// |
| 62 | + // |
| 63 | + // Event handlers |
| 64 | + // |
| 65 | + ////////////////////////////////////////////////////////////////////// |
| 66 | + public function timerTick() { |
| 67 | + $now = time(); |
| 68 | + foreach (array_keys($this->particles) as $n) { |
| 69 | + list($particle,$level,$expby,) = $this->particles[$n]; |
| 70 | + if ($level !== null && $now > $expby) { |
| 71 | + $this->deSpawn($particle,$level); |
| 72 | + $this->particles[$n][1] = null; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function onQuit(PlayerQuitEvent $e){ |
| 78 | + $n = self::iName($e->getPlayer()); |
| 79 | + if (isset($this->particles[$n])) { |
| 80 | + list($p,$level,,) = $this->particles[$n]; |
| 81 | + unset($this->particles[$n]); |
| 82 | + $this->deSpawn($p,$level); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public function onMove(PlayerMoveEvent $e) { |
| 87 | + $n = self::iName($e->getPlayer()); |
| 88 | + if (!isset($this->particles[$n])) return; |
| 89 | + list($p,$level,,) = $this->particles[$n]; |
| 90 | + if ($level === null) return; |
| 91 | + $pw = $e->getPlayer(); |
| 92 | + $p->x = $pw->getX(); |
| 93 | + $p->y = $pw->getY()+2+count($this->particles[$n][3])*0.5; |
| 94 | + $p->z = $pw->getZ(); |
| 95 | + $pw->getLevel()->addParticle($p); |
| 96 | + } |
| 97 | + |
| 98 | + public function onChat(PlayerChatEvent $e){ |
| 99 | + if ($e->isCancelled()) return; |
| 100 | + $pw = $e->getPlayer(); |
| 101 | + // Non players are handled normally |
| 102 | + if (!($pw instanceof Player)) return; |
| 103 | + |
| 104 | + $msg = $e->getMessage(); |
| 105 | + if (substr($msg,0,1) == ":") { |
| 106 | + // This messages goes to everybody on the server... |
| 107 | + // no need to do much... |
| 108 | + if (!$this->access($pw,"floatchat.broadcast.server")) { |
| 109 | + $e->setCancelled(); |
| 110 | + return; |
| 111 | + } |
| 112 | + $e->setMessage(substr($msg,1)); |
| 113 | + return; |
| 114 | + } |
| 115 | + if (substr($msg,0,1) == ".") { |
| 116 | + $target = []; |
| 117 | + if (!$this->access($pw,"floatchat.broadcast.level")) { |
| 118 | + $e->setCancelled(); |
| 119 | + return; |
| 120 | + } |
| 121 | + // Send this message to everybody on this level |
| 122 | + $e->setMessage(substr($msg,1)); |
| 123 | + foreach ($e->getRecipients() as $pr) { |
| 124 | + if ($pr instanceof Player) { |
| 125 | + if (!$pr->hasPermission("floatchat.spy") && |
| 126 | + $pr->getLevel() != $pw->getLevel()) continue; |
| 127 | + } |
| 128 | + $target[] = $pr; |
| 129 | + } |
| 130 | + $e->setRecipients($target); |
| 131 | + return; |
| 132 | + } |
| 133 | + $target = []; |
| 134 | + foreach ($e->getRecipients() as $pr) { |
| 135 | + if ($pr instanceof Player) { |
| 136 | + if (!$pr->hasPermission("floatchat.spy") && $pr != $pw) continue; |
| 137 | + } |
| 138 | + $target[] = $pr; |
| 139 | + } |
| 140 | + $e->setRecipients($target); |
| 141 | + echo __METHOD__.",".__LINE__."\n";//##DEBUG |
| 142 | + |
| 143 | + $n = self::iName($pw); |
| 144 | + if (!isset($this->particles[$n])) |
| 145 | + $this->particles[$n] = [new FloatingTextParticle($pw,""), null, 0, ""]; |
| 146 | + |
| 147 | + $p = $this->particles[$n][0]; |
| 148 | + $msg = $e->getMessage(); |
| 149 | + if ($p->isInvisible()) { |
| 150 | + $this->particles[$n][3] = [ $msg ]; |
| 151 | + $p->setText(TextFormat::YELLOW.$msg); |
| 152 | + $p->setInvisible(false); |
| 153 | + } else { |
| 154 | + $this->particles[$n][3][] = $msg; |
| 155 | + while (count($this->particles[$n][3]) > 3) { |
| 156 | + array_shift($this->particles[$n][3]); |
| 157 | + } |
| 158 | + $p->setText(TextFormat::YELLOW.implode("\n",$this->particles[$n][3])); |
| 159 | + } |
| 160 | + $p->x = $pw->getX(); |
| 161 | + $p->y = $pw->getY()+2+count($this->particles[$n][3])*0.5; |
| 162 | + $p->z = $pw->getZ(); |
| 163 | + $this->particles[$n][1] = $pw->getLevel(); |
| 164 | + $this->particles[$n][2] = time()+$this->timeout; |
| 165 | + |
| 166 | + $pw->getLevel()->addParticle($p); |
| 167 | + } |
| 168 | +} |
0 commit comments