z_mirror/CursorEnhancements

git clone https://git.raiguard.me/z_mirror/CursorEnhancements

Caleb HeuerConsider all possible recipes for quick-craft (#17)b4a035a

smart-quickbar
1.7 KiB64 linesraw
1local util = require("__CursorEnhancements__/scripts/util")
2
3local crafting_controllers = {
4  [defines.controllers.character] = true,
5  [defines.controllers.god] = true,
6}
7
8--- @param e EventData.CustomInputEvent
9local function on_quick_craft(e)
10  local player = game.get_player(e.player_index)
11  if not player then
12    return
13  end
14
15  if not crafting_controllers[player.controller_type] then
16    return
17  end
18
19  local selected = e.selected_prototype
20  if not selected then
21    return
22  end
23
24  if selected.base_type == "entity" and selected.name == "entity-ghost" then
25    -- Get ghost entity name
26    local real_selected = player.selected
27    if not real_selected or real_selected.name ~= "entity-ghost" then
28      return
29    end
30    selected.name = real_selected.ghost_name
31  end
32
33  local recipes = util.get_selected_recipes(selected)
34  if not recipes then
35    return
36  end
37
38  for _, recipe in pairs(recipes) do
39    local craftable_count = player.cheat_mode and math.huge or player.get_craftable_count(recipe.name) --[[@as uint]]
40    if craftable_count == 0 then
41      goto continue
42    end
43    local main_product = recipe.main_product or recipe.products[1]
44    if not main_product then
45      goto continue
46    end
47    local craft_count_setting = player.mod_settings["cen-quick-craft-count"].value --[[@as uint]]
48    local craft_count = math.min(craftable_count, math.ceil(craft_count_setting / main_product.amount) --[[@as uint]])
49    player.begin_crafting({ recipe = recipe.name, count = craft_count })
50    -- Language server and formatter don't like a label after break...
51    do
52      break
53    end
54    ::continue::
55  end
56end
57
58local quick_craft = {}
59
60quick_craft.events = {
61  ["cen-quick-craft"] = on_quick_craft,
62}
63
64return quick_craft