z_mirror/MouseOverConstruction

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

Caleb HeuerFix item duplication when constructing pyanodons wind turbines7c145b8

insertion
1.5 KiB65 linesraw
1--- @param player_index PlayerIndex
2local function cancel(player_index)
3  storage.repairing[player_index] = nil
4end
5
6--- @param player LuaPlayer
7--- @param entity LuaEntity
8--- @return boolean
9local function start(player, entity)
10  if not entity.valid then
11    return false
12  end
13
14  if not player.mod_settings["moc-enable-repairing"].value then
15    return false
16  end
17
18  if not entity.health or entity.health == entity.prototype.get_max_health(entity.quality) then
19    return false
20  end
21
22  storage.repairing[player.index] = entity.position
23  return true
24end
25
26local function on_tick()
27  if not storage.repairing then
28    return
29  end
30
31  for player_index, position in pairs(storage.repairing) do
32    local player = game.get_player(player_index)
33    if not player then
34      cancel(player_index)
35      goto continue
36    end
37    local entity = player.selected
38    if entity and entity.health and entity.health < entity.prototype.get_max_health(entity.quality) then
39      player.repair_state = { repairing = true, position = position }
40    else
41      cancel(player_index)
42    end
43    ::continue::
44  end
45end
46
47local function init()
48  --- @type table<PlayerIndex, MapPosition>
49  storage.repairing = {}
50end
51
52--- @class repair_handler : event_handler
53local repair_handler = {}
54
55repair_handler.on_init = init
56repair_handler.on_configuration_changed = init
57
58repair_handler.events = {
59  [defines.events.on_tick] = on_tick,
60}
61
62repair_handler.cancel = cancel
63repair_handler.start = start
64
65return repair_handler