From 21f8796e8963b1d389a961069e9a02c411e5cc45 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 Jun 2025 03:12:24 +0300 Subject: [PATCH] better(?) way to handle eid translation, local eid 0 is not used now, some additional fixes --- src/Player.php | 43 ++++++------------- src/entity/Entity.php | 12 ++---- src/network/protocol/ProtocolInfo.php | 3 +- .../protocol/packet/AddEntityPacket.php | 9 ++++ .../protocol/packet/AddItemEntityPacket.php | 9 +++- src/network/protocol/packet/AddMobPacket.php | 10 ++++- .../protocol/packet/AddPaintingPacket.php | 10 ++++- .../protocol/packet/AddPlayerPacket.php | 10 ++++- src/network/protocol/packet/AnimatePacket.php | 17 ++++++++ .../protocol/packet/DropItemPacket.php | 10 ++++- .../protocol/packet/EntityEventPacket.php | 18 +++++++- .../protocol/packet/InteractPacket.php | 21 ++++++++- .../packet/MoveEntityPacket_PosRot.php | 16 +++++++ .../protocol/packet/MovePlayerPacket.php | 16 +++++++ .../protocol/packet/PlayerActionPacket.php | 10 ++++- .../packet/PlayerArmorEquipmentPacket.php | 19 +++++++- .../protocol/packet/PlayerEquipmentPacket.php | 19 +++++++- .../protocol/packet/RemoveBlockPacket.php | 8 ++++ .../protocol/packet/RemoveEntityPacket.php | 10 ++++- .../protocol/packet/RemovePlayerPacket.php | 8 ++++ src/network/protocol/packet/RespawnPacket.php | 9 +++- .../protocol/packet/RotateHeadPacket.php | 17 ++++++++ .../protocol/packet/SendInventoryPacket.php | 18 ++++++++ .../protocol/packet/SetEntityDataPacket.php | 8 ++++ .../protocol/packet/SetEntityLinkPacket.php | 19 ++++++++ .../protocol/packet/SetEntityMotionPacket.php | 9 +++- .../protocol/packet/StartGamePacket.php | 10 ++++- .../protocol/packet/TakeItemEntityPacket.php | 10 +++++ src/network/protocol/packet/UseItemPacket.php | 8 ++++ src/network/raknet/RakNetDataPacket.php | 12 +++++- src/network/raknet/RakNetParser.php | 1 + src/world/Level.php | 2 +- 32 files changed, 345 insertions(+), 56 deletions(-) diff --git a/src/Player.php b/src/Player.php index cefe1f21..5b99f7a3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -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; } /** diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c1107ac5..975fb90c 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -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); } } diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 118975fa..c2e9ebfb 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -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: diff --git a/src/network/protocol/packet/AddEntityPacket.php b/src/network/protocol/packet/AddEntityPacket.php index ac2c1a75..36d7848b 100644 --- a/src/network/protocol/packet/AddEntityPacket.php +++ b/src/network/protocol/packet/AddEntityPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index fb68f60a..4c250c8d 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddMobPacket.php b/src/network/protocol/packet/AddMobPacket.php index 5b2d9a90..56620be3 100644 --- a/src/network/protocol/packet/AddMobPacket.php +++ b/src/network/protocol/packet/AddMobPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddPaintingPacket.php b/src/network/protocol/packet/AddPaintingPacket.php index bd50ab0a..60337c46 100644 --- a/src/network/protocol/packet/AddPaintingPacket.php +++ b/src/network/protocol/packet/AddPaintingPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AddPlayerPacket.php b/src/network/protocol/packet/AddPlayerPacket.php index 044f119a..166a27e1 100644 --- a/src/network/protocol/packet/AddPlayerPacket.php +++ b/src/network/protocol/packet/AddPlayerPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index a8ea6820..e8ce1225 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 41531160..30c487bf 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 44e9b06a..daa00929 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -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; + } + } } \ No newline at end of file diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 2d33773d..dcc7f53c 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index c2143c18..02930e1f 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 5325cf76..dc234b51 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index cdcc64ad..743bf674 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php index d32be316..c867232c 100644 --- a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 06e65fee..62af566e 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index d3e3a969..c314c20b 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index 27d6c42b..fded3c76 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemovePlayerPacket.php b/src/network/protocol/packet/RemovePlayerPacket.php index f23cd4df..4847aff6 100644 --- a/src/network/protocol/packet/RemovePlayerPacket.php +++ b/src/network/protocol/packet/RemovePlayerPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 1d3c208e..7fc4fad6 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 21cc7cf0..78157858 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 1bddca97..375b7032 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 8626885d..c30f35c9 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 5dca8516..e01fa40a 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index e4cf70b9..bc8ff9a1 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/StartGamePacket.php b/src/network/protocol/packet/StartGamePacket.php index a746ea3a..293a1b23 100644 --- a/src/network/protocol/packet/StartGamePacket.php +++ b/src/network/protocol/packet/StartGamePacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 85221c38..c64095d9 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -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; + } } \ No newline at end of file diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 32e2c651..6c2875da 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -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; + } } diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 5f64ac47..25fd2e93 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -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; } diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 764bf64f..900280a4 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -228,6 +228,7 @@ class RakNetParser{ $data->splitCount = $splitCount; $data->splitID = $splitID; $data->splitIndex = $splitIndex; + $data->localEids = true; $data->setBuffer($buf); } return $data; diff --git a/src/world/Level.php b/src/world/Level.php index d3729af5..b07569f1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -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(); } }