z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
d5c13bd
1.14
1local table =require ( "__flib__/table" ) 2 3--- @class InventorySyncData 4--- @field filters table<integer, string?> 5--- @field hand_location uint? 6--- @field inventory LuaInventory 7 8--- @param player LuaPlayer 9local function pre_sync ( player ) 10local prefix =table . find ( defines . controllers , player . controller_type ) .. "_" 11local hand_location =player . hand_location or {} 12--- @type table<string, InventorySyncData> 13local sync_tables ={} 14for _ , name in ipairs ( { "cursor" , "main" , "guns" , "armor" , "ammo" } ) do 15--- @type table<integer, string?> 16local sync_filters ={} 17local sync_inventory 18local inventory_def =defines . inventory [ prefix .. name ] 19if name == "cursor" then 20sync_inventory =game . create_inventory ( 1 ) 21local cursor_stack =player . cursor_stack 22if cursor_stack and cursor_stack . valid_for_read then 23sync_inventory [ 1 ]. transfer_stack ( cursor_stack ) 24end 25elseif inventory_def then 26local source_inventory =player . get_inventory ( inventory_def ) --[[@as LuaInventory]] 27local get_filter =source_inventory . get_filter 28local set_filter =source_inventory . set_filter 29local supports_filters =source_inventory . supports_filters () 30local source_inventory_len =# source_inventory --[[@as uint16]] 31sync_inventory =game . create_inventory ( source_inventory_len ) 32for i =1 , source_inventory_len do 33--- @cast i uint 34sync_inventory [ i ]. transfer_stack ( source_inventory [ i ]) 35if supports_filters then 36sync_filters [ i ] =get_filter ( i ) 37set_filter ( i , nil ) 38end 39end 40end 41if sync_inventory then 42--- @type InventorySyncData 43sync_tables [ name ] ={ 44filters =sync_filters , 45hand_location =hand_location . inventory == inventory_def and hand_location . slot or nil , 46inventory =sync_inventory , 47} 48end 49end 50global . inventory_sync [ player . index ] =sync_tables 51end 52 53--- @param player LuaPlayer 54local function post_sync ( player ) 55-- determine prefix based on controller type 56local prefix =table . find ( defines . controllers , player . controller_type ) .. "_" 57-- iterate all inventories 58local sync_data =global . inventory_sync [ player . index ] 59if not sync_data then 60return 61end 62global . inventory_sync [ player . index ] =nil 63-- Cursor first to allow setting the hand, then armor to correct the inventory size 64for _ , name in ipairs ( { "cursor" , "armor" , "main" , "guns" , "ammo" } ) do 65local sync_table =sync_data [ name ] 66-- God mode doesn't have every inventory 67if not sync_table then 68goto continue 69end 70local sync_filters =sync_table . filters 71local sync_inventory =sync_table . inventory 72if name == "cursor" and player . cursor_stack then 73player . cursor_stack . transfer_stack ( sync_inventory [ 1 ]) 74else 75local inventory_def =defines . inventory [ prefix .. name ] 76if not inventory_def then 77goto continue 78end 79local destination_inventory =player . get_inventory ( inventory_def ) --[[@as LuaInventory]] 80local set_filter =destination_inventory . set_filter 81local supports_filters =destination_inventory . supports_filters () 82for i =1 , math . min ( # destination_inventory , # sync_inventory ) do 83--- @cast i uint 84if supports_filters then 85set_filter ( i , sync_filters [ i ]) 86end 87destination_inventory [ i ]. clear () 88destination_inventory [ i ]. transfer_stack ( sync_inventory [ i ]) 89end 90local hand_location =sync_table . hand_location 91if hand_location and hand_location <= # destination_inventory then 92player . hand_location ={ inventory =inventory_def , slot =hand_location } 93end 94end 95sync_inventory . destroy () 96:: continue :: 97end 98end 99 100--- @param e EventData.on_pre_player_toggled_map_editor 101local function on_pre_player_toggled_map_editor ( e ) 102local player =game . get_player ( e . player_index ) 103if not player or not player . cheat_mode then 104return 105end 106if not player . mod_settings [ "ee-inventory-sync" ]. value then 107return 108end 109pre_sync ( player ) 110end 111 112--- @param e EventData.on_player_toggled_map_editor 113local function on_player_toggled_map_editor ( e ) 114local player =game . get_player ( e . player_index ) 115if not player or not player . cheat_mode then 116return 117end 118if not player . mod_settings [ "ee-inventory-sync" ]. value then 119return 120end 121post_sync ( player ) 122end 123 124local inventory_sync ={} 125 126inventory_sync . on_init =function () 127--- @type table<uint, table<string, InventorySyncData?>> 128global . inventory_sync ={} 129end 130 131inventory_sync . events ={ 132[ defines . events . on_pre_player_toggled_map_editor ] =on_pre_player_toggled_map_editor , 133[ defines . events . on_player_toggled_map_editor ] =on_player_toggled_map_editor , 134} 135 136return inventory_sync