init.lua 2.4 KB

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