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

@ -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;
}
}

View file

@ -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){

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}