From b7cf5d08cccc64f275fc2c7d3425ba988f314874 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 27 Jan 2014 14:00:53 +0100 Subject: [PATCH] Save and send hotbar history --- src/API/PlayerAPI.php | 2 +- src/Player.php | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 562fb4987..b5ac7fae7 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -454,7 +454,7 @@ class PlayerAPI{ "z" => $this->server->spawn->z, ), "inventory" => array_fill(0, PLAYER_SURVIVAL_SLOTS, array(AIR, 0, 0)), - "slot" => 0, + "hotbar" => array(0, -1, -1, -1, -1, -1, -1, -1, -1), "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 a9f3ca4ff..104fe1df2 100644 --- a/src/Player.php +++ b/src/Player.php @@ -52,6 +52,7 @@ class Player{ public $spawned = false; public $inventory; public $slot; + public $hotbar; public $armor = array(); public $loggedIn = false; public $gamemode; @@ -111,6 +112,7 @@ class Player{ $this->gamemode = $this->server->gamemode; $this->level = $this->server->api->level->getDefault(); $this->slot = 0; + $this->hotbar = array(0, -1, -1, -1, -1, -1, -1, -1, -1); $this->packetStats = array(0,0); $this->server->schedule(2, array($this, "handlePacketQueues"), array(), true); $this->server->schedule(20 * 60, array($this, "clearQueue"), array(), true); @@ -251,7 +253,7 @@ class Player{ } } $this->data->set("inventory", $inv); - $this->data->set("slot", $this->slot); + $this->data->set("hotbar", $this->hotbar); $armor = array(); foreach($this->armor as $slot => $item){ @@ -1367,10 +1369,13 @@ class Player{ )); if(($this->gamemode & 0x01) === 0x01){ $this->slot = 0; - }elseif($this->data->exists("slot")){ - $this->slot = (int) $this->data->get("slot"); + $this->hotbar = array(); + }elseif($this->data->exists("hotbar")){ + $this->hotbar = $this->data->get("hotbar"); + $this->slot = $this->hotbar[0]; }else{ $this->slot = -1;//0 + $this->hotbar = array(-1, -1, -1, -1, -1, -1, -1, -1, -1); } $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, array("player" => $this)); $this->eid = $this->entity->eid; @@ -1529,8 +1534,15 @@ class Player{ $data["meta"] = $data["item"]->getMetadata(); if($this->server->handle("player.equipment.change", $data) !== false){ $this->slot = $data["slot"]; + if(($this->gamemode & 0x01) === SURVIVAL){ + if(!in_array($this->slot, $this->hotbar)){ + array_pop($this->hotbar); + array_unshift($this->hotbar, $this->slot); + } + } }else{ - $this->sendInventorySlot($data["slot"]); + //$this->sendInventorySlot($data["slot"]); + $this->sendInventory(); } if($this->entity->inAction === true){ $this->entity->inAction = false; @@ -2201,10 +2213,10 @@ class Player{ return; } $hotbar = array(); - $hotbar[] = $this->slot == -1 ? -1:($this->slot + 9); - for($i = 1; $i < 9; ++$i){ - $hotbar[] = -1; + foreach($this->hotbar as $slot){ + $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } + $this->dataPacket(MC_CONTAINER_SET_CONTENT, array( "windowid" => 0, "count" => count($this->inventory),