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