PlayerInventory & PlayerInventoryUI: You can now specify groups as materials for primitive crafting in your inventory. #34
This commit is contained in:
@@ -42,10 +42,17 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
|
||||
OutputStack:set_count(OutputStack:get_count() * fieldValue)
|
||||
|
||||
local recipeStacks = {}
|
||||
local recipeGroups = {}
|
||||
for _, itemName in ipairs(craft.recipe) do
|
||||
local item = ItemStack(itemName)
|
||||
item:set_count(item:get_count() * fieldValue)
|
||||
table.insert(recipeStacks, item)
|
||||
|
||||
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
|
||||
if group == nil then
|
||||
local item = ItemStack(itemName)
|
||||
item:set_count(item:get_count() * fieldValue)
|
||||
table.insert(recipeStacks, item)
|
||||
else
|
||||
recipeGroups[group] = tonumber(count)
|
||||
end
|
||||
end
|
||||
|
||||
for _, item in ipairs(recipeStacks) do
|
||||
@@ -55,6 +62,28 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
|
||||
end
|
||||
end
|
||||
|
||||
local mainList = inv:get_list('main')
|
||||
for group, count in pairs(recipeGroups) do
|
||||
for _, item in ipairs(mainList) do
|
||||
if core.get_item_group(item:get_name(), group) > 0 then
|
||||
local itemCount = item:get_count()
|
||||
if count > itemCount then
|
||||
count = count - itemCount
|
||||
table.insert(recipeStacks, item)
|
||||
else
|
||||
item:set_count(count)
|
||||
table.insert(recipeStacks, item)
|
||||
count = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if count > 0 then
|
||||
core.chat_send_player(playerName, 'Not enough materials for crafting!')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if inv:room_for_item('main', OutputStack) then
|
||||
for _, item in ipairs(recipeStacks) do
|
||||
inv:remove_item('main', item)
|
||||
|
||||
Reference in New Issue
Block a user