Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Null;
- class ParticleCube {
- private $plugin;
- private $server;
- private $player;
- private $time;
- private $period;
- // Batch packet с патиклами
- private $particlesPacket;
- private $pos1 = array(
- 'x' => null,
- 'y' => null,
- 'z' => null);
- private $pos2 = array(
- 'x' => null,
- 'y' => null,
- 'z' => null);
- /**
- * @param PluginBase $plugin Основной класс плагина
- * @param Server $server Сервер
- * @param Player $player Игрок, которому будут показываться патиклы
- * @param Vector3 $pos1 Первый блок
- * @param Vector3 $pos2 Второй блок
- * @param integer $time Сколько секунд будет показываться патиклы
- * @param integer $period Через сколько тиков патиклы будут показываться заново
- */
- public function __construct($plugin, $server, $player, $pos1, $pos2, $time = 5, $period = 10) {
- $this->plugin = $plugin;
- $this->server = $server;
- $this->player = $player;
- $this->pos1 = $pos1;
- $this->pos2 = $pos2;
- $this->time = $time;
- $this->period = $period;
- $this->fixPosition();
- $this->generateParticle();
- $this->createTask();
- }
- private function fixPosition() {
- foreach ($this->pos1 as $axis => &$value) {
- if ($this->pos2[$axis] < $value) {
- $newPos2 = $value;
- $value = $this->pos2[$axis];
- $this->pos2[$axis] = $newPos2;
- }
- }
- }
- private function generateParticle() {
- if (class_exists('\pocketmine\network\protocol\LevelEventPacket', false)) {
- $pk = new \pocketmine\network\protocol\LevelEventPacket;
- $pk->evid = \pocketmine\network\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 10;
- }else {
- $pk = new \pocketmine\network\mcpe\protocol\LevelEventPacket;
- $pk->evid = \pocketmine\network\mcpe\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 10;
- }
- $pk->data = 2; // lifetime
- $particles = array();
- $addParticle = function ($x, $y, $z, $axis, $value, $pk) {
- $pos = array('x' => $x, 'y' => $y, 'z' => $z);
- $pos[$axis] = $value;
- $particle = clone $pk;
- $particle->x = $pos['x'];
- $particle->y = $pos['y'];
- $particle->z = $pos['z'];
- $particle->encode();
- return $particle;
- };
- // Это не говнокод, это Шедевр! Ну серьезно! Без шуток!
- foreach ($this->pos1 as $axis => $value) {
- if (abs($this->pos1[$axis] - $this->pos2[$axis]) < 500) $delta = 0.5;
- else $delta = abs($this->pos1[$axis] - $this->pos2[$axis]) / 500;
- for ($i = $this->pos1[$axis]; $i <= $this->pos2[$axis]; $i += $delta) {
- $particles[] = $addParticle($this->pos1['x'], $this->pos1['y'], $this->pos1['z'], $axis, $i, $pk);
- $particles[] = $addParticle($this->pos1['x'], $this->pos1['y'], $this->pos2['z'], $axis, $i, $pk);
- $particles[] = $addParticle($this->pos1['x'], $this->pos2['y'], $this->pos1['z'], $axis, $i, $pk);
- $particles[] = $addParticle($this->pos2['x'], $this->pos1['y'], $this->pos1['z'], $axis, $i, $pk);
- $particles[] = $addParticle($this->pos2['x'], $this->pos2['y'], $this->pos2['z'], $axis, $i, $pk);
- }
- }
- $this->batchPackets($particles);
- }
- private function batchPackets($packets) {
- if (class_exists('\pocketmine\network\protocol\BatchPacket', false)) {
- $pk = new \pocketmine\network\protocol\BatchPacket;
- }else {
- $pk = new \pocketmine\network\mcpe\protocol\BatchPacket;
- }
- foreach ($packets as $packet)
- $pk->payload .= \pocketmine\utils\Binary::writeUnsignedVarInt(strlen($packet->buffer)) . $packet->buffer;
- $pk->payload = zlib_encode($pk->payload, ZLIB_ENCODING_DEFLATE, 1);
- $pk->encode();
- $this->particlesPacket = $pk;
- }
- private function createTask() {
- $task = new SendParticlesTask($this->plugin, $this->player, $this->particlesPacket, $this->period, $this->time);
- $this->server->getScheduler()->scheduleRepeatingTask($task, $this->period);
- }
- }
- class SendParticlesTask extends \pocketmine\scheduler\PluginTask {
- private $player;
- private $particles;
- // Сколько раз должен выполниться таск
- private $count;
- public function __construct($plugin, $player, $particles, $period, $time) {
- parent::__construct($plugin);
- $this->player = $player;
- $this->particles = $particles;
- $this->count = $time * 20 / $period;
- }
- public function onRun($currentTick) {
- $this->player->dataPacket($this->particles, false, true);
- if ($this->count-- < 0) $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement