From 624eb93058422b880d93b5616fffd69b5d2b3c06 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 27 Jan 2014 04:00:26 +0100 Subject: [PATCH] Save and send last slot used by player --- src/API/PlayerAPI.php | 1 + src/Player.php | 10 +++++++++- src/network/CustomPacketHandler.php | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index ed2e45d6a..562fb4987 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -454,6 +454,7 @@ class PlayerAPI{ "z" => $this->server->spawn->z, ), "inventory" => array_fill(0, PLAYER_SURVIVAL_SLOTS, array(AIR, 0, 0)), + "slot" => 0, "armor" => array_fill(0, 4, array(AIR, 0)), "gamemode" => $this->server->gamemode, "health" => 20, diff --git a/src/Player.php b/src/Player.php index 2e1ee4de9..a9f3ca4ff 100644 --- a/src/Player.php +++ b/src/Player.php @@ -251,6 +251,7 @@ class Player{ } } $this->data->set("inventory", $inv); + $this->data->set("slot", $this->slot); $armor = array(); foreach($this->armor as $slot => $item){ @@ -1366,6 +1367,8 @@ class Player{ )); if(($this->gamemode & 0x01) === 0x01){ $this->slot = 0; + }elseif($this->data->exists("slot")){ + $this->slot = (int) $this->data->get("slot"); }else{ $this->slot = -1;//0 } @@ -1487,7 +1490,6 @@ class Player{ if($this->spawned === false){ break; } - $data["eid"] = $this->eid; $data["player"] = $this; @@ -2198,10 +2200,16 @@ class Player{ if(($this->gamemode & 0x01) === CREATIVE){ return; } + $hotbar = array(); + $hotbar[] = $this->slot == -1 ? -1:($this->slot + 9); + for($i = 1; $i < 9; ++$i){ + $hotbar[] = -1; + } $this->dataPacket(MC_CONTAINER_SET_CONTENT, array( "windowid" => 0, "count" => count($this->inventory), "slots" => $this->inventory, + "hotbar" => $hotbar, )); } diff --git a/src/network/CustomPacketHandler.php b/src/network/CustomPacketHandler.php index b56d97759..9bd9d9678 100644 --- a/src/network/CustomPacketHandler.php +++ b/src/network/CustomPacketHandler.php @@ -800,12 +800,29 @@ class CustomPacketHandler{ for($s = 0; $s < $this->data["count"] and !$this->feof(); ++$s){ $this->data["slots"][$s] = Utils::readSlot($this); } + if($this->data["windowid"] == 0){ + $slots = min(9, Utils::readShort($this->get(2), false)); + $this->data["hotbar"] = array(); + if($slots > 0){ + for($s = 0; $s < $slots; ++$s){ + $this->data["hotbar"][$s] = Utils::readInt($this->get(4)); + } + } + } }else{ $this->raw .= chr($this->data["windowid"]); $this->raw .= Utils::writeShort(count($this->data["slots"])); foreach($this->data["slots"] as $slot){ $this->raw .= Utils::writeSlot($slot); } + if($this->data["windowid"] == 0 and isset($this->data["hotbar"])){ + if(count($this->data["hotbar"]) > 0){ + $this->raw .= Utils::writeShort(count($this->data["hotbar"])); + foreach($this->data["hotbar"] as $slot){ + $this->raw .= Utils::writeInt($slot); + } + } + } } break; case MC_CONTAINER_SET_DATA: