z_mirror/flib

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

Caleb HeuerRestore old dictionary "test" (demo)0b3b558

trunk
2.7 KiB60 linesraw
1local flib_dictionary = require("__flib__.dictionary-lite")
2
3-- Build demo dictionaries for various kinds of prototypes. Dictionaries don't need to be sorted like this, they can
4-- contain anything and everything. For example, in many of my mods I will make a "search" dictionary containing all
5-- of the things I need to search, prefixed by their type (i.e. `fluid/crude-oil`, `item/iron-plate`, etc).
6local function build_dictionaries()
7  for type, prototypes in pairs({
8    entity = prototypes.entity,
9    equipment = prototypes.equipment,
10    equipment_category = prototypes.equipment_category,
11    fluid = prototypes.fluid,
12    fuel_category = prototypes.fuel_category,
13    item = prototypes.item,
14    item_group = prototypes.item_group,
15    recipe = prototypes.recipe,
16    recipe_category = prototypes.recipe_category,
17    resource_category = prototypes.resource_category,
18    technology = prototypes.technology,
19  }) do
20    flib_dictionary.new(type)
21    for name, prototype in pairs(prototypes) do
22      -- Fall back to the internal name if the localised name is invalid. If you don't want to include invalid strings
23      -- then simply pass `prototype.localised_name` and the resulting dictionary will only include valid translations.
24      flib_dictionary.add(type, name, { "?", prototype.localised_name, name })
25    end
26  end
27end
28
29-- Handle all relevant events automatically. Be sure to add this line before defining any of your own event handlers.
30-- `dictionary-lite` has handlers for `on_init`, `on_configuration_changed`, `on_tick`, `on_string_translated`, and `on_player_joined_game`.
31flib_dictionary.handle_events()
32
33script.on_init(function()
34  -- If you override any handlers for the events listed above, you will need to manually call the requisite functions.
35  flib_dictionary.on_init()
36  build_dictionaries()
37end)
38
39script.on_configuration_changed(function()
40  flib_dictionary.on_configuration_changed()
41  -- Dictionaries should be built both during `on_init` and `on_configuration_changed`.
42  build_dictionaries()
43end)
44
45-- `on_player_dictionaries_ready` is raised when a given player's dictionaries are... ready. Shocking!
46--- @param e flib.on_player_dictionaries_ready
47script.on_event(flib_dictionary.on_player_dictionaries_ready, function(e)
48  -- Alternatively, you can get specific dictionaries with `flib_dictionary.get(e.player_index, "item")` et al.
49  -- For the aforementioned "search" dictionary, I do not handle this event, and instead call `get()` during my
50  -- search logic and use it if it exists, otherwise falling back to the internal prototype names.
51  local dicts = flib_dictionary.get_all(e.player_index)
52  if not dicts then
53    return
54  end
55  if __DebugAdapter then
56    __DebugAdapter.print(dicts)
57  else
58    log(serpent.block(dicts))
59  end
60end)