second cleanup. Bugfix: do not store user object references in table, usernames only
This commit is contained in:
parent
bfd3ecfdbd
commit
69235692dd
48
init.lua
48
init.lua
@ -1,31 +1,23 @@
|
|||||||
-- constant subspace size
|
-- constant subspace size
|
||||||
local c_subspacesize = 3
|
local c_subspacesize = 3
|
||||||
|
|
||||||
|
|
||||||
-- Check if the subspace still enabled for user (or can be disabled)
|
-- Check if the subspace still enabled for user (or can be disabled)
|
||||||
local function ssw_is_enabled(user)
|
local function ssw_is_enabled(name)
|
||||||
if not user then -- if user leave the game, disable them
|
user = minetest.get_player_by_name(name)
|
||||||
|
-- if user leave the game, disable them
|
||||||
|
if not user then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
-- user does not hold the walker in the hand
|
||||||
local item = user:get_wielded_item()
|
local item = user:get_wielded_item()
|
||||||
if not item or item:get_name() ~= "subspacewalker:walker" then
|
if not item or item:get_name() ~= "subspacewalker:walker" then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
-- all ok, still active
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Hide a node
|
|
||||||
local function hide_node(pos)
|
|
||||||
local node = minetest.get_node(pos)
|
|
||||||
if node and node.name ~= "air" and node.name ~= "subspacewalker:subspace" and node.name ~= 'ignore' then
|
|
||||||
-- Save the node's original name
|
|
||||||
minetest.get_meta(pos):set_string("subspacewalker", node.name)
|
|
||||||
-- Swap in placeholder node
|
|
||||||
node.name = "subspacewalker:subspace"
|
|
||||||
minetest.swap_node(pos, node)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- get y offset for sneaking or jumping
|
-- get y offset for sneaking or jumping
|
||||||
local function get_player_y_offset(user)
|
local function get_player_y_offset(user)
|
||||||
local control = user:get_player_control()
|
local control = user:get_player_control()
|
||||||
@ -39,7 +31,7 @@ local function get_player_y_offset(user)
|
|||||||
return y
|
return y
|
||||||
end
|
end
|
||||||
|
|
||||||
-- subspacewalker runtime object
|
-- subspacewalker runtime data
|
||||||
local subspacewalker = {
|
local subspacewalker = {
|
||||||
users_in_subspace = {},
|
users_in_subspace = {},
|
||||||
timer = 0,
|
timer = 0,
|
||||||
@ -54,7 +46,7 @@ minetest.register_tool("subspacewalker:walker", {
|
|||||||
tool_capabilities = {},
|
tool_capabilities = {},
|
||||||
range = 0,
|
range = 0,
|
||||||
on_use = function(itemstack, user, pointed_thing)
|
on_use = function(itemstack, user, pointed_thing)
|
||||||
subspacewalker.users_in_subspace[user:get_player_name()] = user
|
subspacewalker.users_in_subspace[user:get_player_name()] = true
|
||||||
end,
|
end,
|
||||||
on_place = function(itemstack, user, pointed_thing)
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
subspacewalker.users_in_subspace[user:get_player_name()] = nil
|
subspacewalker.users_in_subspace[user:get_player_name()] = nil
|
||||||
@ -71,16 +63,24 @@ minetest.register_globalstep(function(dtime)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
for name, user in pairs(subspacewalker.users_in_subspace) do
|
for name,_ in pairs(subspacewalker.users_in_subspace) do
|
||||||
if not ssw_is_enabled(user) then
|
if not ssw_is_enabled(name) then
|
||||||
subspacewalker.users_in_subspace[user:get_player_name()] = nil
|
subspacewalker.users_in_subspace[name] = nil
|
||||||
else
|
else
|
||||||
local userpos = user:getpos()
|
local userpos = user:getpos()
|
||||||
local ydelta = get_player_y_offset(user)
|
local ydelta = get_player_y_offset(user)
|
||||||
for x=userpos.x-c_subspacesize, userpos.x+c_subspacesize do
|
for x=userpos.x-c_subspacesize, userpos.x+c_subspacesize do
|
||||||
for y=userpos.y+ydelta, userpos.y+c_subspacesize*2 do -- we need the ground under the user
|
for y=userpos.y+ydelta, userpos.y+c_subspacesize*2 do -- we need the ground under the user
|
||||||
for z=userpos.z-c_subspacesize, userpos.z+c_subspacesize do
|
for z=userpos.z-c_subspacesize, userpos.z+c_subspacesize do
|
||||||
hide_node({x=x,y=y,z=z})
|
local pos = {x=x,y=y,z=z}
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
if node and node.name ~= "air" and node.name ~= "subspacewalker:subspace" and node.name ~= 'ignore' then
|
||||||
|
-- Save the node's original name
|
||||||
|
minetest.get_meta(pos):set_string("subspacewalker", node.name)
|
||||||
|
-- Swap in placeholder node
|
||||||
|
node.name = "subspacewalker:subspace"
|
||||||
|
minetest.swap_node(pos, node)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -113,9 +113,9 @@ minetest.register_abm({
|
|||||||
|
|
||||||
local can_be_restored = true
|
local can_be_restored = true
|
||||||
-- check if the node can be restored
|
-- check if the node can be restored
|
||||||
for name, user in pairs(subspacewalker.users_in_subspace) do
|
for name, _ in pairs(subspacewalker.users_in_subspace) do
|
||||||
if not ssw_is_enabled(user) then
|
if not ssw_is_enabled(name) then
|
||||||
subspacewalker.users_in_subspace[user:get_player_name()] = nil
|
subspacewalker.users_in_subspace[name] = nil
|
||||||
else
|
else
|
||||||
local userpos = user:getpos()
|
local userpos = user:getpos()
|
||||||
local ydelta = get_player_y_offset(user)
|
local ydelta = get_player_y_offset(user)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user