z_mirror/Krastorio2

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

Caleb HeuerFix util.entity_flying_textb9d041a3

discrete-steel-pipes
2.6 KiB106 linesraw
1local util = {}
2
3--- @class FlyingTextOptions
4--- @field color Color?
5--- @field position MapPosition?
6--- @field sound LuaPlayer.play_sound_param?
7
8--- @param player LuaPlayer
9--- @param text LocalisedString
10--- @param options FlyingTextOptions?
11function util.flying_text_with_sound(player, text, options)
12  options = options or {}
13  options.sound = options.sound or { path = "utility/cannot_build" }
14  player.create_local_flying_text({
15    color = options.color,
16    create_at_cursor = not options.position,
17    position = options.position,
18    text = text,
19  })
20  player.play_sound(options.sound)
21end
22
23--- @param entity LuaEntity
24--- @param text LocalisedString
25--- @param color Color?
26function util.entity_flying_text(entity, text, color)
27  for _, player in pairs(game.players) do
28    if player.surface == entity.surface then
29      player.create_local_flying_text({
30        text = text,
31        color = color,
32        position = entity.position,
33      })
34    end
35  end
36end
37
38--- @param entity LuaEntity
39--- @param text LocalisedString
40--- @param color Color
41function util.change_mode_fx(entity, text, color)
42  -- Flying text
43  util.entity_flying_text(entity, text, color)
44  -- Welding sound
45  game.play_sound({
46    path = "kr-welding",
47    position = entity.position,
48    volume_modifier = 1.0,
49  })
50  -- Welding particle
51  entity.surface.create_particle({
52    name = "kr-welding-particle",
53    position = { entity.position.x, entity.position.y + 1 },
54    movement = { 0.0, -0.05 },
55    height = 1.0,
56    vertical_speed = 0.015,
57    frame_speed = 1,
58  })
59end
60
61--- @param commands_list table<string, function>
62function util.add_commands(commands_list)
63  for name, func in pairs(commands_list) do
64    commands.add_command(name, { "command-help." .. name }, function(e)
65      local player = game.get_player(e.player_index) --[[@as LuaPlayer]]
66      if not player.admin then
67        player.print({ "cant-run-command-not-admin", name })
68        return
69      end
70
71      func(e)
72    end)
73  end
74end
75
76--- @param e EventData.on_player_setup_blueprint
77--- @return LuaItemStack?
78function util.get_blueprint(e)
79  local player = game.get_player(e.player_index)
80  if not player then
81    return
82  end
83
84  local bp = player.blueprint_to_setup
85  if bp and bp.valid_for_read then
86    return bp
87  end
88
89  bp = player.cursor_stack
90  if not bp or not bp.valid_for_read then
91    return
92  end
93
94  if bp.type == "blueprint-book" then
95    local item_inventory = bp.get_inventory(defines.inventory.item_main)
96    if item_inventory then
97      bp = item_inventory[bp.active_index]
98    else
99      return
100    end
101  end
102
103  return bp
104end
105
106return util