Gistrec

Show сube area by particle

Aug 18th, 2017
331
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ЗаменитеНаВаш;
  4.  
  5. class ParticleCube {
  6.  
  7.     private $plugin;
  8.  
  9.     private $server;
  10.  
  11.     private $player;
  12.  
  13.     private $time;
  14.  
  15.     private $period;
  16.  
  17.     // Batch packet с патиклами
  18.     private $particlesPacket;
  19.  
  20.     private $pos1 = array(
  21.         'x' => null,
  22.         'y' => null,
  23.         'z' => null);
  24.  
  25.     private $pos2 = array(
  26.         'x' => null,
  27.         'y' => null,
  28.         'z' => null);
  29.  
  30.     /**
  31.      * @param PluginBase $plugin  Основной класс плагина
  32.      * @param Server     $server  Сервер
  33.      * @param Player     $player  Игрок, которому будут показываться патиклы
  34.      * @param Vector3    $pos1    Первый блок
  35.      * @param Vector3    $pos2    Второй блок
  36.      * @param integer    $time    Сколько секунд будет показываться патиклы
  37.      * @param integer    $period  Через сколько тиков патиклы будут показываться заного
  38.      */
  39.     public function __construct($plugin, $server, $player, $pos1, $pos2, $time = 5, $period = 10) {
  40.         $this->plugin = $plugin;
  41.         $this->server = $server;
  42.         $this->player = $player;
  43.         $this->pos1 = $pos1;
  44.         $this->pos2 = $pos2;
  45.         $this->time = $time;
  46.         $this->period = $period;
  47.         $this->fixPosition();
  48.         $this->generateParticle();
  49.         $this->createTask();
  50.     }
  51.  
  52.     private function fixPosition() {
  53.         foreach ($this->pos1 as $axis => &$value) {
  54.             if ($this->pos2[$axis] < $value) {
  55.                 $newPos2 = $value;
  56.                 $value = $this->pos2[$axis];
  57.                 $this->pos2[$axis] = $newPos2;
  58.             }
  59.         }
  60.     }
  61.  
  62.     private function generateParticle() {
  63.         // Create redstone particle
  64.         if (class_exists('pocketmine\network\mcpe\protocol\LevelEventPacket', false)) {
  65.             $pk = new \pocketmine\network\mcpe\protocol\LevelEventPacket;
  66.             $pk->evid = \pocketmine\network\mcpe\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 10;
  67.         }else {
  68.             $pk = new \pocketmine\network\protocol\LevelEventPacket;
  69.             $pk->evid = \pocketmine\network\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 10;
  70.         }
  71.         $pk->data = 2; // lifetime
  72.  
  73.         $particles = array();
  74.  
  75.         for ($x = $this->pos1['x']; $x < $this->pos2['x']; $x += 1) {
  76.             for ($y = $this->pos1['y']; $y < $this->pos2['y']; $y += 1) {
  77.                 for ($z = $this->pos1['z']; $z < $this->pos2['z']; $z += 1) {
  78.                     $particle = clone $pk;
  79.                     $particle->x = $x;
  80.                     $particle->y = $y;
  81.                     $particle->z = $z;
  82.                     $particle->encode();
  83.                     $particles[] = $particle;
  84.                 }
  85.             }
  86.         }
  87.  
  88.         $this->batchPackets($particles);
  89.     }
  90.  
  91.     private function batchPackets($packets) {
  92.         if (class_exists('pocketmine\network\mcpe\protocol\BatchPacket', false))
  93.             $pk = new \pocketmine\network\mcpe\protocol\BatchPacket;
  94.         else $pk = new \pocketmine\network\protocol\BatchPacket;
  95.  
  96.         foreach ($packets as $packet)
  97.             $pk->payload .= \pocketmine\utils\Binary::writeUnsignedVarInt(strlen($packet->buffer)) . $packet->buffer;
  98.  
  99.         $pk->payload = zlib_encode($pk->payload, ZLIB_ENCODING_DEFLATE, 1);
  100.         $pk->encode();
  101.  
  102.         $this->particlesPacket = $pk;
  103.     }
  104.  
  105.     private function createTask() {
  106.         $task = new SendParticlesTask($this->plugin, $this->player, $this->particlesPacket, $this->period, $this->time);
  107.         $this->server->getScheduler()->scheduleRepeatingTask($task, $this->period);
  108.     }
  109. }
  110.  
  111. class SendParticlesTask extends \pocketmine\scheduler\PluginTask {
  112.  
  113.     private $player;
  114.     private $particles;
  115.  
  116.     // Сколько раз должен выполниться таск
  117.     private $count;
  118.  
  119.     public function __construct($plugin, $player, $particles, $period, $time) {
  120.         parent::__construct($plugin);
  121.         $this->player = $player;
  122.         $this->particles = $particles;
  123.         $this->count = $time * 20 / $period;
  124.     }
  125.  
  126.     public function onRun($currentTick) {
  127.         $this->player->dataPacket($this->particles, false, true);
  128.         if ($this->count-- < 0) $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId());
  129.     }
  130. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment