z_mirror/CursorEnhancements

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

Caleb HeuerFixesb1fdad5

trunk
3.4 KiB120 linesraw
1--- @class Util
2local util = {}
3
4--- @param player LuaPlayer
5--- @return ItemWithQualityID?
6function util.get_cursor_item(player)
7  local cursor_ghost = player.cursor_ghost
8  if cursor_ghost then
9    return cursor_ghost
10  end
11  local cursor_stack = player.cursor_stack
12  if cursor_stack and cursor_stack.valid_for_read then
13    return { name = cursor_stack.prototype, quality = cursor_stack.quality }
14  end
15end
16
17--- @param selected SelectedPrototypeData?
18--- @return string?
19function util.get_selected_item(selected)
20  if not selected then
21    return
22  end
23  local type = selected.base_type
24  if type == "item" then
25    return selected.name
26  elseif type == "entity" then
27    local entity = prototypes.entity[selected.name]
28    local item = (entity.items_to_place_this or {})[1]
29    if item then
30      return item.name
31    end
32  elseif type == "recipe" then
33    local recipe = prototypes.recipe[selected.name]
34    local product = recipe.products[1]
35    if product and product.type == "item" then
36      return product.name
37    end
38  end
39end
40
41--- @param selected SelectedPrototypeData?
42--- @return LuaCustomTable<string, LuaRecipePrototype>?
43function util.get_selected_recipes(selected)
44  if not selected then
45    return
46  end
47  if selected.base_type == "recipe" then
48    return { prototypes.recipe[selected.name] }
49  end
50
51  local item = util.get_selected_item(selected)
52  if not item then
53    return
54  end
55
56  return prototypes.get_recipe_filtered({
57    { filter = "has-product-item", elem_filters = { { filter = "name", name = item } } },
58  })
59end
60
61--- @param player LuaPlayer
62--- @return boolean
63function util.is_cheating(player)
64  if script.active_mods["brave-new-world"] then
65    return false
66  end
67  if
68    player.cheat_mode
69    and not (player.controller_type == defines.controllers.god and script.active_mods["space-exploration"])
70  then
71    return true
72  end
73  return player.controller_type == defines.controllers.editor
74end
75
76--- @param player LuaPlayer
77--- @param item ItemWithQualityID
78--- @return boolean success
79function util.set_cursor(player, item)
80  if player.controller_type == defines.controllers.remote then
81    player.cursor_ghost = item
82    return true
83  end
84  local inventory = player.get_main_inventory()
85  if not inventory then
86    return false
87  end
88  local cursor_stack = player.cursor_stack
89  if not cursor_stack or not player.clear_cursor() then
90    return false
91  end
92  local inventory_stack, stack_index = inventory.find_item_stack(item)
93  if not inventory_stack or not stack_index then
94    local spec = { name = item.name.name, quality = item.quality, count = item.name.stack_size }
95    if util.is_cheating(player) and inventory.can_insert(spec) then
96      inventory.insert(spec)
97      inventory_stack, stack_index = inventory.find_item_stack(item)
98    else
99      player.cursor_ghost = item
100      return true
101    end
102  end
103  --- @cast inventory_stack LuaItemStack
104  --- @cast stack_index uint
105  if not cursor_stack.transfer_stack(inventory_stack) then
106    return false
107  end
108  local inventory_def
109  if player.controller_type == defines.controllers.character then
110    inventory_def = defines.inventory.character_main
111  elseif player.controller_type == defines.controllers.editor then
112    inventory_def = defines.inventory.editor_main
113  elseif player.controller_type == defines.controllers.god then
114    inventory_def = defines.inventory.god_main
115  end
116  player.hand_location = { inventory = inventory_def, slot = stack_index }
117  return true
118end
119
120return util