init.lua 2.8 KB

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