z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
48ea56d
trunk
1local util =require ( "scripts.util" ) 2 3local crafting_controllers ={ 4[ defines . controllers . character ] =true , 5[ defines . controllers . god ] =true , 6} 7 8--- @param e EventData.CustomInputEvent 9local function on_quick_craft ( e ) 10local player =game . get_player ( e . player_index ) 11if not player then 12return 13end 14 15if not crafting_controllers [ player . controller_type ] then 16return 17end 18 19local selected =e . selected_prototype 20if not selected then 21return 22end 23 24if selected . base_type == "entity" and selected . name == "entity-ghost" then 25-- Get ghost entity name 26local real_selected =player . selected 27if not real_selected or real_selected . name ~= "entity-ghost" then 28return 29end 30selected . name =real_selected . ghost_name 31end 32 33-- Prioritize items over resources 34if selected . derived_type == "resource" then 35local cursor_stack =player . cursor_stack 36local cursor_ghost =player . cursor_ghost 37if cursor_stack and cursor_stack . valid_for_read then 38selected ={ 39base_type ="item" , 40derived_type =cursor_stack . type , 41name =cursor_stack . name , 42quality =cursor_stack . quality . name , 43} 44elseif cursor_ghost then 45selected ={ 46base_type ="item" , 47derived_type =cursor_ghost . type , 48name =cursor_ghost . name . name , 49quality =cursor_ghost . quality . name , 50} 51end 52end 53 54local recipes =util . get_selected_recipes ( selected ) 55if not recipes then 56return 57end 58 59-- TODO: Crafting functions cannot have quality specified; Add this to the API. 60for _ , recipe in pairs ( recipes ) do 61local craftable_count =player . cheat_mode and math . huge or player . get_craftable_count ( recipe . name ) --[[@as uint]] 62if craftable_count == 0 then 63goto continue 64end 65local main_product =recipe . main_product or recipe . products [ 1 ] 66if not main_product then 67goto continue 68end 69local craft_count_setting =player . mod_settings [ "cen-quick-craft-count" ]. value --[[@as uint]] 70local main_product_amount =main_product . amount 71if not main_product_amount then 72goto continue 73end 74local craft_count =math . min ( craftable_count , math . ceil ( craft_count_setting / main_product_amount ) --[[@as uint]] ) 75player . begin_crafting ( { recipe =recipe . name , count =craft_count } ) 76-- Language server and formatter don't like a label after break... 77do 78break 79end 80:: continue :: 81end 82end 83 84local quick_craft ={} 85 86quick_craft . events ={ 87[ "cen-quick-craft" ] =on_quick_craft , 88} 89 90return quick_craft