init.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. dofile(minetest.get_modpath("creative") .. "/tool.lua")
  40. -- Unlimited node placement
  41. minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
  42. if placer and placer:is_player() then
  43. return creative.is_enabled_for(placer:get_player_name())
  44. end
  45. end)
  46. -- Don't pick up if the item is already in the inventory
  47. local old_handle_node_drops = minetest.handle_node_drops
  48. function minetest.handle_node_drops(pos, drops, digger)
  49. if not digger or not digger:is_player() or
  50. not creative.is_enabled_for(digger:get_player_name()) then
  51. return old_handle_node_drops(pos, drops, digger)
  52. end
  53. local inv = digger:get_inventory()
  54. if inv then
  55. for _, item in ipairs(drops) do
  56. if not inv:contains_item("main", item, true) then
  57. inv:add_item("main", item)
  58. end
  59. end
  60. end
  61. end