z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
326e445
proxies
1local cursor ={} 2 3local event =require ( "__flib__.event" ) 4 5local player_data =require ( "scripts.player-data" ) 6 7function cursor . check_stack ( cursor_stack , cursor_ghost ) 8if cursor_stack and cursor_stack . valid_for_read then 9return cursor_stack . name 10elseif cursor_ghost then 11return cursor_ghost . name 12else 13return 14end 15end 16 17function cursor . set_stack ( player , cursor_stack , player_table , item_name ) 18local is_cheating =player . cheat_mode or ( player . controller_type == defines . controllers . editor ) 19local spawn_item =player_table . settings . spawn_items_when_cheating 20 21-- space exploration - don't spawn items when in the satellite view 22if script . active_mods [ "space-exploration" ] and player . controller_type == defines . controllers . god then 23spawn_item =false 24end 25 26-- main inventory 27player_data . ensure_valid_inventory ( player , player_table ) 28local main_inventory =player_table . main_inventory 29 30-- set cursor stack 31local item_stack , item_stack_index =main_inventory . find_item_stack ( item_name ) 32if item_stack and item_stack . valid then 33if player . clean_cursor () then 34-- actually transfer from the main inventory, then set the hand location 35cursor_stack . transfer_stack ( item_stack ) 36player . hand_location ={ inventory =main_inventory . index , slot =item_stack_index } 37end 38return true 39elseif spawn_item and is_cheating then 40local stack_spec ={ name =item_name , count =game . item_prototypes [ item_name ]. stack_size } 41-- insert into main inventory first, then transfer and set the hand location 42if main_inventory . can_insert ( stack_spec ) and player . clean_cursor () then 43main_inventory . insert ( stack_spec ) 44local new_stack , new_stack_index =main_inventory . find_item_stack ( item_name ) 45cursor_stack . transfer_stack ( new_stack ) 46player . hand_location ={ inventory =main_inventory . index , slot =new_stack_index } 47else 48player . { "cen-message.main-inventory-full" } 49end 50return true 51elseif player . clean_cursor () then 52player_table . last_item =item_name 53player . cursor_ghost =item_name 54return true 55end 56end 57 58function cursor . scroll ( player_index , direction ) 59local player =game . get_player ( player_index ) 60local player_table =global . players [ player_index ] 61local cursor_stack =player . cursor_stack 62local current_item =cursor . check_stack ( cursor_stack , player . cursor_ghost ) 63 64if current_item then 65-- check personal registry, then the global registry 66local registry =player_table . registry [ current_item ] 67if not registry or not registry [ direction ] then 68registry =global . registry [ current_item ] 69end 70-- set stack 71if registry and registry [ direction ] then 72cursor . set_stack ( player , cursor_stack , player_table , registry [ direction ]) 73end 74end 75end 76 77function cursor . create_proxy ( player , selected , item ) 78-- pass through request proxy 79if selected . type == "item-request-proxy" then 80selected =selected . proxy_target 81if not selected then return end 82end 83-- item info 84local prototype =game . item_prototypes [ item ] 85local item_name =prototype . localised_name 86local stack_size =prototype . stack_size 87-- flying text content 88local text 89-- proxy 90local proxy =global . proxies . by_target [ selected . unit_number ] 91 92-- create / don't create proxy 93if selected . can_insert { name =item } then 94if proxy then 95-- update proxy's request count 96local requests =proxy . item_requests 97local request_count =stack_size + ( requests [ item ] or 0 ) 98proxy . item_requests ={ table . unpack ( requests ), [ item ] =request_count } 99-- flying text 100text ={ "cen-message.updated-request-count" , request_count , item_name } 101else 102-- create the proxy 103proxy =selected . surface . create_entity { 104name ="item-request-proxy" , 105position =selected . position , 106force =selected . force , 107target =selected , 108modules ={ [ item ] =stack_size } 109} 110-- store global references 111global . proxies . by_target [ selected . unit_number ] =proxy 112global . proxies . by_proxy [ proxy . unit_number ] =selected . unit_number 113-- register to on_entity_destroyed 114event . register_on_entity_destroyed ( proxy ) 115-- flying text 116text ={ "cen-message.requesting-from-network" , stack_size , item_name } 117end 118else 119-- flying text 120text ={ "cen-message.cannot-request" , item_name } 121-- play sound 122player . play_sound { path ="utility/cannot_build" } 123end 124-- create flying text 125player . create_local_flying_text { 126text =text , 127position =selected . position , 128} 129end 130 131return cursor