Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. local pkg = {}
  2.  
  3. local base = Vec3(0,0,0)
  4. local options = {
  5. numHeads = 5
  6. }
  7. local data = {}
  8.  
  9. local function log(text)
  10. --spell:execute("say %s",text)
  11. end
  12.  
  13. local function saveData()
  14. local text = str(data)
  15. log("saving data: "..text)
  16. spell.pos = base
  17. spell.block = Blocks.get("command_block"):withNbt({Command=text})
  18. end
  19.  
  20. local function loadData()
  21. spell.pos = base
  22. if spell.block.nbt then
  23. local text = spell.block.nbt.Command
  24. if text ~= nil then
  25. local code = "return "..text
  26. local func = load(code)
  27. data = func()
  28. log("loaded data: "..str(text))
  29. end
  30. end
  31. end
  32.  
  33. local function giveHeads(player, num)
  34. log("giving heads to player: "..player.name..": "..num)
  35. for i=1,num do
  36. player:giveHead()
  37. end
  38. end
  39.  
  40.  
  41. local function handle(player)
  42. log("handling player: "..player.name)
  43. local entry = data[player.name]
  44. if entry==nil then
  45. entry = {
  46. headsGiven=0
  47. }
  48. data[player.name] = entry
  49. end
  50. local heads = options.numHeads - entry.headsGiven
  51. log("player has heads given already: "..entry.headsGiven)
  52. giveHeads(player, heads)
  53. entry.headsGiven = entry.headsGiven + heads
  54. saveData()
  55. end
  56.  
  57. function pkg.heads( aOptions)
  58. options = aOptions or options
  59.  
  60. base = spell.pos
  61. spell:singleton("mickkay.heads")
  62. loadData()
  63.  
  64. local q = Events.connect("PlayerLoggedInEvent")
  65. while true do
  66. local e = q:pop()
  67. handle(e.player)
  68. end
  69. end
  70.  
  71. Help.on(pkg.heads) [[
  72. () - Gives each player 5 heads at first login.
  73. ]]
  74.  
  75. return pkg
Add Comment
Please, Sign In to add comment