From 107afb434b689879cde9c46cd959d4ec1c13b13b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 13 Nov 2022 14:52:51 +0300 Subject: [PATCH] baby animals will grow now, small changes to despawn command --- src/API/EntityAPI.php | 10 ++--- src/API/PlayerAPI.php | 4 +- src/entity/Ageable.php | 9 ++++ src/entity/Animal.php | 60 +++++++++++++++++++++----- src/entity/animal/Chicken.php | 1 + src/material/item/generic/SpawnEgg.php | 40 ++++++----------- 6 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index c797b25c..e942a6d2 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -55,7 +55,7 @@ class EntityAPI{ } $mobName = ucfirst(array_flip($mob)[$type]); - if(strtolower($args[1] === "baby") or strtolower($args[2] === "baby") and $type > 13){ + if((strtolower($args[1] === "baby") or strtolower($args[2] === "baby")) and !Utils::in_range($type, 10, 13)){ $output .= "$mobName cannot be a baby!"; break; } @@ -77,13 +77,13 @@ class EntityAPI{ $output .= "Cannot spawn > 25 mobs"; break; } - + $isBaby = false; if(isset($args[2]) and strtolower($args[2]) === 'baby'){//summon [amount] [baby] - $isBaby = 1; + $isBaby = true; } for($cnt = $amount; $cnt > 0; --$cnt){ - $this->summon($pos, ENTITY_MOB, $type, $isBaby === 1 ? ["IsBaby" => 1] : []); + $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => $isBaby]); } $output .= "$amount ".($isBaby === 1 ? "Baby" : "")." $mobName".(($type !== 13 || $amount > 1) ? "s" : "")." spawned in $x, $y, $z."; @@ -102,7 +102,7 @@ class EntityAPI{ break; }else{ if($args[0] === "all"){ - $l = $this->server->query("SELECT EID FROM entities WHERE class = 2 or 3 or 4 or 5;"); + $l = $this->server->query("SELECT EID FROM entities WHERE class = 2 or class = 3 or class = 4 or class = 5;"); }else{ $array = explode(",", strtolower($args[0])); if(count($array) > 4){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 8a10eea1..db299022 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -434,8 +434,8 @@ class PlayerAPI{ $clients[$e->CID] = $e; } } - } - return $clients; + } return $clients; + } return $this->server->clients; } diff --git a/src/entity/Ageable.php b/src/entity/Ageable.php index 7d90d4c8..89c3c4df 100644 --- a/src/entity/Ageable.php +++ b/src/entity/Ageable.php @@ -2,4 +2,13 @@ interface Ageable{ public function isBaby(); + /** + * Get Entity Age. It can be negative. + */ + public function getAge(); + /** + * Set Entity age. + * @param int $i + */ + public function setAge($i); } \ No newline at end of file diff --git a/src/entity/Animal.php b/src/entity/Animal.php index a761a1f4..7600a371 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -7,22 +7,60 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public $parent; public $inLove; //do NOT add it into metadata, it doesnt send it to player + public $age; + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ + parent::__construct($level, $eid, $class, $type, $data); + + $this->setAge(isset($data["Age"]) ? $data["Age"] : 0); + if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){ + $this->setAge(-24000); + } + } + public function isBaby(){ - if(!isset($this->data["IsBaby"])){ - $this->data["IsBaby"] = false; - } - return $this->data["IsBaby"]; + return $this->getAge() < 0; } - public function spawnChild(){ - //TODO - //$c->parent = $this->eid; + public function breed(){ + $c = $this->spawnChild(); + if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){ + $c->parent = &$this; + $this->server->api->entity->spawnToAll($c); + } } + + public function update(){ + parent::update(); + $age = $this->getAge() + 1; //100 - fast. debug, 1 - normal + if($age >= 0 && $this->isBaby()){ + $this->setAge($age); + $this->updateMetadata(); + }else{ + $this->setAge($age); + } + } + + public function getAge(){ + return $this->age; + } + + public function setAge($i){ + $this->age = $i; + } + + public function spawnChild() + { + return $this->server->api->entity->add($this->level, $this->class, $this->type, [ + "x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1), + "y" => $this->y, + "z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1), + "IsBaby" => true, + "Age" => -24000, + ]); + } + public function getMetadata(){ $d = parent::getMetadata(); - if(!isset($this->data["IsBaby"])){ - $this->data["IsBaby"] = 0; - } $d[14]["value"] = $this->isBaby(); return $d; } @@ -30,8 +68,8 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public function createSaveData(){ $data = parent::createSaveData(); $data["IsBaby"] = $this->isBaby(); + $data["Age"] = $this->getAge(); return $data; - } public function isInLove(){ diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 27554646..a0fead64 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -37,4 +37,5 @@ class Chicken extends Animal{ [[$this->fire > 0 ? COOKED_CHICKEN : RAW_CHICKEN], 0, 1] ]; } + } \ No newline at end of file diff --git a/src/material/item/generic/SpawnEgg.php b/src/material/item/generic/SpawnEgg.php index a577d33d..32382ad8 100644 --- a/src/material/item/generic/SpawnEgg.php +++ b/src/material/item/generic/SpawnEgg.php @@ -8,32 +8,18 @@ class SpawnEggItem extends Item{ } public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $ageable = false; - switch($this->meta){ - case MOB_CHICKEN: - case MOB_SHEEP: - case MOB_COW: - case MOB_PIG: - $ageable = true; - case MOB_ZOMBIE: - case MOB_CREEPER: - case MOB_SKELETON: - case MOB_SPIDER: - case MOB_PIGMAN: - $data = array( - "x" => $block->x + 0.5, - "y" => $block->y, - "z" => $block->z + 0.5, - "IsBaby" => $ageable ? Utils::chance(5) ? 1 : 0 : 0, - ); - $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); - ServerAPI::request()->api->entity->spawnToAll($e); - if(($player->gamemode & 0x01) === 0){ - --$this->count; - } - return true; - break; - } - return false; + $ageable = $this->meta === MOB_CHICKEN || $this->meta === MOB_COW || $this->meta === MOB_SHEEP || $this->meta === MOB_PIG; + $data = array( + "x" => $block->x + 0.5, + "y" => $block->y, + "z" => $block->z + 0.5, + "IsBaby" => $ageable ? Utils::chance(5) ? 1 : 0 : 0 + ); + $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); + ServerAPI::request()->api->entity->spawnToAll($e); + if(($player->gamemode & 0x01) === 0){ + -- $this->count; + } + return true; } } \ No newline at end of file