From 8aacc2779520e87692d3f455534ffe7aa897545f Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sun, 12 Apr 2026 19:20:59 +0800 Subject: [PATCH] Basically Fixed Inventory --- src/Player.php | 35 +++++++++++++++++-- .../protocol/packet/PlayerEquipmentPacket.php | 4 +-- src/network/protocol/packet/UseItemPacket.php | 14 +------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/Player.php b/src/Player.php index 3ca13466..ce2568c0 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2166,6 +2166,9 @@ class Player{ $this->iusername = strtolower($this->username); $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; $this->PROTOCOL = $packet->PROTOCOL; + if($this->PROTOCOL <= ProtocolInfo12::CURRENT_PROTOCOL_12){ + Player::$experimentalHotbar = false; + } if(count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)){ $this->close("server is full!", false); return; @@ -2806,6 +2809,13 @@ class Player{ $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ //TODO Emulate and Calcuate slot for version under 0.6.0 + if($this->hasItem($data["item"], $data["meta"])) { + $handHeldItem = BlockAPI::getItem($data["item"], $data["meta"]); + $this->setCurrentSlot($handHeldItem); + } + } + //nc: check item desync $heldItem = $this->getHeldItem(); $pmeta = ($packet->meta & 0xff); @@ -2850,7 +2860,12 @@ class Player{ $this->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); break; }elseif($packet->face === 0xff){ - + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ //TODO Emulate and Calcuate slot for version under 0.6.0 + if($this->hasItem($data["item"], $data["meta"])) { + $handHeldItem = BlockAPI::getItem($data["item"], $data["meta"]); + $this->setCurrentSlot($handHeldItem); + } + } $slotItem = $this->getHeldItem(); if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way $x = $packet->x * 0.000030518; @@ -3224,6 +3239,12 @@ class Player{ $packet->eid = $this->eid; $prevItem = $packet->item; + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ //TODO Emulate and Calcuate slot for version under 0.6.0 + if($this->hasItem($prevItem->getID(), $prevItem->getMetadata())) { + $handHeldItem = BlockAPI::getItem($prevItem->getID(), $prevItem->getMetadata()); + $this->setCurrentSlot($handHeldItem); + } + } $newItem = $this->getHeldItem(); if(($n = $this->isExpectedSetSlot(-1, $prevItem))){ @@ -3264,7 +3285,6 @@ class Player{ $this->toCraft = []; $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $data["eid"] = $packet->eid; - $data["unknown"] = $packet->randomly; $data["randomly"] = $packet->randomly; $data["item"] = $packet->item; $data["player"] = $this; @@ -3583,8 +3603,19 @@ class Player{ */ public function getHeldItem(){ + if($this->PROTOCOL <= Protocolinfo9::CURRENT_PROTOCOL_9){ + ConsoleAPI::debug("Slot may exist issue , plz check the correct slot using setCurrentSlot first !"); + } return $this->getSlot($this->slot); } + + /** + * Dont use this if the protocol version >= MCPE 0.7 + * @return Boolean + */ + public function setCurrentSlot(Item $heldItem){ + $this->slot = $this->hasItem($heldItem->getID(), $heldItem->getMetadata()) ?: 0; + } public function stopSleep(){ $this->isSleeping = false; diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 66bfea7d..1eb04c52 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -23,7 +23,7 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ $this->eid = $this->getInt(); $this->item = $this->getShort(); $this->meta = $this->getShort(); - if($this->PROTOCOL === ProtocolInfo::CURRENT_PROTOCOL){$this->slot = $this->getSignedByte();} + if($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12){$this->slot = $this->getSignedByte();} } public function encode(){ @@ -31,7 +31,7 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ $this->putInt($this->eid); $this->putShort(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->item)); $this->putShort($this->meta); - if($this->PROTOCOL === ProtocolInfo::CURRENT_PROTOCOL){$this->putByte($this->slot);} + if($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12){$this->putByte($this->slot);} } public function eidsToGlobal(Player $p){ diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index be89ec6b..feb41cbf 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -41,10 +41,6 @@ class UseItemPacket extends RakNetDataPacket{ $this->item = $this->getShort(); $this->meta = $this->getByte(); $this->eid = $this->getInt(); - if ($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9) { - $this->unknownInt1 = $this->getInt(); - return; - } $this->fx = $this->getFloat(); $this->fy = $this->getFloat(); $this->fz = $this->getFloat(); @@ -56,15 +52,7 @@ class UseItemPacket extends RakNetDataPacket{ } public function encode(){ - /*$this->reset(); - $this->putInt($this->x); - $this->putInt($this->y); - $this->putInt($this->z); - $this->putInt($this->face); - $this->putShort($this->item); - $this->putByte($this->meta); - $this->putInt($this->eid); - $this->putInt($this->unknownInt1);*/ + } public function eidsToGlobal(Player $p){ if($this->localEids){