diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ed8d47c1..dcf00cc1 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -1,9 +1,9 @@ server = ServerAPI::request(); + foreach(static::$creative as $slot => [$id, $meta]){ + $rind = ($id << 24) | ($meta); + static::$reverseCreativeLookup[$rind] = $slot; + } + + self::$creativeHotbarSlots[0] = static::hasCreativeItem(STONE, 0); + self::$creativeHotbarSlots[1] = static::hasCreativeItem(COBBLESTONE, 0); + self::$creativeHotbarSlots[2] = static::hasCreativeItem(DIRT, 0); + self::$creativeHotbarSlots[3] = static::hasCreativeItem(PLANKS, 0); + self::$creativeHotbarSlots[4] = static::hasCreativeItem(PLANKS, 1); + self::$creativeHotbarSlots[5] = static::hasCreativeItem(TORCH, 0); + self::$creativeHotbarSlots[6] = static::hasCreativeItem(BRICK_STAIRS, 0); + self::$creativeHotbarSlots[7] = static::hasCreativeItem(COBBLE_WALL, 0); + self::$creativeHotbarSlots[8] = static::hasCreativeItem(SAPLING, 0); + } public static function get($id, $meta = 0, $v = false){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f8986212..21859b06 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -429,7 +429,6 @@ class PlayerAPI{ "z" => $this->server->spawn->z, ], "inventory" => array_fill(0, PLAYER_SURVIVAL_SLOTS, [AIR, 0, 0]), - "hotbar" => [0, 1, 2, 3, 4, 5, 6, 7, 8], "armor" => array_fill(0, 4, [AIR, 0]), "gamemode" => $this->server->gamemode, "health" => 20, @@ -439,23 +438,32 @@ class PlayerAPI{ "slot-count" => 7, "bed-position" => null, ]; - - if(!file_exists(DATA_PATH . "players/" . $iname . ".yml")){ + $ev = true; + $sav = false; + if(!file_exists(DATA_PATH . "players/$iname.yml")){ if(PocketMinecraftServer::$SAVE_PLAYER_DATA && $create){ - console("[NOTICE] Player data not found for \"" . $iname . "\", creating new profile"); - $data = new Config(DATA_PATH . "players/" . $iname . ".yml", CONFIG_YAML, $default); - $data->save(); + console("[NOTICE] Player data not found for \"$iname\", creating new profile"); + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $sav = true; }else{ - return new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); + $ev = false; } + }else{ + $data = new Config(DATA_PATH . "players/$iname.yml", CONFIG_YAML, $default); } - - $data = new Config(DATA_PATH . "players/" . $iname . ".yml", CONFIG_YAML, $default); - + if(($data->get("gamemode") & 0x01) === 1){ $data->set("health", 20); } - $this->server->handle("player.offline.get", $data); + if(!$data->exists("hotbar")){ + if(($data->get("gamemode") & 1) == CREATIVE) $hb = BlockAPI::$creativeHotbarSlots; + else $hb = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + $data->set("hotbar", $hb); + } + if($ev) $this->server->handle("player.offline.get", $data); + if($sav) $data->save(); + return $data; } diff --git a/src/Player.php b/src/Player.php index c194bcf6..258b5a99 100755 --- a/src/Player.php +++ b/src/Player.php @@ -30,6 +30,7 @@ class Player{ public $armor = []; public $loggedIn = false; public $gamemode; + public $gmKickSoon = false; public $lastBreak; public $windowCnt = 2; public $windows = []; @@ -1405,7 +1406,7 @@ class Player{ foreach($this->inventory as $slot => $item){ $inv[$slot] = BlockAPI::getItem(AIR, 0, 0); } - $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + $this->hotbar = $gm == CREATIVE ? BlockAPI::$creativeHotbarSlots : [0, 1, 2, 3, 4, 5, 6, 7, 8]; $this->lastCorrect = $this->entity->copy(); $this->blocked = true; if(($this->gamemode & 1) != ($gm & 1)){ @@ -1413,6 +1414,7 @@ class Player{ } $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ", you've to do a forced reconnect.\n"); + $this->gmKickSoon = true; $this->server->schedule(30, [$this, "close"], "gamemode change", false, true); //Forces a kick } @@ -1543,17 +1545,29 @@ class Player{ } if($this->sendingInventoryRequired){ - if($this->gamemode !== CREATIVE){ + + $pk = new ContainerSetContentPacket; + $pk->windowid = 0; + if($this->gmKickSoon){ + $pk->slots = []; + $hotbar = []; + }else if($this->gamemode !== CREATIVE){ $hotbar = []; foreach($this->hotbar as $slot){ $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } - $pk = new ContainerSetContentPacket; - $pk->windowid = 0; $pk->slots = $this->inventory; - $pk->hotbar = $hotbar; - $this->dataPacket($pk); + }else{ + $pk->slots = []; + for($i = 0; $i < 9; ++$i){ + [$id, $meta] = BlockAPI::$creative[$this->hotbar[$i]] ?? [0, 0]; + $pk->slots[$i] = BlockAPI::getItem($id, $meta, 1); + } + $hotbar = [9, 10, 11, 12, 13, 14, 15, 16, 17]; + } + $pk->hotbar = $hotbar; + $this->dataPacket($pk); $this->sendingInventoryRequired = false; } @@ -2194,18 +2208,17 @@ class Player{ $pk->status = 0; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x01){ - $this->slot = 0; - $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ + if($this->data->exists("hotbar")){ $this->hotbar = $this->data->get("hotbar"); - $this->slot = $this->hotbar[0]; }else{ - $this->slot = 0; - $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; + if(($this->gamemode & 0x01) === 0x01) $this->hotbar = BlockAPI::$creativeHotbarSlots; + else $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; } + + $this->slot = $this->hotbar[0]; + for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? + if($this->hotbar[$i] > (($this->gamemode & 1) == 0 ? 36 : count(BlockAPI::$creative))) $this->hotbar[$i] = -1; if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; } if($this->data->exists("slot-count")){ @@ -2438,6 +2451,7 @@ class Player{ array_unshift($this->hotbar, $this->slot); } } + if(Player::$experimentalHotbar) $this->sendInventory(); }else{ $this->slot = $packet->slot; @@ -2979,8 +2993,20 @@ class Player{ if($this->spawned === false or $this->blocked === true){ break; } - if($packet->windowid === 0){ //crafting + + if(($this->gamemode & 1) == CREATIVE){ //sets hotbar item - works only in creative(-based) mode + if($this->gamemode == VIEW) break; //TODO maybe send inventory? + if($packet->slot < 0 || $packet->slot > 8) break; + $it = $packet->item; + $yes = BlockAPI::hasCreativeItem($it->getID(), $it->getMetadata()); + if($yes === false){ //TODO maybe send inventory + break; + } + $this->hotbar[$packet->slot] = $yes; + return; + } + $slot = $this->getSlot($packet->slot); $citem = $packet->item;