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