z_mirror/Tapeline
git clone https://git.raiguard.me/z_mirror/Tapeline
6968d04
3.0
1local flib_bounding_box =require ( "__flib__.bounding-box" ) 2 3--- @param player LuaPlayer 4--- @return LuaItemStack? 5local function get_tool ( player ) 6local cursor_stack =player . cursor_stack 7if cursor_stack and cursor_stack . valid_for_read and cursor_stack . name == "tl-tool" then 8return cursor_stack 9end 10end 11 12--- @param player LuaPlayer 13--- @param box BoundingBox? 14local function set_tool ( player , box ) 15local cursor_stack =player . cursor_stack 16if cursor_stack then 17if not cursor_stack . valid_for_read or cursor_stack . name == "tl-tool" or player . clear_cursor () then 18cursor_stack . set_stack ( { name ="tl-tool" , count =10 } ) 19if box then 20cursor_stack . label =flib_bounding_box . width ( box ) .. "x" .. flib_bounding_box . height ( box ) 21else 22cursor_stack . label ="" 23end 24end 25if player . controller_type == defines . controllers . character and player . character_build_distance_bonus < 1000000 then 26player . character_build_distance_bonus =player . character_build_distance_bonus + 1000000 27end 28end 29end 30 31--- @param e EventData.on_player_cursor_stack_changed 32local function on_player_cursor_stack_changed ( e ) 33local player =game . get_player ( e . player_index ) --[[@as LuaPlayer]] 34local player_tool =get_tool ( player ) 35if player_tool and not storage . holding_tool [ player . index ] then 36storage . holding_tool [ player . index ] =true 37set_tool ( player ) 38elseif not player_tool and storage . holding_tool [ player . index ] then 39storage . holding_tool [ player . index ] =nil 40-- FIXME: Handle player controller changes 41if player . controller_type == defines . controllers . character and player . character_build_distance_bonus >= 1000000 then 42player . character_build_distance_bonus =player . character_build_distance_bonus - 1000000 43end 44end 45end 46 47--- @param e EventData.on_built_entity 48local function on_built_entity ( e ) 49local entity =e . entity 50if not entity . valid then 51return 52end 53local name =entity . name 54if name == "entity-ghost" then 55name =entity . ghost_name 56end 57if name ~= "tl-dummy-entity" then 58return 59end 60local player =game . get_player ( e . player_index ) 61if not player then 62return 63end 64local drawing =storage . drawing [ e . player_index ] 65if not drawing then 66return 67end 68set_tool ( player , drawing . box ) 69end 70 71--- @param e EventData.on_player_selected_area|EventData.on_player_alt_selected_area 72local function on_player_selected_area ( e ) 73local player =game . get_player ( e . player_index ) 74if not player then 75return 76end 77set_tool ( player ) 78end 79 80local function on_init () 81--- @type table<uint, boolean> 82storage . holding_tool ={} 83end 84 85local tool ={} 86 87tool . on_init =on_init 88 89tool . events ={ 90[ defines . events . on_built_entity ] =on_built_entity , 91[ defines . events . on_player_alt_selected_area ] =on_player_selected_area , 92[ defines . events . on_player_cursor_stack_changed ] =on_player_cursor_stack_changed , 93[ defines . events . on_player_selected_area ] =on_player_selected_area , 94} 95 96return tool