item wield: auto swap to hotbar 7 for now

master
cora 2021-03-02 13:23:30 +01:00
parent f2c9c54489
commit 1cdb01a1f9
3 changed files with 64 additions and 28 deletions

View File

@ -13,13 +13,13 @@ function core.parse_pos(param)
return true, vector.round(p)
end
return false, "Invalid position (" .. param .. ")"
end
end
function core.parse_relative_pos(param)
local success, pos = core.parse_pos(param:gsub("~", "0"))
if success then pos = vector.round(vector.add(core.localplayer:get_pos(), pos)) end
return success, pos
end
end
function core.find_item(item, mini, maxi)
for index, stack in ipairs(core.get_inventory("current_player").main) do
@ -29,16 +29,6 @@ function core.find_item(item, mini, maxi)
end
end
function core.switch_to_item(item)
local i = core.find_item(item)
if i then
core.localplayer:set_wield_index(i)
return true
else
return false
end
end
function core.get_pointed_thing()
local pos = core.camera:get_pos()
local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 7))

View File

@ -109,12 +109,15 @@ end
minetest.register_globalstep(ws.step_globalhacks)
function ws.get_reachable_positions(range)
function ws.get_reachable_positions(range,under)
under=under or false
range=range or 2
local rt={}
local lp=minetest.localplayer:get_pos()
local ylim=range
if under then ylim=-1 end
for x = -range,range,1 do
for y = -range,range,1 do
for y = -range,ylim,1 do
for z = -range,range,1 do
table.insert(rt,vector.add(lp,vector.new(x,y,z)))
end
@ -199,26 +202,43 @@ function ws.find_item_in_table(items,rnd)
return false
end
local function find_named(inv, name)
function ws.find_named(inv, name)
if not inv then return -1 end
if not name then return end
for i, v in ipairs(inv) do
--minetest.display_chat_message(name)
if v:get_name():find(name) then
return i
end
end
end
local hotbar_slot=7
function ws.to_hotbar(it)
local mv = InventoryAction("move")
mv:from("current_player", "main", it)
mv:to("current_player", "main", hotbar_slot)
mv:apply()
end
function ws.switch_to_item(itname)
if not minetest.localplayer then return false end
local plinv = minetest.get_inventory("current_player")
local pos = ws.find_named(plinv.main, itname)
if pos then
ws.to_hotbar(pos)
minetest.localplayer:set_wield_index(hotbar_slot)
return true
end
return false
end
function core.switch_to_item(item) return ws.switch_to_item(item) end
function ws.switch_inv_or_echest(name,max_count)
if not minetest.localplayer then return false end
local plinv = minetest.get_inventory("current_player")
if ws.switch_to_item(name) then return true end
local pos = find_named(plinv.main, name)
if pos then
minetest.localplayer:set_wield_index(pos)
return true
end
local epos = find_named(plinv.enderchest, name)
local epos = ws.find_named(plinv.enderchest, name)
if epos then
local tpos
for i, v in ipairs(plinv.main) do
@ -227,16 +247,22 @@ function ws.switch_inv_or_echest(name,max_count)
break
end
end
if tpos and not plinv.main[hotbar_slot]:is_empty() then
local mov = InventoryAction("move")
mov:from("current_player", "enderchest", hotbar_slot)
mov:to("current_player", "main", tpos)
mov:apply()
end
if tpos then
local mv = InventoryAction("move")
mv:from("current_player", "enderchest", epos)
mv:to("current_player", "main", tpos)
mv:to("current_player", "main", hotbar_slot)
if max_count then
mv:set_count(max_count)
end
mv:apply()
minetest.localplayer:set_wield_index(tpos)
minetest.localplayer:set_wield_index(hotbar_slot)
return true
end
end

View File

@ -119,11 +119,13 @@ function scaffold.can_place_wielded_at(pos)
return not wield_empty and scaffold.can_place_at(pos)
end
function scaffold.find_any_swap(items)
local ts=8
for i, v in ipairs(items) do
local n = minetest.find_item(v)
if n then
minetest.localplayer:set_wield_index(n)
ws.switch_to_item(v)
return true
end
end
@ -194,11 +196,13 @@ end
function scaffold.dig(pos)
if not inside_constraints(pos) then return end
if is_diggable(pos) then
local nd=minetest.get_node_or_nil(pos)
minetest.select_best_tool(nd.name)
if emicor then emicor.supertool()
end
--minetest.select_best_tool(nd.name)
minetest.dig_node(pos)
minetest.select_best_tool(nd.name)
end
return false
end
@ -219,7 +223,23 @@ ws.rg('SnapYaw','Bots','snapyaw',function() ws.setdir(snapdir) end,function() sn
scaffold.register_template_scaffold("Constrain", "scaffold_constrain", function()end,false,function() scaffold.reset() end)
ws.rg("LockYaw","Scaffold", "scaffold_lockyaw", function(pos) end, function() minetest.settings:set_bool('afly_snap',true) end, function() minetest.settings:set_bool('afly_snap',false) end)
ws.rg("LockYaw","Scaffold", "scaffold_lockyaw", function(pos)
if minetest.settings:get_bool("freecam") then return end
local y=minetest.localplayer:get_yaw()
local yy=nil
if ( y < 45 or y > 315 ) then
yy=0
elseif (y < 135) then
yy=90
elseif (y < 225 ) then
yy=180
elseif ( y < 315 ) then
yy=270
end
if yy ~= nil then
minetest.localplayer:set_yaw(yy)
end
end)
scaffold.register_template_scaffold("CheckScaffold", "scaffold_check", function(pos)