-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgive-head.lua
86 lines (77 loc) · 1.81 KB
/
give-head.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- claiming-heads/give-head.lua
-- /lua require('claiming-heads.give-head').enable(true)
local pkg = {}
local module = ...
local CMD = 'give-head'
local USAGE = "/give-head [<player>] [<head's owner>]"
local showError
local getPlayers
local namesOf
local tell
function pkg.enable(value)
value = value or true
if value then
Commands.register(CMD,string.format([[
require('%s').giveHead(...)
]], module), USAGE, 1)
else
pcall(function()
Commands.deregister(CMD)
end)
end
end
function pkg.giveHead(toSelector, headSelector)
if not toSelector then
showError("Usage: "..USAGE)
return
end
local toPlayers = getPlayers(toSelector)
local heads = nil
if headSelector then
heads = getPlayers(headSelector)
end
for _,p in pairs(toPlayers) do
if heads then
for _,h in pairs(heads) do
spell:execute([[/give %s skull 1 3 {SkullOwner:"%s"}]], p, h)
end
else
spell:execute([[/give %s skull 1 3 {SkullOwner:"%s"}]], p, p)
end
end
end
function getPlayers(selector)
local prefix = string.sub(selector, 1, 1)
if prefix=="@" then
return namesOf(Entities.find(selector))
end
local players = Entities.find(string.format("@a[name=%s]",selector))
if #players == 0 then
return {selector}
else
return namesOf(players)
end
end
function namesOf(players)
local result = {}
for _,p in pairs(players) do
if type(p) == "Player" then
table.insert(result, p.name)
end
end
return result
end
function showError(message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
tell(spell.owner.name, message, 'red')
end
function tell(to, message, color)
color = color or 'white'
spell:execute([[
/tellraw %s [{"text":"%s","color":"%s"}]
]], to, message, color)
end
return pkg