diff --git a/src/items/Inventory.cpp b/src/items/Inventory.cpp index 92f925ef..282f361f 100644 --- a/src/items/Inventory.cpp +++ b/src/items/Inventory.cpp @@ -45,6 +45,10 @@ void Inventory::move( } } +void Inventory::resize(uint newSize) { + slots.resize(newSize); +} + void Inventory::deserialize(const dv::value& src) { id = src["id"].asInteger(1); auto& slotsarr = src["slots"]; diff --git a/src/items/Inventory.hpp b/src/items/Inventory.hpp index e12029f8..176ad7ee 100644 --- a/src/items/Inventory.hpp +++ b/src/items/Inventory.hpp @@ -35,6 +35,8 @@ public: size_t end = -1 ); + void resize(uint newSize); + void deserialize(const dv::value& src) override; dv::value serialize() const override; diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 596c4b78..272332fb 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -39,10 +39,10 @@ void ChunksStorage::remove(int x, int z) { } } -static void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) { +static void check_voxels(const ContentIndices& indices, Chunk* chunk) { for (size_t i = 0; i < CHUNK_VOL; i++) { blockid_t id = chunk->voxels[i].id; - if (indices->blocks.get(id) == nullptr) { + if (indices.blocks.get(id) == nullptr) { auto logline = logger.error(); logline << "corruped block detected at " << i << " of chunk "; logline << chunk->x << "x" << chunk->z; @@ -59,9 +59,26 @@ std::shared_ptr ChunksStorage::create(int x, int z) { auto chunk = std::make_shared(x, z); store(chunk); if (auto data = regions.getVoxels(chunk->x, chunk->z)) { + const auto& indices = *level->content->getIndices(); + chunk->decode(data.get()); + check_voxels(indices, chunk.get()); auto invs = regions.fetchInventories(chunk->x, chunk->z); + auto iterator = invs.begin(); + while (iterator != invs.end()) { + uint index = iterator->first; + const auto& def = indices.blocks.require(chunk->voxels[index].id); + if (def.inventorySize == 0) { + iterator = invs.erase(iterator); + continue; + } + auto& inventory = iterator->second; + if (def.inventorySize != inventory->size()) { + inventory->resize(def.inventorySize); + } + ++iterator; + } chunk->setBlockInventories(std::move(invs)); auto entitiesData = regions.fetchEntities(chunk->x, chunk->z); @@ -74,7 +91,6 @@ std::shared_ptr ChunksStorage::create(int x, int z) { for (auto& entry : chunk->inventories) { level->inventories->store(entry.second); } - verifyLoadedChunk(level->content->getIndices(), chunk.get()); } if (auto lights = regions.getLights(chunk->x, chunk->z)) { chunk->lightmap.set(lights.get());