-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHookHandlers.lua
409 lines (305 loc) · 12.3 KB
/
HookHandlers.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
-- HookHandlers.lua
-- Implements the handlers for hooks, as needed to prevent players from interacting with areas
local function OnPlayerBrokenBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ)
-- Update the area's DateLastChanged:
local Area = FindPlayerAreaByCoords(a_Player, a_BlockX, a_BlockZ)
if (Area ~= nil) then
Area.NumBrokenBlocks = Area.NumBrokenBlocks + 1
g_DB:UpdateAreaStats(Area)
end
-- Allow other plugins to execute:
return false
end
local function OnPlayerDestroyed(a_Player)
-- TODO: Remove the player's areas from the global list
--[[
NOTE: We can't do it by simply removing the areas, we support multiple logins of the same player, so we
need to take care of one of the multiple logins disconnecting, but the rest staying - can't just erase the
areas.
So for now we "leak" the areas, it shouldn't be too much anyway - a few kilobytes at most per each unique
playername.
--]]
-- RemovePlayerAreas(a_Player)
-- RemovePlayerAllowances(a_Player)
end
local function OnPlayerLeftClick(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status)
if ((a_BlockFace >= 0) and (HandleTemplatingLeftClick(a_Player, a_BlockX, a_BlockZ))) then
return true;
end
local CanInteract, Reason = CanPlayerInteractWithBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ);
if (CanInteract) then
return false;
end
a_Player:SendMessage("You are not allowed to dig here. " .. Reason);
return true;
end
local function OnPlayerPlacedBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ)
-- Update the area's DateLastChanged:
local Area = FindPlayerAreaByCoords(a_Player, a_BlockX, a_BlockZ)
if (Area ~= nil) then
Area.NumPlacedBlocks = Area.NumPlacedBlocks + 1
g_DB:UpdateAreaStats(Area)
end
-- Allow other plugins to execute:
return false
end
local function OnPlayerPlacingBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta)
local CanInteract, Reason = CanPlayerInteractWithBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ);
if (CanInteract) then
return false;
end
a_Player:SendMessage("You are not allowed to build here. " .. Reason);
return true;
end
local function OnPlayerRightClick(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_Status)
if (a_BlockFace < 0) then
-- This really means "use item" and no valid coords are given
return false;
end
if (HandleTemplatingRightClick(a_Player, a_BlockX, a_BlockZ)) then
return true;
end
local BlockX, BlockY, BlockZ = AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
local CanInteract, Reason = CanPlayerInteractWithBlock(a_Player, BlockX, BlockY, BlockZ);
if (CanInteract) then
return false;
end
if (a_Player:GetEquippedItem().m_ItemType == E_BLOCK_AIR) then
return false
end
a_Player:SendMessage("You are not allowed to build here. " .. Reason);
return true;
end
local function OnPlayerSpawned(a_Player)
-- Read this player's areas for this world:
local WorldName = a_Player:GetWorld():GetName();
local PlayerName = a_Player:GetName();
SetPlayerAreas(a_Player, g_DB:LoadPlayerAreasInWorld(WorldName, PlayerName));
SetPlayerAllowances(WorldName, PlayerName, g_DB:LoadPlayerAllowancesInWorld(WorldName, PlayerName));
return false;
end
--- Returns the gallery that covers the entire chunk's area
-- Returns nil if no such gallery
local function GetGalleryForEntireChunk(a_WorldName, a_ChunkX, a_ChunkZ)
-- Check params:
assert(type(a_WorldName) == "string")
assert(type(a_ChunkX) == "number")
assert(type(a_ChunkZ) == "number")
local MinX = a_ChunkX * 16;
local MinZ = a_ChunkZ * 16;
local MaxX = MinX + 16;
local MaxZ = MinZ + 16;
for idx, gal in ipairs(g_Galleries) do
if (
(gal.WorldName == a_WorldName) and
(gal.AreaMinX <= MinX) and (gal.AreaMaxX >= MaxX) and
(gal.AreaMinZ <= MinZ) and (gal.AreaMaxZ >= MaxZ)
) then
return gal;
end
end
return nil;
end
--- Imprints the specified chunk with the specified gallery's template, where (if) they intersect
-- If a_ClearAbove is true, the AreaTemplateSchematicTop is applied, too
local function ImprintChunkWithGallery(a_MinX, a_MinZ, a_MaxX, a_MaxZ, a_ChunkDesc, a_Gallery, a_ClearAbove)
if (a_Gallery.AreaTemplateSchematic == nil) then
-- This gallery doesn't have a template
return;
end
if (
(a_Gallery.AreaMaxX < a_MinX) or (a_Gallery.AreaMinX > a_MaxX) or
(a_Gallery.AreaMaxZ < a_MinZ) or (a_Gallery.AreaMinZ > a_MaxZ)
) then
-- This gallery doesn't intersect the chunk
return;
end
-- Calc the bounds of the areas that should be present in this chunk:
local SizeX = a_Gallery.AreaSizeX;
local SizeZ = a_Gallery.AreaSizeZ;
local StartX = a_Gallery.AreaMinX + SizeX * math.floor((a_MinX - a_Gallery.AreaMinX) / SizeX);
local EndX = a_Gallery.AreaMinX + SizeX * math.ceil ((a_MaxX - a_Gallery.AreaMinX) / SizeX);
local StartZ = a_Gallery.AreaMinZ + SizeZ * math.floor((a_MinZ - a_Gallery.AreaMinZ) / SizeZ);
local EndZ = a_Gallery.AreaMinZ + SizeZ * math.ceil ((a_MaxZ - a_Gallery.AreaMinZ) / SizeZ);
if (StartX < a_Gallery.AreaMinX) then
StartX = a_Gallery.AreaMinX
end
if (EndX > a_Gallery.AreaMaxX) then
EndX = a_Gallery.AreaMaxX
end
if (StartZ < a_Gallery.AreaMinZ) then
StartZ = a_Gallery.AreaMinZ
end
if (EndZ > a_Gallery.AreaMaxZ) then
EndZ = a_Gallery.AreaMaxZ
end
local FromX = StartX - a_ChunkDesc:GetChunkX() * 16;
local ToX = EndX - a_ChunkDesc:GetChunkX() * 16 - 1;
local FromZ = StartZ - a_ChunkDesc:GetChunkZ() * 16;
local ToZ = EndZ - a_ChunkDesc:GetChunkZ() * 16 - 1;
-- Imprint the schematic into the chunk
local Template = a_Gallery.AreaTemplateSchematic;
local TemplateTop = a_Gallery.AreaTemplateSchematicTop;
local Top = a_Gallery.AreaTop;
for z = FromZ, ToZ, a_Gallery.AreaSizeZ do
for x = FromX, ToX, a_Gallery.AreaSizeX do
a_ChunkDesc:WriteBlockArea(Template, x, 0, z);
if (a_ClearAbove and TemplateTop) then
a_ChunkDesc:WriteBlockArea(TemplateTop, x, Top, z);
end
end
end
-- Imprint the biome into the portion of the chunk covered by the gallery
local Biome = a_Gallery.Biome;
if (Biome ~= nil) then
local BlockX = a_ChunkDesc:GetChunkX() * 16;
local BlockZ = a_ChunkDesc:GetChunkZ() * 16;
local MinX = 0;
if (a_Gallery.AreaMinX > BlockX) then
MinX = a_Gallery.AreaMinX - BlockX;
end
local MaxX = 15;
if (a_Gallery.AreaMaxX < BlockX + 15) then
MaxX = a_Gallery.AreaMaxX - BlockX;
end
local MinZ = 0;
if (a_Gallery.AreaMinZ > BlockZ) then
MinZ = a_Gallery.AreaMinZ - BlockZ;
end
local MaxZ = 15;
if (a_Gallery.AreaMaxZ < BlockZ + 15) then
MaxZ = a_Gallery.AreaMaxZ - BlockZ;
end
for z = MinZ, MaxZ do for x = MinX, MaxX do
a_ChunkDesc:SetBiome(x, z, Biome);
end end
end
-- Fix the heightmap after all those changes:
a_ChunkDesc:UpdateHeightmap();
end
--- Imprints the specified chunk with all the intersecting galleries' templates
-- if a_ClearAbove is true, the AreaTemplateSchematicTop is applied, too
local function ImprintChunk(a_WorldName, a_ChunkX, a_ChunkZ, a_ChunkDesc, a_ClearAbove)
-- Check params:
assert(type(a_WorldName) == "string")
assert(type(a_ChunkX) == "number")
assert(type(a_ChunkZ) == "number")
local MinX = a_ChunkX * 16;
local MinZ = a_ChunkZ * 16;
local MaxX = MinX + 16;
local MaxZ = MinZ + 16;
for idx, gal in ipairs(g_Galleries) do
if (gal.WorldName == a_WorldName) then
ImprintChunkWithGallery(MinX, MinZ, MaxX, MaxZ, a_ChunkDesc, gal, a_ClearAbove);
end
end
end
local function OnChunkGenerated(a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc)
local WorldName = a_World:GetName()
local Gallery = GetGalleryForEntireChunk(WorldName, a_ChunkX, a_ChunkZ);
if (Gallery and Gallery.AreaTemplateSchematic) then
-- The chunk has already been generated in OnChunkGenerating(), skip it
return false;
end
-- Imprint whatever galleries intersect the chunk:
ImprintChunk(WorldName, a_ChunkX, a_ChunkZ, a_ChunkDesc, true);
end
local function OnChunkGenerating(a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc)
local WorldName = a_World:GetName()
local Gallery = GetGalleryForEntireChunk(WorldName, a_ChunkX, a_ChunkZ);
if (not(Gallery) or not(Gallery.AreaTemplateSchematic)) then
-- The chunks is not entirely covered by a gallery, or the gallery doesn't use a schematic
return false;
end
-- The entire chunk is in a single gallery. Imprint the gallery schematic:
GetGalleryForEntireChunk(WorldName, a_ChunkX, a_ChunkZ)
a_ChunkDesc:SetUseDefaultComposition(false);
a_ChunkDesc:SetUseDefaultHeight(false);
a_ChunkDesc:SetUseDefaultFinish(false);
if (Gallery.Biome ~= nil) then
a_ChunkDesc:SetUseDefaultBiomes(false);
-- The biome will be set in ImprintChunk()
end
ImprintChunk(WorldName, a_ChunkX, a_ChunkZ, a_ChunkDesc, false);
return true;
end
local function OnExploding(a_World, a_ExplosionSize, a_CanCauseFire, a_BlockX, a_BlockY, a_BlockZ, a_Source, a_Data)
-- Find any gallery close enough to the explosion:
local MinX = a_BlockX - a_ExplosionSize - 1;
local MaxX = a_BlockX + a_ExplosionSize + 1;
local MinZ = a_BlockZ - a_ExplosionSize - 1;
local MaxZ = a_BlockZ + a_ExplosionSize + 1;
for idx, gal in ipairs(g_Galleries) do
if (
(gal.World == a_World) and
(MaxX >= gal.MinX) and
(MinX <= gal.MaxX) and
(MaxZ >= gal.MinZ) and
(MinZ <= gal.MaxZ)
) then
-- Gallery found, abort the explosion:
return true;
end
end
-- Let it explode
return false;
end
--- This function gets called by WorldEdit each time a player performs a WE operation
-- Return true to abort the operation, false to continue
function WorldEditCallback(a_AffectedAreaCuboid, a_Player, a_World, a_Operation)
-- If the minima and maxima aren't in the same gallery, disallow (yes, even admins canNOT use those)
local GalMin = FindGalleryByCoords(a_World, a_AffectedAreaCuboid.p1.x, a_AffectedAreaCuboid.p1.z);
local GalMax = FindGalleryByCoords(a_World, a_AffectedAreaCuboid.p2.x, a_AffectedAreaCuboid.p2.z);
if (GalMin ~= GalMax) then
a_Player:SendMessage(cChatColor.LightPurple .. "WorldEdit actions across galleries are not allowed");
return true;
end
if (GalMin == nil) then
-- Outside the galleries, check the config and permissions:
if (g_Config.AllowWEOutsideGalleries) then
return false;
end
if (a_Player:HasPermission("gallery.admin.worldeditoutside")) then
return false;
end
a_Player:SendMessage(cChatColor.LightPurple .. "Cannot allow WorldEdit action, outside the galleries");
return true;
end
-- If the minima and maxima aren't in the same area, disallow unless admin override permission
local Area = FindPlayerAreaByCoords(a_Player, a_AffectedAreaCuboid.p1.x, a_AffectedAreaCuboid.p1.z);
if (
(Area == nil) or
(a_AffectedAreaCuboid.p1.x < Area.StartX) or (a_AffectedAreaCuboid.p1.z < Area.StartZ) or -- Min is on sidewalk
(a_AffectedAreaCuboid.p2.x >= Area.EndX) or (a_AffectedAreaCuboid.p2.z >= Area.EndZ) -- Max not in area / on sidewalk
) then
-- The player doesn't own this area, allow WE only with an admin permission
if (a_Player:HasPermission("gallery.admin.worldedit")) then
return false;
end
a_Player:SendMessage(cChatColor.LightPurple .. "Cannot allow WorldEdit action, you don't own the area");
return true;
end
if (a_Player:HasPermission("gallery.worldedit")) then
return false;
end
a_Player:SendMessage(cChatColor.LightPurple .. "You don't have permission to use WorldEdit here");
return true;
end
local function OnPluginsLoaded()
-- Add a WE hook
cPluginManager:CallPlugin("WorldEdit", "AddHook", "OnAreaChanging", "Gallery", "WorldEditCallback");
end
--- Registers all hook handlers
function InitHookHandlers()
cPluginManager:AddHook(cPluginManager.HOOK_CHUNK_GENERATED, OnChunkGenerated)
cPluginManager:AddHook(cPluginManager.HOOK_CHUNK_GENERATING, OnChunkGenerating)
cPluginManager:AddHook(cPluginManager.HOOK_EXPLODING, OnExploding)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_BROKEN_BLOCK, OnPlayerBrokenBlock)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_DESTROYED, OnPlayerDestroyed)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_LEFT_CLICK, OnPlayerLeftClick)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_PLACED_BLOCK, OnPlayerPlacedBlock)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_PLACING_BLOCK, OnPlayerPlacingBlock)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_RIGHT_CLICK, OnPlayerRightClick)
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_SPAWNED, OnPlayerSpawned)
cPluginManager:AddHook(cPluginManager.HOOK_PLUGINS_LOADED, OnPluginsLoaded)
end