Doors: Allow on_rightclick to be overidden

Allow mods such as protection mods to over ride on_rightclick.
Usecase is creating shared doors without the need for keys.
This commit is contained in:
Ryan Nolan 2018-12-20 22:45:24 +00:00 committed by Paramat
parent ec248d3cb0
commit bae58b3806
2 changed files with 43 additions and 23 deletions

View File

@ -160,6 +160,12 @@ Doors API
The doors mod allows modders to register custom doors and trapdoors. The doors mod allows modders to register custom doors and trapdoors.
`doors.registered_doors[name] = Door definition`
* Table of registered doors, indexed by door name
`doors.registered_trapdoors[name] = Trapdoor definition`
* Table of registered trap doors, indexed by trap door name
`doors.register_door(name, def)` `doors.register_door(name, def)`
* Registers new door * Registers new door
@ -195,6 +201,13 @@ The doors mod allows modders to register custom doors and trapdoors.
has the permissions needed to open this door. If omitted then no has the permissions needed to open this door. If omitted then no
permission checks are performed. permission checks are performed.
`doors.door_toggle(pos, node, clicker)`
* Toggle door open or shut
* `pos` Position of the door
* `node` Node definition
* `clicker` Player definition for the player that clicked on the door
### Door definition ### Door definition
description = "Door description", description = "Door description",
@ -206,6 +219,8 @@ The doors mod allows modders to register custom doors and trapdoors.
sound_open = sound play for open door, -- optional sound_open = sound play for open door, -- optional
sound_close = sound play for close door, -- optional sound_close = sound play for close door, -- optional
protected = false, -- If true, only placer can open the door (locked for others) protected = false, -- If true, only placer can open the door (locked for others)
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- optional function containing the on_rightclick callback, defaults to a doors.door_toggle-wrapper
### Trapdoor definition ### Trapdoor definition
@ -218,6 +233,10 @@ The doors mod allows modders to register custom doors and trapdoors.
sound_open = sound play for open door, -- optional sound_open = sound play for open door, -- optional
sound_close = sound play for close door, -- optional sound_close = sound play for close door, -- optional
protected = false, -- If true, only placer can open the door (locked for others) protected = false, -- If true, only placer can open the door (locked for others)
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- function containing the on_rightclick callback
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- function containing the on_rightclick callback
### Fence gate definition ### Fence gate definition
@ -227,6 +246,8 @@ The doors mod allows modders to register custom doors and trapdoors.
material = "default:wood", material = "default:wood",
groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2}, groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
sounds = default.node_sound_wood_defaults(), -- optional sounds = default.node_sound_wood_defaults(), -- optional
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- function containing the on_rightclick callback
Dungeon Loot API Dungeon Loot API

View File

@ -1,10 +1,8 @@
-- our API object -- our API object
doors = {} doors = {}
-- private data doors.registered_doors = {}
local _doors = {} doors.registered_trapdoors = {}
_doors.registered_doors = {}
_doors.registered_trapdoors = {}
local function replace_old_owner_information(pos) local function replace_old_owner_information(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
@ -18,7 +16,7 @@ end
-- returns an object to a door object or nil -- returns an object to a door object or nil
function doors.get(pos) function doors.get(pos)
local node_name = minetest.get_node(pos).name local node_name = minetest.get_node(pos).name
if _doors.registered_doors[node_name] then if doors.registered_doors[node_name] then
-- A normal upright door -- A normal upright door
return { return {
pos = pos, pos = pos,
@ -26,23 +24,23 @@ function doors.get(pos)
if self:state() then if self:state() then
return false return false
end end
return _doors.door_toggle(self.pos, nil, player) return doors.door_toggle(self.pos, nil, player)
end, end,
close = function(self, player) close = function(self, player)
if not self:state() then if not self:state() then
return false return false
end end
return _doors.door_toggle(self.pos, nil, player) return doors.door_toggle(self.pos, nil, player)
end, end,
toggle = function(self, player) toggle = function(self, player)
return _doors.door_toggle(self.pos, nil, player) return doors.door_toggle(self.pos, nil, player)
end, end,
state = function(self) state = function(self)
local state = minetest.get_meta(self.pos):get_int("state") local state = minetest.get_meta(self.pos):get_int("state")
return state %2 == 1 return state %2 == 1
end end
} }
elseif _doors.registered_trapdoors[node_name] then elseif doors.registered_trapdoors[node_name] then
-- A trapdoor -- A trapdoor
return { return {
pos = pos, pos = pos,
@ -50,16 +48,16 @@ function doors.get(pos)
if self:state() then if self:state() then
return false return false
end end
return _doors.trapdoor_toggle(self.pos, nil, player) return doors.trapdoor_toggle(self.pos, nil, player)
end, end,
close = function(self, player) close = function(self, player)
if not self:state() then if not self:state() then
return false return false
end end
return _doors.trapdoor_toggle(self.pos, nil, player) return doors.trapdoor_toggle(self.pos, nil, player)
end, end,
toggle = function(self, player) toggle = function(self, player)
return _doors.trapdoor_toggle(self.pos, nil, player) return doors.trapdoor_toggle(self.pos, nil, player)
end, end,
state = function(self) state = function(self)
return minetest.get_node(self.pos).name:sub(-5) == "_open" return minetest.get_node(self.pos).name:sub(-5) == "_open"
@ -130,7 +128,7 @@ local transform = {
}, },
} }
function _doors.door_toggle(pos, node, clicker) function doors.door_toggle(pos, node, clicker)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
node = node or minetest.get_node(pos) node = node or minetest.get_node(pos)
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
@ -373,10 +371,11 @@ function doors.register(name, def)
name = name, name = name,
sounds = { def.sound_close, def.sound_open }, sounds = { def.sound_close, def.sound_open },
} }
if not def.on_rightclick then
def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
_doors.door_toggle(pos, node, clicker) doors.door_toggle(pos, node, clicker)
return itemstack return itemstack
end
end end
def.after_dig_node = function(pos, node, meta, digger) def.after_dig_node = function(pos, node, meta, digger)
minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z}) minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
@ -444,8 +443,8 @@ function doors.register(name, def)
def.mesh = "door_b.obj" def.mesh = "door_b.obj"
minetest.register_node(":" .. name .. "_b", def) minetest.register_node(":" .. name .. "_b", def)
_doors.registered_doors[name .. "_a"] = true doors.registered_doors[name .. "_a"] = true
_doors.registered_doors[name .. "_b"] = true doors.registered_doors[name .. "_b"] = true
end end
doors.register("door_wood", { doors.register("door_wood", {
@ -532,7 +531,7 @@ end
----trapdoor---- ----trapdoor----
function _doors.trapdoor_toggle(pos, node, clicker) function doors.trapdoor_toggle(pos, node, clicker)
node = node or minetest.get_node(pos) node = node or minetest.get_node(pos)
replace_old_owner_information(pos) replace_old_owner_information(pos)
@ -565,7 +564,7 @@ function doors.register_trapdoor(name, def)
local name_opened = name.."_open" local name_opened = name.."_open"
def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
_doors.trapdoor_toggle(pos, node, clicker) doors.trapdoor_toggle(pos, node, clicker)
return itemstack return itemstack
end end
@ -668,8 +667,8 @@ function doors.register_trapdoor(name, def)
minetest.register_node(name_opened, def_opened) minetest.register_node(name_opened, def_opened)
minetest.register_node(name_closed, def_closed) minetest.register_node(name_closed, def_closed)
_doors.registered_trapdoors[name_opened] = true doors.registered_trapdoors[name_opened] = true
_doors.registered_trapdoors[name_closed] = true doors.registered_trapdoors[name_closed] = true
end end
doors.register_trapdoor("doors:trapdoor", { doors.register_trapdoor("doors:trapdoor", {