]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/client/util.lua
Small AutoTool Fix
[dragonfireclient.git] / builtin / client / util.lua
1 function core.parse_pos(param)
2         local p = {}
3         local playerpos = core.localplayer:get_pos()
4         p.x, p.y, p.z = string.match(param, "^([~|%d.-]+)[, ] *([~|%d.-]+)[, ] *([~|%d.-]+)$")
5         for k, v in pairs(p) do
6                 if p[k] == "~" then
7                         p[k] = playerpos[k]
8                 else
9                         p[k] = tonumber(v)
10                 end
11         end
12         if p.x and p.y and p.z then
13                 return true, vector.round(p)
14         end
15         return false, "Invalid position (" .. param .. ")"
16 end 
17
18 function core.parse_relative_pos(param)
19         local success, pos = core.parse_pos(param:gsub("~", "0"))
20         if success then pos = vector.round(vector.add(core.localplayer:get_pos(), pos)) end
21         return success, pos
22 end 
23
24 function core.find_item(item, mini, maxi)
25         for index, stack in ipairs(core.get_inventory("current_player").main) do
26                 if (not mini or index >= mini) and (not maxi or index <= maxi) and stack:get_name() == item then
27                         return index
28                 end
29         end
30 end
31
32 function core.switch_to_item(item)
33         local i = core.find_item(item)
34         if i then
35                 core.localplayer:set_wield_index(i)
36                 return true
37         else
38                 return false
39         end
40 end
41
42 function core.get_pointed_thing()
43         local pos = core.camera:get_pos()
44         local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 7))
45         local player = core.localplayer
46         if not player then return end
47         local item = player:get_wielded_item()
48         if not item then return end
49         local def = core.get_item_def(item:get_name())
50         local ray = core.raycast(pos, pos2, true, core.settings:get_bool("point_liquids") or def and def.liquids_pointable)
51         return ray and ray:next()
52 end
53
54 function core.close_formspec(formname)
55         return core.show_formspec(formname, "")
56 end