From fb13a6123bb69a38413a6b040b5c1a967cd8eefb Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 8 Jun 2024 22:57:49 +0300 Subject: [PATCH] fixed unmount (hopefully one last time) --- src/Player.php | 29 +++++++++++++------- src/astarnavigator/TileNavigator.php | 3 ++- src/entity/Creature.php | 4 +++ src/entity/Entity.php | 18 ++++++++++--- src/entity/Living.php | 21 +++++++++------ src/entity/ai/path/PathFollower.php | 6 ++--- src/entity/animal/Pig.php | 31 +++++++++++++++------- src/entity/object/Minecart.php | 21 +++++++++++++++ src/material/item/generic/MinecartItem.php | 2 +- src/world/Level.php | 1 + 10 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/Player.php b/src/Player.php index 0422054e..cda81a4e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -76,6 +76,15 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + /** + * Sent by a client while it is linked to some entity. + * @var float $moveStrafe + * @var float $moveForward + * @var boolean $isJumping + * @var boolean $isSneaking + */ + public $moveStrafe, $moveForward, $isJumping, $isSneaking; + /** * @param integer $clientID * @param string $ip @@ -727,12 +736,7 @@ class Player{ $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; $pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"]; $pk->type = 0; - if($data["type"] == $this->entity->eid){ - console("dont send to {$this}"); - break; - } $this->dataPacket($pk); - console("nya"); break; case "tile.update": if($data->level === $this->level){ @@ -2467,8 +2471,15 @@ class Player{ } break; case ProtocolInfo::PLAYER_INPUT_PACKET: + + $this->moveStrafe = $packet->moveStrafe; + $this->moveForward = $packet->moveForward; + $this->isJumping = $packet->isJumping; + $this->isSneaking = $packet->isSneaking; + if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ $this->entity->stopRiding(); + break; } if($this->entity->linkedEntity != 0){ //TODO better riding $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; @@ -2477,7 +2488,7 @@ class Player{ $this->entity->stopRiding(); break; } - $pk = new SetEntityMotionPacket; + /*$pk = new SetEntityMotionPacket; $pk->eid = $e->eid; $pk->speedX = ($this->entity->x - $e->x)*1.2; $pk->speedY = ($this->entity->y - $e->y)*1.2; @@ -2487,10 +2498,8 @@ class Player{ $p->dataPacket(clone $pk); } } - - $e->setPosition($this->entity); - $e->sendMoveUpdate(); - //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1); + console("{$e} {$this->entity}"); + //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1);*/ } break; diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 98ce61e3..c29c4fec 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -19,6 +19,7 @@ class TileNavigator } public function reconstructPath($path, $current){ + console("path built"); $totalPath = [$current]; while (isset($path[(string)$current])) { @@ -172,7 +173,7 @@ class TileNavigator $path[$neighbor] = $current; if($targetDist < $closestToTargetDist){ - console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)"); + //console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)"); $closestToTarget = $neighbor; $closestToTargetDist = $targetDist; diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 159ff7f6..8ee78114 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -73,6 +73,10 @@ abstract class Creature extends Living{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->dataPacket($pk); + + if($this->linkedEntity != 0 && $this->isRider){ + $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); + } } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 810422a0..a9ebe505 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -69,7 +69,8 @@ class Entity extends Position public $level; public $isRiding = false; public $lastUpdate; - public $linkedEntity = null; + public $linkedEntity = 0; + public $isRider = false; public $check = true; public $width = 1; public $height = 1; @@ -1104,6 +1105,12 @@ class Entity extends Position $pk->did = -$this->data["Tile"]; $player->dataPacket($pk); } + + if($this->linkedEntity != 0 && $this->isRider){ + console("send $player"); + $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it + } + } public function counterUpdate(){ @@ -1276,7 +1283,7 @@ class Entity extends Position ], "speedX" => $this->speedX, "speedY" => $this->speedY, - "speedZ" => $this->speedZ + "speedZ" => $this->speedZ, ]; if($this->class === ENTITY_OBJECT){ @@ -1392,9 +1399,11 @@ class Entity extends Position if(isset($this->level->entityList[$this->linkedEntity])){ $e = $this->level->entityList[$this->linkedEntity]; if(!$e->dead && !$e->closed){ - $e->setRiding($e, $this->eid); //why mojang $this->linkedEntity = 0; $e->linkedEntity = 0; + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => -1, "type" => 1]); + $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => -1, "type" => 1]); + $this->isRider = false; } }else{ $this->linkedEntity = 0; @@ -1407,7 +1416,7 @@ class Entity extends Position ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); return; } - console("unlink $this $e"); + if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ $this->linkedEntity = 0; $e->linkedEntity = 0; @@ -1415,6 +1424,7 @@ class Entity extends Position }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]); } diff --git a/src/entity/Living.php b/src/entity/Living.php index 48ac144a..dfc82c4e 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -134,15 +134,21 @@ abstract class Living extends Entity implements Pathfindable{ //$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level); $pl = null; if(count($this->level->players) > 0){ - $pl = array_values($this->level->players)[0]; + //$pl = array_values($this->level->players)[0]; //$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level); } if(self::$pathfind){ - $this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10); + $this->path = $this->pathFinder->navigate( + $this->level, + (int)$this->x, (int)$this->y, (int)$this->z, + //(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, + (int)$this->x + mt_rand(-16, 16), (int)$this->y, (int)$this->z + mt_rand(-16, 16), + 16 + ); } - if($this->path){ + /*if($this->path){ console("Found path of length ".count($this->path)); foreach($this->path as $node){ @@ -161,7 +167,7 @@ abstract class Living extends Entity implements Pathfindable{ $player->dataPacket($pk); } } - } + }*/ } $this->pathFollower->followPath(); } @@ -182,14 +188,13 @@ abstract class Living extends Entity implements Pathfindable{ } - $this->ai->mobController->movementTick(); - $this->ai->mobController->rotateTick(); - $this->ai->mobController->jumpTick(); + //$this->ai->mobController->movementTick(); + //$this->ai->mobController->rotateTick(); + //$this->ai->mobController->jumpTick(); if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; - $this->moveStrafing *= 0.98; $this->moveForward *= 0.98; $savedLandFactor = $this->landMovementFactor; diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php index a48a2293..c17cde93 100644 --- a/src/entity/ai/path/PathFollower.php +++ b/src/entity/ai/path/PathFollower.php @@ -24,7 +24,7 @@ class PathFollower{ $this->entity->ai->mobController->setMovingTarget($moveX, $moveY, $moveZ, 1.0); if($this->entity->boundingBox->isXYZInsideNS($moveX, $moveY, $moveZ)){ ++$this->entity->currentIndex; - console("next"); + //console("next"); $this->entity->currentNode = null; } @@ -35,13 +35,13 @@ class PathFollower{ $this->entity->path = null; $this->entity->currentIndex = 0; - foreach($this->entity->pathEIDS as $eid){ + /*foreach($this->entity->pathEIDS as $eid){ $pk = new RemoveEntityPacket(); $pk->eid = $eid; foreach($this->entity->level->players as $player){ $player->dataPacket($pk); } - } + }*/ } } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 926ef3dd..d754697c 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -10,15 +10,14 @@ class Pig extends Animal implements Rideable{ $this->setName("Pig"); $this->setSpeed(0.25); - $this->ai->addTask(new TaskRandomWalk(1.0)); - $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(1.5)); - $this->ai->addTask(new TaskLookAround()); + //$this->ai->addTask(new TaskRandomWalk(1.0)); + //$this->ai->addTask(new TaskLookAtPlayer(6)); + //$this->ai->addTask(new TaskPanic(1.5)); + //$this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); - $this->ai->addTask(new TaskTempt(1.0)); - $this->ai->addTask(new TaskMate(1.0)); - $this->ai->addTask(new TaskFollowParent(1.1)); - + //$this->ai->addTask(new TaskTempt(1.0)); + //$this->ai->addTask(new TaskMate(1.0)); + //$this->ai->addTask(new TaskFollowParent(1.1)); } /** * @return boolean @@ -37,7 +36,21 @@ class Pig extends Animal implements Rideable{ public function canRide($e){ return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; } - + + public function updateEntityMovement(){ + if($this->linkedEntity != 0){ + $e = $this->level->entityList[$this->linkedEntity] ?? false; + if($e instanceof Entity){ + $this->setAIMoveSpeed($this->getSpeed()); + $this->moveStrafing = $e->player->moveStrafe; + $this->moveForward = $e->player->moveForward; + $this->yaw = $e->headYaw; + } + } + + parent::updateEntityMovement(); + } + public function interactWith(Entity $e, $action) { if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 4371f48c..8ec0d713 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -78,6 +78,27 @@ class Minecart extends Vehicle{ ]; } + public function update($now){ + if($this->closed === true){ + return false; + } + + + } + + 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 } diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index a1924adb..41f88f38 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -13,7 +13,7 @@ class MinecartItem extends Item{ $server = ServerAPI::request(); $data = [ "x" => $target->getX() + 0.5, - "y" => $target->getY() + 0.8, + "y" => $target->getY() + 1, "z" => $target->getZ() + 0.5, ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); diff --git a/src/world/Level.php b/src/world/Level.php index 78b1b90b..56ffdee2 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -295,6 +295,7 @@ class Level{ if($extra !== false){ $entities = []; + foreach($this->entityList as $entity){ if($entity instanceof Entity){ $entities[] = $entity->createSaveData();