better entity linking system

This commit is contained in:
GameHerobrine 2025-07-16 12:35:12 +03:00
parent c95a8d5cff
commit d7763f445a
13 changed files with 353 additions and 227 deletions

View file

@ -53,9 +53,9 @@ class EntityAPI{
if(is_int($args[0])) $type = $args[0]; if(is_int($args[0])) $type = $args[0];
else $type = $mob[strtolower($args[0])] ?? 0; else $type = $mob[strtolower($args[0])] ?? 0;
if($type < 10 || $type > 36){ 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)){ 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!"; return "$mobName cannot be a baby!";
@ -67,6 +67,37 @@ class EntityAPI{
$level = $issuer->entity->level; $level = $issuer->entity->level;
$pos = new Position($x, $y, $z, $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 <mob> if(count($args) === 1){//summon <mob>
$this->summon($pos, ENTITY_MOB, $type); $this->summon($pos, ENTITY_MOB, $type);
return "$mobName spawned in $x, $y, $z."; return "$mobName spawned in $x, $y, $z.";

View file

@ -3117,7 +3117,7 @@ class Player{
} }
break; break;
case ProtocolInfo::SET_ENTITY_LINK_PACKET: case ProtocolInfo::SET_ENTITY_LINK_PACKET:
if($this->entity->linkedEntity != 0){ if($this->entity->riding != 0){
$this->entity->stopRiding(); $this->entity->stopRiding();
} }
break; break;
@ -3127,10 +3127,10 @@ class Player{
$this->entity->moveForward = $packet->moveForward; $this->entity->moveForward = $packet->moveForward;
$this->entity->moveStrafing = $packet->moveStrafe; $this->entity->moveStrafing = $packet->moveStrafe;
if($this->entity->linkedEntity != 0){ if($this->entity->riding != 0){
$e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; $e = $this->entity->level->entityList[$this->entity->riding] ?? false;
if($e === 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(); $this->entity->stopRiding();
break; break;
} }

View file

@ -56,33 +56,34 @@ abstract class Creature extends Living{
public function getArmorValue(){ public function getArmorValue(){
return 0; 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)){ if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){
return false; return false;
} }
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddMobPacket; $player->addEntity($this);
$pk->eid = $this->eid; console("ADD MOB");
$pk->type = $this->type; $pk = new AddMobPacket;
$pk->x = $this->x; $pk->eid = $this->eid;
$pk->y = $this->y; $pk->type = $this->type;
$pk->z = $this->z; $pk->x = $this->x;
$pk->yaw = $this->yaw; $pk->y = $this->y;
$pk->pitch = $this->pitch; $pk->z = $this->z;
$pk->metadata = $this->getMetadata(); $pk->yaw = $this->yaw;
$player->entityQueueDataPacket($pk); $pk->pitch = $this->pitch;
$pk->metadata = $this->getMetadata();
$pk = new SetEntityMotionPacket; $player->entityQueueDataPacket($pk);
$pk->eid = $this->eid;
$pk->speedX = $this->speedX; $pk = new SetEntityMotionPacket;
$pk->speedY = $this->speedY; $pk->eid = $this->eid;
$pk->speedZ = $this->speedZ; $pk->speedX = $this->speedX;
$player->entityQueueDataPacket($pk); $pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
if($this->linkedEntity != 0 && $this->isRider){ $player->entityQueueDataPacket($pk);
$player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); $this->sendLinkPackets($player);
return true;
} }
return true; return false;
} }
} }

View file

@ -73,10 +73,8 @@ class Entity extends Position
public $server; public $server;
private $isStatic; private $isStatic;
public $level; public $level;
public $isRiding = false;
public $lastUpdate; public $lastUpdate;
public $linkedEntity = 0;
public $isRider = false;
public $check = true; public $check = true;
public $width = 1; public $width = 1;
public $height = 1; public $height = 1;
@ -118,6 +116,18 @@ class Entity extends Position
public $moveStrafing, $moveForward; 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 = []) public function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{ {
$this->random = new XorShift128Random(); $this->random = new XorShift128Random();
@ -727,6 +737,14 @@ class Entity extends Position
return false; return false;
} }
public function getRidingHeight(){
return $this->yOffset;
}
public function getRideHeight(){
return $this->height * 0.75;
}
public function isInVoid(){ public function isInVoid(){
return $this->y < -1.6; 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){ public function update($now){
if($this->closed === true){ if($this->closed === true){
return false; return false;
@ -751,6 +796,10 @@ class Entity extends Position
} }
++$this->counter; ++$this->counter;
if($this->riding != 0){
$this->handleRiding();
}
if($this->isStatic === false){ if($this->isStatic === false){
if(!$this->isPlayer()){ if(!$this->isPlayer()){
$this->updateLast(); $this->updateLast();
@ -1135,66 +1184,80 @@ class Entity extends Position
$this->server->api->dhandle("entity.metadata", $this); $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 * @param Player $player
* @return boolean * @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)){ if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){
return false; return false;
} }
switch($this->class) { if($this->class != ENTITY_PLAYER) return false;
case ENTITY_PLAYER: if($this->player->connected !== true or $this->player->spawned === false) return false;
if($this->player->connected !== true or $this->player->spawned === false){ if(!$player->hasEntity($this)){
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();
$player->addEntity($this); $pkm = new MovePlayerPacket();
if($this->level != $player->entity->level || $this->player->gamemode === SPECTATOR) $this->player->setInvisibleFor($player, true, send:false); $pk->clientID = 0; // $this->player->clientID;
$pk = new AddPlayerPacket(); $pk->username = $this->player->username;
$pkm = new MovePlayerPacket(); $pkm->eid = $pk->eid = $this->eid;
$pk->clientID = 0; // $this->player->clientID;
$pk->username = $this->player->username; if($this->player->isInvisibleFor($player)){
$pkm->eid = $pk->eid = $this->eid; $pkm->x = $pk->x = -256;
$pkm->y = $pk->y = 128;
if($this->player->isInvisibleFor($player)){ $pkm->z = $pk->z = -256;
$pkm->x = $pk->x = -256; $pkm->yaw = $pk->yaw = 0;
$pkm->y = $pk->y = 128; $pkm->pitch = $pk->pitch = 0;
$pkm->z = $pk->z = -256; $pkm->bodyYaw = 0;
$pkm->yaw = $pk->yaw = 0; }else{
$pkm->pitch = $pk->pitch = 0; $pkm->x = $pk->x = $this->x;
$pkm->bodyYaw = 0; $pkm->y = $pk->y = $this->y;
}else{ $pkm->z = $pk->z = $this->z;
$pkm->x = $pk->x = $this->x; $pkm->yaw = $pk->yaw = $this->yaw;
$pkm->y = $pk->y = $this->y; $pkm->pitch = $pk->pitch = $this->pitch;
$pkm->z = $pk->z = $this->z; $pkm->bodyYaw = $this->headYaw;
$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();
$pk->itemID = 0; $player->entityQueueDataPacket($pk);
$pk->itemAuxValue = 0; //AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees)
$pk->metadata = $this->getMetadata(); $player->entityQueueDataPacket($pkm);
$player->entityQueueDataPacket($pk);
//AddPlayerPacket doesnt read yaw correctly in vanilla(uses PacketUtil::Rot_degreesToChar instead of PacketUtil::Rot_charToDegrees) $pk = new PlayerEquipmentPacket();
$player->entityQueueDataPacket($pkm); $pk->eid = $this->eid;
$pk->item = $this->player->getSlot($this->player->slot)->getID();
$pk = new PlayerEquipmentPacket(); $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata();
$pk->eid = $this->eid; $pk->slot = 0;
$pk->item = $this->player->getSlot($this->player->slot)->getID(); $player->entityQueueDataPacket($pk);
$pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); $this->player->sendArmor($player);
$pk->slot = 0;
$player->entityQueueDataPacket($pk); $this->sendLinkPackets($player);
$this->player->sendArmor($player); return true;
break;
} }
return false;
if($this->linkedEntity != 0 && $this->isRider){
$player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it
}
} }
public function counterUpdate(){ public function counterUpdate(){
@ -1222,6 +1285,12 @@ class Entity extends Position
{ {
if($this->closed === false){ if($this->closed === false){
$this->closed = true; $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); $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); 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){ public function setPos($x, $y, $z){
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;
@ -1505,25 +1559,32 @@ class Entity extends Position
$this->boundingBox->maxZ = $z + $this->radius; $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])){ if($riding == null){
ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); if($this->riding != 0){
return; $riding = $this->server->api->entity->get($this->riding);
} if($riding instanceof Entity){
$riding->rider = 0;
if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ }else{
$this->linkedEntity = 0; ConsoleAPI::warn("SR Failed to get entity that is being ridden by $this");
$e->linkedEntity = 0; }
$this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => $type]); }
$this->riding = 0;
}else{ }else{
$this->linkedEntity = $e->eid; if(!isset($this->level->entityList[$riding->eid])){
$e->linkedEntity = $this->eid; ConsoleAPI::warn("Tried linking $this with $riding that doesnt exist in the world!");
$this->isRider = true; return;
$this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); }
$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() public function isPlayer()
@ -1725,7 +1786,7 @@ class Entity extends Position
public function __toString() 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 * @throws Exception
*/ */
public static function getSizeOf($e) public static function getSizeOf($e)
@ -1751,12 +1812,18 @@ class Entity extends Position
/** /**
* * @deprecated Use {@link Entity::getHeight} or {@link Entity::getWidth} instead
* @deprecated Use {@link getHeight} or {@link getWidth} instead
* @throws Exception * @throws Exception
*/ */
public function getSize() public function getSize()
{ {
throw new Exception("Use getHeight or getWidth method instead of this."); throw new Exception("Use getHeight or getWidth method instead of this.");
} }
/**
* @deprecated use {@link Entity::setRiding} instead
*/
public function stopRiding(){
$this->setRiding(null);
}
} }

View file

@ -199,22 +199,25 @@ abstract class Projectile extends Entity{
return $data; return $data;
} }
public function spawn($player) public function spawn(Player $player)
{ {
$player->addEntity($this); if(!$player->hasEntity($this)){
$player->addEntity($this);
$pk = new AddEntityPacket(); $pk = new AddEntityPacket();
$pk->eid = $this->eid; $pk->eid = $this->eid;
$pk->type = $this->type; $pk->type = $this->type;
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
$pk->z = $this->z; $pk->z = $this->z;
$pk->speedX = $this->speedX; $pk->speedX = $this->speedX;
$pk->speedY = $this->speedY; $pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ; $pk->speedZ = $this->speedZ;
$pk->did = 0; $pk->did = 0;
$player->entityQueueDataPacket($pk); $player->entityQueueDataPacket($pk);
return true; $this->sendLinkPackets($player);
return true;
}
return false;
} }
} }

View file

@ -92,6 +92,7 @@ class MobController
public function movementTick(){ public function movementTick(){
$this->entity->moveForward = 0; $this->entity->moveForward = 0;
if($this->updateMove){ if($this->updateMove){
$this->updateMove = false; $this->updateMove = false;
$v1 = floor($this->entity->boundingBox->minY + 0.5); $v1 = floor($this->entity->boundingBox->minY + 0.5);

View file

@ -34,7 +34,7 @@ class Pig extends Animal implements Rideable{
} }
public function canRide($e){ public function canRide($e){
return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; return $this->isSaddled() && $this->rider == 0;
} }
public function updateEntityMovement(){ public function updateEntityMovement(){

View file

@ -245,13 +245,11 @@ class Arrow extends Entity{
$this->doBlocksCollision(); $this->doBlocksCollision();
} }
} }
public function spawn($player){ public function spawn(Player $player){
if($this->type === OBJECT_ARROW){ if(!($this->type == OBJECT_ARROW)) return false;
if(!$player->hasEntity($this)){
$player->addEntity($this); $player->addEntity($this);
$pk = new AddEntityPacket; $pk = new AddEntityPacket;
$pk->eid = $this->eid; $pk->eid = $this->eid;
@ -264,6 +262,9 @@ class Arrow extends Entity{
$pk->speedY = $this->speedY; $pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ; $pk->speedZ = $this->speedZ;
$player->entityQueueDataPacket($pk); $player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
} }
return false;
} }
} }

View file

@ -21,18 +21,23 @@ class FallingSand extends Entity{
} }
public function spawn($player) public function spawn($player)
{ {
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddEntityPacket(); $player->addEntity($this);
$pk->eid = $this->eid; $pk = new AddEntityPacket();
$pk->type = $this->type; $pk->eid = $this->eid;
$pk->x = $this->x; $pk->type = $this->type;
$pk->y = $this->y; $pk->x = $this->x;
$pk->z = $this->z; $pk->y = $this->y;
$pk->speedX = $this->speedX; $pk->z = $this->z;
$pk->speedY = $this->speedY; $pk->speedX = $this->speedX;
$pk->speedZ = $this->speedZ; $pk->speedY = $this->speedY;
$pk->did = -$this->data["Tile"]; $pk->speedZ = $this->speedZ;
$player->entityQueueDataPacket($pk); $pk->did = -$this->data["Tile"];
$player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
}
return false;
} }
public function update($now){ public function update($now){

View file

@ -42,27 +42,32 @@ class ItemEntity extends Entity{
} }
public function spawn($player) public function spawn(Player $player)
{ {
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddItemEntityPacket(); $player->addEntity($this);
$pk->eid = $this->eid; $pk = new AddItemEntityPacket();
$pk->x = $this->x; $pk->eid = $this->eid;
$pk->y = $this->y; $pk->x = $this->x;
$pk->z = $this->z; $pk->y = $this->y;
$pk->yaw = $this->yaw; $pk->z = $this->z;
$pk->pitch = $this->pitch; $pk->yaw = $this->yaw;
$pk->roll = 0; $pk->pitch = $this->pitch;
$pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); $pk->roll = 0;
$pk->metadata = $this->getMetadata(); $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack);
$player->entityQueueDataPacket($pk); $pk->metadata = $this->getMetadata();
$player->entityQueueDataPacket($pk);
$pk = new SetEntityMotionPacket();
$pk->eid = $this->eid; $pk = new SetEntityMotionPacket();
$pk->speedX = $this->speedX; $pk->eid = $this->eid;
$pk->speedY = $this->speedY; $pk->speedX = $this->speedX;
$pk->speedZ = $this->speedZ; $pk->speedY = $this->speedY;
$player->entityQueueDataPacket($pk); $pk->speedZ = $this->speedZ;
$player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
}
return false;
} }

View file

@ -72,6 +72,10 @@ class Minecart extends Vehicle{
return !$this->dead; return !$this->dead;
} }
public function getRideHeight(){
return ($this->height * 0) - 0.3;
}
public function getDrops(){ public function getDrops(){
return [ return [
[MINECART, 0, 1] [MINECART, 0, 1]
@ -166,8 +170,8 @@ class Minecart extends Vehicle{
$this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal; $this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal;
$this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal;
if($this->linkedEntity != 0 && !$this->isRider){ if($this->rider != 0){
$rider = $this->level->entityList[$this->linkedEntity] ?? false; $rider = $this->level->entityList[$this->rider] ?? false;
if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){ if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){
if($rider->moveForward > 0){ if($rider->moveForward > 0){
$v32 = sin($this->yaw * M_PI / 180); $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); $this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z);
$dx = $this->speedX; $dx = $this->speedX;
$dz = $this->speedZ; $dz = $this->speedZ;
if($this->linkedEntity != 0 && !$this->isRider){ if($this->rider != 0){
$dx *= 0.75; $dx *= 0.75;
$dz *= 0.75; $dz *= 0.75;
} }
@ -283,7 +287,7 @@ class Minecart extends Vehicle{
public function applyNaturalSlowdown(){ 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->speedX *= $mult;
$this->speedY = 0; $this->speedY = 0;
@ -394,7 +398,7 @@ class Minecart extends Vehicle{
$ind = "$chunkX $chunkZ"; $ind = "$chunkX $chunkZ";
foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){
$e = ($this->level->entityList[$entid] ?? null); $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->isPushable() && $e->boundingBox->intersectsWith($bb)){
if($e->isPlayer()){ if($e->isPlayer()){
$this->applyCollision($e); $this->applyCollision($e);
@ -412,38 +416,35 @@ class Minecart extends Vehicle{
public function close() public function close()
{ {
parent::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(){ public function isPushable(){
return false; //TODO replace with true return false; //TODO replace with true
} }
public function spawn($player){ public function spawn(Player $player){
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddEntityPacket; $player->addEntity($this);
$pk->eid = $this->eid; $pk = new AddEntityPacket;
$pk->type = $this->type; $pk->eid = $this->eid;
$pk->x = $this->x; $pk->type = $this->type;
$pk->y = $this->y; //+ $this->yOffset; $pk->x = $this->x;
$pk->z = $this->z; $pk->y = $this->y; //+ $this->yOffset;
$pk->yaw = $this->yaw; $pk->z = $this->z;
$pk->pitch = $this->pitch; $pk->yaw = $this->yaw;
$player->entityQueueDataPacket($pk); $pk->pitch = $this->pitch;
$player->entityQueueDataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid; $pk = new SetEntityMotionPacket;
$pk->speedX = $this->speedX; $pk->eid = $this->eid;
$pk->speedY = $this->speedY; $pk->speedX = $this->speedX;
$pk->speedZ = $this->speedZ; $pk->speedY = $this->speedY;
$player->entityQueueDataPacket($pk); $pk->speedZ = $this->speedZ;
$player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
}
return false;
} }
public function interactWith(Entity $e, $action){ public function interactWith(Entity $e, $action){
@ -451,14 +452,14 @@ class Minecart extends Vehicle{
$e->setRiding($this); $e->setRiding($this);
return true; 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? return false; //TODO more vanilla way?
} }
parent::interactWith($e, $action); parent::interactWith($e, $action);
} }
public function canRide($e) public function canRide($e)
{ {
return $this->linkedEntity == 0 && $e->linkedEntity == 0; return $this->rider == 0;
} }
} }

View file

@ -197,15 +197,20 @@ class Painting extends Entity{
return $data; return $data;
} }
public function spawn($player){ public function spawn(Player $player){
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddPaintingPacket; $player->addEntity($this);
$pk->eid = $this->eid; $pk = new AddPaintingPacket;
$pk->x = (int) $this->xPos; $pk->eid = $this->eid;
$pk->y = (int) $this->yPos; $pk->x = (int) $this->xPos;
$pk->z = (int) $this->zPos; $pk->y = (int) $this->yPos;
$pk->direction = $this->direction; $pk->z = (int) $this->zPos;
$pk->title = $this->motive; $pk->direction = $this->direction;
$player->entityQueueDataPacket($pk); $pk->title = $this->motive;
$player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
}
return false;
} }
} }

View file

@ -65,18 +65,24 @@ class PrimedTNT extends Entity{
$this->lastUpdate = $now; $this->lastUpdate = $now;
} }
public function spawn($player){ public function spawn(Player $player){
$player->addEntity($this); if(!$player->hasEntity($this)){
$pk = new AddEntityPacket; $player->addEntity($this);
$pk->eid = $this->eid; $pk = new AddEntityPacket;
$pk->type = $this->type; $pk->eid = $this->eid;
$pk->x = $this->x; $pk->type = $this->type;
$pk->y = $this->y; $pk->x = $this->x;
$pk->z = $this->z; $pk->y = $this->y;
$pk->did = 1; $pk->z = $this->z;
$pk->speedX = $this->speedX; $pk->did = 1;
$pk->speedY = $this->speedY; $pk->speedX = $this->speedX;
$pk->speedZ = $this->speedZ; $pk->speedY = $this->speedY;
$player->entityQueueDataPacket($pk); $pk->speedZ = $this->speedZ;
$player->entityQueueDataPacket($pk);
$this->sendLinkPackets($player);
return true;
}
return false;
} }
} }