From 69d4e52d44dbbd7d5936a84a98595cdfa3d3f283 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 25 Sep 2022 00:49:56 +0300 Subject: [PATCH] get entity names or sizes by eids or instances --- src/PocketMinecraftServer.php | 1 - src/entity/Entity.php | 25 ++++++++++++++++++++++++- src/entity/EntityRegistry.php | 3 ++- src/entity/animal/Chicken.php | 2 +- src/entity/animal/Cow.php | 2 +- src/entity/animal/Pig.php | 2 +- src/entity/animal/Sheep.php | 2 +- src/entity/monster/Creeper.php | 2 +- src/entity/monster/PigZombie.php | 2 +- src/entity/monster/Skeleton.php | 2 +- src/entity/monster/Spider.php | 2 +- src/entity/monster/Zombie.php | 2 +- 12 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index b80953f7..8571ab67 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -32,7 +32,6 @@ class PocketMinecraftServer{ console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port); EntityRegistry::registerEntities(); - define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); $this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID; $this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b4a4b91b..6ef0c2fe 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -123,6 +123,29 @@ class Entity extends Position{ } } + + /** + * @param mixed $e Entity instance or EID + */ + public static function getNameOf($e){ + if($e instanceof Entity){ + return $e->getName(); + }elseif(($ent = ServerAPI::request()->api->entity->get($e)) != false){ + return $ent->getName(); + } + } + public static function getSizeOf($e){ + if($e instanceof Entity){ + return $e->getSize(); + }elseif(($ent = ServerAPI::request()->api->entity->get($e)) != false){ + return $ent->getSize(); + } + } + + public function getSize(){ + return $this->size; + } + public function lookOn($entity){ $horizontal = sqrt(pow(($target->entity->x - $entity->x), 2) + pow(($target->entity->z - $entity->z) , 2)); $vertical = $target->entity->y - ($entity->y + -0.5); /*0.5 = $entity->getEyeHeight()*/ @@ -683,7 +706,7 @@ class Entity extends Position{ public function setName($name){ $this->name = $name; - $this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); + //$this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); is this neccessary for database? } public function look($pos2){ diff --git a/src/entity/EntityRegistry.php b/src/entity/EntityRegistry.php index b6e26fc5..7834b896 100644 --- a/src/entity/EntityRegistry.php +++ b/src/entity/EntityRegistry.php @@ -19,6 +19,7 @@ class PropertyEntity{ return $this->name; } } + class EntityList{ public function getEntities(){ return $this->entities; @@ -26,12 +27,12 @@ class EntityList{ public function addEntity(PropertyEntity $property){ $this->entities[$property->getEntityType()] = $property; } - public function getEntityFromTypeAndClass($type, $class){ return isset($this->entities[$type]) && $this->entities[$type]->getEntityClass() === $class ? $this->entities[$type] : 0; } private $entities = []; } + class EntityRegistry{ public static $entityList; /*Register all entities*/ diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index b39deb2f..53e07462 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -8,7 +8,7 @@ class Chicken extends Animal{ $server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg")); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->update(); - //$this->setName('Chicken'); + $this->setName('Chicken'); $this->size = $this->isBaby() ? 0.35 : 0.7; } public function isFood($id){ diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index a1d2780f..390f2d88 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -6,7 +6,7 @@ class Cow extends Animal{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); $this->update(); - //$this->setName('Cow'); + $this->setName('Cow'); $this->size = $this->isBaby() ? 0.65 : 1.4; } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 10778b05..42411d0c 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -9,7 +9,7 @@ class Pig extends Animal{ $this->update(); $this->server = ServerAPI::request(); //$this->server->schedule(60, array($this, "testMovement")); - //$this->setName('Pig'); + $this->setName('Pig'); $this->size = $this->isBaby() ? 0.60 : 1.1875; } public function isFood($id){ diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 5ea8380e..f3b16362 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -8,7 +8,7 @@ class Sheep extends Animal{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->update(); - //$this->setName('Sheep'); + $this->setName('Sheep'); $this->size = $this->isBaby() ? 0.65 : 1.3; } diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 6c00c308..40c24ac6 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -6,7 +6,7 @@ class Creeper extends Monster{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:20, "generic"); $this->update(); - //$this->setName('Creeper'); + $this->setName('Creeper'); $this->size = 1.6; //not original } diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index e0fad1a1..409f81b6 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -5,7 +5,7 @@ class PigZombie extends Monster{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic"); $this->update(); - //$this->setName('Pigman'); + $this->setName('Pigman'); $this->size = 1.8; //not original } diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 75695e13..d2fee9c4 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -6,7 +6,7 @@ class Skeleton extends Monster{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic"); $this->update(); - //$this->setName('Skeleton'); + $this->setName('Skeleton'); $this->size = 1.9; } diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 28e37a6f..e631e14b 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -6,7 +6,7 @@ class Spider extends Monster{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:20, "generic"); $this->update(); - //$this->setName('Spider'); + $this->setName('Spider'); $this->size = 0.6; //not original } diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 7e70bff2..bb03900b 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -6,7 +6,7 @@ class Zombie extends Monster{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic"); $this->update(); - //$this->setName('Zombie'); + $this->setName('Zombie'); $this->size = 1.95; }