fix: actual block inventory size not updating on inventory-size property update

This commit is contained in:
MihailRis 2024-11-17 16:58:58 +03:00
parent 352ef6485a
commit 1ba5b0ce33
3 changed files with 25 additions and 3 deletions

View File

@ -45,6 +45,10 @@ void Inventory::move(
} }
} }
void Inventory::resize(uint newSize) {
slots.resize(newSize);
}
void Inventory::deserialize(const dv::value& src) { void Inventory::deserialize(const dv::value& src) {
id = src["id"].asInteger(1); id = src["id"].asInteger(1);
auto& slotsarr = src["slots"]; auto& slotsarr = src["slots"];

View File

@ -35,6 +35,8 @@ public:
size_t end = -1 size_t end = -1
); );
void resize(uint newSize);
void deserialize(const dv::value& src) override; void deserialize(const dv::value& src) override;
dv::value serialize() const override; dv::value serialize() const override;

View File

@ -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++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = chunk->voxels[i].id; blockid_t id = chunk->voxels[i].id;
if (indices->blocks.get(id) == nullptr) { if (indices.blocks.get(id) == nullptr) {
auto logline = logger.error(); auto logline = logger.error();
logline << "corruped block detected at " << i << " of chunk "; logline << "corruped block detected at " << i << " of chunk ";
logline << chunk->x << "x" << chunk->z; logline << chunk->x << "x" << chunk->z;
@ -59,9 +59,26 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
auto chunk = std::make_shared<Chunk>(x, z); auto chunk = std::make_shared<Chunk>(x, z);
store(chunk); store(chunk);
if (auto data = regions.getVoxels(chunk->x, chunk->z)) { if (auto data = regions.getVoxels(chunk->x, chunk->z)) {
const auto& indices = *level->content->getIndices();
chunk->decode(data.get()); chunk->decode(data.get());
check_voxels(indices, chunk.get());
auto invs = regions.fetchInventories(chunk->x, chunk->z); 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)); chunk->setBlockInventories(std::move(invs));
auto entitiesData = regions.fetchEntities(chunk->x, chunk->z); auto entitiesData = regions.fetchEntities(chunk->x, chunk->z);
@ -74,7 +91,6 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
for (auto& entry : chunk->inventories) { for (auto& entry : chunk->inventories) {
level->inventories->store(entry.second); level->inventories->store(entry.second);
} }
verifyLoadedChunk(level->content->getIndices(), chunk.get());
} }
if (auto lights = regions.getLights(chunk->x, chunk->z)) { if (auto lights = regions.getLights(chunk->x, chunk->z)) {
chunk->lightmap.set(lights.get()); chunk->lightmap.set(lights.get());