z_mirror/MouseOverConstruction

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

Caleb HeuerFix item duplication when constructing pyanodons wind turbines7c145b8

insertion
1.4 KiB50 linesraw
1local common = require("scripts.common")
2
3--- @param player LuaPlayer
4--- @param entity LuaEntity
5--- @return boolean succeeded
6return function(player, entity)
7  if not entity.valid then
8    return false
9  end
10
11  if not player.mod_settings["moc-enable-upgrading"].value then
12    return false
13  end
14
15  if not entity.to_be_upgraded() then
16    return false
17  end
18
19  local upgrade_prototype, upgrade_quality = entity.get_upgrade_target()
20  if not upgrade_prototype then
21    return false
22  end
23  --- @cast upgrade_quality -?
24
25  if not common.check_can_place_entity(player, entity, upgrade_prototype) then
26    return false
27  end
28
29  --- @type LuaEntity?
30  local underground_neighbour = entity.type == "underground-belt" and entity.neighbours or nil
31
32  local upgraded = common.build(player, entity, upgrade_prototype, upgrade_quality)
33  if not upgraded then
34    return false
35  end
36
37  if not underground_neighbour or not underground_neighbour.valid then
38    -- Still return true because the selected entity was upgraded.
39    return true
40  end
41
42  local neighbour_upgrade_prototype, neighbour_upgrade_quality = underground_neighbour.get_upgrade_target()
43  if neighbour_upgrade_prototype then
44    --- @cast neighbour_upgrade_quality -?
45    -- Upgrading underground neighbours doesn't obey the usual build distance restrictions, so no need to check it here.
46    common.build(player, underground_neighbour, neighbour_upgrade_prototype, neighbour_upgrade_quality)
47  end
48
49  return true
50end