summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua
blob: eec1f8042745fe2570c93e0c8243d784b67844d0 (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
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

-- CommandHandlers.lua
-- Defines the individual command handlers





function InitializeCommandHandlers()
	local PlgMgr = cRoot:Get():GetPluginManager();
	for idx, Cmd in ipairs(CommandReg()) do
		PlgMgr:BindCommand(Cmd[2], Cmd[3], Cmd[1], Cmd[4]);
	end
end





--- Handles the ProtAdd command
function HandleAddArea(a_Split, a_Player)
	-- Command syntax: ProtAdd username1 [username2] [username3] ...
	if (#a_Split < 2) then
		a_Player:SendMessage("Not enough parameters. Expected a list of usernames.");
		return true;
	end
	
	-- Get the cuboid that the player had selected
	local CmdState = GetCommandStateForPlayer(a_Player);
	if (CmdState == nil) then
		a_Player:SendMessage("Cannot add area, internal plugin error (CmdState == nil)");
		return true;
	end
	local Cuboid = CmdState:GetCurrentCuboid();
	if (Cuboid == nil) then
		a_Player:SendMessage("Cannot add area, no area has been selected. Use a ProtWand lclk / rclk to select area first");
		return true;
	end
	
	-- Put all allowed players into a table:
	AllowedNames = {};
	for i = 2, #a_Split do
		table.insert(AllowedNames, a_Split[i]);
	end
	
	-- Add the area to the storage
	local AreaID = g_Storage:AddArea(Cuboid, a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames);
	a_Player:SendMessage("Area added, ID " .. AreaID);
	
	-- Reload all currently logged in players
	ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
	
	return true;
end





function HandleAddAreaCoords(a_Split, a_Player)
	-- Command syntax: ProtAddCoords x1 z1 x2 z2 username1 [username2] [username3] ...
	if (#a_Split < 6) then
		a_Player:SendMessage("Not enough parameters. Expected <x1> <z1> <x2> <z2> coords and a list of usernames.");
		return true;
	end
	
	-- Convert the coords to a cCuboid
	local x1, z1 = tonumber(a_Split[2]), tonumber(a_Split[3]);
	local x2, z2 = tonumber(a_Split[4]), tonumber(a_Split[5]);
	if ((x1 == nil) or (z1 == nil) or (x2 == nil) or (z2 == nil)) then
		a_Player:SendMessage("Cannot parse coords.");
		return true;
	end
	local Cuboid = cCuboid(x1, 0, z1, x2, 255, z1);
	Cuboid:Sort();
	
	-- Put all allowed players into a table:
	AllowedNames = {};
	for i = 6, #a_Split do
		table.insert(AllowedNames, a_Split[i]);
	end
	
	-- Add the area to the storage
	local AreaID = g_Storage:AddArea(Cuboid, a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames);
	a_Player:SendMessage("Area added, ID = " .. AreaID);
	
	-- Reload all currently logged in players
	ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
	
	return true;
end





function HandleAddAreaUser(a_Split, a_Player)
	-- Command syntax: ProtAddUser AreaID username1 [username2] [username3] ...
	if (#a_Split < 3) then
		a_Player:SendMessage("Not enough parameters. Expected <AreaID> and a list of usernames.");
		return true;
	end
	
	-- Put all allowed players into a table:
	AllowedNames = {};
	for i = 3, #a_Split do
		table.insert(AllowedNames, a_Split[i]);
	end
	
	-- Add the area to the storage
	g_Storage:AddAreaUsers(tonumber(a_Split[2]), a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames);
	a_Player:SendMessage("Users added: " .. table.concat(AllowedNames, ", "));
	
	-- Reload all currently logged in players
	ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
	
	return true;
end





function HandleDelArea(a_Split, a_Player)
	-- Command syntax: ProtDelArea AreaID
	if (#a_Split ~= 2) then
		a_Player:SendMessage("Parameter mismatch. Expected <AreaID>.");
		return true;
	end
	
	-- Parse the AreaID
	local AreaID = tonumber(a_Split[2]);
	if (AreaID == nil) then
		a_Player:SendMessage("Cannot parse <AreaID>.");
		return true;
	end
	
	-- Delete the area
	g_Storage:DelArea(a_Player:GetWorld():GetName(), AreaID);

	-- Reload all currently logged in players
	ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
	
	return true;
end





function HandleGiveWand(a_Split, a_Player)
	local NumGiven = a_Player:GetInventory():AddItem(cConfig:GetWandItem());
	if (NumGiven == 1) then
		a_Player:SendMessage("Wand given");
	else
		a_Player:SendMessage("Cannot give wand, no space in your inventory");
	end
	return true;
end





function HandleListAreas(a_Split, a_Player)
	-- Command syntax: ProtListAreas [x, z]
	
	local x, z;
	if (#a_Split == 1) then
		-- Get the last "wanded" coord
		local CmdState = GetCommandStateForPlayer(a_Player);
		if (CmdState == nil) then
			a_Player:SendMessage("Cannot list area, internal plugin error (CmdState == nil)");
			return true;
		end
		x, z = CmdState:GetLastCoords();
		if ((x == nil) or (z == nil)) then
			a_Player:SendMessage("Cannot list areas, no query point has been selected. Use a ProtWand lclk / rclk to select a point first");
			return true;
		end
	elseif (#a_Split == 3) then
		-- Parse the coords from the command params
		x = tonumber(a_Split[2]);
		z = tonumber(a_Split[3]);
		if ((x == nil) or (z == nil)) then
			a_Player:SendMessage("Cannot list areas, cannot parse coords in params");
			return true;
		end
	else
		-- Wrong number of params, report back to the user
		a_Player:SendMessage("Cannot list areas, syntax error. Expected either no params or <x> <z>.");
		return true;
	end
	
	a_Player:SendMessage("Listing protection areas intersecting block column {" .. x .. ", " .. z .. "}:");
	
	-- List areas intersecting the coords
	local PlayerName = a_Player:GetName();
	local WorldName = a_Player:GetWorld():GetName();
	g_Storage:ForEachArea(x, z, WorldName,
		function(AreaID, MinX, MinZ, MaxX, MaxZ, CreatorName)
			local Coords = AreaID .. ": {" ..
				MinX .. ", " .. MinZ .. "} - {" ..
				MaxX .. ", " .. MaxZ .. "} ";
			local Allowance;
			if (g_Storage:IsAreaAllowed(AreaID, PlayerName, WorldName)) then
				Allowance = "Allowed";
			else
				Allowance = "NOT allowed";
			end
			a_Player:SendMessage("  " .. Coords .. Allowance .. ", Created by " .. CreatorName);
		end
	);
	
	a_Player:SendMessage("Area list finished");
	return true;
end





function HandleRemoveUser(a_Split, a_Player)
	-- Command syntax: ProtRemUser AreaID UserName
	if (#a_Split ~= 3) then
		a_Player:SendMessage("Parameter mismatch. Expected <AreaID> <UserName>.");
		return true;
	end
	
	-- Parse the AreaID
	local AreaID = tonumber(a_Split[2]);
	if (AreaID == nil) then
		a_Player:SendMessage("Cannot parse <AreaID>.");
		return true;
	end
	
	-- Remove the user from the DB
	g_Storage:RemoveUser(AreaID, a_Split[3], a_Player:GetWorld():GetName());
	return true;
end





function HandleRemoveUserAll(a_Split, a_Player)
	-- Command syntax: ProtRemUserAll UserName
	if (#a_Split ~= 2) then
		a_Player:SendMessage("Parameter mismatch. Expected <UserName>.");
		return true;
	end
	
	-- Remove the user from the DB
	g_Storage.RemoveUserAll(a_Split[2], a_Player:GetWorld():GetName());
	return true;
end