forked from bippity/AdvancedWarpplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.cs
562 lines (505 loc) · 21.8 KB
/
Commands.cs
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System;
using Microsoft.Xna.Framework;
using TShockAPI;
using Terraria;
using System.Collections.Generic;
using System.Linq;
namespace AdvancedWarpplates
{
/// <summary>
/// Deals with any commands a player might execute
/// </summary>
public class Commands
{
private readonly WarpplateManager Manager;
private readonly Player[] Players;
public Commands(WarpplateManager manager, Player[] players)
{
Manager = manager;
Players = players;
}
public void MainCommand(CommandArgs args)
{
string cmd = "help";
if (args.Parameters.Count > 0)
{
cmd = args.Parameters[0].ToLower();
}
switch (cmd)
{
case "set":
SetWarpplate(args);
break;
case "del":
case "delete":
case "remove":
DeleteWarpplate(args);
break;
case "setdest":
case "setdestination":
SetWarpplateDestination(args);
break;
case "deldest":
case "deletedestination":
RemoveWarpplateDestination(args);
break;
case "info":
WarpplateInformation(args);
break;
case "list":
ListWarpplates(args);
break;
case "allow":
case "toggle":
WarpplateAllow(args);
break;
case "reload":
ReloadWarpplates(args);
break;
case "delay":
SetWarpplateDelay(args);
break;
case "width":
SetWarpplateWidth(args);
break;
case "height":
SetWarpplateHeight(args);
break;
case "resize":
case "size":
SetWarpplateSize(args);
break;
case "label":
case "display":
SetWarpplateLabel(args);
break;
case "dim":
case "dimension":
if (!args.Player.HasPermission("warpplate.setdimensional"))
{
args.Player.SendErrorMessage("You do not have permission to set a dimensional warpplate.");
break;
}
SetWarpplateDimension(args);
break;
case "help":
default:
{
int pageParamIndex = 0;
if (args.Parameters.Count > 1)
pageParamIndex = 1;
if (!PaginationTools.TryParsePageNumber(args.Parameters, pageParamIndex, args.Player, out int pageNumber))
return;
List<String> lines = new List<String>{
"set <name> - Set new warpplate at your position.",
"delete <name> - Deletes the given warpplate.",
"setdest <name> <destination> - Set destination for the given warpplate.",
"deldest <name> - Delete the destination for the given warpplate.",
"info <name> - Display info for the given warpplate.",
"list - List all warpplates in the world.",
"toggle - Enable/Disable activating warpplates for yourself.",
"reload - Reload warpplate information from database.",
"delay [<name>] <delay in seconds> - Set delay for warpplate.",
"width [<name>] <width in blocks> - Set width for warpplate.",
"height [<name>] <height in blocks> - Set height for warpplate.",
"resize [<name>] <width> <height> - Resize dimensions for warpplate.",
"label [<name>] <label name> - Set the label name for the warpplate destination."
};
if (args.Player.HasPermission("warpplate.setdimensional"))
{
lines.Add("dim [<name>] <dimension name> - Set dimension destination for the warpplate.");
}
PaginationTools.SendPage(
args.Player, pageNumber, lines,
new PaginationTools.Settings
{
HeaderFormat = "Warpplate Sub-Commands ({0}/{1}):",
FooterFormat = "Type {0}wp {{0}} for more sub-commands.".SFormat(TShock.Config.Settings.CommandSpecifier)
}
);
break;
}
}
}
/// <summary>
/// Updates or gives a warpplate a delay
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateDelay(CommandArgs args)
{
// Use the given warpplate name or find the warpplate they are standing in
string region;
if (args.Parameters.Count == 3)
region = args.Parameters[1];
else if (args.Parameters.Count == 2)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper synatx: /wp delay [<warpplate name>] <delay in seconds>");
args.Player.SendErrorMessage("Set 0 for immediate warp");
return;
}
// Check if the warpplate exists
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
// Update the delay of the given warpplate
if (int.TryParse(args.Parameters[args.Parameters.Count - 1], out int Delay))
{
warpplate.Delay = Delay + 1;
if (await Manager.UpdateWarpplate(warpplate.Name))
args.Player.SendSuccessMessage("Set delay of {0} to {1} seconds", warpplate.Name, Delay);
else
args.Player.SendErrorMessage("Something went wrong");
}
else
args.Player.SendErrorMessage("Bad number specified");
}
/// <summary>
/// Resizes a warpplate's width
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateWidth(CommandArgs args)
{
string region;
if (args.Parameters.Count == 3)
region = args.Parameters[1];
else if (args.Parameters.Count == 2)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp width [<warpplate name>] <width in blocks>");
return;
}
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
if (Int32.TryParse(args.Parameters[args.Parameters.Count - 1], out int Width))
{
Rectangle r;
r = warpplate.Area;
r.Width = Width;
warpplate.Area = r;
if (await Manager.UpdateWarpplate(warpplate.Name))
args.Player.SendSuccessMessage("Set width of {0} to {1} blocks", warpplate.Name, Width);
else
args.Player.SendErrorMessage("Something went wrong");
}
else
args.Player.SendErrorMessage("Invalid number: " + args.Parameters[args.Parameters.Count - 1]);
}
/// <summary>
/// Updates a warpplates label
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateLabel(CommandArgs args)
{
string region;
if (args.Parameters.Count == 3)
region = args.Parameters[1];
else if (args.Parameters.Count == 2)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp label [<warpplate name>] <label>");
args.Player.SendErrorMessage("Type /wp label [<warpplate name>] \"\" to set label to default (warpplate name)");
return;
}
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
string label = args.Parameters[args.Parameters.Count - 1];
warpplate.Label = label;
if (await Manager.UpdateWarpplate(warpplate.Name))
args.Player.SendSuccessMessage(String.Format("Set label of {0} to {1}", warpplate.Name, warpplate.GetLabelOrDefault()));
else
args.Player.SendErrorMessage("Something went wrong");
}
/// <summary>
/// Updates a warpplates dimension destination
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateDimension(CommandArgs args)
{
string region;
if (args.Parameters.Count == 3)
region = args.Parameters[1];
else if (args.Parameters.Count == 2)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp dim [<warpplate name>] <dimension name>");
args.Player.SendErrorMessage("Type /wp dim [<warpplate name>] \"\" to set dimension to none");
return;
}
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
// Get Dimension Name from parameters
string dimension = args.Parameters[args.Parameters.Count - 1];
warpplate.Destination = dimension;
// Set type to 0 (non-dimensional) if the specified name was empty
if (dimension.Length == 0)
{
warpplate.Type = 0;
}
else
{
warpplate.Type = 1;
}
// Update warpplate in DB
if (await Manager.SetDestination(warpplate.Name, dimension, warpplate.Type))
{
if (warpplate.Type == 0)
{
args.Player.SendSuccessMessage(String.Format("Removed Dimension Destination from {0}", warpplate.Name));
}
else
{
args.Player.SendSuccessMessage(String.Format("Set Dimension Destination of {0} to {1}", warpplate.Name, dimension));
}
}
else
{
args.Player.SendErrorMessage("Something went wrong");
}
}
/// <summary>
/// Resizes a warpplates height
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateHeight(CommandArgs args)
{
string region;
if (args.Parameters.Count == 3)
region = args.Parameters[1];
else if (args.Parameters.Count == 2)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp height [<warpplate name>] <height in blocks>");
return;
}
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
if (Int32.TryParse(args.Parameters[args.Parameters.Count - 1], out int Height))
{
Rectangle area;
area = warpplate.Area;
area.Height = Height;
warpplate.Area = area;
await Manager.UpdateWarpplate(warpplate.Name);
if (await Manager.UpdateWarpplate(warpplate.Name))
{
args.Player.SendSuccessMessage("Set height of {0} to {1} blocks", warpplate.Name, Height);
}
else
{
args.Player.SendErrorMessage("Something went wrong");
}
}
else
{
args.Player.SendErrorMessage("Bad number specified");
}
}
/// <summary>
/// Resizes a warpplate
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateSize(CommandArgs args)
{
string region;
if (args.Parameters.Count == 4)
region = args.Parameters[1];
else if (args.Parameters.Count == 3)
region = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
else
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp resize [<warpplate name>] <width> <height>");
return;
}
Warpplate warpplate = Manager.GetWarpplateByName(region);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such warpplate");
return;
}
if (Int32.TryParse(args.Parameters[args.Parameters.Count - 2], out int Width) &&
Int32.TryParse(args.Parameters[args.Parameters.Count - 1], out int Height))
{
Rectangle area;
area = warpplate.Area;
area.Width = Width;
area.Height = Height;
warpplate.Area = area;
await Manager.UpdateWarpplate(warpplate.Name);
if (await Manager.UpdateWarpplate(warpplate.Name))
{
args.Player.SendSuccessMessage("Set size of {0} to {1}x{2}", warpplate.Name, Width, Height);
}
else
{
args.Player.SendErrorMessage("Something went wrong");
}
}
else
args.Player.SendErrorMessage("Bad number specified");
}
/// <summary>
/// Adds a new warpplate with a given name
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplate(CommandArgs args)
{
if (args.Parameters.Count < 2)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp set <warpplate name>");
return;
}
if (Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY) != null)
{
args.Player.SendErrorMessage("There is already a Warpplate located here. Find a new place");
return;
}
string regionName = String.Join(" ", args.Parameters.Skip(1));
var x = ((((int)args.Player.X) / 16) - 1);
var y = (((int)args.Player.Y) / 16);
var width = 2;
var height = 3;
if (await Manager.AddWarpplate(x, y, width, height, regionName, "", Main.worldID.ToString()))
{
args.Player.SendSuccessMessage("Warpplate created: " + regionName);
}
else
{
args.Player.SendErrorMessage("Warpplate already created: " + regionName + " already exists");
}
}
/// <summary>
/// Deletes a warpplate with a given name
/// </summary>
/// <param name="args">The command arguments</param>
public async void DeleteWarpplate(CommandArgs args)
{
if (args.Parameters.Count < 2)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp delete <warpplate name>");
return;
}
string regionName = String.Join(" ", args.Parameters.Skip(1));
if (await Manager.DeleteWarpplate(regionName))
{
args.Player.SendInfoMessage("Deleted Warpplate: " + regionName);
}
else
args.Player.SendErrorMessage("Could not find specified Warpplate");
}
/// <summary>
/// Sets the destination of a warpplate
/// </summary>
/// <param name="args">The command arguments</param>
public async void SetWarpplateDestination(CommandArgs args)
{
if (args.Parameters.Count < 3)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp setdest <Warpplate Name> <Name of Destination Warpplate>");
return;
}
if (await Manager.SetDestination(args.Parameters[1], args.Parameters[2]))
{
args.Player.SendInfoMessage("Destination " + args.Parameters[2] + " added to Warpplate " + args.Parameters[1]);
}
else
args.Player.SendErrorMessage("Could not find specified Warpplate or destination");
}
/// <summary>
/// Removes a warpplates destination
/// </summary>
/// <param name="args">The command arguments</param>
public async void RemoveWarpplateDestination(CommandArgs args)
{
if (args.Parameters.Count < 2)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /wp delete <Warpplate Name>");
return;
}
if (await Manager.RemoveDestination(args.Parameters[1]))
{
args.Player.SendInfoMessage("Removed Destination From Warpplate " + args.Parameters[1]);
}
else
args.Player.SendErrorMessage("Could not find specified Warpplate or destination");
}
public void WarpplateInformation(CommandArgs args)
{
string warpplateName;
if (args.Parameters.Count > 1)
{
warpplateName = string.Join(" ", args.Parameters.Skip(1));
}
else
{
warpplateName = Manager.InAreaWarpplateName(args.Player.TileX, args.Player.TileY);
}
var warpplate = Manager.GetWarpplateByName(warpplateName);
if (warpplate == null)
{
args.Player.SendErrorMessage("No such Warpplate");
}
else
{
args.Player.SendMessage("Name: " + warpplate.Name + "; Label: " + warpplate.GetLabelOrDefault()
+ "; Destination: " + warpplate.GetDestinationOrDefault() + ";", Color.HotPink);
args.Player.SendMessage("X: " + warpplate.WarpplatePos.X + "; Y: " + warpplate.WarpplatePos.Y +
"; W: " + warpplate.Area.Width + "; H: " + warpplate.Area.Height + "; Delay: " + (warpplate.Delay - 1), Color.HotPink);
}
}
public void ListWarpplates(CommandArgs args)
{
if (!PaginationTools.TryParsePageNumber(args.Parameters, 1, args.Player, out int pageNumber))
return;
var wpNames = Manager.ListAllWarpplates();
PaginationTools.SendPage(args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(wpNames),
new PaginationTools.Settings
{
HeaderFormat = "Warpplates ({0}/{1}):",
FooterFormat = "Type {0}lwp {{0}} for more.".SFormat(TShock.Config.Settings.CommandSpecifier),
NothingToDisplayString = "There are currently no warpplates defined."
});
}
public async void ReloadWarpplates(CommandArgs args)
{
await Manager.ReloadAllWarpplates();
args.Player.SendSuccessMessage("Warpplates reloaded!");
}
public void WarpplateAllow(CommandArgs args)
{
if (!Players[args.Player.Index].CanUseWarpplates)
{
args.Player.SendSuccessMessage("Warpplates are now turned on for you");
}
else
{
args.Player.SendSuccessMessage("Warpplates are now turned off for you");
}
Players[args.Player.Index].CanUseWarpplates = !Players[args.Player.Index].CanUseWarpplates;
}
}
}