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