Advertisement
Guest User

Untitled

a guest
Feb 6th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.03 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. --
  3. -- Minetest Mod "Travelpoints" Version 1.4 2015-03-27
  4. --
  5. -- By Racso Rhodes
  6. --
  7. -- travelpoints/init.lua
  8. --
  9. --------------------------------------------------------------------------------
  10. -- License of source code, textures and sounds: WTFPL V2
  11. --------------------------------------------------------------------------------
  12. -- Copyright (C) 2013-2015 Racso Rhodes <racsorhodes@gmail.com>
  13. -- This work is free. You can redistribute it and/or modify it under the
  14. -- terms of the Do What The Fuck You Want To Public License, Version 2,
  15. -- as published by Sam Hocevar. See the COPYING file for more details.
  16. --------------------------------------------------------------------------------
  17. -- TOC
  18. --------------------------------------------------------------------------------
  19. --
  20. -- On Load
  21. --
  22. -- Chat Commands
  23. --
  24. -- /tpback
  25. -- /tpdrop
  26. -- /tpgdrop
  27. -- /tpggo
  28. -- /tpgo
  29. -- /tpgset
  30. -- /travelpads
  31. -- /travelpoints
  32. -- /tpset
  33. --
  34. --------------------------------------------------------------------------------
  35.  
  36. --------------------------------------------------------------------------------
  37. -- On Load
  38. --------------------------------------------------------------------------------
  39.  
  40. -- Register privilege - travelpoints.
  41. --
  42. -- This allows users to set, drop and use their own travelpoints.
  43. -- Also allows usage of global travelpoints.
  44. --
  45. minetest.register_privilege("travelpoints", "Can use the Travelpoints chat commands.")
  46.  
  47. -- Register privilege - travelpads
  48. --
  49. -- This allows the placing of travelpoints:tansporter_pad nodes.
  50. --
  51. minetest.register_privilege("travelpads", "Can place Travelpoint Transporter Pads.")
  52.  
  53. -- Register privilege - tpglobal
  54. --
  55. -- This allows the saving and dropping of global travelpoints.
  56. --
  57. minetest.register_privilege("tpglobal", "Can set and drop global travelpoints.")
  58.  
  59. -- Initialize mod table.
  60. travelpoints = {}
  61.  
  62. -- Get path to this mod.
  63. travelpoints.modpath = minetest.get_modpath(minetest.get_current_modname())
  64.  
  65. -- Get path to current world.
  66. travelpoints.worldpath = minetest.get_worldpath()
  67.  
  68. -- Get filesystem directory delimiter.
  69. travelpoints.delimiter = string.sub(package.config, 1, 1)
  70.  
  71. -- Set travelpoints_tables directory path.
  72. travelpoints.travelpoints_tables = travelpoints.worldpath .. travelpoints.delimiter .. "travelpoints_tables"
  73.  
  74. -- Create directory if it does not exist.
  75. os.execute("mkdir \"" .. travelpoints.travelpoints_tables .. "\"")
  76.  
  77. -- Set version for /travelpoints.
  78. travelpoints.version_number = "1.4"
  79.  
  80. -- Set version date for /travelpoints.
  81. travelpoints.version_date = "2015-03-27"
  82.  
  83. -- Initialize restrictions table.
  84. travelpoints.restrictions = {}
  85.  
  86. -- Load config file.
  87. dofile(travelpoints.modpath .. travelpoints.delimiter .. "config.lua")
  88.  
  89. -- Load functions file.
  90. dofile(travelpoints.modpath .. travelpoints.delimiter .. "functions.lua")
  91.  
  92. -- Load nodes file.
  93. dofile(travelpoints.modpath .. travelpoints.delimiter .. "nodes.lua")
  94.  
  95. -- Validate config setting values.
  96. travelpoints.validate_config()
  97.  
  98. -- Get world specific restrictions.
  99. travelpoints.get_world_restrictions()
  100.  
  101. --------------------------------------------------------------------------------
  102. -- Chat Commands
  103. --------------------------------------------------------------------------------
  104.  
  105. --[/tpback]--------------------------------------------------------------------
  106. --
  107. -- Returns player to the location they last used /tpgo <title>.
  108. --
  109. minetest.register_chatcommand("tpback", {
  110. params = "",
  111. description = "Teleports you back to the location where \"/tpgo <title>\" was last used.",
  112. privs = {travelpoints=true},
  113. func = function(name, param)
  114.  
  115. -- Get travelpoints_table.
  116. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  117.  
  118. -- Check for return location.
  119. if not travelpoints_table._back then
  120. travelpoints.print_notice(name, "You have no return location.")
  121. return
  122. end
  123.  
  124. -- Get player.
  125. local player = minetest.get_player_by_name(name)
  126.  
  127. -- Teleport player.
  128. player:setpos(travelpoints_table._back)
  129.  
  130. -- Report.
  131. travelpoints.print_notice(name, "You have returned to " .. minetest.pos_to_string(travelpoints_table._back) .. "." )
  132.  
  133. -- Clear return location.
  134. if travelpoints.restrictions.clear_back_pos > 0 then
  135. travelpoints_table._back = nil
  136. travelpoints.save_travelpoints_table("user", name, travelpoints_table)
  137. end
  138.  
  139. end,
  140. })
  141.  
  142. --[/tpdrop]--------------------------------------------------------------------
  143. --
  144. -- Allows player to remove the specified travelpoint for the current world, if
  145. -- "all" is used, all travelpoints are removed.
  146. --
  147. minetest.register_chatcommand("tpdrop", {
  148. params = "<title> | all",
  149. description = "Removes the travelpoint specified by <title>. To remove all of your travelpoints for this world, use \"/tpdrop all\".",
  150. privs = {travelpoints=true},
  151. func = function(name, param)
  152.  
  153. ------------------------------------------------------------------------
  154. -- /tpdrop
  155. ------------------------------------------------------------------------
  156. if param == "" then
  157.  
  158. travelpoints.print_notice(name, "Error: No travelpoint was specified.")
  159.  
  160. return
  161.  
  162. ------------------------------------------------------------------------
  163. -- /tpdrop all
  164. ------------------------------------------------------------------------
  165. elseif param == "all" then
  166.  
  167. -- Get travelpoints_table.
  168. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  169.  
  170. -- Initialize new travelpoints_table.
  171. local tpt = {}
  172.  
  173. -- Step through travelpoints_table.
  174. for key, value in pairs(travelpoints_table) do
  175.  
  176. -- Find keys that begin with an underscore and add to new table.
  177. if string.find(key, "^_[%w_]+$") then
  178. tpt[key] = value
  179. end
  180.  
  181. end
  182.  
  183. -- Overwrite existing travelpoints_table with new table.
  184. if travelpoints.save_travelpoints_table("user", name, tpt) then
  185.  
  186. -- Report success.
  187. travelpoints.print_notice(name, "You have removed all of your travelpoints for this world." )
  188.  
  189. else
  190.  
  191. -- Report error.
  192. travelpoints.print_notice(name, "Error: Your travelpoints for this world could not be removed.")
  193.  
  194. end
  195.  
  196. return
  197.  
  198. ------------------------------------------------------------------------
  199. -- /tpdrop <title>
  200. ------------------------------------------------------------------------
  201. else
  202.  
  203. -- Get Title.
  204. local title = string.match(param, "^([^ ]+)%s*")
  205.  
  206. -- Validate Title.
  207. local notice = travelpoints.validate_title(title)
  208. if notice ~= nil then
  209. travelpoints.print_notice(name, notice)
  210. return
  211. end
  212.  
  213. -- Get travelpoints_table.
  214. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  215.  
  216. -- Check if <title> is a valid travelpoint.
  217. if travelpoints_table[title] == nil then
  218. travelpoints.print_notice(name, "Error: Travelpoint \"" .. title .. "\" does not exist.")
  219. return
  220. end
  221.  
  222. -- Remove travelpoint from table.
  223. travelpoints_table[title] = nil
  224.  
  225. -- Save travelpoints_table.
  226. if travelpoints.save_travelpoints_table("user", name, travelpoints_table) then
  227.  
  228. -- Report success.
  229. travelpoints.print_notice(name, "Travelpoint \"" .. title .. "\" has been removed." )
  230.  
  231. else
  232.  
  233. -- Report error.
  234. travelpoints.print_notice(name, "Error: Travelpoint \"" .. title .. "\" could not be removed.")
  235.  
  236. end
  237.  
  238. end
  239.  
  240. end,
  241. })
  242.  
  243. --[/tpgdrop]--------------------------------------------------------------------
  244. --
  245. -- Removes global travelpoints, or all if specified.
  246. --
  247. minetest.register_chatcommand("tpgdrop", {
  248. params = "<title> | all",
  249. description = "Removes the global travelpoint specified by <title>. To remove all global travelpoints for this world, use \"/tpgdrop all\".",
  250. privs = {tpglobal=true},
  251. func = function(name, param)
  252.  
  253. ------------------------------------------------------------------------
  254. -- /tpgdrop
  255. ------------------------------------------------------------------------
  256. if param == "" then
  257.  
  258. travelpoints.print_notice(name, "Error: No travelpoint was specified.")
  259.  
  260. return
  261.  
  262. ------------------------------------------------------------------------
  263. -- /tpgdrop all
  264. ------------------------------------------------------------------------
  265. elseif param == "all" then
  266.  
  267. -- Check if user has server privilege.
  268. if minetest.get_player_privs(name)["server"] then
  269.  
  270. -- Get travelpoints table.
  271. local travelpoints_table = travelpoints.get_travelpoints_table("global", name)
  272.  
  273. -- Initialize new travelpoints table.
  274. local tpt = {}
  275.  
  276. -- Overwrite existing travelpoints table with new table.
  277. if travelpoints.save_travelpoints_table("global", name, tpt) then
  278.  
  279. -- Report success.
  280. travelpoints.print_notice(name, "You have removed all global travelpoints for this world." )
  281.  
  282. else
  283.  
  284. -- Report error.
  285. travelpoints.print_notice(name, "Error: Global travelpoints for this world could not be removed.")
  286.  
  287. end
  288.  
  289. return
  290.  
  291. else
  292.  
  293. -- Report error.
  294. travelpoints.print_notice(name, "Server privilege required to drop all global travelpoints.")
  295.  
  296. end
  297.  
  298. ------------------------------------------------------------------------
  299. -- /tpgdrop <title>
  300. ------------------------------------------------------------------------
  301. else
  302.  
  303. -- Get Title.
  304. local title = string.match(param, "^([^ ]+)%s*")
  305.  
  306. -- Validate Title.
  307. local notice = travelpoints.validate_title(title)
  308. if notice ~= nil then
  309. travelpoints.print_notice(name, notice)
  310. return
  311. end
  312.  
  313. -- Get travelpoints_table.
  314. local travelpoints_table = travelpoints.get_travelpoints_table("global", name)
  315.  
  316. -- Check if <title> is a valid travelpoint.
  317. if travelpoints_table[title] == nil then
  318. travelpoints.print_notice(name, "Error: Global travelpoint \"" .. title .. "\" does not exist.")
  319. return
  320. end
  321.  
  322. -- Remove travelpoint from table.
  323. travelpoints_table[title] = nil
  324.  
  325. -- Save travelpoints_table.
  326. if travelpoints.save_travelpoints_table("global", name, travelpoints_table) then
  327.  
  328. -- Report success.
  329. travelpoints.print_notice(name, "Global travelpoint \"" .. title .. "\" has been removed." )
  330.  
  331. else
  332.  
  333. -- Report error.
  334. travelpoints.print_notice(name, "Error: Global travelpoint \"" .. title .. "\" could not be removed.")
  335.  
  336. end
  337.  
  338. end
  339.  
  340. end,
  341. })
  342.  
  343. --[/tpggo]-----------------------------------------------------------------------
  344. --
  345. -- Teleports player to specified global travelpoint, or displays available
  346. -- global travelpoints if no title given.
  347. --
  348. minetest.register_chatcommand("tpggo", {
  349. params = "(nothing) | <title>",
  350. description = "Teleports you to the specified global travelpoint. If no travelpoint given, a list of available global travelpoints is displayed.",
  351. privs = {travelpoints=true},
  352. func = function(name, param)
  353.  
  354. -- Get global travelpoints table.
  355. local global_travelpoints_table = travelpoints.get_travelpoints_table("global", name)
  356.  
  357. -- Get player's travelpoints table.
  358. local user_travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  359.  
  360. -- Assume no cooldown until calculated otherwise.
  361. local cooldown_remaining = "none"
  362.  
  363. -- Get current time.
  364. local now = os.time()
  365.  
  366. -- Check if coodown needs to be calculated.
  367. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.cooldown > 0 ) and ( not minetest.get_player_privs(name)["server"] ) then
  368.  
  369. if user_travelpoints_table._cooldown ~= nil then
  370.  
  371. -- Cooldown timestamp.
  372. local coolstamp = user_travelpoints_table._cooldown
  373.  
  374. -- Seconds since cooldown timestamp.
  375. local seconds_since = ( now - coolstamp )
  376.  
  377. -- Check if seconds since last /tpgo <title> or /tpggo <title>
  378. -- is less than cooldown time.
  379. if seconds_since < travelpoints.restrictions.cooldown then
  380.  
  381. -- Get time remaining for cooldown.
  382. cooldown_remaining = travelpoints.get_duration(travelpoints.restrictions.cooldown - seconds_since)
  383.  
  384. end
  385.  
  386. end
  387.  
  388. end
  389.  
  390. ------------------------------------------------------------------------
  391. -- /tpggo
  392. ------------------------------------------------------------------------
  393. if param == "" then
  394.  
  395. -- Get travelpoints array.
  396. local travelpoints_array = travelpoints.get_travelpoints_array("global", name)
  397.  
  398. -- Check if there are any travelpoints.
  399. if #travelpoints_array > 0 then
  400.  
  401. -- Begin output.
  402. travelpoints.print_notice(name, "Available global travelpoints:")
  403.  
  404. -- Step through travelpoints_array.
  405. for index, value in ipairs(travelpoints_array) do
  406.  
  407. -- Extract title from value: "<title> (<x>, <y>, <z>)"
  408. local title = string.match(value, "^([^ ]+)%s+")
  409.  
  410. -- Output lines.
  411. -- <n>. <title> (<x>, <y>, <z>). Saved on <date> at <time>. Descripton: <desc>
  412. travelpoints.print_notice(name, index .. ". \"" .. title .. "\" " .. minetest.pos_to_string(global_travelpoints_table[title].pos) .. ". Saved on " .. os.date("%Y-%m-%d at %I:%M:%S %p", global_travelpoints_table[title].timestamp) .. ". Description: " .. global_travelpoints_table[title].desc)
  413.  
  414. end
  415.  
  416. else
  417. travelpoints.print_notice(name, "There are no saved global travelpoints.")
  418. end
  419.  
  420. -- Cooldown remaining.
  421. if cooldown_remaining ~= "none" then
  422. travelpoints.print_notice(name, "Your remaining cooldown is: " .. cooldown_remaining .. ".")
  423. end
  424.  
  425. return
  426.  
  427. ------------------------------------------------------------------------
  428. -- /tpggo <title>
  429. ------------------------------------------------------------------------
  430. else
  431.  
  432. -- Check if player is on cooldown.
  433. if cooldown_remaining == "none" then
  434.  
  435. -- Get Title.
  436. local title = string.match(param, "^([^ ]+)%s*")
  437.  
  438. -- Validate Title.
  439. local notice = travelpoints.validate_title(title)
  440. if notice ~= nil then
  441. travelpoints.print_notice(name, notice)
  442. return
  443. end
  444.  
  445. -- Check for specified travelpoint.
  446. if not global_travelpoints_table[title] then
  447. travelpoints.print_notice(name, "Error: Global travelpoint \"" .. title .. "\"does not exist.")
  448. return
  449. end
  450.  
  451. -- Set location for /tpback
  452. user_travelpoints_table._back = travelpoints.get_location(name)
  453.  
  454. -- Get player.
  455. local player = minetest.get_player_by_name(name)
  456.  
  457. -- Teleport player.
  458. player:setpos(global_travelpoints_table[title].pos)
  459.  
  460. -- Report.
  461. travelpoints.print_notice(name, "Teleported to global travelpoint: \"" .. title .. "\". Use /tpback to return to " .. minetest.pos_to_string(user_travelpoints_table._back) .. "." )
  462.  
  463. -- Set cooldown if needed.
  464. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.cooldown > 0 ) then
  465. user_travelpoints_table._cooldown = now
  466. end
  467.  
  468. -- Save player's travelpoints table.
  469. travelpoints.save_travelpoints_table("user", name, user_travelpoints_table)
  470.  
  471. else
  472.  
  473. -- Report
  474. travelpoints.print_notice(name, "Time remaining on your cooldown: " .. cooldown_remaining .. ".")
  475.  
  476. end
  477.  
  478. end
  479.  
  480. end,
  481. })
  482.  
  483. --[/tpgo]-----------------------------------------------------------------------
  484. --
  485. -- Teleports player to specified travelpoint, or displays available
  486. -- travelpoints if no title given.
  487. --
  488. minetest.register_chatcommand("tpgo", {
  489. params = "(nothing) | <title>",
  490. description = "Teleports you to the specified travelpoint. If no travelpoint given, a list of available travelpoints is displayed.",
  491. privs = {travelpoints=true},
  492. func = function(name, param)
  493.  
  494. -- Get travelpoints_table.
  495. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  496.  
  497. -- Assume no cooldown until calculated otherwise.
  498. local cooldown_remaining = "none"
  499.  
  500. -- Get current time.
  501. local now = os.time()
  502.  
  503. -- Check if coodown needs to be calculated.
  504. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.cooldown > 0 ) and ( not minetest.get_player_privs(name)["server"] ) then
  505.  
  506. if travelpoints_table._cooldown ~= nil then
  507.  
  508. -- Cooldown timestamp.
  509. local coolstamp = travelpoints_table._cooldown
  510.  
  511. -- Seconds since cooldown timestamp.
  512. local seconds_since = ( now - coolstamp )
  513.  
  514. -- Check if seconds since last /tpgo <title> is less than cooldown time.
  515. if seconds_since < travelpoints.restrictions.cooldown then
  516.  
  517. -- Get time remaining for cooldown.
  518. cooldown_remaining = travelpoints.get_duration(travelpoints.restrictions.cooldown - seconds_since)
  519.  
  520. end
  521.  
  522. end
  523.  
  524. end
  525.  
  526. ------------------------------------------------------------------------
  527. -- /tpgo
  528. ------------------------------------------------------------------------
  529. if param == "" then
  530.  
  531. -- Get travelpoints_array.
  532. local travelpoints_array = travelpoints.get_travelpoints_array("user", name)
  533.  
  534. -- Check if player has any travelpoints.
  535. if #travelpoints_array > 0 then
  536.  
  537. -- Begin output.
  538. travelpoints.print_notice(name, "Your available travelpoints:")
  539.  
  540. -- Step through travelpoints_array.
  541. for index, value in ipairs(travelpoints_array) do
  542.  
  543. -- Extract title from value: "<title> (<x>, <y>, <z>)"
  544. local title = string.match(value, "^([^ ]+)%s+")
  545.  
  546. -- Output lines.
  547. -- <n>. <title> (<x>, <y>, <z>). Saved on <date> at <time>. Descripton: <desc>
  548. travelpoints.print_notice(name, index .. ". \"" .. title .. "\" " .. minetest.pos_to_string(travelpoints_table[title].pos) .. ". Saved on " .. os.date("%Y-%m-%d at %I:%M:%S %p", travelpoints_table[title].timestamp) .. ". Description: " .. travelpoints_table[title].desc)
  549.  
  550. end
  551.  
  552. else
  553. travelpoints.print_notice(name, "You have no saved travelpoints.")
  554. end
  555.  
  556. -- Check conditions for handling max_travelpoints.
  557. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.max_travelpoints > 0 ) and ( not minetest.get_player_privs(name)["server"] ) then
  558. local max_travelpoints = travelpoints.restrictions.max_travelpoints
  559. if max_travelpoints > 0 then
  560. if tp_count < max_travelpoints then
  561. if tp_count == 0 then
  562. travelpoints.print_notice(name, "You can set " .. max_travelpoints .. " travelpoints.")
  563. else
  564. travelpoints.print_notice(name, "You can set " .. ( max_travelpoints - tp_count ) .. " more travelpoints.")
  565. end
  566. elseif tp_count == max_travelpoints then
  567. travelpoints.print_notice(name, "You can set no more travelpoints unless you /tpdrop older ones. Maximum allowed is " .. max_travelpoints .. ".")
  568. end
  569. end
  570. end
  571.  
  572. -- Cooldown remaining.
  573. if cooldown_remaining ~= "none" then
  574. travelpoints.print_notice(name, "Your remaining cooldown is: " .. cooldown_remaining .. ".")
  575. end
  576.  
  577. return
  578.  
  579. ------------------------------------------------------------------------
  580. -- /tpgo <title>
  581. ------------------------------------------------------------------------
  582. else
  583.  
  584. -- Check if player is on cooldown.
  585. if cooldown_remaining == "none" then
  586.  
  587. -- Get Title.
  588. local title = string.match(param, "^([^ ]+)%s*")
  589.  
  590. -- Validate Title.
  591. local notice = travelpoints.validate_title(title)
  592. if notice ~= nil then
  593. travelpoints.print_notice(name, notice)
  594. return
  595. end
  596.  
  597. -- Check for specified travelpoint.
  598. if not travelpoints_table[title] then
  599. travelpoints.print_notice(name, "Error: Travelpoint \"" .. title .. "\"does not exist.")
  600. return
  601. end
  602.  
  603. -- Set location for /tpback
  604. travelpoints_table._back = travelpoints.get_location(name)
  605.  
  606. -- Get player.
  607. local player = minetest.get_player_by_name(name)
  608.  
  609. -- Teleport player.
  610. player:setpos(travelpoints_table[title].pos)
  611.  
  612. -- Report.
  613. travelpoints.print_notice(name, "Teleported to travelpoint: \"" .. title .. "\". Use /tpback to return to " .. minetest.pos_to_string(travelpoints_table._back) .. "." )
  614.  
  615. -- Set cooldown if needed.
  616. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.cooldown > 0 ) then
  617. travelpoints_table._cooldown = now
  618. end
  619.  
  620. -- Save travelpoints_table.
  621. travelpoints.save_travelpoints_table("user", name, travelpoints_table)
  622.  
  623. else
  624.  
  625. -- Report
  626. travelpoints.print_notice(name, "Time remaining on your cooldown: " .. cooldown_remaining .. ".")
  627.  
  628. end
  629.  
  630. end
  631.  
  632. end,
  633. })
  634.  
  635. --[/tpgset]---------------------------------------------------------------------
  636. --
  637. -- Adds a new travelpoint to the world's global travelpoints table.
  638. --
  639. minetest.register_chatcommand("tpgset", {
  640. params = "<title> | <title> <desc>",
  641. description = "Set a new global travelpoint at your current location. Title required, description optional.",
  642. privs = {tpglobal=true},
  643. func = function(name, param)
  644.  
  645. ------------------------------------------------------------------------
  646. -- /tpgset
  647. ------------------------------------------------------------------------
  648.  
  649. if param == "" then
  650. travelpoints.print_notice(name, "Error: Travelpoint must be saved with a title.")
  651. return
  652. else
  653.  
  654. --------------------------------------------------------------------
  655. -- /tpgset <title> | <title> <desc>
  656. --------------------------------------------------------------------
  657.  
  658. local title, desc, notice, pos
  659.  
  660. -- Get parameters.
  661. if string.find(param, "^[^ ]+%s+.+") then
  662. title, desc = string.match(param, "^([^ ]+)%s+(.+)")
  663. else
  664. title = param
  665. desc = ""
  666. end
  667.  
  668. -- Validate Title.
  669. if title ~= nil then
  670. notice = travelpoints.validate_title(title)
  671. if notice ~= nil then
  672. travelpoints.print_notice(name, notice)
  673. return
  674. end
  675. end
  676.  
  677. -- Validate Description.
  678. if desc ~= "" then
  679. notice = travelpoints.validate_desc(desc)
  680. if notice ~= nil then
  681. travelpoints.print_notice(name, notice)
  682. return
  683. end
  684. end
  685.  
  686. -- Get player's location.
  687. pos = travelpoints.get_location(name)
  688.  
  689. -- Initialize temporary travelpoint table.
  690. local travelpoint = {}
  691.  
  692. -- Build travelpoint table.
  693. travelpoint.pos = pos
  694. travelpoint.desc = desc
  695. travelpoint.timestamp = os.time()
  696.  
  697. -- Get travelpoints_table.
  698. local travelpoints_table = travelpoints.get_travelpoints_table("global", name)
  699.  
  700. -- Check for duplicate title.
  701. if travelpoints_table[title] ~= nil then
  702. travelpoints.print_notice(name, "Error: A global travelpoint already exists for this title: " .. title)
  703. else
  704.  
  705. -- Merge tables.
  706. travelpoints_table[title] = travelpoint
  707.  
  708. -- Save travelpoints_table.
  709. if travelpoints.save_travelpoints_table("global", name, travelpoints_table) then
  710. travelpoints.print_notice(name, "Global travelpoint \"" .. title .. "\" has been saved.")
  711. else
  712. travelpoints.print_notice(name, "Error: Global travelpoint \"" .. title .. "\" could not be saved.")
  713. end
  714.  
  715. end
  716.  
  717. end
  718.  
  719. end,
  720. })
  721.  
  722. --[/travelpads]-----------------------------------------------------------------
  723. --
  724. -- Returns a list of transporter pads the user has placed.
  725. --
  726. minetest.register_chatcommand("travelpads", {
  727. params = "",
  728. description = "Lists the transporter pads you have placed.",
  729. privs = {travelpoints=true, travelpads=true},
  730. func = function(name, param)
  731.  
  732. -- Get travelpoints_table.
  733. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  734.  
  735. -- Initialize array
  736. local travelpads = {}
  737.  
  738. -- Pack array for count and sorting.
  739. if travelpoints_table._travelpads ~= nil then
  740. for key, value in pairs(travelpoints_table._travelpads) do
  741. if key ~= nil then
  742. table.insert(travelpads, value .. "|" .. key)
  743. end
  744. end
  745. end
  746.  
  747. -- Sort values.
  748. table.sort(travelpads, function(A, B) return A > B end)
  749.  
  750. local pad_count = #travelpads
  751.  
  752. local now = os.time()
  753.  
  754. -- List player's travelpads if there are any.
  755. if #travelpads == 0 then
  756. travelpoints.print_notice(name, "You have no placed transporter pads.")
  757. else
  758. local count = 1
  759. for key, value in ipairs(travelpads) do
  760. local values = value:split("|") -- builtin/misc_helpers.lua
  761. local since = now - values[1]
  762. -- <n>. Placed at (<x>, <y>, <z>) <duration> ago.
  763. travelpoints.print_notice(name, count .. ". Placed at " .. values[2] .. " " .. travelpoints.get_duration(since, 3) .. " ago.")
  764. count = count + 1
  765. end
  766. end
  767.  
  768. -- Check conditions for handling max_travelpads.
  769. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.max_travelpads > 0 ) and ( not minetest.get_player_privs(name)["server"] ) then
  770. local max_travelpads = travelpoints.restrictions.max_travelpads
  771. if max_travelpads > 0 then
  772. if pad_count < max_travelpads then
  773. if pad_count == 0 then
  774. travelpoints.print_notice(name, "You can place " .. max_travelpads .. " transporter pads.")
  775. else
  776. travelpoints.print_notice(name, "You can place " .. ( max_travelpads - pad_count ) .. " more transporter pads.")
  777. end
  778. elseif pad_count == max_travelpads then
  779. travelpoints.print_notice(name, "You can place no more transporter pads unless you remove older ones. Maximum allowed is " .. max_travelpads .. ".")
  780. end
  781. end
  782. end
  783.  
  784. end,
  785. })
  786.  
  787. --[/travelpoints]------------------------------------------------------------------
  788. --
  789. -- Gives player information about the mod and allows those with server privs to
  790. -- modify restrictions in game.
  791. --
  792. minetest.register_chatcommand("travelpoints", {
  793. params = "(nothing) | set | set <restriction> <value>",
  794. description = "Provides players with details about the mod. Players with server privilege can use \"/travelpoints set\" to change restrictions.",
  795. privs = {travelpoints=true},
  796. func = function(name, param)
  797.  
  798. ------------------------------------------------------------------------
  799. -- /travelpoints
  800. ------------------------------------------------------------------------
  801.  
  802. if param == "" then
  803.  
  804. if ( not minetest.is_singleplayer() ) then
  805.  
  806. local max_travelpoints
  807. local max_travelpads
  808. local cooldown
  809. local player_cooldown = "none"
  810. local travelpads = {}
  811. local tpback
  812. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  813. local travelpoints_array = travelpoints.get_travelpoints_array("user", name)
  814.  
  815. -- Max travelpoints
  816. if minetest.get_player_privs(name)["server"] then
  817. max_travelpoints = "No limit (server privilege)"
  818. elseif travelpoints.restrictions.max_travelpoints == 0 then
  819. max_travelpoints = "No limit"
  820. else
  821. max_travelpoints = travelpoints.restrictions.max_travelpoints
  822. end
  823.  
  824. -- Max travelpads
  825. if minetest.get_player_privs(name)["server"] then
  826. max_travelpads = "No limit (server privilege)"
  827. elseif travelpoints.restrictions.max_travelpads == 0 then
  828. max_travelpads = "No limit"
  829. else
  830. max_travelpads = travelpoints.restrictions.max_travelpads
  831. end
  832.  
  833. -- Cooldown
  834. if minetest.get_player_privs(name)["server"] then
  835. cooldown = "No cooldown (server privilege)"
  836. elseif travelpoints.restrictions.cooldown == 0 then
  837. cooldown = "No cooldown"
  838. else
  839. cooldown = travelpoints.get_duration(travelpoints.restrictions.cooldown)
  840. end
  841.  
  842. -- Player cooldown
  843. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.cooldown > 0 ) and ( not minetest.get_player_privs(name)["server"] ) then
  844. if travelpoints_table._cooldown ~= nil then
  845. local difference = os.time() - travelpoints_table._cooldown
  846. if difference < travelpoints.restrictions.cooldown then
  847. player_cooldown = travelpoints.get_duration(difference)
  848. end
  849. end
  850. end
  851.  
  852. if travelpoints.restrictions.clear_back_pos > 0 then
  853. tpback = "is cleared after use"
  854. else
  855. tpback = "not cleared after use"
  856. end
  857.  
  858. if travelpoints_table._travelpads ~= nil then
  859. -- Pack array to get travelpad count.
  860. for key, value in pairs(travelpoints_table._travelpads) do
  861. if key ~= nil then
  862. table.insert(travelpads, key)
  863. end
  864. end
  865. end
  866.  
  867. -- Report
  868. travelpoints.print_notice(name, "Running Travelpoints version " .. travelpoints.version_number .. " released on " .. travelpoints.version_date .. ".")
  869. travelpoints.print_notice(name, "Restrictions:")
  870. travelpoints.print_notice(name, "Max Travelpoints: [" .. max_travelpoints .. "] You have: [" .. #travelpoints_array .. "]")
  871. travelpoints.print_notice(name, "Max Transporter Pads: [" .. max_travelpads .. "] You have: [" .. #travelpads .. "]")
  872. travelpoints.print_notice(name, "Cooldown: [" .. cooldown .. "] Your cooldown is: [" .. player_cooldown .. "]")
  873. travelpoints.print_notice(name, "Back Location: [" .. tpback .. "]")
  874.  
  875. else
  876. -- Report
  877. travelpoints.print_notice(name, "Running Travelpoints version " .. travelpoints.version_number .. " released on " .. travelpoints.version_date .. ".")
  878. end
  879.  
  880. ------------------------------------------------------------------------
  881. -- /travelpoints set
  882. ------------------------------------------------------------------------
  883.  
  884. -- Show available restrictions.
  885. elseif param == "set" then
  886.  
  887. -- Check privs.
  888. if minetest.get_player_privs(name)["server"] then
  889. travelpoints.print_notice(name, "Available restrictions to modify with \"/travelpoints set <restriction> <value>\" are:")
  890. travelpoints.print_notice(name, "max_travelpoints <value> - Change travelpoints limit, \"0\" for no limit. Currently: " .. travelpoints.restrictions.max_travelpoints)
  891. travelpoints.print_notice(name, "max_travelpads <value> - Change travelpads limit, \"0\" for no limit. Currently: " .. travelpoints.restrictions.max_travelpads)
  892. travelpoints.print_notice(name, "cooldown <value> - Change cooldown time, \"0\" for no cooldown. Currently: " .. travelpoints.restrictions.cooldown)
  893. travelpoints.print_notice(name, "clear_back_pos <value> - Change /tpback location setting. Currently: " .. travelpoints.restrictions.clear_back_pos)
  894. else
  895. travelpoints.print_notice(name, "Server privilege required for that command.")
  896. end
  897.  
  898. ------------------------------------------------------------------------
  899. -- /travelpoints set <restriction> <value>
  900. ------------------------------------------------------------------------
  901.  
  902. -- Check parameters.
  903. elseif string.find(param, "^set [%w_ ]+") then
  904.  
  905. -- Check privs.
  906. if minetest.get_player_privs(name)["server"] then
  907.  
  908. local restriction, value
  909.  
  910. -- Split parameters
  911. local parameters = param:split(" ") -- builtin/misc_helpers.lua
  912.  
  913. -- Table to test <restriction> against.
  914. local restrictions = { max_travelpoints = true, max_travelpads = true, cooldown = true, clear_back_pos = true }
  915.  
  916. -- Validate <restriction>
  917. if restrictions[parameters[2]] then
  918. restriction = parameters[2]
  919. else
  920. travelpoints.print_notice(name, "Error: Restriction name was mistyped.")
  921. return
  922. end
  923.  
  924. -- Validate <value>
  925. if type(tonumber(parameters[3])) == "number" then
  926. value = tonumber(parameters[3])
  927. else
  928. travelpoints.print_notice(name, "Error: Restriction value must be a number.")
  929. return
  930. end
  931.  
  932. local change_made = false
  933.  
  934. -- Validate input.
  935. local result, error_message = travelpoints.validate_restriction_value(restriction, value)
  936.  
  937. -- Max Travelpoints
  938. if restriction == "max_travelpoints" then
  939.  
  940. if value == travelpoints.restrictions.max_travelpoints then
  941. travelpoints.print_notice(name, "There was no change to max_travelpoints.")
  942. elseif result then
  943. travelpoints.restrictions.max_travelpoints = value
  944. travelpoints.print_notice(name, "Value of max_travelpoints is now " .. travelpoints.restrictions.max_travelpoints .. ".")
  945. change_made = true
  946. else
  947. travelpoints.print_notice(name, error_message)
  948. end
  949.  
  950. -- Max Travelpads
  951. elseif restriction == "max_travelpads" then
  952.  
  953. if value == travelpoints.restrictions.max_travelpads then
  954. travelpoints.print_notice(name, "There was no change to max_travelpads.")
  955. elseif result then
  956. travelpoints.restrictions.max_travelpads = value
  957. travelpoints.print_notice(name, "Value of max_travelpads is now " .. travelpoints.restrictions.max_travelpads .. ".")
  958. change_made = true
  959. else
  960. travelpoints.print_notice(name, error_message)
  961. end
  962.  
  963. -- Cooldown
  964. elseif restriction == "cooldown" then
  965.  
  966. if value == travelpoints.restrictions.cooldown then
  967. travelpoints.print_notice(name, "There was no change to cooldown.")
  968. elseif result then
  969. travelpoints.restrictions.cooldown = value
  970. travelpoints.print_notice(name, "Value of cooldown is now " .. travelpoints.restrictions.cooldown .. ".")
  971. change_made = true
  972. else
  973. travelpoints.print_notice(name, error_message)
  974. end
  975.  
  976. -- Clear Back Pos
  977. elseif restriction == "clear_back_pos" then
  978.  
  979. if value == travelpoints.restrictions.clear_back_pos then
  980. travelpoints.print_notice(name, "There was no change to clear_back_pos.")
  981. elseif result then
  982. travelpoints.restrictions.clear_back_pos = value
  983. travelpoints.print_notice(name, "Value of clear_back_pos is now " .. travelpoints.restrictions.clear_back_pos .. ".")
  984. change_made = true
  985. else
  986. travelpoints.print_notice(name, error_message)
  987. end
  988.  
  989. else
  990. travelpoints.print_notice(name, "Error: Restriction name was mistyped.")
  991. end
  992.  
  993. -- Save changes.
  994. if change_made then
  995. -- Save changes to world's "travelpoints_restrictions" file.
  996. if travelpoints.save_world_restrictions(travelpoints.restrictions) then
  997. travelpoints.print_notice(name, "Restrictions for this world saved.")
  998. else
  999. travelpoints.print_notice(name, "Error: Restrictions for this world could not be saved.")
  1000. end
  1001. end
  1002.  
  1003. else
  1004. travelpoints.print_notice(name, "Server privilege required for that command.")
  1005. end
  1006.  
  1007. else
  1008. travelpoints.print_notice(name, "Error: Command could not be processed, you may have mistyped it.")
  1009. end
  1010.  
  1011. end,
  1012.  
  1013. })
  1014.  
  1015. --[/tpset]---------------------------------------------------------------------
  1016. --
  1017. -- Adds a new travelpoint to the player's travelpoints_table for the current
  1018. -- world.
  1019. --
  1020. minetest.register_chatcommand("tpset", {
  1021. params = "<title> | <title> <desc>",
  1022. description = "Set a new travelpoint at your current location. Title required, description optional.",
  1023. privs = {travelpoints=true},
  1024. func = function(name, param)
  1025.  
  1026. ------------------------------------------------------------------------
  1027. -- /tpset
  1028. ------------------------------------------------------------------------
  1029.  
  1030. if param == "" then
  1031. travelpoints.print_notice(name, "Error: Travelpoint must be saved with a title.")
  1032. return
  1033. else
  1034.  
  1035. ------------------------------------------------------------------------
  1036. -- /tpset <title> | <title> <desc>
  1037. ------------------------------------------------------------------------
  1038.  
  1039. local tp_count = #travelpoints.get_travelpoints_array("user", name)
  1040.  
  1041. -- Handle maximum_travelpoints if it is configured.
  1042. if ( not minetest.is_singleplayer() ) and ( travelpoints.restrictions.max_travelpoints > 0 ) and ( tp_count >= travelpoints.restrictions.max_travelpoints ) and ( not minetest.get_player_privs(name)["server"] ) then
  1043.  
  1044. travelpoints.print_notice(name, "You have already reached your maximum number of travelpoints: " .. travelpoints.restrictions.max_travelpoints .. ".")
  1045.  
  1046. return
  1047.  
  1048. else
  1049.  
  1050. local title, desc, notice, pos
  1051.  
  1052. -- Get parameters.
  1053. if string.find(param, "^[^ ]+%s+.+") then
  1054. title, desc = string.match(param, "^([^ ]+)%s+(.+)")
  1055. else
  1056. title = param
  1057. desc = ""
  1058. end
  1059.  
  1060. -- Validate Title.
  1061. if title ~= nil then
  1062. notice = travelpoints.validate_title(title)
  1063. if notice ~= nil then
  1064. travelpoints.print_notice(name, notice)
  1065. return
  1066. end
  1067. end
  1068.  
  1069. -- Validate Description.
  1070. if desc ~= "" then
  1071. notice = travelpoints.validate_desc(desc)
  1072. if notice ~= nil then
  1073. travelpoints.print_notice(name, notice)
  1074. return
  1075. end
  1076. end
  1077.  
  1078. -- Get player's location.
  1079. pos = travelpoints.get_location(name)
  1080.  
  1081. -- Initialize temporary travelpoint table.
  1082. local travelpoint = {}
  1083.  
  1084. -- Build travelpoint table.
  1085. travelpoint.pos = pos
  1086. travelpoint.desc = desc
  1087. travelpoint.timestamp = os.time()
  1088.  
  1089. -- Get travelpoints_table.
  1090. local travelpoints_table = travelpoints.get_travelpoints_table("user", name)
  1091.  
  1092. -- Check for duplicate title.
  1093. if travelpoints_table[title] ~= nil then
  1094. travelpoints.print_notice(name, "Error: A travelpoint already exists for this title: " .. title)
  1095. else
  1096.  
  1097. -- Merg tables.
  1098. travelpoints_table[title] = travelpoint
  1099.  
  1100. -- Save travelpoints_table.
  1101. if travelpoints.save_travelpoints_table("user", name, travelpoints_table) then
  1102. travelpoints.print_notice(name, "Travelpoint \"" .. title .. "\" has been saved.")
  1103. else
  1104. travelpoints.print_notice(name, "Error: Travelpoint \"" .. title .. "\" could not be saved.")
  1105. end
  1106.  
  1107. end
  1108.  
  1109. end
  1110.  
  1111. end
  1112.  
  1113. end,
  1114. })
  1115.  
  1116. --------------------------------------------------------------------------------
  1117. -- EOF
  1118. --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement