Пример #1
0
 protected function checkNearEntities($tickDiff)
 {
     foreach ($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity) {
         $entity->scheduleUpdate();
         if (!$entity->isAlive()) {
             continue;
         }
         if ($entity instanceof Arrow and $entity->hadCollision) {
             $item = Item::get(Item::ARROW, 0, 1);
             if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                 continue;
             }
             $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity));
             if ($ev->isCancelled()) {
                 continue;
             }
             $pk = new TakeItemEntityPacket();
             $pk->eid = $this->getId();
             $pk->target = $entity->getId();
             Server::broadcastPacket($entity->getViewers(), $pk);
             $pk = new TakeItemEntityPacket();
             $pk->eid = 0;
             $pk->target = $entity->getId();
             $this->dataPacket($pk);
             $this->inventory->addItem(clone $item);
             $entity->kill();
         } elseif ($entity instanceof DroppedItem) {
             if ($entity->getPickupDelay() <= 0) {
                 $item = $entity->getItem();
                 if ($item instanceof Item) {
                     if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                         continue;
                     }
                     $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $entity));
                     if ($ev->isCancelled()) {
                         continue;
                     }
                     switch ($item->getId()) {
                         case Item::WOOD:
                             $this->awardAchievement("mineWood");
                             break;
                         case Item::DIAMOND:
                             $this->awardAchievement("diamond");
                             break;
                     }
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = $this->getId();
                     $pk->target = $entity->getId();
                     Server::broadcastPacket($entity->getViewers(), $pk);
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = 0;
                     $pk->target = $entity->getId();
                     $this->dataPacket($pk);
                     $this->inventory->addItem(clone $item);
                     $entity->kill();
                 }
             }
         }
     }
 }
Пример #2
0
 public function onUpdate($currentTick)
 {
     if (!$this->loggedIn) {
         return false;
     }
     $tickDiff = $currentTick - $this->lastUpdate;
     if ($tickDiff <= 0) {
         return true;
     }
     $this->messageCounter = 2;
     $this->lastUpdate = $currentTick;
     if ($this->dead === true and $this->spawned) {
         ++$this->deadTicks;
         if ($this->deadTicks >= 10) {
             $this->despawnFromAll();
         }
         return $this->deadTicks < 10;
     }
     $this->timings->startTiming();
     $this->lastUpdate = $currentTick;
     if ($this->spawned) {
         $this->processMovement($currentTick);
         $this->entityBaseTick(1);
         if ($this->speed and $this->isSurvival()) {
             $speed = sqrt($this->speed->x ** 2 + $this->speed->z ** 2);
             if ($speed > 0.45) {
                 $this->highSpeedTicks += $speed > 3 ? 2 : 1;
                 if ($this->highSpeedTicks > 40 and !$this->server->getAllowFlight()) {
                     $this->kick("Flying is not enabled on this server");
                     return false;
                 } elseif ($this->highSpeedTicks >= 10 and $this->highSpeedTicks % 4 === 0) {
                     $this->forceMovement = $this->getPosition();
                     $this->speed = null;
                 }
             } elseif ($this->highSpeedTicks > 0) {
                 if ($speed < 22) {
                     $this->highSpeedTicks = 0;
                 } else {
                     $this->highSpeedTicks--;
                 }
             }
         }
         if ($this->onGround) {
             $this->inAirTicks = 0;
         } else {
             if ($this->inAirTicks > 10 and $this->isSurvival() and !$this->isSleeping()) {
                 $expectedVelocity = -$this->gravity / $this->drag - -$this->gravity / $this->drag * exp(-$this->drag * ($this->inAirTicks - 2));
                 $diff = sqrt(abs($this->speed->y - $expectedVelocity));
                 if ($diff > 0.6 and $expectedVelocity < $this->speed->y and !$this->server->getAllowFlight()) {
                     if ($this->inAirTicks < 100) {
                         $this->setMotion(new Vector3(0, $expectedVelocity, 0));
                     } else {
                         $this->kick("Flying is not enabled on this server");
                         return false;
                     }
                 }
             }
             ++$this->inAirTicks;
         }
         foreach ($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity) {
             if ($currentTick - $entity->lastUpdate > 1) {
                 $entity->scheduleUpdate();
             }
             if ($entity instanceof Arrow and $entity->hadCollision) {
                 if ($entity->dead !== true) {
                     $item = Item::get(Item::ARROW, 0, 1);
                     if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                         continue;
                     }
                     $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity));
                     if ($ev->isCancelled()) {
                         continue;
                     }
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = $this->getId();
                     $pk->target = $entity->getId();
                     Server::broadcastPacket($entity->getViewers(), $pk);
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = 0;
                     $pk->target = $entity->getId();
                     $this->dataPacket($pk);
                     $this->inventory->addItem(clone $item);
                     $entity->kill();
                 }
             } elseif ($entity instanceof DroppedItem) {
                 if ($entity->dead !== true and $entity->getPickupDelay() <= 0) {
                     $item = $entity->getItem();
                     if ($item instanceof Item) {
                         if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                             continue;
                         }
                         $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $entity));
                         if ($ev->isCancelled()) {
                             continue;
                         }
                         switch ($item->getId()) {
                             case Item::WOOD:
                                 $this->awardAchievement("mineWood");
                                 break;
                             case Item::DIAMOND:
                                 $this->awardAchievement("diamond");
                                 break;
                         }
                         $pk = new TakeItemEntityPacket();
                         $pk->eid = $this->getId();
                         $pk->target = $entity->getId();
                         Server::broadcastPacket($entity->getViewers(), $pk);
                         $pk = new TakeItemEntityPacket();
                         $pk->eid = 0;
                         $pk->target = $entity->getId();
                         $this->dataPacket($pk);
                         $this->inventory->addItem(clone $item);
                         $entity->kill();
                     }
                 }
             }
         }
     }
     if ($this->nextChunkOrderRun-- <= 0 or $this->chunk === null) {
         $this->orderChunks();
     }
     if (count($this->loadQueue) > 0 or !$this->spawned) {
         $this->sendNextChunk();
     }
     if (count($this->moveToSend->entities) > 0) {
         $this->dataPacket($this->moveToSend);
         $this->moveToSend->entities = [];
         $this->moveToSend->isEncoded = false;
     }
     if (count($this->motionToSend->entities) > 0) {
         $this->dataPacket($this->motionToSend);
         $this->motionToSend->entities = [];
         $this->motionToSend->isEncoded = false;
     }
     if (count($this->batchedPackets) > 0) {
         foreach ($this->batchedPackets as $packet) {
             $this->server->batchPackets([$this], [$packet], false);
         }
         $this->batchedPackets = [];
     }
     $this->lastUpdate = $currentTick;
     $this->timings->stopTiming();
     return true;
 }
Пример #3
0
 protected function checkNearEntities($tickDiff)
 {
     foreach ($this->level->getNearbyEntities($this->boundingBox->grow(0.5, 0.5, 0.5), $this) as $entity) {
         $entity->scheduleUpdate();
         if (!$entity->isAlive()) {
             continue;
         }
         if ($entity instanceof Arrow and $entity->hadCollision) {
             $item = Item::get(Item::ARROW, $entity->getPotionId(), 1);
             $add = false;
             if (!$this->server->allowInventoryCheats and !$this->isCreative()) {
                 if (!$this->getFloatingInventory()->canAddItem($item) or !$this->inventory->canAddItem($item)) {
                     //The item is added to the floating inventory to allow client to handle the pickup
                     //We have to also check if it can be added to the real inventory before sending packets.
                     continue;
                 }
                 $add = true;
             }
             $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity));
             if ($ev->isCancelled()) {
                 continue;
             }
             $pk = new TakeItemEntityPacket();
             $pk->eid = $this->getId();
             $pk->target = $entity->getId();
             Server::broadcastPacket($entity->getViewers(), $pk);
             $pk = new TakeItemEntityPacket();
             $pk->eid = 0;
             $pk->target = $entity->getId();
             $this->dataPacket($pk);
             if ($add) {
                 $this->getFloatingInventory()->addItem(clone $item);
             }
             $entity->kill();
         } elseif ($entity instanceof DroppedItem) {
             if ($entity->getPickupDelay() <= 0) {
                 $item = $entity->getItem();
                 if ($item instanceof Item) {
                     $add = false;
                     if (!$this->server->allowInventoryCheats and !$this->isCreative()) {
                         if (!$this->getFloatingInventory()->canAddItem($item) or !$this->inventory->canAddItem($item)) {
                             continue;
                         }
                         $add = true;
                     }
                     $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $entity));
                     if ($ev->isCancelled()) {
                         continue;
                     }
                     switch ($item->getId()) {
                         case Item::WOOD:
                             $this->awardAchievement("mineWood");
                             break;
                         case Item::DIAMOND:
                             $this->awardAchievement("diamond");
                             break;
                     }
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = $this->getId();
                     $pk->target = $entity->getId();
                     Server::broadcastPacket($entity->getViewers(), $pk);
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = 0;
                     $pk->target = $entity->getId();
                     $this->dataPacket($pk);
                     if ($add) {
                         $this->getFloatingInventory()->addItem(clone $item);
                     }
                     $entity->kill();
                 }
             }
         }
     }
 }
Пример #4
0
 public function onUpdate($currentTick)
 {
     if ($this->dead === true and $this->spawned) {
         ++$this->deadTicks;
         if ($this->deadTicks >= 10) {
             $this->despawnFromAll();
         }
         return $this->deadTicks < 10;
     }
     $this->timings->startTiming();
     $this->lastUpdate = $currentTick;
     if ($this->spawned) {
         $this->processMovement($currentTick);
         $this->entityBaseTick(1);
         if ($this->onGround) {
             $this->inAirTicks = 0;
         } else {
             if ($this->inAirTicks > 100 and $this->isSurvival() and !$this->isSleeping() and $this->spawned and !$this->server->getAllowFlight()) {
                 $this->kick("Flying is not enabled on this server");
                 return false;
             } else {
                 ++$this->inAirTicks;
             }
         }
         foreach ($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity) {
             if ($currentTick - $entity->lastUpdate > 1) {
                 $entity->scheduleUpdate();
             }
             if ($entity instanceof Arrow and $entity->onGround) {
                 if ($entity->dead !== true) {
                     $item = Item::get(Item::ARROW, 0, 1);
                     if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                         continue;
                     }
                     $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity));
                     if ($ev->isCancelled()) {
                         continue;
                     }
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = 0;
                     $pk->target = $entity->getId();
                     $this->dataPacket($pk);
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = $this->getId();
                     $pk->target = $entity->getId();
                     Server::broadcastPacket($entity->getViewers(), $pk);
                     $this->inventory->addItem(clone $item, $this);
                     $entity->kill();
                 }
             } elseif ($entity instanceof DroppedItem) {
                 if ($entity->dead !== true and $entity->getPickupDelay() <= 0) {
                     $item = $entity->getItem();
                     if ($item instanceof Item) {
                         if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                             continue;
                         }
                         $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $entity));
                         if ($ev->isCancelled()) {
                             continue;
                         }
                         switch ($item->getId()) {
                             case Item::WOOD:
                                 $this->awardAchievement("mineWood");
                                 break;
                             case Item::DIAMOND:
                                 $this->awardAchievement("diamond");
                                 break;
                         }
                         $pk = new TakeItemEntityPacket();
                         $pk->eid = 0;
                         $pk->target = $entity->getId();
                         $this->dataPacket($pk);
                         $pk = new TakeItemEntityPacket();
                         $pk->eid = $this->getId();
                         $pk->target = $entity->getId();
                         Server::broadcastPacket($entity->getViewers(), $pk);
                         $this->inventory->addItem(clone $item, $this);
                         $entity->kill();
                     }
                 }
             }
         }
     }
     if ($this->nextChunkOrderRun-- <= 0 or $this->chunk === null) {
         $this->orderChunks();
     }
     if (count($this->loadQueue) > 0 or !$this->spawned) {
         $this->sendNextChunk();
     }
     if (count($this->moveToSend) > 0) {
         $pk = new MoveEntityPacket();
         $pk->entities = $this->moveToSend;
         $this->dataPacket($pk);
         $this->moveToSend = [];
     }
     if (count($this->motionToSend) > 0) {
         $pk = new SetEntityMotionPacket();
         $pk->entities = $this->motionToSend;
         $this->dataPacket($pk);
         $this->motionToSend = [];
     }
     $this->timings->stopTiming();
     return true;
 }
Пример #5
0
 public function onUpdate($currentTick)
 {
     if (!$this->loggedIn) {
         return \false;
     }
     if ($this->dead === \true and $this->spawned) {
         ++$this->deadTicks;
         if ($this->deadTicks >= 10) {
             $this->despawnFromAll();
         }
         return $this->deadTicks < 10;
     }
     $this->timings->startTiming();
     if ($this->spawned) {
         $this->processMovement($currentTick);
         $this->entityBaseTick(1);
         if ($this->onGround) {
             $this->inAirTicks = 0;
         } else {
             if ($this->inAirTicks > 20 and $this->isSurvival() and !$this->isSleeping() and $this->spawned and !$this->server->getAllowFlight()) {
                 $expectedVelocity = -$this->gravity / $this->drag - -$this->gravity / $this->drag * \exp(-$this->drag * ($this->inAirTicks - 2));
                 $diff = ($this->speed->y - $expectedVelocity) ** 2;
                 if ($diff > 0.6 and $expectedVelocity < $this->speed->y) {
                     $this->kick("Disable fly mods.");
                 }
                 return \false;
             } else {
                 ++$this->inAirTicks;
             }
         }
         foreach ($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity) {
             if ($currentTick - $entity->lastUpdate > 1) {
                 $entity->scheduleUpdate();
             }
             if ($entity instanceof Arrow and $entity->hadCollision) {
                 if ($entity->dead !== \true) {
                     $item = Item::get(Item::ARROW, 0, 1);
                     if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                         continue;
                     }
                     $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity));
                     if ($ev->isCancelled()) {
                         continue;
                     }
                     $pk = new TakeItemEntityPacket();
                     $pk->eid = $this->getId();
                     $pk->target = $entity->getId();
                     Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
                     $this->inventory->addItem(clone $item);
                     $entity->kill();
                 }
             } elseif ($entity instanceof DroppedItem) {
                 if ($entity->dead !== \true and $entity->getPickupDelay() <= 0) {
                     $item = $entity->getItem();
                     if ($item instanceof Item) {
                         if ($this->isSurvival() and !$this->inventory->canAddItem($item)) {
                             continue;
                         }
                         $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $entity));
                         if ($ev->isCancelled()) {
                             continue;
                         }
                         switch ($item->getId()) {
                             case Item::WOOD:
                                 $this->awardAchievement("mineWood");
                                 break;
                             case Item::DIAMOND:
                                 $this->awardAchievement("diamond");
                                 break;
                         }
                         $pk = new TakeItemEntityPacket();
                         $pk->eid = $this->getId();
                         $pk->target = $entity->getId();
                         Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
                         $this->inventory->addItem(clone $item);
                         $entity->kill();
                     }
                 }
             }
         }
     }
     if ($this->nextChunkOrderRun-- <= 0 or $this->chunk === \null) {
         $this->orderChunks();
     }
     if (\count($this->loadQueue) > 0 or !$this->spawned) {
         $this->sendNextChunk();
     }
     if (\count($this->moveToSend) > 0) {
         $pk = new MoveEntityPacket();
         $pk->entities = $this->moveToSend;
         $this->batchDataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
         $this->moveToSend = [];
     }
     if (\count($this->motionToSend) > 0) {
         $pk = new SetEntityMotionPacket();
         $pk->entities = $this->motionToSend;
         $this->batchDataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
         $this->motionToSend = [];
     }
     if (\count($this->batchedPackets) > 0) {
         foreach ($this->batchedPackets as $channel => $list) {
             $this->server->batchPackets([$this], $list, \false, $channel);
         }
         $this->batchedPackets = [];
     }
     $this->lastUpdate = $currentTick;
     $this->timings->stopTiming();
     return \true;
 }