diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 973e99c6..85fc5f4d 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -17,6 +17,10 @@ class EntityAPI{ $this->server->api->console->register("summon", "", [$this, "commandHandler"]); $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); + + if(($this->serverSpawnAnimals or $this->serverSpawnMobs) and Entity::$updateOnTick){ + (new MobSpawner())->init(); + } } diff --git a/src/dependencies.php b/src/dependencies.php index e7e44966..eefbe7a8 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -93,6 +93,8 @@ require_once(FILE_PATH . "/src/entity/Creature.php"); require_once(FILE_PATH . "/src/entity/Ageable.php"); require_once(FILE_PATH . "/src/entity/Animal.php"); +require_once(FILE_PATH . "/src/entity/MobSpawner.php"); + require_once(FILE_PATH . "/src/material/Item.php"); require_once(FILE_PATH . "/src/material/item/base/ItemTool.php"); require_once(FILE_PATH . "/src/material/item/base/ItemHoe.php"); diff --git a/src/entity/MobSpawner.php b/src/entity/MobSpawner.php new file mode 100644 index 00000000..61cd69e6 --- /dev/null +++ b/src/entity/MobSpawner.php @@ -0,0 +1,118 @@ +server = ServerAPI::request(); + $this->server->api->schedule(40, [$this, "checkForSpawn"], [], true); + } + + public function checkForSpawn(){ + if(count($this->server->api->player->online()) <= 0) return; + if($this->countMobs() >= 25) return; + $this->spawnMobs(); + } + + public function countMobs(){ + $l = $this->server->query("SELECT EID FROM entities WHERE class = ".ENTITY_MOB.";"); + $cnt = 0; + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $cnt++; + } + } + return $cnt; + } + + public function spawnMobs(){ + $o = $this->server->api->player->online(); + $randPlayer = mt_rand(0, (count($o) - 1)); + $this->world = $this->server->api->player->get($o[$randPlayer])->level; + $this->gameTime = $this->world->getTime(); + + if($this->gameTime <= 9500 and $this->server->api->getProperty("spawn-animals")){ + $this->spawnAnimal(); + } + elseif($this->gameTime >= 10000 and $this->server->api->getProperty("spawn-mobs")){ + $this->spawnMonster(); + } + } + + public function spawnAnimal(){ + $type = mt_rand(10, 13); + $x = mt_rand(5, 250); //need create a radius near player + $z = mt_rand(5, 250); + $y = $this->getY($x, $z); + if(!$y) return; + + $this->addMob(new Position($x+.5, $y, $z+.5, $this->world), ENTITY_MOB, $type); + } + + public function spawnMonster(){ + $type = mt_rand(32, 35); + $x = mt_rand(5, 250); //need create a radius near player + $z = mt_rand(5, 250); + $y = $this->getY($x, $z); + if(!$y) return; + + $this->addMob(new Position($x+.5, $y, $z+.5, $this->world), ENTITY_MOB, $type); + } + + public function getY($x, $z){ + if($this->gameTime <= 9500){ + for($y = 127; $y > 0; --$y){ //get highest block + $block = $this->world->getBlock(new Vector3($x, $y, $z))->getID(); + if($block === GRASS){ + return ++$y; + } + if($block !== AIR){ + if($block == 18 or $block == 78 or $block == 31){ //Ignore Leaves, Snow Layer, Tall Grass + continue; + } + return false; + } + } + return false; + } + else{ //need recode that + for($y = 1; $y < 127; ++$y){ //get lowest block + $block = $this->world->getBlock(new Vector3($x, $y, $z)); + if($block->getID() === AIR){ + $downBlock = $this->world->getBlock(new Vector3($x, $y-1, $z)); + if($y === 1){ + return false; + } + if($downBlock instanceof LiquidBlock or $downBlock instanceof TransparentBlock){ + return false; + } + if(!($this->world->getBlock(new Vector3($x, $y+1, $z)) instanceof TransparentBlock)){ + return false; + } + return $y; + } + } + } + } + + public function addMob(Position $pos, $class, $type){ + $this->server->api->entity->summon($pos, $class, $type); + } + + /*public function checkForDespawn(){ + $players = $this->server->api->player->getAll(); + $randEid = null; + $entity = $this->server->api->entity->get($randEid); + + if(!$this->removeWhenFarAway($entity)) return; + + foreach($players as $p){ + if(Utils::distance($p, $entity) >= 48){ + $this->server->api->entity->remove($eid); + } + } + } + + public function removeWhenFarAway($entity){ + if($entity instanceof Monster) return true; + else return false; + }*/ + +} \ No newline at end of file diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index b5bcf900..4272fd40 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -7,6 +7,7 @@ class Sheep extends Animal{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setName("Sheep"); + $this->data["Sheared"] = isset($this->data["Sheared"]) ? $this->data["Sheared"] : 0; $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3); $this->setSpeed(0.25);