particle_override.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- We need to override the Minetest particle API in order to
  2. -- control which clients receive particles.
  3. local players = {}
  4. local modmeta = minetest.get_mod_storage()
  5. local maxdist = 50
  6. -- Save original functions.
  7. local add_particlespawner = minetest.add_particlespawner
  8. local add_particle = minetest.add_particle
  9. function default.particles_enabled_for(pname)
  10. local key = "particles:" .. pname
  11. if modmeta:get_int(key) == 1 then
  12. return false
  13. end
  14. return true
  15. end
  16. -- Called from the control panel GUI.
  17. function default.enable_particles_for(pname, enable)
  18. local key = "particles:" .. pname
  19. if enable == true then
  20. modmeta:set_int(key, 0)
  21. if minetest.get_player_by_name(pname) then
  22. players[pname] = true
  23. end
  24. else
  25. modmeta:set_int(key, 1)
  26. players[pname] = nil
  27. end
  28. end
  29. function minetest.add_particlespawner_single(data)
  30. local id
  31. local pname = data.playername or ""
  32. if players[pname] then
  33. local player = minetest.get_player_by_name(pname)
  34. if player then
  35. if vector.distance(player:get_pos(), data.minpos) <= maxdist then
  36. id = add_particlespawner(data)
  37. end
  38. end
  39. end
  40. return id
  41. end
  42. function minetest.add_particlespawner(data)
  43. for k, v in pairs(players) do
  44. local player = minetest.get_player_by_name(k)
  45. if player then
  46. if vector.distance(player:get_pos(), data.minpos) <= maxdist then
  47. data.playername = k
  48. add_particlespawner(data)
  49. end
  50. end
  51. end
  52. end
  53. function minetest.add_particle(data)
  54. for k, v in pairs(players) do
  55. local player = minetest.get_player_by_name(k)
  56. if player then
  57. if vector.distance(player:get_pos(), data.pos) <= maxdist then
  58. data.playername = k
  59. add_particle(data)
  60. end
  61. end
  62. end
  63. end
  64. minetest.register_on_joinplayer(function(player)
  65. local pname = player:get_player_name()
  66. local key = "particles:" .. pname
  67. if modmeta:get_int(key) == 0 then
  68. players[pname] = true
  69. else
  70. players[pname] = nil
  71. end
  72. end)
  73. minetest.register_on_leaveplayer(function(player)
  74. local pname = player:get_player_name()
  75. players[pname] = nil
  76. end)