z_mirror/CursorEnhancements

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

raiguardCleanup326e445

proxies
4.8 KiB157 linesraw
1local event = require("__flib__.event")
2local migration = require("__flib__.migration")
3
4local constants = require("constants")
5
6local cursor = require("scripts.cursor")
7local global_data = require("scripts.global-data")
8local migrations = require("scripts.migrations")
9local player_data = require("scripts.player-data")
10local remote_interface = require("scripts.remote-interface")
11
12-- -----------------------------------------------------------------------------
13-- EVENT HANDLERS
14
15-- BOOTSTRAP
16
17event.on_init(function()
18  global_data.init()
19  for i, player in pairs(game.players) do
20    player_data.init(i, player)
21    player_data.refresh(player, global.players[i])
22  end
23end)
24
25event.on_configuration_changed(function(e)
26  if migration.on_config_changed(e, migrations) then
27    global_data.build_global_registry()
28    for i, player in pairs(game.players) do
29      player_data.update_personal_overrides(player, global.players[i])
30    end
31  end
32end)
33
34-- ENTITY
35
36event.on_entity_destroyed(function(e)
37  -- proxies are all we care about and they always have unit numbers
38  if e.unit_number then
39    local proxies = global.proxies
40    local target_unit_number = proxies.by_proxy[e.unit_number]
41    if target_unit_number then
42      proxies.by_proxy[e.unit_number] = nil
43      proxies.by_target[target_unit_number] = nil
44    end
45  end
46end)
47
48-- INPUTS
49
50event.register(constants.item_scroll_input_names, function(e)
51  local _, _, direction = string.find(e.input_name, "^cen-%-scroll%-(%a*)$")
52  cursor.scroll(e.player_index, direction)
53end)
54
55event.register("cen-recall-last-item", function(e)
56  local player = game.get_player(e.player_index)
57  local player_table = global.players[e.player_index]
58  if player_table.last_item
59    and not cursor.set_stack(player, player.cursor_stack, player_table, player_table.last_item)
60  then
61    player.print{"cen-message.unable-to-recall"}
62  end
63end)
64
65event.register("cen-transfer-stack-from-network", function(e)
66  local player = game.get_player(e.player_index)
67  local selected = player.selected
68  local item = cursor.check_stack(player.cursor_stack, player.cursor_ghost)
69  if selected and selected.force == player.force and item then
70    cursor.create_proxy(player, selected, item)
71  end
72end)
73
74-- PLAYER
75
76event.on_player_created(function(e)
77  local player = game.get_player(e.player_index)
78  player_data.init(e.player_index, player)
79  local player_table = global.players[e.player_index]
80  player_data.refresh(player, player_table)
81end)
82
83event.on_player_removed(function(e)
84  global.players[e.player_index] = nil
85end)
86
87event.on_put_item(function(e)
88  global.players[e.player_index].flags.building = true
89end)
90
91event.on_player_cursor_stack_changed(function(e)
92  local player = game.get_player(e.player_index)
93  local player_table = global.players[e.player_index]
94
95  -- check `building` flag
96  -- only proceed if it is true
97  if player_table.flags.building then
98    -- reset the flag
99    player_table.flags.building = false
100
101    local cursor_stack = player.cursor_stack
102    local current_item = cursor_stack and cursor_stack.valid_for_read and cursor_stack.name
103
104    if current_item then
105      player_table.last_item = current_item
106    elseif
107      player_table.settings.ghost_cursor_transitions
108      and not player.cursor_ghost
109    then
110      local last_item = player_table.last_item
111      if last_item then
112        player_data.ensure_valid_inventory(player, player_table)
113        if player_table.main_inventory.get_item_count(last_item) == 0 then
114          local entity = game.item_prototypes[last_item].place_result
115          if entity then
116            cursor.set_stack(player, cursor_stack, player_table, last_item)
117          end
118        end
119      end
120    end
121  end
122end)
123
124event.on_player_main_inventory_changed(function(e)
125  local player = game.get_player(e.player_index)
126  local player_table = global.players[e.player_index]
127
128  if player_table.settings.ghost_cursor_transitions then
129    local cursor_ghost = player.cursor_ghost
130
131    if cursor_ghost then
132      player_data.ensure_valid_inventory(player, player_table)
133      local main_inventory = player_table.main_inventory
134      if main_inventory.get_item_count(cursor_ghost.name) > 0 then
135        cursor.set_stack(player, player.cursor_stack, player_table, cursor_ghost.name)
136      end
137    end
138  end
139end)
140
141-- SETTINGS
142
143event.on_runtime_mod_setting_changed(function(e)
144  if string.sub(e.setting, 1, 4) == "cen-" and e.setting_type == "runtime-per-user" then
145    local player = game.get_player(e.player_index)
146    local player_table = global.players[e.player_index]
147    player_data.update_settings(player, player_table)
148    if e.setting == "cen-personal-registry-overrides" then
149      player_data.update_personal_overrides(player, player_table)
150    end
151  end
152end)
153
154-- -----------------------------------------------------------------------------
155-- REMOTE INTERFACE
156
157remote.add_interface("CursorEnhancements", remote_interface)