-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
527 lines (490 loc) · 23.2 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using TShockAPI;
using TShockAPI.DB;
using TerrariaApi.Server;
using RUDD.Dotnet;
namespace ArcticCircle
{
public class Utils
{
public static List<NetItem> StarterItems = new List<NetItem>();
public static int startHealth, startMana;
public static string[] Parameters = new string[] { "width", "height", "damage", "crit", "knockback", "prefix", "reusedelay", "shoot", "shootspeed", "useammo", "usetime", "autoreuse", "ammo", "scale" };
public const int Width = 0, Height = 1, Damage = 2, Crit = 3, KB = 4, Prefix = 5, ReuseDelay = 6, Shoot = 7, ShootSpeed = 8, UseAmmo = 9, UseTime = 10, AutoReuse = 11, Ammo = 12, Scale = 13;
public static class ClassID
{
public const string None = "None", Ranged = "Ranged", Melee = "Melee", Mage = "Mage";
public static string[] Array = new string[4];
}
public class ClassItem
{
public int id;
public int stack;
public int prefix;
}
public static string Class(int index)
{
return ClassID.Array[index].ToLower();
}
public static int ClassSet(string param)
{
if (!ClassID.Array.Contains(param) || ClassID.Array.Length == 0)
return -1;
for (int i = 0; i < ClassID.Array.Length; i++)
{
if (param.ToLower() == ClassID.Array[i].ToLower())
return i;
}
return -1;
}
public static int IndexTotal()
{
return NetItem.InventorySlots + NetItem.MiscDyeSlots + NetItem.MiscEquipSlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.TrashSlots;
}
public static void UpdateItem(Item item, int slot, int who, bool remove = false, int stack = 1, bool custom = false)
{
if (remove)
{
item.active = false;
item.type = 0;
item.netDefaults(0);
}
else if (custom)
{
Main.player[who].inventory[slot] = item;
item = Main.player[who].inventory[slot];
string itemName = item.Name;
var data = Plugin.Instance.item_data;
if (data.BlockExists(itemName))
{
Block block = data.GetBlock(itemName);
int.TryParse(block.GetValue(Parameters[Damage].TrimEnd(':', '0')), out item.damage);
int.TryParse(block.GetValue(Parameters[Crit].TrimEnd(':', '0')), out item.crit);
float.TryParse(block.GetValue(Parameters[KB].TrimEnd(':', '0')), out item.knockBack);
byte.TryParse(block.GetValue(Parameters[Prefix].TrimEnd(':', '0')), out item.prefix);
int.TryParse(block.GetValue(Parameters[ReuseDelay].TrimEnd(':', '0')), out item.reuseDelay);
int.TryParse(block.GetValue(Parameters[Shoot].TrimEnd(':', '0')), out item.shoot);
float.TryParse(block.GetValue(Parameters[ShootSpeed].TrimEnd(':', '0')), out item.shootSpeed);
int.TryParse(block.GetValue(Parameters[UseAmmo].TrimEnd(':', '0')), out item.useAmmo);
int.TryParse(block.GetValue(Parameters[UseTime].TrimEnd(':', '0')), out item.useTime);
int.TryParse(block.GetValue(Parameters[Width].TrimEnd(':', '0')), out item.width);
int.TryParse(block.GetValue(Parameters[Height].TrimEnd(':', '0')), out item.height);
bool.TryParse(block.GetValue(Parameters[AutoReuse].TrimEnd(':', '0')), out item.autoReuse);
int.TryParse(block.GetValue(Parameters[Ammo].TrimEnd(':', '0')), out item.ammo);
float.TryParse(block.GetValue(Parameters[Scale].TrimEnd(':', '0')), out item.scale);
}
}
item.stack = stack;
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.FromLiteral(item.Name), who, slot, item.prefix);
NetMessage.SendData((int)PacketTypes.PlayerSlot, who, -1, NetworkText.FromLiteral(item.Name), who, slot, item.prefix);
}
#region Reset Players
public static void SetDefaultStats()
{
if (Main.ServerSideCharacter)
{
StarterItems = TShock.ServerSideCharacterConfig.StartingInventory;
if (TShock.ServerSideCharacterConfig.StartingHealth > 500)
startHealth = 500;
else if (TShock.ServerSideCharacterConfig.StartingHealth < 100)
startHealth = 100;
else
startHealth = TShock.ServerSideCharacterConfig.StartingHealth;
if (TShock.ServerSideCharacterConfig.StartingMana > 200)
startMana = 200;
else if (TShock.ServerSideCharacterConfig.StartingMana < 20)
startMana = 20;
else
startMana = TShock.ServerSideCharacterConfig.StartingMana;
}
}
// Credit to: https://tshock.co/xf/index.php?resources/character-reset-ssc.4/
public static void ResetPlayer(TSPlayer player)
{
if (Main.ServerSideCharacter)
{
try
{
ResetStats(player);
ResetInventory(player);
ResetQuests(player);
ResetBanks(player);
}
catch (Exception ex)
{
TShock.Log.ConsoleError("An error occurred while resetting a player!");
TShock.Log.ConsoleError(ex.ToString());
}
}
else
{
TShock.Log.ConsoleError("The ResetPlayer method was called but SSC isn't enabled on this server!");
}
}
public static void ResetStats(TSPlayer player)
{
player.TPlayer.statLife = startHealth;
player.TPlayer.statLifeMax = startHealth;
player.TPlayer.statMana = startMana;
player.TPlayer.statManaMax = startMana;
NetMessage.SendData(4, -1, -1, NetworkText.FromLiteral(player.Name), player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(42, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(16, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(50, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(4, player.Index, -1, NetworkText.FromLiteral(player.Name), player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(42, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(16, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData(50, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
}
public static void ResetInventory(TSPlayer player)
{
ClearInventory(player);
int slot = 0;
Item give;
foreach (NetItem item in StarterItems)
{
give = TShock.Utils.GetItemById(item.NetId);
give.stack = item.Stack;
give.prefix = item.PrefixId;
if (player.InventorySlotAvailable)
{
player.TPlayer.inventory[slot] = give;
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.Empty, player.Index, slot);
slot++;
}
}
}
private static void ClearInventory(TSPlayer player) //The inventory clearing method from ClearInvSSC
{
for (int i = 0; i < NetItem.MaxInventory; i++)
{
if (i < NetItem.InventorySlots) //Main Inventory
{
player.TPlayer.inventory[i].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots) //Armor&Accessory slots
{
var index = i - NetItem.InventorySlots;
player.TPlayer.armor[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots) //Dye Slots
{
var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots);
player.TPlayer.dye[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots) //Misc Equip slots
{
var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots);
player.TPlayer.miscEquips[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots)
{
var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots);
player.TPlayer.miscDyes[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots) //piggy Bank
{
//var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots);
//player.TPlayer.bank.item[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots + NetItem.SafeSlots) //safe Bank
{
//var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots);
//player.TPlayer.bank2.item[index].netDefaults(0);
}
else if (i < NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots + NetItem.SafeSlots + NetItem.ForgeSlots) //Defender's Forge
{
//var index = i - (NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots + NetItem.SafeSlots);
//player.TPlayer.bank3.item[index].netDefaults(0);
}
else
{
player.TPlayer.trashItem.netDefaults(0);
}
}
for (int k = 0; k < NetItem.MaxInventory - (NetItem.SafeSlots + NetItem.PiggySlots + NetItem.ForgeSlots); k++) //clear all slots excluding bank slots, bank slots cleared in ResetBanks method
{
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.Empty, player.Index, (float)k, 0f, 0f, 0);
}
var trashSlot = NetItem.InventorySlots + NetItem.ArmorSlots + NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots + NetItem.PiggySlots + NetItem.SafeSlots;
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.Empty, player.Index, (float)trashSlot, 0f, 0f, 0); //trash slot
for (int k = 0; k < Player.maxBuffs; k++)
{
player.TPlayer.buffType[k] = 0;
}
NetMessage.SendData((int)PacketTypes.PlayerInfo, -1, -1, NetworkText.FromLiteral(player.Name), player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerMana, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerHp, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerBuff, -1, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
for (int k = 0; k < NetItem.MaxInventory - (NetItem.SafeSlots + NetItem.PiggySlots); k++)
{
NetMessage.SendData((int)PacketTypes.PlayerSlot, player.Index, -1, NetworkText.Empty, player.Index, (float)k, 0f, 0f, 0);
}
NetMessage.SendData((int)PacketTypes.PlayerSlot, player.Index, -1, NetworkText.Empty, player.Index, (float)trashSlot, 0f, 0f, 0);
for (int k = 0; k < Player.maxBuffs; k++)
{
player.TPlayer.buffType[k] = 0;
}
NetMessage.SendData((int)PacketTypes.PlayerInfo, player.Index, -1, NetworkText.FromLiteral(player.Name), player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerMana, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerHp, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
NetMessage.SendData((int)PacketTypes.PlayerBuff, player.Index, -1, NetworkText.Empty, player.Index, 0f, 0f, 0f, 0);
}
public static void ResetQuests(TSPlayer player)
{
player.TPlayer.anglerQuestsFinished = 0;
NetMessage.SendData((int)PacketTypes.NumberOfAnglerQuestsCompleted, -1, -1, NetworkText.Empty, player.Index);
NetMessage.SendData((int)PacketTypes.NumberOfAnglerQuestsCompleted, player.Index, -1, NetworkText.Empty, player.Index);
}
public static void ResetBanks(TSPlayer player)
{
for (int k = 0; k < NetItem.PiggySlots; k++)
{
player.TPlayer.bank.item[k].netDefaults(0);
}
for (int k = 0; k < NetItem.SafeSlots; k++)
{
player.TPlayer.bank2.item[k].netDefaults(0);
}
for (int k = 0; k < NetItem.ForgeSlots; k++)
{
player.TPlayer.bank3.item[k].netDefaults(0);
}
for (int k = NetItem.MaxInventory - (NetItem.PiggySlots + NetItem.SafeSlots + NetItem.ForgeSlots) - 1; k < NetItem.MaxInventory; k++)
{
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.Empty, player.Index, (float)k, 0f, 0f, 0);
}
for (int k = NetItem.MaxInventory - (NetItem.PiggySlots + NetItem.SafeSlots + NetItem.ForgeSlots) - 1; k < NetItem.MaxInventory; k++)
{
NetMessage.SendData((int)PacketTypes.PlayerSlot, player.Index, -1, NetworkText.Empty, player.Index, (float)k, 0f, 0f, 0);
}
}
#endregion
#region Team Set
public const string Empty = "0";
public static void TeamTeleport(string name, int whoAmI)
{
string team = Delegates.Informal[GetPlayerTeam(name)];
string[] s = Delegates.Instance.spawn.GetValue(team).Split('x');
string sX = s[0];
string sY = s[1];
float.TryParse(sX, out float x);
float.TryParse(sY, out float y);
TShock.Players[whoAmI].Teleport(x, y);
TShock.Players[whoAmI].SendSuccessMessage(string.Format("You have been sent to {0}'s spawn at {1}:{2}.", team, x, y));
}
public static int GetTeamIndex(string team)
{
for (int j = 0; j < Delegates.Teams.Length; j++)
{
if (Delegates.Teams[j].ToLower().Contains(team.ToLower()))
return j;
}
return 0;
}
public static void SetTeam(int who, int team)
{
Main.player[who].team = team;
//TShock.Players[who].SetTeam(team);
NetMessage.SendData((int)PacketTypes.PlayerTeam, -1, -1, null, who, team);
NetMessage.SendData((int)PacketTypes.PlayerTeam, who, -1, null, who, team);
//TShock.Players[who].SendData(PacketTypes.PlayerTeam, "", who, team);
}
public static int GetPlayerTeam(string name)
{
for (int i = 0; i < Delegates.Teams.Length; i++)
{
var block = Plugin.Instance.teamData.GetBlock(Delegates.Teams[i]);
foreach (string t in block.Contents)
{
if (!string.IsNullOrWhiteSpace(t))
{
if (block.Value(t).ToLower() == name.ToLower())
{
return i;
}
}
}
}
return 0;
}
public static bool SetPlayerTeam(string name, int team)
{
var block = Plugin.Instance.teamData.GetBlock(Delegates.Teams[team]);
foreach (string t in block.Contents)
{
if (!string.IsNullOrWhiteSpace(t))
{
if (block.Value(t) == Empty)
{
RemoveFromTeam(name);
block.WriteValue(block.Key(t), name);
SetTeam(FromName(name).whoAmI, team);
return true;
}
}
}
block = Plugin.Instance.teamData.GetBlock(Delegates.Teams[team]);
if (Delegates.Instance.overflow)
{
RemoveFromTeam(name);
block.AddItem("players" + block.Contents.Length + 1, name);
SetTeam(FromName(name).whoAmI, team);
return true;
}
return false;
}
public static bool RemoveFromTeam(string name)
{
for (int i = 0; i < Delegates.Teams.Length; i++)
{
var block = Plugin.Instance.teamData.GetBlock(Delegates.Teams[i]);
foreach (string t in block.Contents)
{
if (!string.IsNullOrWhiteSpace(t))
{
if (block.Value(t).ToLower() == name.ToLower())
{
block.WriteValue(block.Key(t), Empty);
SetTeam(FromName(name).whoAmI, 0);
return true;
}
}
}
}
return false;
}
public static Player FromName(string name)
{
for (int i = 0; i < Main.player.Length; i++)
{
if (Main.player[i].name.ToLower() == name.ToLower())
return Main.player[i];
}
return Main.player[255];
}
public static int TeamCount(int index)
{
int count = 0;
Block block = Plugin.Instance.teamData.GetBlock(Delegates.Teams[index]);
for (int i = 1; i <= Delegates.Instance.total; i++)
{
if (block.GetValue("players" + i) != "0")
{
count++;
}
}
return count;
}
#endregion
#region Item Tweak
public static int GetParamIndex(string key)
{
var Parameters = Delegates.Parameters;
int choose = 0;
int index = -1;
for (int i = 0; i < Parameters.Length; i++)
{
int same = 0;
for (int k = 0; k < Parameters[i].Length - 1; k++)
{
if (k < key.Length - 1)
{
if (Parameters[i].ToLower().Substring(k, 1) == key.ToLower().Substring(k, 1))
{
if (same++ > choose)
index = i;
}
}
}
choose = same;
}
return index;
}
#endregion
#region Safe Regions v2
public static Region HighestAxis(Player player, out int z)
{
int index = -1;
z = 0;
var all = TShock.Regions.Regions;
if (all.Count == 0)
return null;
for (int i = 0; i < all.Count; i++)
{
if (all[i].InArea((int)player.Center.X / 16, (int)player.Center.Y / 16))
{
if (z < all[i].Z)
{
z = all[i].Z;
index = i;
continue;
}
}
}
return index == -1 ? null : all[index];
}
public static string GetKey(Block block, string value)
{
for (int i = 0; i < block.RawData.Length; i++)
{
var c = block.RawData[i];
if (c != null && !string.IsNullOrWhiteSpace(c) && !string.IsNullOrEmpty(c) && c.Contains(value))
return c.Substring(c.IndexOf(":"));
}
return string.Empty;
}
public static string[] GetRegionNames(Block block)
{
string line = "";
for (int i = 0; i < block.Contents.Length; i++)
{
string name;
string value = (i + 1).ToString();
if ((name = block.GetValue(value)) != "0")
{
line += name + ";";
}
}
return line.Split(';');
}
public static void TogglePvp(int who, bool enabled)
{
Main.player[who].hostile = enabled;
foreach (TSPlayer tsp in TShock.Players)
{
if (tsp != null)
if (tsp.TPlayer.active)
tsp.SendData(PacketTypes.TogglePvp, "", who);
}
}
#endregion
// World replacement
public static void ReplaceTiles(int i, int j, Hooks.TileData data, int style = 0)
{
WorldGen.PlaceTile(i, j, data.type, true, true, -1, style);
if (data.wall != 0)
WorldGen.PlaceWall(i, j, data.wall, true);
OTAPI.Tile.ITile tile = Main.tile[i, j];
tile.type = data.type;
if (data.halfBrick)
{
tile.halfBrick(data.halfBrick);
}
else tile.slope(data.slope);
// TODO: sort out slopes after replacing the tiles. This is returning a System.IndexOutOfRangeException.
int x = Netplay.GetSectionX(i);
int y = Netplay.GetSectionY(j);
foreach (RemoteClient sock in Netplay.Clients.Where(t => t.IsActive))
{
sock.TileSections[x, y] = false;
}
}
}
}