z_mirror/CursorEnhancements

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

Caleb HeuerIgnore construction and logistic robots during auto-ghost-cursor54b4789

trunk
2.6 KiB118 linesraw
1local util = require("scripts.util")
2
3local vehicles = {
4  ["car"] = true,
5  ["locomotive"] = true,
6  ["spidertron"] = true,
7}
8
9local ignored_types = {
10  ["construction-robot"] = true,
11  ["logistic-robot"] = true,
12}
13
14--- @class BuiltItemData
15--- @field item ItemWithQualityID
16--- @field tick uint
17
18--- @param e EventData.on_built_entity
19local function on_built_entity(e)
20  local player = game.get_player(e.player_index)
21  if not player then
22    return
23  end
24
25  if not player.mod_settings["cen-auto-ghost-cursor"].value then
26    return
27  end
28
29  local built_item = storage.built_item[e.player_index]
30  if not built_item then
31    return
32  end
33  storage.built_item[e.player_index] = nil
34  if built_item.tick ~= game.tick then
35    return
36  end
37  if ignored_types[e.entity.type] then
38    return
39  end
40  if script.active_mods["folk-justgo"] and vehicles[e.entity.type] then
41    return
42  end
43
44  -- Don't do anything if the cursor stack is not empty
45  local cursor_stack = player.cursor_stack
46  if not cursor_stack or cursor_stack.valid_for_read then
47    return
48  end
49
50  util.set_cursor(player, built_item.item)
51end
52
53--- @param e EventData.on_pre_build
54local function on_pre_build(e)
55  local player = game.get_player(e.player_index)
56  if not player then
57    return
58  end
59
60  if not player.mod_settings["cen-auto-ghost-cursor"].value then
61    return
62  end
63
64  local cursor_stack = player.cursor_stack
65  if not cursor_stack or not cursor_stack.valid_for_read then
66    return
67  end
68
69  storage.built_item[e.player_index] =
70    { item = { name = cursor_stack.prototype, quality = cursor_stack.quality }, tick = game.tick }
71end
72
73--- @param e EventData.on_player_main_inventory_changed
74local function on_player_main_inventory_changed(e)
75  local player = game.get_player(e.player_index)
76  if not player then
77    return
78  end
79
80  if not player.mod_settings["cen-auto-ghost-cursor"].value then
81    return
82  end
83
84  local cursor_ghost = player.cursor_ghost
85  if not cursor_ghost then
86    return
87  end
88
89  local main_inventory = player.get_main_inventory()
90  if not main_inventory then
91    return
92  end
93
94  local count = main_inventory.get_item_count(cursor_ghost)
95  if count == 0 then
96    return
97  end
98  util.set_cursor(player, cursor_ghost)
99end
100
101local auto_ghost_cursor = {}
102
103auto_ghost_cursor.on_init = function()
104  --- @type table<uint, BuiltItemData?>
105  storage.built_item = {}
106end
107
108auto_ghost_cursor.on_configuration_changed = function()
109  storage.built_item = {}
110end
111
112auto_ghost_cursor.events = {
113  [defines.events.on_built_entity] = on_built_entity,
114  [defines.events.on_player_main_inventory_changed] = on_player_main_inventory_changed,
115  [defines.events.on_pre_build] = on_pre_build,
116}
117
118return auto_ghost_cursor