z_mirror/CursorEnhancements

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

Caleb HeuerMove scripts into a scripts subfolderb4ff864

smart-quickbar
2.3 KiB100 linesraw
1local util = require("__CursorEnhancements__/scripts/util")
2
3--- @class BuiltItemData
4--- @field item string
5--- @field tick uint
6
7--- @param e EventData.on_built_entity
8local function on_built_entity(e)
9  local player = game.get_player(e.player_index)
10  if not player then
11    return
12  end
13
14  if not player.mod_settings["cen-auto-ghost-cursor"].value then
15    return
16  end
17
18  local built_item = global.built_item[e.player_index]
19  if not built_item then
20    return
21  end
22  global.built_item[e.player_index] = nil
23  if built_item.tick ~= game.tick then
24    return
25  end
26
27  -- Don't do anything if the cursor stack is not empty
28  local cursor_stack = player.cursor_stack
29  if not cursor_stack or cursor_stack.valid_for_read then
30    return
31  end
32
33  util.set_cursor(player, built_item.item)
34end
35
36--- @param e EventData.on_pre_build
37local function on_pre_build(e)
38  local player = game.get_player(e.player_index)
39  if not player then
40    return
41  end
42
43  if not player.mod_settings["cen-auto-ghost-cursor"].value then
44    return
45  end
46
47  local cursor_stack = player.cursor_stack
48  if not cursor_stack or not cursor_stack.valid_for_read then
49    return
50  end
51
52  global.built_item[e.player_index] = { item = cursor_stack.name, tick = game.tick }
53end
54
55--- @param e EventData.on_player_main_inventory_changed
56local function on_player_main_inventory_changed(e)
57  local player = game.get_player(e.player_index)
58  if not player then
59    return
60  end
61
62  if not player.mod_settings["cen-auto-ghost-cursor"].value then
63    return
64  end
65
66  local cursor_ghost = player.cursor_ghost
67  if not cursor_ghost then
68    return
69  end
70
71  local main_inventory = player.get_main_inventory()
72  if not main_inventory then
73    return
74  end
75
76  local count = main_inventory.get_item_count(cursor_ghost.name)
77  if count == 0 then
78    return
79  end
80  util.set_cursor(player, cursor_ghost.name)
81end
82
83local auto_ghost_cursor = {}
84
85auto_ghost_cursor.on_init = function()
86  --- @type table<uint, BuiltItemData?>
87  global.built_item = {}
88end
89
90auto_ghost_cursor.on_configuration_changed = function()
91  global.built_item = {}
92end
93
94auto_ghost_cursor.events = {
95  [defines.events.on_built_entity] = on_built_entity,
96  [defines.events.on_player_main_inventory_changed] = on_player_main_inventory_changed,
97  [defines.events.on_pre_build] = on_pre_build,
98}
99
100return auto_ghost_cursor