init.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. creative = {}
  2. local S = minetest.get_translator("creative")
  3. local function update_sfinv(name)
  4. minetest.after(0, function()
  5. local player = minetest.get_player_by_name(name)
  6. if player then
  7. if sfinv.get_page(player):sub(1, 9) == "creative:" then
  8. sfinv.set_page(player, sfinv.get_homepage_name(player))
  9. else
  10. sfinv.set_player_inventory_formspec(player)
  11. end
  12. end
  13. end)
  14. end
  15. minetest.register_privilege("creative", {
  16. description = "Allow player to use creative inventory",
  17. give_to_singleplayer = false,
  18. give_to_admin = false,
  19. on_grant = update_sfinv,
  20. on_revoke = update_sfinv,
  21. })
  22. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  23. if creative_mode_cache
  24. then
  25. minetest.after(1,
  26. function()
  27. minetest.chat_send_all(S("Global creative mode is disabled in this game. Use /grantme creative instead."))
  28. end)
  29. end
  30. function creative.is_enabled_for(name)
  31. return minetest.check_player_privs(name, {creative = true})
  32. end
  33. dofile(minetest.get_modpath("creative") .. "/inventory.lua")
  34. if creative_mode_cache then
  35. -- Dig time is modified according to difference (leveldiff) between tool
  36. -- 'maxlevel' and node 'level'. Digtime is divided by the larger of
  37. -- leveldiff and 1.
  38. -- To speed up digging in creative, hand 'maxlevel' and 'digtime' have been
  39. -- increased such that nodes of differing levels have an insignificant
  40. -- effect on digtime.
  41. local digtime = 42
  42. local caps = {times = {digtime, digtime, digtime}, uses = 0, maxlevel = 256}
  43. minetest.register_item(":", {
  44. type = "none",
  45. wield_image = "wieldhand.png",
  46. wield_scale = {x = 1, y = 1, z = 2.5},
  47. range = 10,
  48. tool_capabilities = {
  49. full_punch_interval = 0.5,
  50. max_drop_level = 3,
  51. groupcaps = {
  52. crumbly = caps,
  53. cracky = caps,
  54. snappy = caps,
  55. choppy = caps,
  56. oddly_breakable_by_hand = caps,
  57. },
  58. damage_groups = {fleshy = 10},
  59. }
  60. })
  61. end
  62. -- Unlimited node placement
  63. minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
  64. if placer and placer:is_player() then
  65. return creative.is_enabled_for(placer:get_player_name())
  66. end
  67. end)
  68. -- Don't pick up if the item is already in the inventory
  69. local old_handle_node_drops = minetest.handle_node_drops
  70. function minetest.handle_node_drops(pos, drops, digger)
  71. if not digger or not digger:is_player() or
  72. not creative.is_enabled_for(digger:get_player_name()) then
  73. return old_handle_node_drops(pos, drops, digger)
  74. end
  75. local inv = digger:get_inventory()
  76. if inv then
  77. for _, item in ipairs(drops) do
  78. if not inv:contains_item("main", item, true) then
  79. inv:add_item("main", item)
  80. end
  81. end
  82. end
  83. end