z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
f44d975
1.14
1local direction_util =require ( "__flib__/direction" ) 2local position =require ( "__flib__/position" ) 3 4--- @type table<defines.direction, Vector> 5local offsets ={ 6[ defines . direction . north ] ={ 0 , - 1 } , 7[ defines . direction . east ] ={ 1 , 0 } , 8[ defines . direction . south ] ={ 0 , 1 } , 9[ defines . direction . west ] ={ - 1 , 0 } , 10} 11 12local transport_belt_connectables ={ 13"transport-belt" , 14"underground-belt" , 15"splitter" , 16"loader" , 17"loader-1x1" , 18"linked-belt" , 19} 20 21--- @param entity LuaEntity 22local function snap ( entity ) 23local offset_direction =entity . direction 24if entity . loader_type == "input" then 25offset_direction =direction_util . opposite ( offset_direction ) 26end 27local belt_position =position . add ( entity . position , offsets [ offset_direction ]) 28local belt = 29entity . surface . find_entities_filtered ( { position =belt_position , type =transport_belt_connectables } )[ 1 ] 30if not belt then 31belt = 32entity . surface . find_entities_filtered ( { position =belt_position , ghost_type =transport_belt_connectables } )[ 1 ] 33end 34if not belt then 35return 36end 37if belt . direction == direction_util . opposite ( entity . direction ) then 38entity . loader_type =entity . loader_type == "output" and "input" or "output" 39end 40end 41 42--- @param entity LuaEntity 43--- @param chest LuaEntity? 44local function sync_chest_filter ( entity , chest ) 45if not chest then 46chest =entity . surface . find_entity ( "ee-infinity-loader-chest" , entity . position ) 47end 48if not chest then 49entity . destroy () 50return 51end 52local filter =entity . get_filter ( 1 ) 53if filter then 54chest . set_infinity_container_filter ( 1 , { 55index =1 , 56name =filter , 57count =game . item_prototypes [ filter ]. stack_size * 5 , 58mode ="exactly" , 59} ) 60else 61chest . set_infinity_container_filter ( 1 , nil ) 62end 63end 64 65--- @param e BuiltEvent 66local function on_built ( e ) 67local entity =e . entity or e . created_entity or e . destination 68if not entity . valid then 69return 70end 71 72if entity . name ~= "ee-infinity-loader" then 73return 74end 75 76-- Create chest 77local chest =entity . surface . create_entity ( { 78name ="ee-infinity-loader-chest" , 79position =entity . position , 80force =entity . force , 81create_build_effect_smoke =false , 82raise_built =true , 83} ) 84 85if not chest then 86entity . destroy () 87return 88end 89 90chest . remove_unfiltered_items =true 91sync_chest_filter ( entity , chest ) 92snap ( entity ) 93end 94 95--- @param e DestroyedEvent 96local function on_destroyed ( e ) 97local entity =e . entity 98if not entity . valid or entity . name ~= "ee-infinity-loader" then 99return 100end 101local chest =entity . surface . find_entity ( "ee-infinity-loader-chest" , entity . position ) 102if chest then 103chest . destroy ( { raise_destroy =true } ) 104end 105end 106 107--- @param e EventData.on_player_rotated_entity 108local function on_rotated ( e ) 109local entity =e . entity 110if not entity . valid or entity . name ~= "ee-infinity-loader" then 111return 112end 113sync_chest_filter ( entity ) 114end 115 116--- @param e EventData.on_entity_settings_pasted 117local function on_settings_pasted ( e ) 118local destination =e . destination 119if not destination . valid or destination . name ~= "ee-infinity-loader" then 120return 121end 122sync_chest_filter ( destination ) 123end 124 125--- @param e EventData.on_gui_opened 126local function on_gui_opened ( e ) 127if e . gui_type ~= defines . gui_type . entity then 128return 129end 130local entity =e . entity --[[@as LuaEntity]] 131if entity . name == "ee-infinity-loader" then 132global . infinity_loader_open [ e . player_index ] =entity 133end 134end 135 136--- @param e EventData.on_gui_closed 137local function on_gui_closed ( e ) 138if e . gui_type ~= defines . gui_type . entity then 139return 140end 141local loader =global . infinity_loader_open [ e . player_index ] 142if loader and loader . valid then 143sync_chest_filter ( loader ) 144global . infinity_loader_open [ e . player_index ] =nil 145end 146end 147 148local infinity_loader ={} 149 150function infinity_loader . on_init () 151--- @type table<uint, LuaEntity> 152global . infinity_loader_open ={} 153end 154 155infinity_loader . events ={ 156[ defines . events . on_built_entity ] =on_built , 157[ defines . events . on_entity_cloned ] =on_built , 158[ defines . events . on_entity_died ] =on_destroyed , 159[ defines . events . on_entity_settings_pasted ] =on_settings_pasted , 160[ defines . events . on_gui_closed ] =on_gui_closed , 161[ defines . events . on_gui_opened ] =on_gui_opened , 162[ defines . events . on_player_mined_entity ] =on_destroyed , 163[ defines . events . on_player_rotated_entity ] =on_rotated , 164[ defines . events . on_robot_built_entity ] =on_built , 165[ defines . events . on_robot_mined_entity ] =on_destroyed , 166[ defines . events . script_raised_built ] =on_built , 167[ defines . events . script_raised_destroy ] =on_destroyed , 168[ defines . events . script_raised_revive ] =on_built , 169} 170 171infinity_loader . on_nth_tick ={ 172[ 15 ] =function () 173for _ , loader in pairs ( global . infinity_loader_open ) do 174sync_chest_filter ( loader ) 175end 176end , 177} 178 179return infinity_loader