-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagichelmet.lua
More file actions
136 lines (118 loc) · 3.12 KB
/
magichelmet.lua
File metadata and controls
136 lines (118 loc) · 3.12 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- red-magic/magichelmet.lua
-- TODO add damage to helmet at every use
-- /lua require("red-magic.magichelmet").start()
-- /lua require("red-magic.magichelmet").giveItem()
local module = ...
require "mickkay.wol.Spell"
local pkg = {}
local VISUALIZER_MODULE= 'claiming.claimvisualizer'
local CLAIMING_MODULE = 'claiming.claiming'
local ITEM_CODE = "Magic Helmet"
local ITEM_DISPLAY_NAME = "Magic Helmet"
local DURABILITY = 77
local USE_DELAY = 20*4
local log
local getNearestClaim
local getMagicItem
local isMagicItemNbt
local addDamage
local getHelmetNbt
local lastUsed = {}
function pkg.giveItem(entity,count)
entity = entity or spell.owner
count = count or 1
local item = Items.get("golden_helmet")
item:putNbt( {
tag= {
MagicItem = ITEM_CODE,
ench= {{id=999, lvl=1}}
}
})
item.displayName = ITEM_DISPLAY_NAME
entity:dropItem(item,count)
end
function pkg.start()
spell:singleton( module)
local claiming = require(CLAIMING_MODULE)
if not claiming then
error("Can't find claiming spell")
end
local queue = Events.collect("SwingArmEvent")
while true do
local event = queue:next()
local player = event.player
if not lastUsed[player.name] or lastUsed[player.name] + USE_DELAY < Time.gametime then
local helmetNbt = getHelmetNbt(player)
if helmetNbt and isMagicItemNbt(helmetNbt) then
--local item = getMagicItem(player)
--if item then
local claim = getNearestClaim(claiming.getApplicableClaims(player.pos), player.pos)
if claim then
local width = claim.width
local center = claim.pos
spell:execute([[
lua require('%s').showBorders('%s', Vec3(%s,%s,%s),%s)
]], VISUALIZER_MODULE, player.name, center.x, center.y, center.z, width)
lastUsed[player.name] = Time.gametime
--addDamage(item,1)
end
end
end
end
end
function pkg.stop()
spell:singleton(module)
end
function getNearestClaim(cs, pos)
local center = Vec3(pos.x, 0, pos.z)
local result = nil
local bestDist = nil
for _,c in pairs(cs) do
local ref = Vec3(c.pos.x, 0, c.pos.z)
local dist = (center-ref):magnitude()
if not bestDist or dist < bestDist then
bestDist = dist
result = c
end
end
return result
end
function getMagicItem( player)
local item = player.mainhand
if item and isMagicItemNbt(item.nbt) then
--log("item.damage=%s",item.damage)
if item.damage > DURABILITY then
player.mainhand = nil
item = nil
end
return item
else
return nil
end
end
function isMagicItemNbt( nbt)
return nbt and nbt.tag and nbt.tag.MagicItem == ITEM_CODE
end
function addDamage(item, amount)
item.damage = item.damage + 1
end
function getHelmetNbt(player)
local inventory=player.nbt.Inventory
if inventory ~= nil then
for _,inv in pairs(inventory) do
if inv.Slot==103 then
return inv
end
end
end
return nil
end
-- Logs the given message into the chat
function log(message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
spell:execute("say %s", message)
end
return pkg