[LuaMod|Inventory] Fixed a small bug preventing items from being gathered.

This commit is contained in:
Quentin Bazin 2020-07-20 09:48:26 +02:00
parent b8d3a36f19
commit 4c41ab080a
6 changed files with 30 additions and 16 deletions

View File

@ -109,9 +109,11 @@ Give an item stack to a player.
Example:
```lua
mod:give_item_stack(player, item_stack)
local items_left = mod:give_item_stack(player, item_stack)
```
This function returns an `ItemStack` containing the items it couldn't place into player's inventory.
### `spawn_entity`
Spawn an entity.

View File

@ -35,7 +35,9 @@ void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount)
ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size, bool mergeOnly) {
if (stringID.empty() || stringID == BLOCK_AIR || amount == 0)
return {BLOCK_AIR, 0};
return ItemStack::Empty;
bool hasChanged = false;
ItemStack ret{stringID, amount};
for (std::size_t i = offset ; ret.amount() && i < (size ? offset + size : m_items.size()) ; ++i) {
@ -49,7 +51,8 @@ ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offse
m_items[i] = ItemStack(stringID, ret.amount());
ret.setAmount(0);
}
m_hasChanged = true;
hasChanged = true;
}
else if (item.stringID() == stringID) {
u16 sum = m_items[i].amount() + ret.amount();
@ -62,12 +65,14 @@ ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offse
ret.setAmount(0);
}
m_hasChanged = true;
hasChanged = true;
}
}
if (!m_hasChanged && mergeOnly && ret.amount())
if (!hasChanged && mergeOnly && ret.amount())
return addStack(ret.item().stringID(), ret.amount(), offset, size);
else if (hasChanged)
m_hasChanged = hasChanged;
return ret;
}

View File

@ -27,6 +27,8 @@
#include "ItemStack.hpp"
#include "Registry.hpp"
const ItemStack ItemStack::Empty{BLOCK_AIR, 0};
const Item &ItemStack::item() const {
return Registry::getInstance().getItemFromStringID(m_stringID);
}

View File

@ -46,6 +46,8 @@ class ItemStack {
static void initUsertype(sol::state &lua);
static const ItemStack Empty;
private:
std::string m_stringID{BLOCK_AIR};

View File

@ -106,16 +106,19 @@ void LuaMod::despawnEntity(EntityWrapper &entity) {
gkError() << "In mod '" + m_id + "': Failed to despawn entity: Missing position and network components";
}
void LuaMod::giveItemStack(ServerPlayer &player, ItemStack *itemStack) {
if (itemStack) {
ItemStack LuaMod::giveItemStack(ServerPlayer &player, ItemStack *itemStack) {
if (!itemStack) {
gkError() << "In mod '" + m_id + "': Failed to add stack to player";
return ItemStack::Empty;
}
// FIXME: This should probably be moved to a mod
ItemStack stackRet = player.inventory().addStack(itemStack->item().stringID(), itemStack->amount(), 9, 24, true);
if (stackRet.amount() != 0)
player.inventory().addStack(stackRet.item().stringID(), stackRet.amount(), 0, 9, true);
stackRet = player.inventory().addStack(stackRet.item().stringID(), stackRet.amount(), 0, 9, true);
m_worldController.server()->sendPlayerInvUpdate(player.clientID(), player.client());
}
else
gkError() << "In mod '" + m_id + "': Failed to add stack to player";
return stackRet;
}

View File

@ -67,7 +67,7 @@ class LuaMod {
void spawnEntity(const std::string &entityID, const sol::table &table);
void despawnEntity(EntityWrapper &entity);
void giveItemStack(ServerPlayer &player, ItemStack *itemStack);
ItemStack giveItemStack(ServerPlayer &player, ItemStack *itemStack);
enum class DefinitionType {
Block,