-
Notifications
You must be signed in to change notification settings - Fork 14
/
Plugin.cs
391 lines (373 loc) · 17.2 KB
/
Plugin.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
using System;
using System.Collections.Generic;
using System.Linq;
using TShockAPI;
using Terraria;
using TerrariaApi.Server;
using System.IO;
using Microsoft.Xna.Framework;
using System.Threading.Tasks;
namespace SmartRegions
{
[ApiVersion(2, 1)]
public class Plugin : TerrariaPlugin
{
private DBConnection DBConnection;
List<SmartRegion> regions;
PlayerData[] players = new PlayerData[255];
struct PlayerData
{
public Dictionary<SmartRegion, DateTime> cooldowns;
public SmartRegion regionToReplace;
public void Reset()
{
cooldowns = new Dictionary<SmartRegion, DateTime>();
regionToReplace = null;
}
}
public Plugin(Main game) : base(game) { }
public override void Initialize()
{
Commands.ChatCommands.Add(new Command("SmartRegions.manage", regionCommand, "smartregion"));
Commands.ChatCommands.Add(new Command("SmartRegions.manage", replaceRegion, "replace"));
ServerApi.Hooks.NetGreetPlayer.Register(this, OnGreetPlayer);
ServerApi.Hooks.GameUpdate.Register(this, OnUpdate);
DBConnection = new DBConnection();
DBConnection.Initialize();
string folder = Path.Combine(TShock.SavePath, "SmartRegions");
if(!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
else
{
ReplaceLegacyRegionStorage();
}
regions = DBConnection.GetRegions();
}
protected override void Dispose(bool Disposing)
{
if(Disposing)
{
ServerApi.Hooks.NetGreetPlayer.Deregister(this, OnGreetPlayer);
ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate);
DBConnection?.Close();
}
base.Dispose(Disposing);
}
public override Version Version
{
get { return new Version("1.4.1"); }
}
public override string Name
{
get { return "Smart Regions"; }
}
public override string Author
{
get { return "GameRoom"; }
}
public override string Description
{
get { return "Runs commands when players enter a region."; }
}
private void OnGreetPlayer(GreetPlayerEventArgs args)
{
players[args.Who].Reset();
}
public static event EventHandler<PlayerInRegionEventArgs> PlayerInRegion;
public class PlayerInRegionEventArgs : EventArgs
{
public TSPlayer Player { get; set; }
public SmartRegion Region { get; set; }
public bool IgnoreRegion { get; set; }
}
void OnUpdate(EventArgs args)
{
foreach(TSPlayer player in TShock.Players)
if(player != null && NetMessage.buffer[player.Index].broadcast)
{
var inRegion = TShock.Regions.InAreaRegionName((int)(player.X / 16), (int)(player.Y / 16));
var hs = new HashSet<string>(inRegion);
var inSmartRegion = regions.Where(x => hs.Contains(x.name)).OrderByDescending(x => x.region.Z);
int regionCounter = 0;
foreach(SmartRegion region in inSmartRegion)
{
PlayerInRegionEventArgs eventArgs = new PlayerInRegionEventArgs()
{
Player = player,
Region = region
};
PlayerInRegion?.Invoke(this, eventArgs);
if (eventArgs.IgnoreRegion)
continue;
if((regionCounter++ == 0 || !region.region.Name.EndsWith("--"))
&& (!players[player.Index].cooldowns.ContainsKey(region)
|| DateTime.UtcNow > players[player.Index].cooldowns[region]))
{
string file = Path.Combine(TShock.SavePath, "SmartRegions", region.command);
if(File.Exists(file))
{
foreach(string command in File.ReadAllLines(file))
{
Commands.HandleCommand(TSPlayer.Server, replaceWithName(command, player));
}
}
else
{
Commands.HandleCommand(TSPlayer.Server, replaceWithName(region.command, player));
}
if(players[player.Index].cooldowns.ContainsKey(region))
{
players[player.Index].cooldowns[region] = DateTime.UtcNow.AddSeconds(region.cooldown);
}
else
{
players[player.Index].cooldowns.Add(region, DateTime.UtcNow.AddSeconds(region.cooldown));
}
}
}
}
}
string replaceWithName(string cmd, TSPlayer player)
{
return cmd.Replace("[PLAYERNAME]", $"\"tsn:{player.Name}\"");
}
public async void regionCommand(CommandArgs args)
{
try
{
await regionCommandInner(args);
}
catch (Exception e)
{
TShock.Log.Error(e.ToString());
args.Player.SendErrorMessage("The command threw an error.");
}
}
public async Task regionCommandInner(CommandArgs args)
{
switch(args.Parameters.ElementAtOrDefault(0))
{
case "add":
{
if(args.Parameters.Count < 4)
{
args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion add <region name> <cooldown> <command or file>");
}
else
{
double cooldown = 0;
if(!double.TryParse(args.Parameters[2], out cooldown))
{
args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion add <region name> <cooldown> <command or file>");
return;
}
string command = string.Join(" ", args.Parameters.GetRange(3, args.Parameters.Count - 3));
if(!TShock.Regions.Regions.Exists(x => x.Name == args.Parameters[1]))
{
args.Player.SendErrorMessage("The region {0} doesn't exist!", args.Parameters[1]);
IEnumerable<string> regionNames = from region_ in TShock.Regions.Regions
where region_.WorldID == Main.worldID.ToString()
select region_.Name;
PaginationTools.SendPage(args.Player, 1, PaginationTools.BuildLinesFromTerms(regionNames),
new PaginationTools.Settings
{
HeaderFormat = "Regions ({0}/{1}):",
FooterFormat = "Type {0}region list {{0}} for more.".SFormat(Commands.Specifier),
NothingToDisplayString = "There are currently no regions defined."
});
}
else
{
string cmdName = "";
for(int i = 1; i < command.Length && command[i] != ' '; i++)
{
cmdName += command[i];
}
Command cmd = Commands.ChatCommands.FirstOrDefault(c => c.HasAlias(cmdName));
if(cmd != null && !cmd.CanRun(args.Player))
{
args.Player.SendErrorMessage("You cannot create a smart region with a command you don't have permission to use yourself!");
return;
}
if(cmd != null && !cmd.AllowServer)
{
args.Player.SendErrorMessage("Your command must be usable by the server!");
return;
}
var existingRegion = regions.FirstOrDefault(x => x.name == args.Parameters[1]);
var newRegion = new SmartRegion
{
name = args.Parameters[1],
cooldown = cooldown,
command = command
};
if(existingRegion != null)
{
players[args.Player.Index].regionToReplace = newRegion;
args.Player.SendErrorMessage("The smart region {0} already exists! Type /replace to replace it.", args.Parameters[1]);
}
else
{
regions.Add(newRegion);
await DBConnection.SaveRegion(newRegion);
args.Player.SendSuccessMessage("Smart region added!");
}
}
}
}
break;
case "remove":
{
if(args.Parameters.Count != 2)
{
args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion remove <regionname>");
}
else
{
var region = regions.FirstOrDefault(x => x.name == args.Parameters[1]);
if(region == null)
{
args.Player.SendErrorMessage("No such smart region exists!");
}
else
{
regions.Remove(region);
await DBConnection.RemoveRegion(region.name);
args.Player.SendSuccessMessage("The smart region {0} was removed!", args.Parameters[1]);
}
}
}
break;
case "check":
{
if(args.Parameters.Count != 2)
{
args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion check <regionname>");
}
else
{
var region = regions.FirstOrDefault(x => x.name == args.Parameters[1]);
if(region == null)
{
args.Player.SendInfoMessage("That region doesn't have a command associated with it.");
}
else
{
string file = Path.Combine(TShock.SavePath, "SmartRegions", region.command), commands;
if(File.Exists(file)) commands = "s:\n" + File.ReadAllText(file);
else commands = ":\n" + region.command;
args.Player.SendInfoMessage("The region {0} has a cooldown of {1} second{2} and uses the command{3}", args.Parameters[1], region.cooldown, region.cooldown == 1.0 ? "" : "s", commands);
}
}
}
break;
case "list":
{
int pageNumber = 1;
int maxDist = int.MaxValue;
if(args.Parameters.Count > 1)
{
int.TryParse(args.Parameters[1], out pageNumber);
}
if(args.Parameters.Count > 2)
{
if(args.Player == TSPlayer.Server)
{
args.Player.SendErrorMessage("You cannot use the distance argument if you're the server.");
return;
}
int.TryParse(args.Parameters[2], out maxDist);
}
List<SmartRegion> regionList = regions;
if(maxDist < int.MaxValue)
{
regionList = regionList
.Where(r => r.region != null && Vector2.Distance(args.Player.TPlayer.position, r.region.Area.Center() * 16) < maxDist * 16)
.ToList();
}
List<string> regionNames = regionList.Select(r => r.name).ToList();
regionNames.Sort();
if(regionNames.Count == 0)
{
string suffix = "";
if(maxDist < int.MaxValue)
{
suffix = " nearby";
}
args.Player.SendErrorMessage($"There are no smart regions{suffix}.");
}
else
{
PaginationTools.SendPage(
args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(regionNames),
new PaginationTools.Settings
{
HeaderFormat = "Smart regions ({0}/{1}):",
FooterFormat = string.Format("Type {0}smartregion list {{0}} for more.", Commands.Specifier)
}
);
}
}
break;
default:
{
args.Player.SendInfoMessage("/smartregion sub-commands:\nadd <region name> <cooldown> <command or file>\nremove <region name>\ncheck <region name>\nlist [page] [max dist]");
}
break;
}
}
void ReplaceLegacyRegionStorage()
{
string path = Path.Combine(TShock.SavePath, "SmartRegions", "config.txt");
if(File.Exists(path))
{
var tasks = new List<Task>();
try
{
string[] lines = File.ReadAllLines(path);
for(int i = 0; i < lines.Length; i += 3)
{
var task = DBConnection.SaveRegion(new SmartRegion
{
name = lines[i],
command = lines[i + 1],
cooldown = double.Parse(lines[i + 2])
});
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
File.Delete(path);
}
catch(Exception e)
{
TShock.Log.Error(e.ToString());
}
}
}
public async void replaceRegion(CommandArgs args)
{
try
{
if (players[args.Player.Index].regionToReplace == null)
{
args.Player.SendErrorMessage("You can't do that right now!");
}
else
{
regions.RemoveAll(x => x.name == players[args.Player.Index].regionToReplace.name);
regions.Add(players[args.Player.Index].regionToReplace);
await DBConnection.SaveRegion(players[args.Player.Index].regionToReplace);
players[args.Player.Index].regionToReplace = null;
args.Player.SendSuccessMessage("Region successfully replaced!");
}
}
catch (Exception e)
{
TShock.Log.Error(e.ToString());
args.Player.SendErrorMessage("The command threw an error.");
}
}
}
}