mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +00:00
Implement metadata-aware version of InvRef:remove_item() (#15771)
This commit is contained in:
parent
0890125962
commit
e51221d247
6 changed files with 41 additions and 24 deletions
|
@ -693,11 +693,11 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
|
|||
return false;
|
||||
}
|
||||
|
||||
ItemStack InventoryList::removeItem(const ItemStack &item)
|
||||
ItemStack InventoryList::removeItem(const ItemStack &item, bool match_meta)
|
||||
{
|
||||
ItemStack removed;
|
||||
for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
|
||||
if (i->name == item.name) {
|
||||
if (i->name == item.name && (!match_meta || i->metadata == item.metadata)) {
|
||||
u32 still_to_remove = item.count - removed.count;
|
||||
ItemStack leftover = removed.addItem(i->takeItem(still_to_remove),
|
||||
m_itemdef);
|
||||
|
|
|
@ -262,7 +262,7 @@ public:
|
|||
// If not as many items exist as requested, removes as
|
||||
// many as possible.
|
||||
// Returns the items that were actually removed.
|
||||
ItemStack removeItem(const ItemStack &item);
|
||||
ItemStack removeItem(const ItemStack &item, bool match_meta);
|
||||
|
||||
// Takes some items from a slot.
|
||||
// If there are not enough, takes as many as it can.
|
||||
|
|
|
@ -319,9 +319,7 @@ int InvRef::l_contains_item(lua_State *L)
|
|||
const char *listname = luaL_checkstring(L, 2);
|
||||
ItemStack item = read_item(L, 3, getServer(L)->idef());
|
||||
InventoryList *list = getlist(L, ref, listname);
|
||||
bool match_meta = false;
|
||||
if (lua_isboolean(L, 4))
|
||||
match_meta = readParam<bool>(L, 4);
|
||||
bool match_meta = readParam<bool>(L, 4, false);
|
||||
if (list) {
|
||||
lua_pushboolean(L, list->containsItem(item, match_meta));
|
||||
} else {
|
||||
|
@ -330,7 +328,7 @@ int InvRef::l_contains_item(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
|
||||
// remove_item(self, listname, itemstack or itemstring or table or nil, [match_meta]) -> itemstack
|
||||
// Returns the items that were actually removed
|
||||
int InvRef::l_remove_item(lua_State *L)
|
||||
{
|
||||
|
@ -339,8 +337,9 @@ int InvRef::l_remove_item(lua_State *L)
|
|||
const char *listname = luaL_checkstring(L, 2);
|
||||
ItemStack item = read_item(L, 3, getServer(L)->idef());
|
||||
InventoryList *list = getlist(L, ref, listname);
|
||||
bool match_meta = readParam<bool>(L, 4, false);
|
||||
if(list){
|
||||
ItemStack removed = list->removeItem(item);
|
||||
ItemStack removed = list->removeItem(item, match_meta);
|
||||
if(!removed.empty())
|
||||
reportInventoryChange(L, ref);
|
||||
LuaItemStack::create(L, removed);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue