implement official intllib fallback boilerplate

This commit is contained in:
codexp 2018-04-05 20:06:28 +02:00
parent 07916ff19f
commit 29aefc511f
2 changed files with 92 additions and 49 deletions

View File

@ -1,5 +1,5 @@
--[[ --[[
Teleporter networks that allow players to choose a destination out of a list Teleporter networks that allow players to choose a destination out of a list
Copyright (C) 2013 Sokomine Copyright (C) 2013 Sokomine
@ -18,7 +18,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
Version: 2.2 (with optional abm for self-healing) Version: 2.2 (with optional abm for self-healing)
Please configure this mod in config.lua Please configure this mod in config.lua
Changelog: Changelog:
@ -78,28 +78,26 @@
travelnet = {}; travelnet = {};
travelnet.targets = {}; travelnet.targets = {};
travelnet.path = minetest.get_modpath(minetest.get_current_modname())
-- Boilerplate to support localized strings if intllib mod is installed.
if minetest.get_modpath( "intllib" ) and intllib then
travelnet.S = intllib.Getter()
else
travelnet.S = function(s) return s end
end
local S = travelnet.S; -- Intllib
local S = dofile(travelnet.path .. "/intllib.lua")
travelnet.S = S
minetest.register_privilege("travelnet_attach", { description = S("allows to attach travelnet boxes to travelnets of other players"), give_to_singleplayer = false}); minetest.register_privilege("travelnet_attach", { description = S("allows to attach travelnet boxes to travelnets of other players"), give_to_singleplayer = false});
minetest.register_privilege("travelnet_remove", { description = S("allows to dig travelnet boxes which belog to nets of other players"), give_to_singleplayer = false}); minetest.register_privilege("travelnet_remove", { description = S("allows to dig travelnet boxes which belog to nets of other players"), give_to_singleplayer = false});
-- read the configuration -- read the configuration
dofile(minetest.get_modpath("travelnet").."/config.lua"); -- the normal, default travelnet dofile(travelnet.path.."/config.lua"); -- the normal, default travelnet
-- TODO: save and restore ought to be library functions and not implemented in each individual mod! -- TODO: save and restore ought to be library functions and not implemented in each individual mod!
-- called whenever a station is added or removed -- called whenever a station is added or removed
travelnet.save_data = function() travelnet.save_data = function()
local data = minetest.serialize( travelnet.targets ); local data = minetest.serialize( travelnet.targets );
local path = minetest.get_worldpath().."/mod_travelnet.data"; local path = minetest.get_worldpath().."/mod_travelnet.data";
@ -116,7 +114,7 @@ end
travelnet.restore_data = function() travelnet.restore_data = function()
local path = minetest.get_worldpath().."/mod_travelnet.data"; local path = minetest.get_worldpath().."/mod_travelnet.data";
local file = io.open( path, "r" ); local file = io.open( path, "r" );
if( file ) then if( file ) then
local data = file:read("*all"); local data = file:read("*all");
@ -218,7 +216,7 @@ travelnet.reset_formspec = function( meta )
"field[0.3,2.8;9,0.9;station_network;"..S("Assign to Network:")..";".. "field[0.3,2.8;9,0.9;station_network;"..S("Assign to Network:")..";"..
minetest.formspec_escape(station_network or "").."]".. minetest.formspec_escape(station_network or "").."]"..
"label[0.3,3.1;"..S("You can have more than one network. If unsure, use \"%s\""):format(tostring(station_network)).."\".]".. "label[0.3,3.1;"..S("You can have more than one network. If unsure, use \"%s\""):format(tostring(station_network))..".]"..
"field[0.3,4.4;9,0.9;owner;"..S("Owned by:")..";]".. "field[0.3,4.4;9,0.9;owner;"..S("Owned by:")..";]"..
"label[0.3,4.7;"..S("Unless you know what you are doing, leave this empty.").."]".. "label[0.3,4.7;"..S("Unless you know what you are doing, leave this empty.").."]"..
"button_exit[1.3,5.3;1.7,0.7;station_help_setup;"..S("Help").."]".. "button_exit[1.3,5.3;1.7,0.7;station_help_setup;"..S("Help").."]"..
@ -235,7 +233,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then
is_elevator = true; is_elevator = true;
end end
if( not( meta )) then if( not( meta )) then
return; return;
@ -245,7 +243,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
local station_name = meta:get_string( "station_name" ); local station_name = meta:get_string( "station_name" );
local station_network = meta:get_string( "station_network" ); local station_network = meta:get_string( "station_network" );
if( not( owner_name ) if( not( owner_name )
or not( station_name ) or station_network == '' or not( station_name ) or station_network == ''
or not( station_network )) then or not( station_network )) then
@ -275,7 +273,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
if( not( travelnet.targets[ owner_name ] )) then if( not( travelnet.targets[ owner_name ] )) then
travelnet.targets[ owner_name ] = {}; travelnet.targets[ owner_name ] = {};
end end
-- first station on this network? -- first station on this network?
if( not( travelnet.targets[ owner_name ][ station_network ] )) then if( not( travelnet.targets[ owner_name ][ station_network ] )) then
travelnet.targets[ owner_name ][ station_network ] = {}; travelnet.targets[ owner_name ][ station_network ] = {};
@ -318,15 +316,15 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
-- collect all station names in a table -- collect all station names in a table
local stations = {}; local stations = {};
for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do
table.insert( stations, k ); table.insert( stations, k );
end end
-- minetest.chat_send_player(puncher_name, "stations: "..minetest.serialize( stations )); -- minetest.chat_send_player(puncher_name, "stations: "..minetest.serialize( stations ));
local ground_level = 1; local ground_level = 1;
if( is_elevator ) then if( is_elevator ) then
table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].pos.y > table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].pos.y >
travelnet.targets[ owner_name ][ station_network ][ b ].pos.y end); travelnet.targets[ owner_name ][ station_network ][ b ].pos.y end);
-- find ground level -- find ground level
local vgl_timestamp = 999999999999; local vgl_timestamp = 999999999999;
@ -335,7 +333,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
travelnet.targets[ owner_name ][ station_network ][ k ].timestamp = os.time(); travelnet.targets[ owner_name ][ station_network ][ k ].timestamp = os.time();
end end
if( travelnet.targets[ owner_name ][ station_network ][ k ].timestamp < vgl_timestamp ) then if( travelnet.targets[ owner_name ][ station_network ][ k ].timestamp < vgl_timestamp ) then
vgl_timestamp = travelnet.targets[ owner_name ][ station_network ][ k ].timestamp; vgl_timestamp = travelnet.targets[ owner_name ][ station_network ][ k ].timestamp;
ground_level = index; ground_level = index;
end end
end end
@ -346,10 +344,10 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
travelnet.targets[ owner_name ][ station_network ][ k ].nr = tostring( ground_level - index ); travelnet.targets[ owner_name ][ station_network ][ k ].nr = tostring( ground_level - index );
end end
end end
else else
-- sort the table according to the timestamp (=time the station was configured) -- sort the table according to the timestamp (=time the station was configured)
table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].timestamp < table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].timestamp <
travelnet.targets[ owner_name ][ station_network ][ b ].timestamp end); travelnet.targets[ owner_name ][ station_network ][ b ].timestamp end);
end end
@ -387,9 +385,9 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
else else
-- swap the actual data by which the stations are sorted -- swap the actual data by which the stations are sorted
local old_timestamp = travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp; local old_timestamp = travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp;
travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp = travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp =
travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp; travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp;
travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp = travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp =
old_timestamp; old_timestamp;
-- for elevators, only the "G"(round) marking is moved; no point in swapping stations -- for elevators, only the "G"(round) marking is moved; no point in swapping stations
@ -410,7 +408,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
x = 4; x = 4;
end end
for index,k in ipairs( stations ) do for index,k in ipairs( stations ) do
-- check if there is an elevator door in front that needs to be opened -- check if there is an elevator door in front that needs to be opened
local open_door_cmd = false; local open_door_cmd = false;
@ -418,7 +416,7 @@ travelnet.update_formspec = function( pos, puncher_name, fields )
open_door_cmd = true; open_door_cmd = true;
end end
if( k ~= station_name or open_door_cmd) then if( k ~= station_name or open_door_cmd) then
i = i+1; i = i+1;
-- new column -- new column
@ -475,7 +473,7 @@ travelnet.add_target = function( station_name, network_name, pos, player_name, m
if( this_node.name == 'travelnet:elevator' ) then if( this_node.name == 'travelnet:elevator' ) then
-- owner_name = '*'; -- the owner name is not relevant here -- owner_name = '*'; -- the owner name is not relevant here
is_elevator = true; is_elevator = true;
network_name = tostring( pos.x )..','..tostring( pos.z ); network_name = tostring( pos.x )..','..tostring( pos.z );
if( not( station_name ) or station_name == '' ) then if( not( station_name ) or station_name == '' ) then
station_name = S('at %s m'):format(tostring( pos.y )); station_name = S('at %s m'):format(tostring( pos.y ));
end end
@ -517,7 +515,7 @@ travelnet.add_target = function( station_name, network_name, pos, player_name, m
if( not( travelnet.targets[ owner_name ] )) then if( not( travelnet.targets[ owner_name ] )) then
travelnet.targets[ owner_name ] = {}; travelnet.targets[ owner_name ] = {};
end end
-- first station on this network? -- first station on this network?
if( not( travelnet.targets[ owner_name ][ network_name ] )) then if( not( travelnet.targets[ owner_name ][ network_name ] )) then
travelnet.targets[ owner_name ][ network_name ] = {}; travelnet.targets[ owner_name ][ network_name ] = {};
@ -544,7 +542,7 @@ travelnet.add_target = function( station_name, network_name, pos, player_name, m
"Please choose a diffrent/new network name."):format(travelnet.MAX_STATIONS_PER_NETWORK)); "Please choose a diffrent/new network name."):format(travelnet.MAX_STATIONS_PER_NETWORK));
return; return;
end end
-- add this station -- add this station
travelnet.targets[ owner_name ][ network_name ][ station_name ] = {pos=pos, timestamp=os.time() }; travelnet.targets[ owner_name ][ network_name ][ station_name ] = {pos=pos, timestamp=os.time() };
@ -560,7 +558,7 @@ travelnet.add_target = function( station_name, network_name, pos, player_name, m
meta:set_string( "owner", owner_name ); meta:set_string( "owner", owner_name );
meta:set_int( "timestamp", travelnet.targets[ owner_name ][ network_name ][ station_name ].timestamp); meta:set_int( "timestamp", travelnet.targets[ owner_name ][ network_name ][ station_name ].timestamp);
meta:set_string("formspec", meta:set_string("formspec",
"size[12,10]".. "size[12,10]"..
"field[0.3,0.6;6,0.7;station_name;"..S("Station:")..";".. minetest.formspec_escape(meta:get_string("station_name")).."]".. "field[0.3,0.6;6,0.7;station_name;"..S("Station:")..";".. minetest.formspec_escape(meta:get_string("station_name")).."]"..
"field[0.3,3.6;6,0.7;station_network;"..S("Network:")..";"..minetest.formspec_escape(meta:get_string("station_network")).."]" ); "field[0.3,3.6;6,0.7;station_network;"..S("Network:")..";"..minetest.formspec_escape(meta:get_string("station_network")).."]" );
@ -594,7 +592,7 @@ travelnet.open_close_door = function( pos, player, mode )
-- do not close the elevator door if it is already closed -- do not close the elevator door if it is already closed
if( mode==1 and ( door_node.name == 'travelnet:elevator_door_glass_closed' if( mode==1 and ( door_node.name == 'travelnet:elevator_door_glass_closed'
or door_node.name == 'travelnet:elevator_door_steel_closed' or door_node.name == 'travelnet:elevator_door_steel_closed'
-- handle doors that change their facedir -- handle doors that change their facedir
or ( door_node.param2 == this_node.param2 or ( door_node.param2 == this_node.param2
and door_node.name ~= 'travelnet:elevator_door_glass_open' and door_node.name ~= 'travelnet:elevator_door_glass_open'
@ -605,12 +603,12 @@ travelnet.open_close_door = function( pos, player, mode )
if( mode==2 and ( door_node.name == 'travelnet:elevator_door_glass_open' if( mode==2 and ( door_node.name == 'travelnet:elevator_door_glass_open'
or door_node.name == 'travelnet:elevator_door_steel_open' or door_node.name == 'travelnet:elevator_door_steel_open'
-- handle doors that change their facedir -- handle doors that change their facedir
or ( door_node.param2 ~= this_node.param2 or ( door_node.param2 ~= this_node.param2
and door_node.name ~= 'travelnet:elevator_door_glass_closed' and door_node.name ~= 'travelnet:elevator_door_glass_closed'
and door_node.name ~= 'travelnet:elevator_door_steel_closed'))) then and door_node.name ~= 'travelnet:elevator_door_steel_closed'))) then
return; return;
end end
if( mode==2 ) then if( mode==2 ) then
minetest.after( 1, minetest.registered_nodes[ door_node.name ].on_rightclick, pos2, door_node, player ); minetest.after( 1, minetest.registered_nodes[ door_node.name ].on_rightclick, pos2, door_node, player );
else else
@ -674,8 +672,8 @@ travelnet.on_receive_fields = function(pos, formname, fields, player)
local station_name = meta:get_string( "station_name" ); local station_name = meta:get_string( "station_name" );
local station_network = meta:get_string( "station_network" ); local station_network = meta:get_string( "station_network" );
if( not( owner_name ) if( not( owner_name )
or not( station_name ) or not( station_name )
or not( station_network ) or not( station_network )
or not( travelnet.targets[ owner_name ] ) or not( travelnet.targets[ owner_name ] )
or not( travelnet.targets[ owner_name ][ station_network ] )) then or not( travelnet.targets[ owner_name ][ station_network ] )) then
@ -710,7 +708,7 @@ travelnet.on_receive_fields = function(pos, formname, fields, player)
end end
local this_node = minetest.get_node( pos ); local this_node = minetest.get_node( pos );
if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then
for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do
if( travelnet.targets[ owner_name ][ station_network ][ k ].nr --..' ('..tostring( travelnet.targets[ owner_name ][ station_network ][ k ].pos.y )..'m)' if( travelnet.targets[ owner_name ][ station_network ][ k ].nr --..' ('..tostring( travelnet.targets[ owner_name ][ station_network ][ k ].pos.y )..'m)'
== fields.target) then == fields.target) then
@ -740,7 +738,7 @@ travelnet.on_receive_fields = function(pos, formname, fields, player)
if( travelnet.travelnet_sound_enabled ) then if( travelnet.travelnet_sound_enabled ) then
minetest.sound_play("128590_7037-lq.mp3", {pos = pos, gain = 1.0, max_hear_distance = 10,}) minetest.sound_play("128590_7037-lq.mp3", {pos = pos, gain = 1.0, max_hear_distance = 10,})
end end
if( travelnet.travelnet_effect_enabled ) then if( travelnet.travelnet_effect_enabled ) then
minetest.add_entity( {x=pos.x,y=pos.y+0.5,z=pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns minetest.add_entity( {x=pos.x,y=pos.y+0.5,z=pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns
end end
@ -754,7 +752,7 @@ travelnet.on_receive_fields = function(pos, formname, fields, player)
if( travelnet.travelnet_sound_enabled ) then if( travelnet.travelnet_sound_enabled ) then
minetest.sound_play("travelnet_travel.wav", {pos = target_pos, gain = 1.0, max_hear_distance = 10,}) minetest.sound_play("travelnet_travel.wav", {pos = target_pos, gain = 1.0, max_hear_distance = 10,})
end end
if( travelnet.travelnet_effect_enabled ) then if( travelnet.travelnet_effect_enabled ) then
minetest.add_entity( {x=target_pos.x,y=target_pos.y+0.5,z=target_pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns minetest.add_entity( {x=target_pos.x,y=target_pos.y+0.5,z=target_pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns
end end
@ -787,7 +785,7 @@ travelnet.on_receive_fields = function(pos, formname, fields, player)
elseif( param2==3 ) then elseif( param2==3 ) then
yaw = 270; yaw = 270;
end end
player:set_look_horizontal( math.rad( yaw )); player:set_look_horizontal( math.rad( yaw ));
player:set_look_vertical( math.rad( 0 )); player:set_look_vertical( math.rad( 0 ));
end end
@ -809,19 +807,19 @@ travelnet.remove_box = function( pos, oldnode, oldmetadata, digger )
local station_network = oldmetadata.fields[ "station_network" ]; local station_network = oldmetadata.fields[ "station_network" ];
-- station is not known? then just remove it -- station is not known? then just remove it
if( not( owner_name ) if( not( owner_name )
or not( station_name ) or not( station_name )
or not( station_network ) or not( station_network )
or not( travelnet.targets[ owner_name ] ) or not( travelnet.targets[ owner_name ] )
or not( travelnet.targets[ owner_name ][ station_network ] )) then or not( travelnet.targets[ owner_name ][ station_network ] )) then
minetest.chat_send_player( digger:get_player_name(), S("Error")..": ".. minetest.chat_send_player( digger:get_player_name(), S("Error")..": "..
S("Could not find the station that is to be removed.")); S("Could not find the station that is to be removed."));
return; return;
end end
travelnet.targets[ owner_name ][ station_network ][ station_name ] = nil; travelnet.targets[ owner_name ][ station_network ][ station_name ] = nil;
-- inform the owner -- inform the owner
minetest.chat_send_player( owner_name, S("Station '%s'"):format(station_name ).." ".. minetest.chat_send_player( owner_name, S("Station '%s'"):format(station_name ).." "..
S("has been REMOVED from the network '%s'."):format(station_network)); S("has been REMOVED from the network '%s'."):format(station_network));
@ -901,17 +899,17 @@ end
if( travelnet.travelnet_enabled ) then if( travelnet.travelnet_enabled ) then
dofile(minetest.get_modpath("travelnet").."/travelnet.lua"); -- the travelnet node definition dofile(travelnet.path.."/travelnet.lua"); -- the travelnet node definition
end end
if( travelnet.elevator_enabled ) then if( travelnet.elevator_enabled ) then
dofile(minetest.get_modpath("travelnet").."/elevator.lua"); -- allows up/down transfers only dofile(travelnet.path.."/elevator.lua"); -- allows up/down transfers only
end end
if( travelnet.doors_enabled ) then if( travelnet.doors_enabled ) then
dofile(minetest.get_modpath("travelnet").."/doors.lua"); -- doors that open and close automaticly when the travelnet or elevator is used dofile(travelnet.path.."/doors.lua"); -- doors that open and close automaticly when the travelnet or elevator is used
end end
if( travelnet.abm_enabled ) then if( travelnet.abm_enabled ) then
dofile(minetest.get_modpath("travelnet").."/restore_network_via_abm.lua"); -- restore travelnet data when players pass by broken networks dofile(travelnet.path.."/restore_network_via_abm.lua"); -- restore travelnet data when players pass by broken networks
end end
-- upon server start, read the savefile -- upon server start, read the savefile

45
intllib.lua Normal file
View File

@ -0,0 +1,45 @@
-- Fallback functions for when `intllib` is not installed.
-- Code released under Unlicense <http://unlicense.org>.
-- Get the latest version of this file at:
-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua
local function format(str, ...)
local args = { ... }
local function repl(escape, open, num, close)
if escape == "" then
local replacement = tostring(args[tonumber(num)])
if open == "" then
replacement = replacement..close
end
return replacement
else
return "@"..open..num..close
end
end
return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl))
end
local gettext, ngettext
if minetest.get_modpath("intllib") then
if intllib.make_gettext_pair then
-- New method using gettext.
gettext, ngettext = intllib.make_gettext_pair()
else
-- Old method using text files.
gettext = intllib.Getter()
end
end
-- Fill in missing functions.
gettext = gettext or function(msgid, ...)
return format(msgid, ...)
end
ngettext = ngettext or function(msgid, msgid_plural, n, ...)
return format(n==1 and msgid or msgid_plural, ...)
end
return gettext, ngettext