z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
898cd6b
1.14
1local util =require ( "__EditorExtensions__/scripts/util" ) 2 3local wagon_names ={ 4[ "ee-infinity-cargo-wagon" ] =true , 5[ "ee-infinity-fluid-wagon" ] =true , 6} 7 8--- @param player LuaPlayer 9--- @param entity LuaEntity 10local function open_gui ( player , entity ) 11player . opened =global . wagons [ entity . unit_number ]. proxy 12end 13 14--- @param e BuiltEvent 15local function on_entity_built ( e ) 16local entity =e . created_entity or e . entity or e . destination 17if not entity or not entity . valid or not wagon_names [ entity . name ] then 18return 19end 20 21local proxy =entity . surface . create_entity ( { 22name ="ee-infinity-wagon-" .. ( entity . name == "ee-infinity-cargo-wagon" and "chest" or "pipe" ), 23position =entity . position , 24force =entity . force , 25} ) 26if not proxy then 27return 28end 29 30-- Create all api lookups here to save time in on_tick() 31local data ={ 32flip =0 , 33proxy =proxy , 34proxy_fluidbox =proxy . fluidbox , 35proxy_inv =proxy . get_inventory ( defines . inventory . chest ), 36wagon =entity , 37wagon_fluidbox =entity . fluidbox , 38wagon_inv =entity . get_inventory ( defines . inventory . cargo_wagon ), 39wagon_last_position =entity . position , 40wagon_name =entity . name , 41} 42global . wagons [ entity . unit_number ] =data 43 44-- Apply any pre-existing filters 45local tags =e . tags 46if not tags or not tags . EditorExtensions then 47return 48end 49local tags =tags . EditorExtensions 50if entity . name == "ee-infinity-cargo-wagon" then 51-- LEGACY: Before v1.9.16, we did not store the `remove unfiltered items` setting 52if tags . filters then 53data . proxy . infinity_container_filters =tags . filters 54data . proxy . remove_unfiltered_items =tags . remove_unfiltered_items 55else 56--- @cast tags InfinityInventoryFilter[] 57data . proxy . infinity_container_filters =tags 58end 59elseif entity . name == "ee-infinity-fluid-wagon" then 60--- @cast tags InfinityPipeFilter 61proxy . set_infinity_pipe_filter ( tags ) 62end 63end 64 65--- @param e DestroyedEvent 66local function on_entity_destroyed ( e ) 67local entity =e . entity 68if not entity or not entity . valid or not wagon_names [ entity . name ] then 69return 70end 71 72local proxy =global . wagons [ entity . unit_number ]. proxy 73if proxy and proxy . valid then 74proxy . destroy () 75end 76global . wagons [ entity . unit_number ] =nil 77end 78 79--- @param e EventData.on_marked_for_deconstruction 80local function on_marked_for_deconstruction ( e ) 81local entity =e . entity 82if not entity . valid or entity . name ~= "ee-infinity-cargo-wagon" then 83return 84end 85global . wagons [ entity . unit_number ]. flip =3 86entity . get_inventory ( defines . inventory . cargo_wagon ). clear () 87end 88 89--- @param e EventData.on_cancelled_deconstruction 90local function on_cancelled_deconstruction ( e ) 91local entity =e . entity 92if not entity . valid or entity . name ~= "ee-infinity-cargo-wagon" then 93return 94end 95-- Resume syncing 96global . wagons [ entity . unit_number ]. flip =0 97end 98 99local function on_tick () 100local abs =math . abs 101for _ , t in pairs ( global . wagons ) do 102if t . wagon . valid and t . proxy . valid then 103if t . wagon_name == "ee-infinity-cargo-wagon" then 104if t . flip == 0 then 105t . wagon_inv . clear () 106for n , c in pairs ( t . proxy_inv . get_contents ()) do 107t . wagon_inv . insert ( { name =n , count =c } ) 108end 109t . flip =1 110elseif t . flip == 1 then 111t . proxy_inv . clear () 112for n , c in pairs ( t . wagon_inv . get_contents ()) do 113t . proxy_inv . insert ( { name =n , count =c } ) 114end 115t . flip =0 116end 117elseif t . wagon_name == "ee-infinity-fluid-wagon" then 118if t . flip == 0 then 119local fluid =t . proxy_fluidbox [ 1 ] 120t . wagon_fluidbox [ 1 ] =fluid 121and fluid . amount > 0 122and { 123name =fluid . name , 124amount =( abs ( fluid . amount ) * 250 ), 125temperature =fluid . temperature , 126} 127or nil 128t . flip =1 129elseif t . flip == 1 then 130local fluid =t . wagon_fluidbox [ 1 ] 131t . proxy_fluidbox [ 1 ] =fluid 132and fluid . amount > 0 133and { 134name =fluid . name , 135amount =( abs ( fluid . amount ) / 250 ), 136temperature =fluid . temperature , 137} 138or nil 139t . flip =0 140end 141end 142local position =t . wagon . position 143local last_position =t . wagon_last_position 144if last_position . x ~= position . x or last_position . y ~= position . y then 145t . proxy . teleport ( t . wagon . position ) 146t . wagon_last_position =last_position 147end 148end 149end 150end 151 152--- @param e EventData.on_gui_opened 153local function on_gui_opened ( e ) 154local player =game . get_player ( e . player_index ) 155if not player then 156return 157end 158local entity =e . entity 159if not entity or not entity . valid or not wagon_names [ entity . name ] then 160return 161end 162open_gui ( player , entity ) 163end 164 165--- @param e EventData.CustomInputEvent 166local function on_linked_open_gui ( e ) 167local player =game . get_player ( e . player_index ) 168if not player then 169return 170end 171local entity =player . selected 172if not entity or entity . name ~= "ee-infinity-fluid-wagon" then 173return 174end 175if not player . can_reach_entity ( entity ) then 176return 177end 178open_gui ( player , entity ) 179end 180 181--- @param e EventData.on_entity_settings_pasted 182local function on_entity_settings_pasted ( e ) 183local source , destination =e . source , e . destination 184if not source . valid or not destination . valid then 185return 186end 187if not wagon_names [ source . name ] or not wagon_names [ destination . name ] then 188return 189end 190if source . name ~= destination . name then 191return 192end 193 194global . wagons [ destination . unit_number ]. proxy . copy_settings ( global . wagons [ source . unit_number ]. proxy ) 195end 196 197--- @param e EventData.on_player_setup_blueprint 198local function on_player_setup_blueprint ( e ) 199local blueprint =util . get_blueprint ( e ) 200if not blueprint then 201return 202end 203 204local entities =blueprint . get_blueprint_entities () 205if not entities then 206return 207end 208for i , entity in pairs ( entities ) do 209--- @cast i uint 210if not wagon_names [ entity . name ] then 211goto continue 212end 213local real_entity =e . surface . find_entity ( entity . name , entity . position ) 214if not real_entity then 215goto continue 216end 217local proxy =global . wagons [ real_entity . unit_number ]. proxy 218--- @type InfinityPipeFilter|InfinityInventoryFilter[]? 219local tags ={} 220if entity . name == "ee-infinity-cargo-wagon" then 221tags . filters =proxy . infinity_container_filters 222tags . remove_unfiltered_items =proxy . remove_unfiltered_items 223else 224tags =proxy . get_infinity_pipe_filter () 225end 226blueprint . set_blueprint_entity_tag ( i , "EditorExtensions" , tags --[[@as AnyBasic]] ) 227:: continue :: 228end 229end 230 231local infinity_wagon ={} 232 233infinity_wagon . on_init =function () 234global . wagons ={} 235end 236 237infinity_wagon . events ={ 238[ defines . events . on_built_entity ] =on_entity_built , 239[ defines . events . on_cancelled_deconstruction ] =on_cancelled_deconstruction , 240[ defines . events . on_entity_cloned ] =on_entity_built , 241[ defines . events . on_entity_died ] =on_entity_destroyed , 242[ defines . events . on_entity_settings_pasted ] =on_entity_settings_pasted , 243[ defines . events . on_gui_opened ] =on_gui_opened , 244[ defines . events . on_marked_for_deconstruction ] =on_marked_for_deconstruction , 245[ defines . events . on_player_mined_entity ] =on_entity_destroyed , 246[ defines . events . on_player_setup_blueprint ] =on_player_setup_blueprint , 247[ defines . events . on_robot_built_entity ] =on_entity_built , 248[ defines . events . on_robot_mined_entity ] =on_entity_destroyed , 249[ defines . events . on_tick ] =on_tick , 250[ defines . events . script_raised_built ] =on_entity_built , 251[ defines . events . script_raised_destroy ] =on_entity_destroyed , 252[ defines . events . script_raised_revive ] =on_entity_built , 253[ "ee-linked-open-gui" ] =on_linked_open_gui , 254} 255 256return infinity_wagon