Advertisement
Gistrec

ParticleCube_v2

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