Skip to content

Instantly share code, notes, and snippets.

@mkarneim
Created January 11, 2018 21:36
Show Gist options
  • Select an option

  • Save mkarneim/d9200715f2e60be23a51c180716c903f to your computer and use it in GitHub Desktop.

Select an option

Save mkarneim/d9200715f2e60be23a51c180716c903f to your computer and use it in GitHub Desktop.
Wol Spell
local pkg = {}
local base = Vec3(0,0,0)
local options = {
numHeads = 5
}
local data = {}
local function log(text)
--spell:execute("say %s",text)
end
local function saveData()
local text = str(data)
log("saving data: "..text)
spell.pos = base
spell.block = Blocks.get("command_block"):withNbt({Command=text})
end
local function loadData()
spell.pos = base
if spell.block.nbt then
local text = spell.block.nbt.Command
if text ~= nil then
local code = "return "..text
local func = load(code)
data = func()
log("loaded data: "..str(text))
end
end
end
local function giveHeads(player, num)
log("giving heads to player: "..player.name..": "..num)
for i=1,num do
player:giveHead()
end
end
local function handle(player)
log("handling player: "..player.name)
local entry = data[player.name]
if entry==nil then
entry = {
headsGiven=0
}
data[player.name] = entry
end
local heads = options.numHeads - entry.headsGiven
log("player has heads given already: "..entry.headsGiven)
giveHeads(player, heads)
entry.headsGiven = entry.headsGiven + heads
saveData()
end
function pkg.heads( aOptions)
options = aOptions or options
base = spell.pos
spell:singleton("mickkay.heads")
loadData()
local q = Events.connect("PlayerLoggedInEvent")
while true do
local e = q:pop()
handle(e.player)
end
end
Help.on(pkg.heads) [[
() - Gives each player 5 heads at first login.
]]
return pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment