z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
54b4789
trunk
1local util =require ( "scripts.util" ) 2 3local vehicles ={ 4[ "car" ] =true , 5[ "locomotive" ] =true , 6[ "spidertron" ] =true , 7} 8 9local ignored_types ={ 10[ "construction-robot" ] =true , 11[ "logistic-robot" ] =true , 12} 13 14--- @class BuiltItemData 15--- @field item ItemWithQualityID 16--- @field tick uint 17 18--- @param e EventData.on_built_entity 19local function on_built_entity ( e ) 20local player =game . get_player ( e . player_index ) 21if not player then 22return 23end 24 25if not player . mod_settings [ "cen-auto-ghost-cursor" ]. value then 26return 27end 28 29local built_item =storage . built_item [ e . player_index ] 30if not built_item then 31return 32end 33storage . built_item [ e . player_index ] =nil 34if built_item . tick ~= game . tick then 35return 36end 37if ignored_types [ e . entity . type ] then 38return 39end 40if script . active_mods [ "folk-justgo" ] and vehicles [ e . entity . type ] then 41return 42end 43 44-- Don't do anything if the cursor stack is not empty 45local cursor_stack =player . cursor_stack 46if not cursor_stack or cursor_stack . valid_for_read then 47return 48end 49 50util . set_cursor ( player , built_item . item ) 51end 52 53--- @param e EventData.on_pre_build 54local function on_pre_build ( e ) 55local player =game . get_player ( e . player_index ) 56if not player then 57return 58end 59 60if not player . mod_settings [ "cen-auto-ghost-cursor" ]. value then 61return 62end 63 64local cursor_stack =player . cursor_stack 65if not cursor_stack or not cursor_stack . valid_for_read then 66return 67end 68 69storage . built_item [ e . player_index ] = 70{ item ={ name =cursor_stack . prototype , quality =cursor_stack . quality } , tick =game . tick } 71end 72 73--- @param e EventData.on_player_main_inventory_changed 74local function on_player_main_inventory_changed ( e ) 75local player =game . get_player ( e . player_index ) 76if not player then 77return 78end 79 80if not player . mod_settings [ "cen-auto-ghost-cursor" ]. value then 81return 82end 83 84local cursor_ghost =player . cursor_ghost 85if not cursor_ghost then 86return 87end 88 89local main_inventory =player . get_main_inventory () 90if not main_inventory then 91return 92end 93 94local count =main_inventory . get_item_count ( cursor_ghost ) 95if count == 0 then 96return 97end 98util . set_cursor ( player , cursor_ghost ) 99end 100 101local auto_ghost_cursor ={} 102 103auto_ghost_cursor . on_init =function () 104--- @type table<uint, BuiltItemData?> 105storage . built_item ={} 106end 107 108auto_ghost_cursor . on_configuration_changed =function () 109storage . built_item ={} 110end 111 112auto_ghost_cursor . events ={ 113[ defines . events . on_built_entity ] =on_built_entity , 114[ defines . events . on_player_main_inventory_changed ] =on_player_main_inventory_changed , 115[ defines . events . on_pre_build ] =on_pre_build , 116} 117 118return auto_ghost_cursor