z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
326e445
proxies
1local event =require ( "__flib__.event" ) 2local migration =require ( "__flib__.migration" ) 3 4local constants =require ( "constants" ) 5 6local cursor =require ( "scripts.cursor" ) 7local global_data =require ( "scripts.global-data" ) 8local migrations =require ( "scripts.migrations" ) 9local player_data =require ( "scripts.player-data" ) 10local remote_interface =require ( "scripts.remote-interface" ) 11 12-- ----------------------------------------------------------------------------- 13-- EVENT HANDLERS 14 15-- BOOTSTRAP 16 17event . on_init ( function () 18global_data . init () 19for i , player in pairs ( game . players ) do 20player_data . init ( i , player ) 21player_data . refresh ( player , global . players [ i ]) 22end 23end ) 24 25event . on_configuration_changed ( function ( e ) 26if migration . on_config_changed ( e , migrations ) then 27global_data . build_global_registry () 28for i , player in pairs ( game . players ) do 29player_data . update_personal_overrides ( player , global . players [ i ]) 30end 31end 32end ) 33 34-- ENTITY 35 36event . on_entity_destroyed ( function ( e ) 37-- proxies are all we care about and they always have unit numbers 38if e . unit_number then 39local proxies =global . proxies 40local target_unit_number =proxies . by_proxy [ e . unit_number ] 41if target_unit_number then 42proxies . by_proxy [ e . unit_number ] =nil 43proxies . by_target [ target_unit_number ] =nil 44end 45end 46end ) 47 48-- INPUTS 49 50event . register ( constants . item_scroll_input_names , function ( e ) 51local _ , _ , direction =string . find ( e . input_name , "^cen-%-scroll%-(%a*)$" ) 52cursor . scroll ( e . player_index , direction ) 53end ) 54 55event . register ( "cen-recall-last-item" , function ( e ) 56local player =game . get_player ( e . player_index ) 57local player_table =global . players [ e . player_index ] 58if player_table . last_item 59and not cursor . set_stack ( player , player . cursor_stack , player_table , player_table . last_item ) 60then 61player . { "cen-message.unable-to-recall" } 62end 63end ) 64 65event . register ( "cen-transfer-stack-from-network" , function ( e ) 66local player =game . get_player ( e . player_index ) 67local selected =player . selected 68local item =cursor . check_stack ( player . cursor_stack , player . cursor_ghost ) 69if selected and selected . force == player . force and item then 70cursor . create_proxy ( player , selected , item ) 71end 72end ) 73 74-- PLAYER 75 76event . on_player_created ( function ( e ) 77local player =game . get_player ( e . player_index ) 78player_data . init ( e . player_index , player ) 79local player_table =global . players [ e . player_index ] 80player_data . refresh ( player , player_table ) 81end ) 82 83event . on_player_removed ( function ( e ) 84global . players [ e . player_index ] =nil 85end ) 86 87event . on_put_item ( function ( e ) 88global . players [ e . player_index ]. flags . building =true 89end ) 90 91event . on_player_cursor_stack_changed ( function ( e ) 92local player =game . get_player ( e . player_index ) 93local player_table =global . players [ e . player_index ] 94 95-- check `building` flag 96-- only proceed if it is true 97if player_table . flags . building then 98-- reset the flag 99player_table . flags . building =false 100 101local cursor_stack =player . cursor_stack 102local current_item =cursor_stack and cursor_stack . valid_for_read and cursor_stack . name 103 104if current_item then 105player_table . last_item =current_item 106elseif 107player_table . settings . ghost_cursor_transitions 108and not player . cursor_ghost 109then 110local last_item =player_table . last_item 111if last_item then 112player_data . ensure_valid_inventory ( player , player_table ) 113if player_table . main_inventory . get_item_count ( last_item ) == 0 then 114local entity =game . item_prototypes [ last_item ]. place_result 115if entity then 116cursor . set_stack ( player , cursor_stack , player_table , last_item ) 117end 118end 119end 120end 121end 122end ) 123 124event . on_player_main_inventory_changed ( function ( e ) 125local player =game . get_player ( e . player_index ) 126local player_table =global . players [ e . player_index ] 127 128if player_table . settings . ghost_cursor_transitions then 129local cursor_ghost =player . cursor_ghost 130 131if cursor_ghost then 132player_data . ensure_valid_inventory ( player , player_table ) 133local main_inventory =player_table . main_inventory 134if main_inventory . get_item_count ( cursor_ghost . name ) > 0 then 135cursor . set_stack ( player , player . cursor_stack , player_table , cursor_ghost . name ) 136end 137end 138end 139end ) 140 141-- SETTINGS 142 143event . on_runtime_mod_setting_changed ( function ( e ) 144if string . sub ( e . setting , 1 , 4 ) == "cen-" and e . setting_type == "runtime-per-user" then 145local player =game . get_player ( e . player_index ) 146local player_table =global . players [ e . player_index ] 147player_data . update_settings ( player , player_table ) 148if e . setting == "cen-personal-registry-overrides" then 149player_data . update_personal_overrides ( player , player_table ) 150end 151end 152end ) 153 154-- ----------------------------------------------------------------------------- 155-- REMOTE INTERFACE 156 157remote . add_interface ( "CursorEnhancements" , remote_interface )