summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua
blob: 6e8881b72ec49e48ccb22e3c01bdae48194c53b2 (plain) (blame)
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

-- ProtectionAreas.lua
-- Defines the main plugin entrypoint, as well as some utility functions





--- Prefix for all messages logged to the server console
PluginPrefix = "ProtectionAreas: ";

--- Bounds for the area loading. Areas less this far in any direction from the player will be loaded into cPlayerAreas
g_AreaBounds = 48;

--- If a player moves this close to the PlayerAreas bounds, the PlayerAreas will be re-queried
g_AreaSafeEdge = 12;





--- Called by MCS when the plugin loads
-- Returns true if initialization successful, false otherwise
function Initialize(a_Plugin)
	a_Plugin:SetName("ProtectionAreas");
	a_Plugin:SetVersion(1);
	
	if (not(InitializeStorage())) then
		LOGWARNING(PluginPrefix .. "failed to initialize Storage, plugin is disabled");
		return false;
	end
	InitializeHooks(a_Plugin);
	InitializeCommandHandlers();
	
	-- We might be reloading, so there may be players already present in the server; reload all of them
	cRoot:Get():ForEachWorld(
		function(a_World)
			ReloadAllPlayersInWorld(a_World:GetName());
		end
	);
	
	return true;
end





--- Loads a cPlayerAreas object from the DB for the player, and assigns it to the player map
function LoadPlayerAreas(a_Player)
	local PlayerID = a_Player:GetUniqueID();
	local PlayerX = math.floor(a_Player:GetPosX());
	local PlayerZ = math.floor(a_Player:GetPosZ());
	local WorldName = a_Player:GetWorld():GetName();
	g_PlayerAreas[PlayerID] = g_Storage:LoadPlayerAreas(a_Player:GetName(), PlayerX, PlayerZ, WorldName);
end





function ReloadAllPlayersInWorld(a_WorldName)
	local World = cRoot:Get():GetWorld(a_WorldName);
	World:ForEachPlayer(LoadPlayerAreas);
end