z_mirror/MouseOverConstruction

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

Caleb HeuerFix item duplication when constructing pyanodons wind turbines7c145b8

insertion
1.3 KiB60 linesraw
1--- @param player_index PlayerIndex
2local function cancel(player_index)
3  storage.deconstructing[player_index] = nil
4end
5
6--- @param player LuaPlayer
7--- @param entity LuaEntity
8--- @return boolean started
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-deconstruction"].value then
15    return false
16  end
17
18  if not entity.to_be_deconstructed() then
19    return false
20  end
21
22  storage.deconstructing[player.index] = entity.position
23  return true
24end
25
26local function on_tick()
27  if not storage.deconstructing then
28    return
29  end
30
31  for player_index, position in pairs(storage.deconstructing) do
32    local player = game.get_player(player_index)
33    if not player then
34      cancel(player_index)
35      goto continue
36    end
37    player.mining_state = { mining = true, position = position }
38    ::continue::
39  end
40end
41
42local function init()
43  --- @type table<PlayerIndex, MapPosition>
44  storage.deconstructing = {}
45end
46
47--- @class deconstruct_handler : event_handler
48local deconstruct_handler = {}
49
50deconstruct_handler.on_init = init
51deconstruct_handler.on_configuration_changed = init
52
53deconstruct_handler.events = {
54  [defines.events.on_tick] = on_tick,
55}
56
57deconstruct_handler.cancel = cancel
58deconstruct_handler.start = start
59
60return deconstruct_handler