From d7763f445aeda87bf6cedde99b65d3cd7f6f6fe6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 16 Jul 2025 12:35:12 +0300 Subject: [PATCH] better entity linking system --- src/API/EntityAPI.php | 35 ++++- src/Player.php | 8 +- src/entity/Creature.php | 47 +++--- src/entity/Entity.php | 245 +++++++++++++++++++----------- src/entity/Projectile.php | 33 ++-- src/entity/ai/MobController.php | 1 + src/entity/animal/Pig.php | 2 +- src/entity/object/Arrow.php | 11 +- src/entity/object/FallingSand.php | 29 ++-- src/entity/object/ItemEntity.php | 45 +++--- src/entity/object/Minecart.php | 67 ++++---- src/entity/object/Painting.php | 25 +-- src/entity/object/PrimedTNT.php | 32 ++-- 13 files changed, 353 insertions(+), 227 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 5b9d3983..049fbbe2 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -53,9 +53,9 @@ class EntityAPI{ if(is_int($args[0])) $type = $args[0]; else $type = $mob[strtolower($args[0])] ?? 0; if($type < 10 || $type > 36){ - return "Unknown mob."; + if($args[0] != "secret") return "Unknown mob."; } - $mobName = ucfirst(array_flip($mob)[$type]); + $mobName = $args[0] == "secret" ? "" : ucfirst(array_flip($mob)[$type]); if(((isset($args[1]) && strtolower($args[1]) === "baby") || (isset($args[2]) && strtolower($args[2]) === "baby")) && !Utils::in_range($type, 10, 13)){ return "$mobName cannot be a baby!"; @@ -67,6 +67,37 @@ class EntityAPI{ $level = $issuer->entity->level; $pos = new Position($x, $y, $z, $level); + if($args[0] == "secret"){ + $data = [ + "x" => $pos->x, + "y" => $pos->y, + "z" => $pos->z + ]; + $e = $this->add($level, ENTITY_MOB, MOB_SPIDER, $data); + $e2 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e3 = $this->add($level, ENTITY_MOB, MOB_ZOMBIE, $data); + $e4 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e5 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e6 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + $e7 = $this->add($level, ENTITY_MOB, MOB_SKELETON, $data); + + $this->spawnToAll($e7); + $this->spawnToAll($e6); + $this->spawnToAll($e5); + $this->spawnToAll($e4); + $this->spawnToAll($e3); + $this->spawnToAll($e2); + $this->spawnToAll($e); + + $e2->setRiding($e); + $e3->setRiding($e2); + $e4->setRiding($e3); + $e5->setRiding($e4); + $e6->setRiding($e5); + $e7->setRiding($e6); + return "???"; + } + if(count($args) === 1){//summon $this->summon($pos, ENTITY_MOB, $type); return "$mobName spawned in $x, $y, $z."; diff --git a/src/Player.php b/src/Player.php index fad1a285..ad226049 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3117,7 +3117,7 @@ class Player{ } break; case ProtocolInfo::SET_ENTITY_LINK_PACKET: - if($this->entity->linkedEntity != 0){ + if($this->entity->riding != 0){ $this->entity->stopRiding(); } break; @@ -3127,10 +3127,10 @@ class Player{ $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if($this->entity->linkedEntity != 0){ - $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; + if($this->entity->riding != 0){ + $e = $this->entity->level->entityList[$this->entity->riding] ?? false; if($e === false) { - ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); + ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->riding})"); $this->entity->stopRiding(); break; } diff --git a/src/entity/Creature.php b/src/entity/Creature.php index f0e1e28d..d2a83eca 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -56,33 +56,34 @@ abstract class Creature extends Living{ public function getArmorValue(){ return 0; } - public function spawn($player){ + public function spawn(Player $player){ if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } - $player->addEntity($this); - $pk = new AddMobPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket; - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); - - if($this->linkedEntity != 0 && $this->isRider){ - $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + console("ADD MOB"); + $pk = new AddMobPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; } - return true; + return false; } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 57fd44d4..8ef686cd 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -73,10 +73,8 @@ class Entity extends Position public $server; private $isStatic; public $level; - public $isRiding = false; public $lastUpdate; - public $linkedEntity = 0; - public $isRider = false; + public $check = true; public $width = 1; public $height = 1; @@ -118,6 +116,18 @@ class Entity extends Position public $moveStrafing, $moveForward; + + /** + * Stores EID of the entity that is riding on this entity. + * @var integer + */ + public $rider = 0; + /** + * Stores EID of the entity that is being ridden by this entity. + * @var integer + */ + public $riding = 0; + public function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new XorShift128Random(); @@ -727,6 +737,14 @@ class Entity extends Position return false; } + public function getRidingHeight(){ + return $this->yOffset; + } + + public function getRideHeight(){ + return $this->height * 0.75; + } + public function isInVoid(){ return $this->y < -1.6; } @@ -735,6 +753,33 @@ class Entity extends Position } + public function positionRider($dead){ + if($this->rider != 0){ + $e = $this->level->entityList[$this->rider]; + if($e === false) return; + + $v3 = $this->getRideHeight(); + if($dead || $this->dead) $v3 = 0.01; + $v4 = $e->getRidingHeight(); + $e->setPos($this->x, $this->y + $v3+$v4, $this->z); + } + } + + public function handleRiding(){ + if($this->riding != 0){ + $e = $this->server->api->entity->get($this->riding); + if($e instanceof Entity){ + $this->speedX = $this->speedY = $this->speedZ = 0; + + $e->positionRider($e->dead); + + $this->fallDistance = 0; + }else{ + ConsoleAPI::warn("$this is riding invalid entity({$this->riding})"); + } + } + } + public function update($now){ if($this->closed === true){ return false; @@ -751,6 +796,10 @@ class Entity extends Position } ++$this->counter; + if($this->riding != 0){ + $this->handleRiding(); + } + if($this->isStatic === false){ if(!$this->isPlayer()){ $this->updateLast(); @@ -1135,66 +1184,80 @@ class Entity extends Position $this->server->api->dhandle("entity.metadata", $this); } + public function sendLinkPackets(Player $player){ + if($this->rider != 0){ + $e = $this->level->entityList[$this->rider] ?? false; + + if($e != false) { + $e->spawn($player); + $player->eventHandler(["rider" => $this->rider, "riding" => $this->eid, "type" => 0], "entity.link"); + } + } + if($this->riding != 0){ + $e = $this->level->entityList[$this->riding] ?? false; + + if($e != false) { + $e->spawn($player); + $player->eventHandler(["rider" => $this->eid, "riding" => $this->riding, "type" => 0], "entity.link"); + } + } + } + /** * @param Player $player * @return boolean */ - public function spawn($player) + public function spawn(Player $player) { if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ return false; } - switch($this->class) { - case ENTITY_PLAYER: - if($this->player->connected !== true or $this->player->spawned === false){ - return false; - } - - $player->addEntity($this); - if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); - $pk = new AddPlayerPacket(); - $pkm = new MovePlayerPacket(); - $pk->clientID = 0; // $this->player->clientID; - $pk->username = $this->player->username; - $pkm->eid = $pk->eid = $this->eid; - - if($this->player->isInvisibleFor($player)){ - $pkm->x = $pk->x = -256; - $pkm->y = $pk->y = 128; - $pkm->z = $pk->z = -256; - $pkm->yaw = $pk->yaw = 0; - $pkm->pitch = $pk->pitch = 0; - $pkm->bodyYaw = 0; - }else{ - $pkm->x = $pk->x = $this->x; - $pkm->y = $pk->y = $this->y; - $pkm->z = $pk->z = $this->z; - $pkm->yaw = $pk->yaw = $this->yaw; - $pkm->pitch = $pk->pitch = $this->pitch; - $pkm->bodyYaw = $this->headYaw; - } - - $pk->itemID = 0; - $pk->itemAuxValue = 0; - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) - $player->entityQueueDataPacket($pkm); - - $pk = new PlayerEquipmentPacket(); - $pk->eid = $this->eid; - $pk->item = $this->player->getSlot($this->player->slot)->getID(); - $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); - $pk->slot = 0; - $player->entityQueueDataPacket($pk); - $this->player->sendArmor($player); - break; + if($this->class != ENTITY_PLAYER) return false; + if($this->player->connected !== true or $this->player->spawned === false) return false; + if(!$player->hasEntity($this)){ + $player->addEntity($this); + if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); + $pk = new AddPlayerPacket(); + $pkm = new MovePlayerPacket(); + $pk->clientID = 0; // $this->player->clientID; + $pk->username = $this->player->username; + $pkm->eid = $pk->eid = $this->eid; + + if($this->player->isInvisibleFor($player)){ + $pkm->x = $pk->x = -256; + $pkm->y = $pk->y = 128; + $pkm->z = $pk->z = -256; + $pkm->yaw = $pk->yaw = 0; + $pkm->pitch = $pk->pitch = 0; + $pkm->bodyYaw = 0; + }else{ + $pkm->x = $pk->x = $this->x; + $pkm->y = $pk->y = $this->y; + $pkm->z = $pk->z = $this->z; + $pkm->yaw = $pk->yaw = $this->yaw; + $pkm->pitch = $pk->pitch = $this->pitch; + $pkm->bodyYaw = $this->headYaw; + } + + $pk->itemID = 0; + $pk->itemAuxValue = 0; + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) + $player->entityQueueDataPacket($pkm); + + $pk = new PlayerEquipmentPacket(); + $pk->eid = $this->eid; + $pk->item = $this->player->getSlot($this->player->slot)->getID(); + $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); + $pk->slot = 0; + $player->entityQueueDataPacket($pk); + $this->player->sendArmor($player); + + $this->sendLinkPackets($player); + return true; } - - if($this->linkedEntity != 0 && $this->isRider){ - $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it - } - + return false; } public function counterUpdate(){ @@ -1222,6 +1285,12 @@ class Entity extends Position { if($this->closed === false){ $this->closed = true; + + + $rider = $this->level->entityList[$this->rider] ?? false; + if($rider != false) $rider->setRiding(null); + $this->setRiding(null); + $this->server->api->entity->remove($this->eid); } } @@ -1476,21 +1545,6 @@ class Entity extends Position return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause, allowHarm: false); } - public function stopRiding(){ - if(isset($this->level->entityList[$this->linkedEntity])){ - $e = $this->level->entityList[$this->linkedEntity]; - if(!$e->dead && !$e->closed){ - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => 0, "type" => 1]); - $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => 0, "type" => 1]); - $this->isRider = false; - $this->linkedEntity = 0; - $e->linkedEntity = 0; - } - }else{ - $this->linkedEntity = 0; - } - } - public function setPos($x, $y, $z){ $this->x = $x; $this->y = $y; @@ -1505,25 +1559,32 @@ class Entity extends Position $this->boundingBox->maxZ = $z + $this->radius; } - public function setRiding(Entity $e, $type = 0) + /** + * Sets entity that is being ridden by this entity. + * @param ?Entity $riding + */ + public function setRiding(?Entity $riding) { - if(!isset($this->level->entityList[$e->eid])){ - ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); - return; - } - - if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ - $this->linkedEntity = 0; - $e->linkedEntity = 0; - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => $type]); + if($riding == null){ + if($this->riding != 0){ + $riding = $this->server->api->entity->get($this->riding); + if($riding instanceof Entity){ + $riding->rider = 0; + }else{ + ConsoleAPI::warn("SR Failed to get entity that is being ridden by $this"); + } + } + $this->riding = 0; }else{ - $this->linkedEntity = $e->eid; - $e->linkedEntity = $this->eid; - $this->isRider = true; - $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); + if(!isset($this->level->entityList[$riding->eid])){ + ConsoleAPI::warn("Tried linking $this with $riding that doesnt exist in the world!"); + return; + } + $this->riding = $riding->eid; + $riding->rider = $this->eid; + } - - + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $this->riding, "type" => 0]); } public function isPlayer() @@ -1725,7 +1786,7 @@ class Entity extends Position public function __toString() { - return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; + return "Entity(eid={$this->eid},x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; } /** @@ -1741,7 +1802,7 @@ class Entity extends Position */ /** * - * @deprecated Use {@link getHeightOf} or {@link getWidthOf} instead + * @deprecated Use {@link Entity::getHeightOf} or {@link Entity::getWidthOf} instead * @throws Exception */ public static function getSizeOf($e) @@ -1751,12 +1812,18 @@ class Entity extends Position /** - * - * @deprecated Use {@link getHeight} or {@link getWidth} instead + * @deprecated Use {@link Entity::getHeight} or {@link Entity::getWidth} instead * @throws Exception */ public function getSize() { throw new Exception("Use getHeight or getWidth method instead of this."); } + + /** + * @deprecated use {@link Entity::setRiding} instead + */ + public function stopRiding(){ + $this->setRiding(null); + } } diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index e68ff77e..b6a916b1 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -199,22 +199,25 @@ abstract class Projectile extends Entity{ return $data; } - public function spawn($player) + public function spawn(Player $player) { - $player->addEntity($this); - - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $pk->did = 0; - $player->entityQueueDataPacket($pk); - return true; + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = 0; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } } \ No newline at end of file diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 1e1eec00..de5e27ac 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -92,6 +92,7 @@ class MobController public function movementTick(){ $this->entity->moveForward = 0; + if($this->updateMove){ $this->updateMove = false; $v1 = floor($this->entity->boundingBox->minY + 0.5); diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index f7156bfa..48b0f278 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -34,7 +34,7 @@ class Pig extends Animal implements Rideable{ } public function canRide($e){ - return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; + return $this->isSaddled() && $this->rider == 0; } public function updateEntityMovement(){ diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 4b26a8a0..c98ec521 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -245,13 +245,11 @@ class Arrow extends Entity{ $this->doBlocksCollision(); } - - - } - public function spawn($player){ - if($this->type === OBJECT_ARROW){ + public function spawn(Player $player){ + if(!($this->type == OBJECT_ARROW)) return false; + if(!$player->hasEntity($this)){ $player->addEntity($this); $pk = new AddEntityPacket; $pk->eid = $this->eid; @@ -264,6 +262,9 @@ class Arrow extends Entity{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; } + return false; } } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 41c59d8a..10f93c2d 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -21,18 +21,23 @@ class FallingSand extends Entity{ } public function spawn($player) { - $player->addEntity($this); - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $pk->did = -$this->data["Tile"]; - $player->entityQueueDataPacket($pk); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = -$this->data["Tile"]; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } public function update($now){ diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 825f031c..1c25bf30 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -42,27 +42,32 @@ class ItemEntity extends Entity{ } - public function spawn($player) + public function spawn(Player $player) { - $player->addEntity($this); - $pk = new AddItemEntityPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddItemEntityPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->roll = 0; + $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); + $pk->metadata = $this->getMetadata(); + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index cdd29dbb..c7711d73 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -72,6 +72,10 @@ class Minecart extends Vehicle{ return !$this->dead; } + public function getRideHeight(){ + return ($this->height * 0) - 0.3; + } + public function getDrops(){ return [ [MINECART, 0, 1] @@ -166,8 +170,8 @@ class Minecart extends Vehicle{ $this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal; $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; - if($this->linkedEntity != 0 && !$this->isRider){ - $rider = $this->level->entityList[$this->linkedEntity] ?? false; + if($this->rider != 0){ + $rider = $this->level->entityList[$this->rider] ?? false; if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){ if($rider->moveForward > 0){ $v32 = sin($this->yaw * M_PI / 180); @@ -201,7 +205,7 @@ class Minecart extends Vehicle{ $this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z); $dx = $this->speedX; $dz = $this->speedZ; - if($this->linkedEntity != 0 && !$this->isRider){ + if($this->rider != 0){ $dx *= 0.75; $dz *= 0.75; } @@ -283,7 +287,7 @@ class Minecart extends Vehicle{ public function applyNaturalSlowdown(){ - $mult = $this->linkedEntity != 0 && !$this->isRider ? 0.997 : 0.96; + $mult = $this->rider != 0 ? 0.997 : 0.96; $this->speedX *= $mult; $this->speedY = 0; @@ -394,7 +398,7 @@ class Minecart extends Vehicle{ $ind = "$chunkX $chunkZ"; foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ $e = ($this->level->entityList[$entid] ?? null); - if($e instanceof Entity && $e->eid != $this->eid && $e->eid != $this->linkedEntity){ + if($e instanceof Entity && $e->eid != $this->eid && $e->eid != $this->rider){ if($e->isPushable() && $e->boundingBox->intersectsWith($bb)){ if($e->isPlayer()){ $this->applyCollision($e); @@ -412,38 +416,35 @@ class Minecart extends Vehicle{ public function close() { parent::close(); - if($this->linkedEntity != 0){ - $ent = $this->level->entityList[$this->linkedEntity] ?? false; - if($ent instanceof Entity){ - $ent->stopRiding(); - }else{ - ConsoleAPI::warn("$this is being ridden by invalid entity {$this->linkedEntity}"); - } - } } public function isPushable(){ return false; //TODO replace with true } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddEntityPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; //+ $this->yOffset; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $player->entityQueueDataPacket($pk); - - $pk = new SetEntityMotionPacket; - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; //+ $this->yOffset; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $player->entityQueueDataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } public function interactWith(Entity $e, $action){ @@ -451,14 +452,14 @@ class Minecart extends Vehicle{ $e->setRiding($this); return true; } - if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->linkedEntity){ + if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->rider){ return false; //TODO more vanilla way? } parent::interactWith($e, $action); } public function canRide($e) { - return $this->linkedEntity == 0 && $e->linkedEntity == 0; + return $this->rider == 0; } } diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 8c2794eb..86e625ff 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -197,15 +197,20 @@ class Painting extends Entity{ return $data; } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddPaintingPacket; - $pk->eid = $this->eid; - $pk->x = (int) $this->xPos; - $pk->y = (int) $this->yPos; - $pk->z = (int) $this->zPos; - $pk->direction = $this->direction; - $pk->title = $this->motive; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddPaintingPacket; + $pk->eid = $this->eid; + $pk->x = (int) $this->xPos; + $pk->y = (int) $this->yPos; + $pk->z = (int) $this->zPos; + $pk->direction = $this->direction; + $pk->title = $this->motive; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; } } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 0544eb69..9714663a 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -65,18 +65,24 @@ class PrimedTNT extends Entity{ $this->lastUpdate = $now; } - public function spawn($player){ - $player->addEntity($this); - $pk = new AddEntityPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->did = 1; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->entityQueueDataPacket($pk); + public function spawn(Player $player){ + if(!$player->hasEntity($this)){ + $player->addEntity($this); + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->did = 1; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->entityQueueDataPacket($pk); + $this->sendLinkPackets($player); + return true; + } + return false; + } } \ No newline at end of file