z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
b4a035a
smart-quickbar
1--- @class Util 2local util ={} 3 4--- @param player LuaPlayer 5--- @return string? 6function util . get_cursor_item ( player ) 7local cursor_stack =player . cursor_stack 8if not cursor_stack or not cursor_stack . valid_for_read then 9cursor_stack ={} 10end 11local cursor_ghost =player . cursor_ghost or {} 12return cursor_stack . name or cursor_ghost . name 13end 14 15--- @param selected SelectedPrototypeData? 16--- @return string? 17function util . get_selected_item ( selected ) 18if not selected then 19return 20end 21local type =selected . base_type 22if type == "item" then 23return selected . name 24elseif type == "entity" then 25local entity =game . entity_prototypes [ selected . name ] 26local item =( entity . items_to_place_this or {} )[ 1 ] 27if item then 28return item . name 29end 30elseif type == "recipe" then 31local recipe =game . recipe_prototypes [ selected . name ] 32local product =recipe . products [ 1 ] 33if product and product . type == "item" then 34return product . name 35end 36end 37end 38 39--- @param selected SelectedPrototypeData? 40--- @return LuaCustomTable<string, LuaRecipePrototype>? 41function util . get_selected_recipes ( selected ) 42if not selected then 43return 44end 45if selected . base_type == "recipe" then 46return { game . recipe_prototypes [ selected . name ] } 47end 48 49local item =util . get_selected_item ( selected ) 50if not item then 51return 52end 53 54return game . get_filtered_recipe_prototypes ( { 55{ filter ="has-product-item" , elem_filters ={ { filter ="name" , name =item } } } , 56} ) 57end 58 59--- @param player LuaPlayer 60--- @return boolean 61function util . is_cheating ( player ) 62if 63player . cheat_mode 64and not ( player . controller_type == defines . controllers . god and script . active_mods [ "space-exploration" ]) 65then 66return true 67end 68return player . controller_type == defines . controllers . editor 69end 70 71--- @param player LuaPlayer 72--- @param item string 73--- @return boolean success 74function util . set_cursor ( player , item ) 75local inventory =player . get_main_inventory () 76if not inventory then 77return false 78end 79local cursor_stack =player . cursor_stack 80if not cursor_stack or not player . clear_cursor () then 81return false 82end 83local inventory_stack , stack_index =inventory . find_item_stack ( item ) 84if not inventory_stack or not stack_index then 85local stack_size =game . item_prototypes [ item ]. stack_size 86if util . is_cheating ( player ) and inventory . can_insert ( { name =item , count =stack_size } ) then 87inventory . insert ( { name =item , count =stack_size } ) 88inventory_stack , stack_index =inventory . find_item_stack ( item ) 89else 90player . cursor_ghost =item 91return true 92end 93end 94--- @cast inventory_stack LuaItemStack 95--- @cast stack_index uint 96if not cursor_stack . transfer_stack ( inventory_stack ) then 97return false 98end 99local inventory_def 100if player . controller_type == defines . controllers . character then 101inventory_def =defines . inventory . character_main 102elseif player . controller_type == defines . controllers . editor then 103inventory_def =defines . inventory . editor_main 104elseif player . controller_type == defines . controllers . god then 105inventory_def =defines . inventory . god_main 106end 107player . hand_location ={ inventory =inventory_def , slot =stack_index } 108return true 109end 110 111return util