Build 02
- provided support for current screwdriver API - refactored wrench and screwdriver callbacks - moved all protection checks into helper function - removed duplicate callbacks for door node removal - provided support for current falling node API - allowed customization of door-locked sound fx - included protection violation hooks when digging - refactored door and trapdoor toggle functions - simplified parameters for checking locked doors
This commit is contained in:
parent
8bbb75c7dd
commit
37b695f483
107
api.lua
107
api.lua
|
@ -1,5 +1,5 @@
|
||||||
--------------------------------------------------------
|
--------------------------------------------------------
|
||||||
-- Minetest :: Doors Redux Mod v1.0 (doors)
|
-- Minetest :: Doors Redux Mod v1.1 (doors)
|
||||||
--
|
--
|
||||||
-- See README.txt for licensing and other information.
|
-- See README.txt for licensing and other information.
|
||||||
-- Copyright (c) 2016-2020, Leslie E. Krause
|
-- Copyright (c) 2016-2020, Leslie E. Krause
|
||||||
|
@ -146,15 +146,28 @@ local function get_door_transform( state, param2, toggle_open, switch_type, swit
|
||||||
center_transform[ hand ][ face ][ is_open ]
|
center_transform[ hand ][ face ][ is_open ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------------------------
|
||||||
|
-- is_door_protected( )
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
local function is_door_protected( pos, ndef, player_name )
|
||||||
|
local owner = minetest.get_meta( pos ):get_string( "doors_owner" )
|
||||||
|
|
||||||
|
if minetest.get_player_privs( player_name ).protection_bypass then return false end
|
||||||
|
|
||||||
|
return ndef.protected and player_name ~= owner
|
||||||
|
end
|
||||||
|
|
||||||
---------------------------------
|
---------------------------------
|
||||||
-- is_door_locked( )
|
-- is_door_locked( )
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
local function is_door_locked( pos, ndef, meta, player_name )
|
local function is_door_locked( pos, ndef, player_name )
|
||||||
|
local meta = minetest.get_meta( pos )
|
||||||
local locking_mode = meta:get_int( "locking_mode" )
|
local locking_mode = meta:get_int( "locking_mode" )
|
||||||
local owner = meta:get_string( "doors_owner" ) -- eventually deprecate this and change to "owner"
|
local owner = meta:get_string( "doors_owner" )
|
||||||
|
|
||||||
if minetest.check_player_privs( player_name, "protection_bypass" ) then return false end
|
if minetest.get_player_privs( player_name ).protection_bypass then return false end
|
||||||
|
|
||||||
if locking_mode == doors.LOCKING_MODE_UNDEFINED then
|
if locking_mode == doors.LOCKING_MODE_UNDEFINED then
|
||||||
if ndef.protected and player_name ~= owner then
|
if ndef.protected and player_name ~= owner then
|
||||||
|
@ -215,8 +228,8 @@ local function toggle_door( pos, node, player )
|
||||||
local ndef = minetest.registered_nodes[ node.name ]
|
local ndef = minetest.registered_nodes[ node.name ]
|
||||||
local closing_mode = meta:get_int( "closing_mode" )
|
local closing_mode = meta:get_int( "closing_mode" )
|
||||||
|
|
||||||
if player and is_door_locked( pos, ndef, meta, player:get_player_name( ) ) then
|
if player and is_door_locked( pos, ndef, player:get_player_name( ) ) then
|
||||||
minetest.sound_play( "doors_door_locked", { pos = pos, gain = 1.0, max_hear_distance = 10 } )
|
minetest.sound_play( ndef.sound_locked, { pos = pos, gain = 0.3, max_hear_distance = 10 } )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -268,8 +281,8 @@ function toggle_trapdoor( pos, node, player )
|
||||||
local ndef = minetest.registered_nodes[ node.name ]
|
local ndef = minetest.registered_nodes[ node.name ]
|
||||||
local closing_mode = meta:get_int( "closing_mode" )
|
local closing_mode = meta:get_int( "closing_mode" )
|
||||||
|
|
||||||
if player and is_trapdoor_locked( pos, ndef, meta, player:get_player_name( ) ) then
|
if player and is_trapdoor_locked( pos, ndef, player:get_player_name( ) ) then
|
||||||
minetest.sound_play( "doors_door_locked", { pos = pos, gain = 1.0, max_hear_distance = 10 } )
|
minetest.sound_play( ndef.sound_locked, { pos = pos, gain = 0.3, max_hear_distance = 10 } )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -311,23 +324,31 @@ local function on_adjust_door( pos, node, player, mode )
|
||||||
local ndef = minetest.registered_nodes[ node.name ]
|
local ndef = minetest.registered_nodes[ node.name ]
|
||||||
local locking_mode = meta:get_int( "locking_mode" )
|
local locking_mode = meta:get_int( "locking_mode" )
|
||||||
local closing_mode = meta:get_int( "closing_mode" )
|
local closing_mode = meta:get_int( "closing_mode" )
|
||||||
|
local player_name = player:get_player_name( )
|
||||||
|
|
||||||
|
if is_door_protected( pos, ndef, player_name ) then return false end
|
||||||
|
|
||||||
if mode == doors.ADJUST_LOCKING and ndef.is_lockable and locking_mode > 0 then
|
if mode == doors.ADJUST_LOCKING and ndef.is_lockable and locking_mode > 0 then
|
||||||
local mode_defs = { "unlocked", "locked", "shared" }
|
local mode_defs = { "unlocked", "locked", "shared" }
|
||||||
|
|
||||||
locking_mode = locking_mode % 3 + 1
|
locking_mode = locking_mode % 3 + 1
|
||||||
minetest.chat_send_player( player:get_player_name( ), "Door locking is set to " .. mode_defs[ locking_mode ] .. "." )
|
minetest.chat_send_player( player_name, "Door locking is set to " .. mode_defs[ locking_mode ] .. "." )
|
||||||
meta:set_int( "locking_mode", locking_mode )
|
meta:set_int( "locking_mode", locking_mode )
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
elseif mode == doors.ADJUST_CLOSING and ndef.is_closable and closing_mode > 0 then
|
elseif mode == doors.ADJUST_CLOSING and ndef.is_closable and closing_mode > 0 then
|
||||||
local mode_defs = { "manual", "auto-close", "hold-open" }
|
local mode_defs = { "manual", "auto-close", "hold-open" }
|
||||||
|
|
||||||
closing_mode = closing_mode % 3 + 1
|
closing_mode = closing_mode % 3 + 1
|
||||||
minetest.chat_send_player( player:get_player_name( ), "Door closing is set to " .. mode_defs[ closing_mode ] .. "." )
|
minetest.chat_send_player( player_name, "Door closing is set to " .. mode_defs[ closing_mode ] .. "." )
|
||||||
meta:set_int( "closing_mode", closing_mode )
|
meta:set_int( "closing_mode", closing_mode )
|
||||||
else
|
|
||||||
minetest.chat_send_player( player:get_player_name( ), "This door does not provide locking and/or closing adjustments." )
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
minetest.chat_send_player( player_name, "This door does not provide locking and/or closing adjustments." )
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local on_adjust_trapdoor = on_adjust_door
|
local on_adjust_trapdoor = on_adjust_door
|
||||||
|
@ -341,25 +362,28 @@ local function on_rotate_door( pos, node, player, mode )
|
||||||
local meta = minetest.get_meta( pos )
|
local meta = minetest.get_meta( pos )
|
||||||
local state = meta:get_int( "state" )
|
local state = meta:get_int( "state" )
|
||||||
|
|
||||||
if mode == screwdriver.ROTATE_FACE and ndef.can_center and ndef.can_offset then
|
if is_door_protected( pos, ndef, player:get_player_name( ) ) then return false end
|
||||||
-- alternate type between center <-> offset
|
|
||||||
local is_open, state, transform = get_door_transform( state, node.param2, false, true, false, false )
|
|
||||||
|
|
||||||
minetest.swap_node( pos, {
|
if mode == screwdriver.ROTATE_FACE then
|
||||||
name = ndef.base_name .. transform.suffix, param2 = transform.param2
|
|
||||||
} )
|
|
||||||
|
|
||||||
meta:set_int( "state", state )
|
|
||||||
else
|
|
||||||
-- alternate hand between left <-> right
|
-- alternate hand between left <-> right
|
||||||
local is_open, state, transform = get_door_transform( state, node.param2, false, false, true, false )
|
local is_open, state, transform = get_door_transform( state, node.param2, false, false, true, false )
|
||||||
|
|
||||||
minetest.swap_node( pos, {
|
minetest.swap_node( pos, { name = ndef.base_name .. transform.suffix, param2 = transform.param2 } )
|
||||||
name = ndef.base_name .. transform.suffix, param2 = transform.param2
|
|
||||||
} )
|
|
||||||
|
|
||||||
meta:set_int( "state", state )
|
meta:set_int( "state", state )
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
elseif mode == screwdriver.ROTATE_AXIS and ndef.can_center and ndef.can_offset then
|
||||||
|
-- alternate type between center <-> offset
|
||||||
|
local is_open, state, transform = get_door_transform( state, node.param2, false, true, false, false )
|
||||||
|
|
||||||
|
minetest.swap_node( pos, { name = ndef.base_name .. transform.suffix, param2 = transform.param2 } )
|
||||||
|
meta:set_int( "state", state )
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local on_rotate_trapdoor = function ( ) end
|
local on_rotate_trapdoor = function ( ) end
|
||||||
|
@ -615,6 +639,9 @@ function doors.register_door( name, def )
|
||||||
if not def.sound_close then
|
if not def.sound_close then
|
||||||
def.sound_close = "doors_door_close"
|
def.sound_close = "doors_door_close"
|
||||||
end
|
end
|
||||||
|
if not def.sound_locked then
|
||||||
|
def.sound_locked = "doors_door_locked"
|
||||||
|
end
|
||||||
|
|
||||||
-- define the placement types
|
-- define the placement types
|
||||||
|
|
||||||
|
@ -641,21 +668,20 @@ function doors.register_door( name, def )
|
||||||
toggle_door( pos, node, player )
|
toggle_door( pos, node, player )
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
def.after_dig_node = function ( pos, node, meta, player )
|
|
||||||
minetest.remove_node( vector.offset_y( pos ) )
|
|
||||||
nodeupdate( vector.offset_y( pos ) )
|
|
||||||
end
|
|
||||||
def.on_destruct = function( pos )
|
def.on_destruct = function( pos )
|
||||||
minetest.remove_node( vector.offset_y( pos ) ) -- hidden node
|
minetest.remove_node( vector.offset_y( pos ) ) -- hidden node
|
||||||
|
minetest.check_for_falling( vector.offset_y( pos ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
if def.protected then
|
if def.protected then
|
||||||
def.can_dig = function ( pos, player )
|
def.can_dig = function ( pos, player )
|
||||||
local player_name = player:get_player_name( )
|
local player_name = player:get_player_name( )
|
||||||
if minetest.get_player_privs( player_name ).protection_bypass then
|
if is_door_protected( pos, def, player_name ) then
|
||||||
|
minetest.record_protection_violation( pos, player_name )
|
||||||
|
return false
|
||||||
|
else
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return minetest.get_meta( pos ):get_string( "doors_owner" ) == player_name
|
|
||||||
end
|
end
|
||||||
def.on_blast = function( ) end
|
def.on_blast = function( ) end
|
||||||
else
|
else
|
||||||
|
@ -732,6 +758,9 @@ function doors.register_trapdoor( name, def )
|
||||||
if not def.sound_close then
|
if not def.sound_close then
|
||||||
def.sound_close = "doors_door_close"
|
def.sound_close = "doors_door_close"
|
||||||
end
|
end
|
||||||
|
if not def.sound_locked then
|
||||||
|
def.sound_locked = "doors_door_locked"
|
||||||
|
end
|
||||||
|
|
||||||
-- define the placement types
|
-- define the placement types
|
||||||
|
|
||||||
|
@ -754,10 +783,12 @@ function doors.register_trapdoor( name, def )
|
||||||
if def.protected then
|
if def.protected then
|
||||||
def.can_dig = function ( pos, player )
|
def.can_dig = function ( pos, player )
|
||||||
local player_name = player:get_player_name( )
|
local player_name = player:get_player_name( )
|
||||||
if minetest.get_player_privs( player_name ).protection_bypass then
|
if is_door_protected( pos, def, player_name ) then
|
||||||
|
minetest.record_protection_violation( pos, player_name )
|
||||||
|
return false
|
||||||
|
else
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return minetest.get_meta( pos ):get_string( "doors_owner" ) == player_name
|
|
||||||
end
|
end
|
||||||
def.after_place_node = function ( pos, player, itemstack, pointed_thing )
|
def.after_place_node = function ( pos, player, itemstack, pointed_thing )
|
||||||
local player_name = player:get_player_name( )
|
local player_name = player:get_player_name( )
|
||||||
|
@ -896,20 +927,16 @@ local function handle_wrench( itemstack, player, pointed_thing, mode, uses )
|
||||||
|
|
||||||
if minetest.is_protected( pos, player_name ) then
|
if minetest.is_protected( pos, player_name ) then
|
||||||
minetest.record_protection_violation( pos, player_name )
|
minetest.record_protection_violation( pos, player_name )
|
||||||
return nil
|
return
|
||||||
elseif ndef.protected and minetest.get_meta( pos ):get_string( "doors_owner" ) ~= player_name then
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if ndef.on_adjust then
|
if ndef.on_adjust then
|
||||||
ndef.on_adjust( vector.new( pos ), { name = node.name, param1 = node.param1, param2 = node.param2 }, player, mode )
|
local has_wear = ndef.on_adjust( vector.new( pos ), { name = node.name, param1 = node.param1, param2 = node.param2 }, player, mode )
|
||||||
|
|
||||||
if not minetest.setting_getbool( "creative_mode" ) then
|
if not minetest.setting_getbool( "creative_mode" ) and has_wear then
|
||||||
itemstack:add_wear( 65535 / config.wrench_usage_limit - 1 )
|
itemstack:add_wear( 65535 / config.wrench_usage_limit - 1 )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return itemstack
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------
|
--------------------
|
||||||
|
|
Loading…
Reference in New Issue