better(?) way to handle eid translation, local eid 0 is not used now, some additional fixes

This commit is contained in:
GameHerobrine 2025-06-29 03:12:24 +03:00
parent 62819f8b12
commit 21f8796e89
32 changed files with 345 additions and 56 deletions

View file

@ -132,20 +132,21 @@ class Player{
/**
* Stores local entity ids. Format: global => local.
* Should be modified only by Player::addEntity or Player::removeEntity.
* Entity ID 0 is reserved for non-existent entities.
* @var array
*/
public $global2localEID = [];
public $global2localEID = [0 => 0];
/**
* Stores global entity ids. Format: local => global.
* Should be modified only by Player::addEntity or Player::removeEntity.
* @var array
*/
public $local2GlobalEID = [];
public $local2GlobalEID = [0 => 0];
/**
* Stores last local entity id. Increments every time Player::addEntity is called.
* @var array
*/
public $lastLocalEID = -1;
public $lastLocalEID = 0;
/**
* @param integer $clientID
@ -1022,8 +1023,8 @@ class Player{
switch($event){
case "entity.link":
$pk = new SetEntityLinkPacket();
$pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"];
$pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"];
$pk->rider = $data["rider"];
$pk->riding = $data["riding"];
$pk->type = 0;
$this->entityQueueDataPacket($pk);
break;
@ -1136,6 +1137,7 @@ class Player{
$this->entityQueueDataPacket($pk);
break;
case "entity.metadata":
if(!$this->hasEntity($data)) break;
$eid = $data->eid;
if($data->level === $this->level){
$pk = new SetEntityDataPacket;
@ -1637,37 +1639,20 @@ class Player{
}
public function convertToGlobalEIDPacket(RakNetDataPacket $pk){
$local = $pk->eid;
$pk->eid = $this->local2GlobalEID[$local] ?? false;
if($pk->eid === false){
PRINT_STACK_TRACE:
ConsoleAPI::warn("Attempting to convert eids to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: ");
if(!$pk->eidsToGlobal($this)){
ConsoleAPI::warn("Failed to convert eids to global in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: ");
foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s);
return false;
}
if($pk->pid() == ProtocolInfo::INTERACT_PACKET){
$local = $pk->target;
$pk->target = $this->local2GlobalEID[$local] ?? false;
if($pk->target === false){
ConsoleAPI::warn("Attempting to convert target eid to global in entity packet {$pk->pid()} but global eid is incorrect! (Global: $local, Player: {$this->ip}:{$this->port}). Stacktrace: ");
foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s);
return false;
}
}
return true;
}
public function convertToLocalEIDPacket(RakNetDataPacket $pk){
if(!isset($pk->_localeid) || !$pk->_localeid){
$global = $pk->eid;
$pk->eid = $this->global2localEID[$global] ?? false;
$pk->_localeid = true;
if($pk->eid === false){
ConsoleAPI::warn("Attempting to convert eids to local in entity packet {$pk->pid()} but local eid is incorrect! (Global: $global, Player: {$this->ip}:{$this->port}). Stacktrace: ");
foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s);
return false;
}
return true;
if(!$pk->eidsToLocal($this)){
ConsoleAPI::warn("Failed to convert eids to local in {$pk->pid()}! (Player: {$this->ip}:{$this->port}). Stacktrace: ");
foreach(explode("\n", (new Exception())->getTraceAsString()) as $s) ConsoleAPI::warn($s);
return false;
}
return true;
}
/**

View file

@ -1466,8 +1466,8 @@ class Entity extends Position
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" => -1, "type" => 1]);
$this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => -1, "type" => 1]);
$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;
@ -1602,13 +1602,7 @@ class Entity extends Position
$pk->eid = $this->eid;
$pk->event = EntityEventPacket::ENTITY_DAMAGE;
foreach($this->level->players as $p){
if(($p->entity instanceof Entity) && $p->entity->eid == $this->eid){
$pk2 = clone $pk;
$pk2->eid = 0;
$p->entityQueueDataPacket($pk2);
}else{
$p->entityQueueDataPacket(clone $pk);
}
$p->entityQueueDataPacket(clone $pk);
}
}

View file

@ -96,7 +96,8 @@ abstract class ProtocolInfo{
self::ADD_ENTITY_PACKET => true,
self::REMOVE_BLOCK_PACKET => true,
self::MOVE_PLAYER_PACKET => true,
self::DROP_ITEM_PACKET => true
self::DROP_ITEM_PACKET => true,
self::SET_ENTITY_LINK_PACKET => true
];
}
/*Unused:

View file

@ -33,5 +33,14 @@ class AddEntityPacket extends RakNetDataPacket{
$this->putShort((int)($this->speedZ * 8000));
}
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -29,5 +29,12 @@ class AddItemEntityPacket extends RakNetDataPacket{
$this->putByte($this->pitch);
$this->putByte($this->roll);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -29,5 +29,13 @@ class AddMobPacket extends RakNetDataPacket{
$this->putByte($this->pitch);
$this->put(Utils::writeMetadata($this->metadata));
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -25,5 +25,13 @@ class AddPaintingPacket extends RakNetDataPacket{
$this->putInt($this->direction);
$this->putString($this->title);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -46,5 +46,13 @@ class AddPlayerPacket extends RakNetDataPacket{
$this->putShort($this->itemAuxValue); //Example: bow shooting power
$this->put(Utils::writeMetadata($this->metadata));
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -22,4 +22,21 @@ class AnimatePacket extends RakNetDataPacket{
$this->putInt($this->eid);
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -18,5 +18,13 @@ class DropItemPacket extends RakNetDataPacket{
public function encode(){
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -31,5 +31,21 @@ class EntityEventPacket extends RakNetDataPacket{
$this->putInt($this->eid);
$this->putByte($this->event);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
}
}

View file

@ -24,5 +24,24 @@ class InteractPacket extends RakNetDataPacket{
$this->putInt($this->eid);
$this->putInt($this->target);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
$this->target = $p->global2localEID[$this->target] ?? false;
if($this->eid === false || $this->target === false) return false;
}
return true;
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
$this->target = $p->local2GlobalEID[$this->target] ?? false;
if($this->eid === false || $this->target === false) return false;
}
return true;
}
}

View file

@ -32,4 +32,20 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{
$this->putFloat($this->pitch);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -34,4 +34,20 @@ class MovePlayerPacket extends RakNetDataPacket{
$this->putFloat($this->bodyYaw);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -24,5 +24,13 @@ class PlayerActionPacket extends RakNetDataPacket{
public function encode(){
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -24,5 +24,22 @@ class PlayerArmorEquipmentPacket extends RakNetDataPacket{
$this->putByte($this->slots[2]);
$this->putByte($this->slots[3]);
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -24,5 +24,22 @@ class PlayerEquipmentPacket extends RakNetDataPacket{
$this->putShort($this->meta);
$this->putByte($this->slot);
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -21,4 +21,12 @@ class RemoveBlockPacket extends RakNetDataPacket{
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -15,5 +15,13 @@ class RemoveEntityPacket extends RakNetDataPacket{
$this->reset();
$this->putInt($this->eid);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -18,4 +18,12 @@ class RemovePlayerPacket extends RakNetDataPacket{
$this->putLong($this->clientID);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -24,5 +24,12 @@ class RespawnPacket extends RakNetDataPacket{
$this->putFloat($this->y);
$this->putFloat($this->z);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -26,4 +26,21 @@ class RotateHeadPacket extends RakNetDataPacket{
$this->putByte($this->rawYaw ? $this->yaw : ($this->yaw / 360 / 0.0039062)); //wraps 360 angle to 0xff
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -38,5 +38,23 @@ class SendInventoryPacket extends RakNetDataPacket{
}
}
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -17,5 +17,13 @@ class SetEntityDataPacket extends RakNetDataPacket{
$this->putInt($this->eid);
$this->put(Utils::writeMetadata($this->metadata));
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -28,4 +28,23 @@ class SetEntityLinkPacket extends RakNetDataPacket{
return ProtocolInfo::SET_ENTITY_LINK_PACKET;
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->rider = $p->local2GlobalEID[$this->rider] ?? false;
$this->riding = $p->local2GlobalEID[$this->riding] ?? false;
if($this->rider === false || $this->riding === false) return false;
}
return true;
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->rider = $p->global2localEID[$this->rider] ?? false;
$this->riding = $p->global2localEID[$this->riding] ?? false;
if($this->rider === false || $this->riding === false) return false;
}
return true;
}
}

View file

@ -21,5 +21,12 @@ class SetEntityMotionPacket extends RakNetDataPacket{
$this->putShort((int) ($this->speedY * 8000));
$this->putShort((int) ($this->speedZ * 8000));
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -27,5 +27,13 @@ class StartGamePacket extends RakNetDataPacket{
$this->putFloat($this->y);
$this->putFloat($this->z);
}
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -18,4 +18,14 @@ class TakeItemEntityPacket extends RakNetDataPacket{
$this->putInt($this->eid);
}
//TODO convert
public function eidsToLocal(Player $p){
if(!$this->localEids){
$this->localEids = true;
$this->eid = $p->global2localEID[$this->eid] ?? false;
$this->target = $p->global2localEID[$this->eid] ?? false;
if($this->eid === false || $this->target === false) return false;
}
return true;
}
}

View file

@ -38,5 +38,13 @@ class UseItemPacket extends RakNetDataPacket{
public function encode(){
}
public function eidsToGlobal(Player $p){
if($this->localEids){
$this->localEids = false;
$this->eid = $p->local2GlobalEID[$this->eid] ?? false;
if($this->eid === false) return false;
}
return true;
}
}

View file

@ -13,10 +13,18 @@ abstract class RakNetDataPacket extends stdClass{
public $splitIndex;
private $offset = 0;
abstract public function encode();
public abstract function encode();
abstract public function decode();
public abstract function decode();
public $localEids = false;
public function eidsToLocal(Player $p){
return true;
}
public function eidsToGlobal(Player $p){
return true;
}
public function getBuffer(){
return $this->buffer;
}

View file

@ -228,6 +228,7 @@ class RakNetParser{
$data->splitCount = $splitCount;
$data->splitID = $splitID;
$data->splitIndex = $splitIndex;
$data->localEids = true;
$data->setBuffer($buf);
}
return $data;

View file

@ -395,7 +395,7 @@ class Level{
$this->server->api->time->set("day", $this);
}
foreach($this->players as $p){
$p->stopSleep();
if($p->isSleeping) $p->stopSleep();
}
}