z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
0b3b558
trunk
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 () 7for type , prototypes in pairs ( { 8entity =prototypes . entity , 9equipment =prototypes . equipment , 10equipment_category =prototypes . equipment_category , 11fluid =prototypes . fluid , 12fuel_category =prototypes . fuel_category , 13item =prototypes . item , 14item_group =prototypes . item_group , 15recipe =prototypes . recipe , 16recipe_category =prototypes . recipe_category , 17resource_category =prototypes . resource_category , 18technology =prototypes . technology , 19} ) do 20flib_dictionary . new ( type ) 21for 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. 24flib_dictionary . add ( type , name , { "?" , prototype . localised_name , name } ) 25end 26end 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. 35flib_dictionary . on_init () 36build_dictionaries () 37end ) 38 39script . on_configuration_changed ( function () 40flib_dictionary . on_configuration_changed () 41-- Dictionaries should be built both during `on_init` and `on_configuration_changed`. 42build_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. 51local dicts =flib_dictionary . get_all ( e . player_index ) 52if not dicts then 53return 54end 55if __DebugAdapter then 56__DebugAdapter . ( dicts ) 57else 58log ( serpent . block ( dicts )) 59end 60end )