Merge pull request #27 from WasabiRaptor/main

Inventory and Hotbar size mismatch fixes
This commit is contained in:
Kae 2024-03-09 10:24:10 +11:00 committed by GitHub
commit 52a6f200ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 11 deletions

View File

@ -330,6 +330,9 @@ void Player::init(World* world, EntityId entityId, EntityMode mode) {
p.second->addCallbacks("celestial", LuaBindings::makeCelestialCallbacks(m_client));
p.second->init(world);
}
for (auto& p : m_inventory->clearOverflow()) {
world->addEntity(ItemDrop::createRandomizedDrop(p,m_movementController->position(),true));
}
}
m_xAimPositionNetState.setInterpolator(world->geometry().xLerpFunction());

View File

@ -756,6 +756,12 @@ Maybe<InventorySlot> PlayerInventory::secondaryHeldSlot() const {
return {};
}
List<ItemPtr> PlayerInventory::clearOverflow(){
auto list = m_inventoryLoadOverflow;
m_inventoryLoadOverflow.clear();
return list;
}
void PlayerInventory::load(Json const& store) {
auto itemDatabase = Root::singleton().itemDatabase();
@ -771,14 +777,21 @@ void PlayerInventory::load(Json const& store) {
//reuse ItemBags so the Inventory pane still works after load()'ing into the same PlayerInventory again (from swap)
auto itemBags = store.get("itemBags").toObject();
eraseWhere(m_bags, [&](auto const& p) { return !itemBags.contains(p.first); });
m_inventoryLoadOverflow.clear();
for (auto const& p : itemBags) {
auto& bagType = p.first;
auto newBag = ItemBag::loadStore(p.second);
auto& bagPtr = m_bags[bagType];
if (bagPtr)
*bagPtr = std::move(newBag);
else
bagPtr = make_shared<ItemBag>(std::move(newBag));
if (m_bags.keys().contains(bagType)) {
auto& bagPtr = m_bags[bagType];
auto size = bagPtr.get()->size();
if (bagPtr)
*bagPtr = std::move(newBag);
else
bagPtr = make_shared<ItemBag>(std::move(newBag));
m_inventoryLoadOverflow.appendAll(bagPtr.get()->resize(size));
} else {
m_inventoryLoadOverflow.appendAll(ItemBag(newBag).items());
}
}
m_swapSlot = itemDatabase->diskLoad(store.get("swapSlot"));
@ -790,10 +803,17 @@ void PlayerInventory::load(Json const& store) {
for (size_t i = 0; i < m_customBar.size(0); ++i) {
for (size_t j = 0; j < m_customBar.size(1); ++j) {
Json cbl = store.get("customBar").get(i).get(j);
Json cbl = store.get("customBar").get(i,JsonArray()).get(j,JsonArray());
auto validateLink = [this](Json link) -> Json {
if ((link.isType(Json::Type::Object))
&& (m_bags.keys().contains(link.getString("type")))
&& (m_bags[link.getString("type")].get()->size() > link.getUInt("location")))
return link;
return Json();
};
m_customBar.at(i, j) = CustomBarLink{
jsonToMaybe<InventorySlot>(cbl.get(0, {}), jsonToInventorySlot),
jsonToMaybe<InventorySlot>(cbl.get(1, {}), jsonToInventorySlot)
jsonToMaybe<InventorySlot>(validateLink(cbl.get(0, Json())), jsonToInventorySlot),
jsonToMaybe<InventorySlot>(validateLink(cbl.get(1, Json())), jsonToInventorySlot)
};
}
}

View File

@ -168,6 +168,8 @@ public:
Maybe<InventorySlot> primaryHeldSlot() const;
Maybe<InventorySlot> secondaryHeldSlot() const;
List<ItemPtr> clearOverflow();
void load(Json const& store);
Json store() const;
@ -219,6 +221,8 @@ private:
NetElementUInt m_customBarGroupNetState;
MultiArray<NetElementData<CustomBarLink>, 2> m_customBarNetState;
NetElementData<SelectedActionBarLocation> m_selectedActionBarNetState;
List<ItemPtr> m_inventoryLoadOverflow;
};
}