Split ownership checking and 6d facedir into separate files. Changed

related functions to use homedecor: and lib_6d: namespaces, as
necessary, instead of simple "homedecor_" name prefixes.
This commit is contained in:
Vanessa Ezekowitz 2013-10-13 13:20:42 -04:00
parent 92d8a53f5b
commit e4c02a82d6
6 changed files with 122 additions and 104 deletions

View File

@ -281,8 +281,8 @@ function homedecor.place_door(itemstack, placer, pointed_thing, name, side)
local node_bottom = minetest.get_node(pos1) local node_bottom = minetest.get_node(pos1)
local node_top = minetest.get_node(pos2) local node_top = minetest.get_node(pos2)
if not homedecor.node_is_owned(pos1, placer) if not homedecor:node_is_owned(pos1, placer)
and not homedecor.node_is_owned(pos2, placer) then and not homedecor:node_is_owned(pos2, placer) then
if not get_nodedef_field(node_bottom.name, "buildable_to") if not get_nodedef_field(node_bottom.name, "buildable_to")
or not get_nodedef_field(node_top.name, "buildable_to") then or not get_nodedef_field(node_top.name, "buildable_to") then

View File

@ -13,6 +13,8 @@
homedecor = {} homedecor = {}
homedecor.disable_signs = minetest.setting_getbool("homedecor.disable_signs")
homedecor.debug = 0 homedecor.debug = 0
homedecor.modpath = minetest.get_modpath("homedecor") homedecor.modpath = minetest.get_modpath("homedecor")
@ -27,57 +29,21 @@ else
S = function ( s ) return s end S = function ( s ) return s end
end end
-- Global stuff
homedecor.disable_signs = minetest.setting_getbool("homedecor.disable_signs")
-- Various Functions
local dbg = function(s) local dbg = function(s)
if homedecor.debug == 1 then if homedecor.debug == 1 then
print('[HomeDecor] ' .. s) print('[HomeDecor] ' .. s)
end end
end end
function homedecor.node_is_owned(pos, placer)
local ownername = false
if type(IsPlayerNodeOwner) == "function" then -- node_ownership mod
if HasOwner(pos, placer) then -- returns true if the node is owned
if not IsPlayerNodeOwner(pos, placer:get_player_name()) then
if type(getLastOwner) == "function" then -- ...is an old version
ownername = getLastOwner(pos)
elseif type(GetNodeOwnerName) == "function" then -- ...is a recent version
ownername = GetNodeOwnerName(pos)
else
ownername = S("someone")
end
end
end
elseif type(isprotect)=="function" then -- glomie's protection mod
if not isprotect(5, pos, placer) then
ownername = S("someone")
end
elseif type(protector)=="table" and type(protector.can_dig)=="function" then -- Zeg9's protection mod
if not protector.can_dig(5, pos, placer) then
ownername = S("someone")
end
end
if ownername ~= false then
minetest.chat_send_player( placer:get_player_name(), S("Sorry, %s owns that spot."):format(ownername) )
return true
else
return false
end
end
if minetest.get_modpath("unified_inventory") or not minetest.setting_getbool("creative_mode") then if minetest.get_modpath("unified_inventory") or not minetest.setting_getbool("creative_mode") then
homedecor.expect_infinite_stacks = false homedecor.expect_infinite_stacks = false
else else
homedecor.expect_infinite_stacks = true homedecor.expect_infinite_stacks = true
end end
dofile(homedecor.modpath.."/ownership.lua")
dofile(homedecor.modpath.."/lib_6d.lua")
dofile(homedecor.modpath.."/misc_nodes.lua") -- the catch-all for all misc nodes dofile(homedecor.modpath.."/misc_nodes.lua") -- the catch-all for all misc nodes
dofile(homedecor.modpath.."/tables.lua") dofile(homedecor.modpath.."/tables.lua")
dofile(homedecor.modpath.."/electronics.lua") dofile(homedecor.modpath.."/electronics.lua")

52
lib_6d.lua Normal file
View File

@ -0,0 +1,52 @@
-- Simplified 6d facedir rotation/prediction library
-- by VanessaE
-- license: WTFPL
lib_6d = {}
local dirs1 = { 20, 23, 22, 21 }
local dirs2 = { 9, 18, 7, 12 }
function lib_6d:rotate_and_place(itemstack, placer, pointed_thing, infinitestacks)
local node = minetest.get_node(pointed_thing.under)
if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].on_rightclick then
local above = pointed_thing.above
local under = pointed_thing.under
local pitch = placer:get_look_pitch()
local pname = minetest.get_node(under).name
local node = minetest.get_node(above)
local fdir = minetest.dir_to_facedir(placer:get_look_dir())
local wield_name = itemstack:get_name()
if not minetest.registered_nodes[pname]
or not minetest.registered_nodes[pname].on_rightclick then
local iswall = (above.x ~= under.x) or (above.z ~= under.z)
local isceiling = (above.x == under.x) and (above.z == under.z) and (pitch > 0)
local pos1 = above
if minetest.registered_nodes[pname]["buildable_to"] then
pos1 = under
iswall = false
end
if not minetest.registered_nodes[minetest.get_node(pos1).name]["buildable_to"] then return end
if iswall then
minetest.add_node(pos1, {name = wield_name, param2 = dirs2[fdir+1] }) -- place wall variant
elseif isceiling then
minetest.add_node(pos1, {name = wield_name, param2 = 20 }) -- place upside down variant
else
minetest.add_node(pos1, {name = wield_name, param2 = 0 }) -- place right side up
end
if not infinitestacks then
itemstack:take_item()
return itemstack
end
end
else
minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack)
end
end

View File

@ -10,56 +10,6 @@ else
S = function ( s ) return s end S = function ( s ) return s end
end end
local dirs1 = { 20, 23, 22, 21 }
local dirs2 = { 9, 18, 7, 12 }
function homedecor.rotate_and_place(itemstack, placer, pointed_thing)
if not homedecor.node_is_owned(pointed_thing.under, placer)
and not homedecor.node_is_owned(pointed_thing.above, placer) then
local node = minetest.get_node(pointed_thing.under)
if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].on_rightclick then
local above = pointed_thing.above
local under = pointed_thing.under
local pitch = placer:get_look_pitch()
local pname = minetest.get_node(under).name
local node = minetest.get_node(above)
local fdir = minetest.dir_to_facedir(placer:get_look_dir())
local wield_name = itemstack:get_name()
if not minetest.registered_nodes[pname]
or not minetest.registered_nodes[pname].on_rightclick then
local iswall = (above.x ~= under.x) or (above.z ~= under.z)
local isceiling = (above.x == under.x) and (above.z == under.z) and (pitch > 0)
local pos1 = above
if minetest.registered_nodes[pname]["buildable_to"] then
pos1 = under
iswall = false
end
if not minetest.registered_nodes[minetest.get_node(pos1).name]["buildable_to"] then return end
if iswall then
minetest.add_node(pos1, {name = wield_name, param2 = dirs2[fdir+1] }) -- place wall variant
elseif isceiling then
minetest.add_node(pos1, {name = wield_name, param2 = 20 }) -- place upside down variant
else
minetest.add_node(pos1, {name = wield_name, param2 = 0 }) -- place right side up
end
if not homedecor.expect_infinite_stacks then
itemstack:take_item()
return itemstack
end
end
else
minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack)
end
end
end
local colors = {"yellow","white"} local colors = {"yellow","white"}
for i in ipairs(colors) do for i in ipairs(colors) do
@ -146,7 +96,10 @@ minetest.register_node('homedecor:glowlight_half_yellow', {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })
@ -179,7 +132,10 @@ minetest.register_node('homedecor:glowlight_quarter_yellow', {
light_source = LIGHT_MAX-1, light_source = LIGHT_MAX-1,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })
@ -216,7 +172,10 @@ minetest.register_node('homedecor:glowlight_half_white', {
light_source = LIGHT_MAX, light_source = LIGHT_MAX,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })
@ -249,7 +208,10 @@ minetest.register_node('homedecor:glowlight_quarter_white', {
light_source = LIGHT_MAX-1, light_source = LIGHT_MAX-1,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })
@ -285,7 +247,10 @@ minetest.register_node('homedecor:glowlight_small_cube_yellow', {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })
@ -318,7 +283,10 @@ minetest.register_node('homedecor:glowlight_small_cube_white', {
light_source = LIGHT_MAX-1, light_source = LIGHT_MAX-1,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
homedecor.rotate_and_place(itemstack, placer, pointed_thing) if not homedecor:node_is_owned(pointed_thing.under, placer)
and not homedecor:node_is_owned(pointed_thing.above, placer) then
lib_6d:rotate_and_place(itemstack, placer, pointed_thing, homedecor.expect_infinite_stacks)
end
return itemstack return itemstack
end end
}) })

32
ownership.lua Normal file
View File

@ -0,0 +1,32 @@
function homedecor:node_is_owned(pos, placer)
local ownername = false
if type(IsPlayerNodeOwner) == "function" then -- node_ownership mod
if HasOwner(pos, placer) then -- returns true if the node is owned
if not IsPlayerNodeOwner(pos, placer:get_player_name()) then
if type(getLastOwner) == "function" then -- ...is an old version
ownername = getLastOwner(pos)
elseif type(GetNodeOwnerName) == "function" then -- ...is a recent version
ownername = GetNodeOwnerName(pos)
else
ownername = S("someone")
end
end
end
elseif type(isprotect)=="function" then -- glomie's protection mod
if not isprotect(5, pos, placer) then
ownername = S("someone")
end
elseif type(protector)=="table" and type(protector.can_dig)=="function" then -- Zeg9's protection mod
if not protector.can_dig(5, pos, placer) then
ownername = S("someone")
end
end
if ownername ~= false then
minetest.chat_send_player( placer:get_player_name(), S("Sorry, %s owns that spot."):format(ownername) )
return true
else
return false
end
end

View File

@ -119,13 +119,13 @@ if not homedecor.disable_signs then
local name local name
name = minetest.get_node(pointed_thing.under).name name = minetest.get_node(pointed_thing.under).name
if fences_with_sign[name] then if fences_with_sign[name] then
if homedecor.node_is_owned(pointed_thing.under, placer) then if homedecor:node_is_owned(pointed_thing.under, placer) then
return itemstack return itemstack
end end
else else
name = minetest.get_node(pointed_thing.above).name name = minetest.get_node(pointed_thing.above).name
local def = minetest.registered_nodes[name] local def = minetest.registered_nodes[name]
if homedecor.node_is_owned(pointed_thing.above, placer) if homedecor:node_is_owned(pointed_thing.above, placer)
or (not def.buildable_to) then or (not def.buildable_to) then
return itemstack return itemstack
end end
@ -202,7 +202,7 @@ if not homedecor.disable_signs then
minetest.pos_to_string(pos) minetest.pos_to_string(pos)
)) ))
end end
if homedecor.node_is_owned(pos, sender) then return end if homedecor:node_is_owned(pos, sender) then return end
homedecor.update_sign(pos, fields) homedecor.update_sign(pos, fields)
end, end,
on_punch = function(pos, node, puncher) on_punch = function(pos, node, puncher)
@ -239,7 +239,7 @@ minetest.register_node(":signs:sign_yard", {
minetest.pos_to_string(pos) minetest.pos_to_string(pos)
)) ))
end end
if homedecor.node_is_owned(pos, sender) then return end if homedecor:node_is_owned(pos, sender) then return end
homedecor.update_sign(pos, fields) homedecor.update_sign(pos, fields)
end, end,
on_punch = function(pos, node, puncher) on_punch = function(pos, node, puncher)
@ -443,7 +443,7 @@ function homedecor.register_fence_with_sign(fencename, fencewithsignname)
local fdir = minetest.dir_to_facedir(placer:get_look_dir()) local fdir = minetest.dir_to_facedir(placer:get_look_dir())
if def_under and def_under.on_rightclick then if def_under and def_under.on_rightclick then
return def_under.on_rightclick(pointed_thing.under, node_under, placer, itemstack) or itemstack return def_under.on_rightclick(pointed_thing.under, node_under, placer, itemstack) or itemstack
elseif (not homedecor.node_is_owned(pointed_thing.under, placer)) elseif (not homedecor:node_is_owned(pointed_thing.under, placer))
and def_under.buildable_to then and def_under.buildable_to then
minetest.add_node(pointed_thing.under, {name = fencename, param2 = fdir}) minetest.add_node(pointed_thing.under, {name = fencename, param2 = fdir})
if not homedecor.expect_infinite_stacks then if not homedecor.expect_infinite_stacks then
@ -451,7 +451,7 @@ function homedecor.register_fence_with_sign(fencename, fencewithsignname)
end end
placer:set_wielded_item(itemstack) placer:set_wielded_item(itemstack)
return itemstack return itemstack
elseif (not homedecor.node_is_owned(pointed_thing.above, placer)) elseif (not homedecor:node_is_owned(pointed_thing.above, placer))
and def_above.buildable_to then and def_above.buildable_to then
minetest.add_node(pointed_thing.above, {name = fencename, param2 = fdir}) minetest.add_node(pointed_thing.above, {name = fencename, param2 = fdir})
if not homedecor.expect_infinite_stacks then if not homedecor.expect_infinite_stacks then
@ -475,7 +475,7 @@ function homedecor.register_fence_with_sign(fencename, fencewithsignname)
minetest.pos_to_string(pos) minetest.pos_to_string(pos)
)) ))
end end
if homedecor.node_is_owned(pos, sender) then return end if homedecor:node_is_owned(pos, sender) then return end
homedecor.update_sign(pos, fields) homedecor.update_sign(pos, fields)
end end
def_sign.on_punch = function(pos, node, puncher, ...) def_sign.on_punch = function(pos, node, puncher, ...)