/** * 刷僵尸计时器 */ public function MobGenerate() { foreach ($this->getServer()->getOnlinePlayers() as $p) { //$this->server->getLogger()->info("准备生成僵尸"); $level = $p->getLevel(); $max = 15; //if ($level->getTime() >= 13500) { //是夜晚 //$this->server->getLogger()->info("时间OK"); $v3 = new Vector3($p->getX() + mt_rand(-$this->birth_r, $this->birth_r), $p->getY(), $p->getZ() + mt_rand(-$this->birth_r, $this->birth_r)); for ($y0 = $p->getY() - 10; $y0 <= $p->getY() + 10; $y0++) { $v3->y = $y0; if ($this->whatBlock($level, $v3) == "block") { //$this->server->getLogger()->info("方块OK"); $v3_1 = $v3; $v3_1->y = $y0 + 1; $v3_2 = $v3; $v3_2->y = $y0 + 2; $random = mt_rand(0, 1); if ($level->getBlock($v3_1)->getID() == 0 and $level->getBlock($v3_2)->getID() == 0) { //找到地面 /** @var Entity[] $zoC */ $zoC = []; /** @var Entity[] $cowc */ $cowc = []; foreach ($level->getEntities() as $zo) { if ($zo instanceof Zombie) { $zoC[] = $zo; } if ($zo instanceof Cow) { $cowc[] = $zo; } } if (count($zoC) > $max) { for ($i = 0; $i < count($zoC) - $max; $i++) { $zoC[$i]->kill(); } } elseif ($random == 0 && $level->getTime() >= 13500) { $pos = new Position($v3->x, $v3->y, $v3->z, $level); $this->server->getPluginManager()->callEvent($ev = new EntityGenerateEvent($pos, Zombie::NETWORK_ID, EntityGenerateEvent::CAUSE_AI_HOLDER)); if (!$ev->isCancelled()) { $this->spawnZombie($ev->getPosition()); } //$this->server->getLogger()->info("生成1僵尸"); } if (count($cowc) > $max) { for ($i = 0; $i < count($cowc) - $max; $i++) { $cowc[$i]->kill(); } } elseif ($random == 1) { $pos = new Position($v3->x, $v3->y, $v3->z, $level); $this->server->getPluginManager()->callEvent($ev = new EntityGenerateEvent($pos, Cow::NETWORK_ID, EntityGenerateEvent::CAUSE_AI_HOLDER)); if (!$ev->isCancelled()) { $this->spawnCow($ev->getPosition()); } //$this->server->getLogger()->info("生成1牛"); } break; } } } } }
public function onUpdate() { if ($this->closed === true) { return false; } $this->timings->startTiming(); if (!$this->chunk instanceof FullChunk) { return false; } if ($this->canUpdate()) { if ($this->getDelay() <= 0) { $success = 0; for ($i = 0; $i < $this->getSpawnCount(); $i++) { $pos = $this->add(mt_rand() / mt_getrandmax() * $this->getSpawnRange(), mt_rand(-1, 1), mt_rand() / mt_getrandmax() * $this->getSpawnRange()); $target = $this->getLevel()->getBlock($pos); $ground = $target->getSide(Vector3::SIDE_DOWN); if ($target->getId() == Item::AIR && $ground->isTopFacingSurfaceSolid()) { $success++; $this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityGenerateEvent($pos, $this->getEntityId(), EntityGenerateEvent::CAUSE_MOB_SPAWNER)); if (!$ev->isCancelled()) { $nbt = new CompoundTag("", ["Pos" => new ListTag("Pos", [new DoubleTag("", $pos->x), new DoubleTag("", $pos->y), new DoubleTag("", $pos->z)]), "Motion" => new ListTag("Motion", [new DoubleTag("", 0), new DoubleTag("", 0), new DoubleTag("", 0)]), "Rotation" => new ListTag("Rotation", [new FloatTag("", mt_rand() / mt_getrandmax() * 360), new FloatTag("", 0)])]); $entity = Entity::createEntity($this->getEntityId(), $this->chunk, $nbt); $entity->spawnToAll(); } } } if ($success > 0) { $this->setDelay(mt_rand($this->getMinSpawnDelay(), $this->getMaxSpawnDelay())); } } else { $this->setDelay($this->getDelay() - 1); } } $this->timings->stopTiming(); return true; }