-
Notifications
You must be signed in to change notification settings - Fork 2
/
NPCBlocker.cs
358 lines (324 loc) · 14 KB
/
NPCBlocker.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
/*
*
* Welcome to this template plugin.
* Level: All Levels
* Purpose: To get a working model for new plugins to be built off. This plugin will
* compile immediately, all you have to do is rename TemplatePlugin to reflect
* the purpose of the plugin.
*
* You may need to delete the references to TerrariaServer and TShockAPI. They
* could be pointing to my current folder. Just remove them and then right-click the
* references folder, go to browse go to the dll folder, and select both.
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.DB;
using Terraria;
using Microsoft.Xna.Framework; // For the SendMessage colors
using System.Linq;
namespace NPCBlocker
{
//[ApiVersion(1, 17)]
[ApiVersion(2, 1)]
public class NPCBlocker : TerrariaPlugin
{
private IDbConnection db;
private List<int> blockedNPC = new List<int>();
public override Version Version
{
get { return new Version(2, 0); }
}
public override string Name
{
get { return "NPC Blocker"; }
}
public override string Author
{
get { return "Olink, updated by moisterrific"; }
}
public override string Description
{
get { return "Bans specific NPCs from spawning."; }
}
public NPCBlocker(Main game)
: base(game)
{
Order = 4;
}
public override void Initialize()
{
//Commands.ChatCommands.Add(new Command("resnpc", AddNpc, "blacknpc", "blocknpc", "bannpc"));
Commands.ChatCommands.Add(new Command("plugin.npc.blocker", AddNpc, "blocknpc", "bannpc", "banmob"));
//Commands.ChatCommands.Add(new Command("resnpc", DelNpc, "whitenpc", "unblocknpc", "unbannpc"));
Commands.ChatCommands.Add(new Command("plugin.npc.blocker", DelNpc, "unblocknpc", "unbannpc", "unbanmob"));
ServerApi.Hooks.NpcSpawn.Register(this, OnSpawn);
ServerApi.Hooks.NpcTransform.Register(this, OnTransform);
StartDB();
}
public void StartDB()
{
SetupDb();
ReadDb();
}
private void SetupDb()
{
//if (TShock.Config.StorageType.ToLower() == "sqlite")
if (TShock.Config.Settings.StorageType.ToLower() == "sqlite")
{
string sql = Path.Combine(TShock.SavePath, "npc_blocker.sqlite");
db = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
}
//else if (TShock.Config.StorageType.ToLower() == "mysql")
else if (TShock.Config.Settings.StorageType.ToLower() == "mysql")
{
try
{
//var hostport = TShock.Config.MySqlHost.Split(':');
var hostport = TShock.Config.Settings.MySqlHost.Split(':');
db = new MySqlConnection();
db.ConnectionString =
String.Format("Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};",
hostport[0],
hostport.Length > 1 ? hostport[1] : "3306",
//TShock.Config.MySqlDbName,
TShock.Config.Settings.MySqlDbName,
TShock.Config.Settings.MySqlUsername,
TShock.Config.Settings.MySqlPassword
);
}
catch (MySqlException ex)
{
//Log.Error(ex.ToString());
TShock.Log.Error(ex.ToString());
throw new Exception("MySql not setup correctly");
}
}
else
{
throw new Exception("Invalid storage type");
}
var table2 = new SqlTable("Blocked_NPC",
new SqlColumn("ID", MySqlDbType.Int32)
);
var creator2 = new SqlTableCreator(db,
db.GetSqlType() == SqlType.Sqlite
? (IQueryBuilder)new SqliteQueryCreator()
: new MysqlQueryCreator());
//creator2.EnsureExists(table2);
creator2.EnsureTableStructure(table2);
}
private void ReadDb()
{
//String query = "SELECT * FROM Blocked_NPC";
string query = "SELECT * FROM Blocked_NPC";
var reader = db.QueryReader(query);
while (reader.Read())
{
blockedNPC.Add(reader.Get<int>("ID"));
}
}
private void AddNpc(CommandArgs args)
{
//if (args.Parameters.Count < 1)
//{
// args.Player.SendMessage("You must specify a npc id to add.", Color.Red);
// return;
//}
//string tile = args.Parameters[0];
//int id;
//if (!int.TryParse(tile, out id))
//{
// args.Player.SendMessage(String.Format("Npc id '{0}' is not a valid number.", id), Color.Red);
// return;
//}
//String query = "INSERT INTO Blocked_NPC (ID) VALUES (@0);";
//if (db.Query(query, id) != 1)
//{
// //Log.ConsoleError("Inserting into the database has failed!");
// TShock.Log.ConsoleError("Inserting into the database has failed!");
// args.Player.SendMessage(String.Format("Inserting into the database has failed!", id), Color.Red);
//}
//else
//{
// args.Player.SendMessage(String.Format("Successfully banned {0}", id), Color.Red);
// blockedNPC.Add(id);
//}
if (args.Parameters.Count < 1)
{
args.Player.SendErrorMessage("Invalid syntax! [c/ffff00:Proper syntax: {0}bannpc <name or ID>]", TShock.Config.Settings.CommandSpecifier);
return;
}
if (args.Parameters[0].Length == 0)
{
args.Player.SendErrorMessage("Invalid NPC type!");
return;
}
// Change it from NPC ID only to NPC ID or name
// To-do: add a way to show list of currently banned NPCs and optional NPC amount to be blocked
// Example: currently blocked NPCs list, blocks more than 5 wraiths from spawning in the world
var npcs = TShock.Utils.GetNPCByIdOrName(args.Parameters[0]);
if (npcs.Count == 0)
{
args.Player.SendErrorMessage("Invalid NPC type!");
}
else if (npcs.Count > 1)
{
args.Player.SendMultipleMatchError(npcs.Select(n => $"{n.FullName}({n.type})"));
}
else
{
var npc = npcs[0];
int id = npc.type;
//String query = "INSERT INTO Blocked_NPC (ID) VALUES (@0);";
string query = "INSERT INTO Blocked_NPC (ID) VALUES (@0);";
//if (db.Query(query, id) != 1) // This check was not working properly if you ban an NPC that's already banned
//{
// //Log.ConsoleError("Inserting into the database has failed!");
// TShock.Log.Error("Inserting into the database has failed!");
// //args.Player.SendMessage(String.Format("Inserting into the database has failed!", id), Color.Red);
// args.Player.SendErrorMessage("Failed to insert into the database! [c/ffff00:{0} (ID: {1}) is already banned!]", npc.FullName, npc.type);
//}
//else
//{
// //args.Player.SendMessage(String.Format("Successfully banned {0}", id), Color.Red);
// args.Player.SendSuccessMessage("Successfully banned {0} (ID: {1}).", npc.FullName, npc.type);
// blockedNPC.Add(id);
//}
if ((db.Query(query, id) != 0) && blockedNPC.Contains(id))
{
args.Player.SendErrorMessage("{0} (ID: {1}) is already banned.", npc.FullName, npc.type);
}
else
{
blockedNPC.Add(id);
if (args.Silent)
{
args.Player.SendSuccessMessage("Banned {0} (ID: {1}). [c/ffff00:This message is only visible to you.]", npc.FullName, npc.type);
}
else
{
TSPlayer.All.SendInfoMessage("{0} has banned {1} from spawning.", args.Player.Name, npc.FullName);
}
}
}
}
private void DelNpc(CommandArgs args)
{
//if (args.Parameters.Count < 1)
//{
// args.Player.SendMessage("You must specify a npc id to remove.", Color.Red);
// return;
//}
//string tile = args.Parameters[0];
//int id;
//if (!int.TryParse(tile, out id))
//{
// args.Player.SendMessage(String.Format("Npc id '{0}' is not a valid number.", id), Color.Red);
// return;
//}
//String query = "DELETE FROM Blocked_NPC WHERE ID = @0;";
//if (db.Query(query, id) != 1)
//{
// //Log.ConsoleError("Removing from the database has failed!");
// TShock.Log.ConsoleError("Removing from the database has failed!");
// args.Player.SendMessage(String.Format("Removing from the database has failed! Are you sure {0} is banned?", id), Color.Red);
//}
//else
//{
// args.Player.SendMessage(String.Format("Successfully unbanned {0}", id), Color.Green);
// blockedNPC.Remove(id);
//}
if (args.Parameters.Count < 1)
{
args.Player.SendErrorMessage("Invalid syntax! [c/ffff00:Proper syntax: {0}unbannpc <name or ID>]", TShock.Config.Settings.CommandSpecifier);
return;
}
if (args.Parameters[0].Length == 0)
{
args.Player.SendErrorMessage("Invalid NPC type!");
return;
}
var npcs = TShock.Utils.GetNPCByIdOrName(args.Parameters[0]);
if (npcs.Count == 0)
{
args.Player.SendErrorMessage("Invalid NPC type!");
}
else if (npcs.Count > 1)
{
args.Player.SendMultipleMatchError(npcs.Select(n => $"{n.FullName}({n.type})"));
}
else
{
var npc = npcs[0];
int id = npc.type;
//String query = "DELETE FROM Blocked_NPC WHERE ID = @0;";
string query = "DELETE FROM Blocked_NPC WHERE ID = @0;";
//if (db.Query(query, id) != 1)
//{
// //Log.ConsoleError("Removing from the database has failed!");
// TShock.Log.Error("Removing from the database has failed!");
// //args.Player.SendMessage(String.Format("Removing from the database has failed! Are you sure {0} is banned?", id), Color.Red);
// args.Player.SendErrorMessage("Failed to remove from the database! [c/ffff00:Are you sure {0} (ID: {1}) is banned?]", npc.FullName, npc.type);
//}
//else
//{
// //args.Player.SendMessage(String.Format("Successfully unbanned {0}", id), Color.Green);
// args.Player.SendSuccessMessage("Successfully unbanned {0} (ID: {1})", npc.FullName, npc.type);
// blockedNPC.Remove(id);
//}
if (db.Query(query, id) == 0) // Works perfectly
{
args.Player.SendErrorMessage("{0} (ID: {1}) is not banned.", npc.FullName, npc.type);
}
else
{
blockedNPC.Remove(id);
if (args.Silent)
{
args.Player.SendSuccessMessage("Unbanned {0} (ID: {1}). [c/ffff00:This message is only visible to you.]", npc.FullName, npc.type);
}
else
{
TSPlayer.All.SendInfoMessage("{0} has unbanned {1} from spawning.", args.Player.Name, npc.FullName);
}
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.NpcSpawn.Deregister(this, OnSpawn);
}
base.Dispose(disposing);
}
private void OnTransform(NpcTransformationEventArgs args)
{
if (args.Handled)
return;
if (blockedNPC.Contains(Main.npc[args.NpcId].netID))
{
Main.npc[args.NpcId].active = false;
}
}
private void OnSpawn(NpcSpawnEventArgs args)
{
if (args.Handled)
return;
if (blockedNPC.Contains(Main.npc[args.NpcId].netID))
{
args.Handled = true;
Main.npc[args.NpcId].active = false;
//args.NpcId = 200;
args.NpcId = Main.maxNPCs;
}
}
}
}