-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewInventory.cs
513 lines (484 loc) · 21.8 KB
/
ViewInventory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TShockAPI;
using Terraria;
using TerrariaApi.Server;
using Microsoft.Xna.Framework;
using System.Data;
using TShockAPI.DB;
using Terraria.ID;
namespace ViewInventory
{
[ApiVersion(2, 1)]
public class ViewInventory : TerrariaPlugin
{
/// <summary>
/// Gets the author(s) of this plugin
/// </summary>
public override string Author => "z枳";
/// <summary>
/// Gets the description of this plugin.
/// A short, one lined description that tells people what your plugin does.
/// </summary>
public override string Description => "查阅背包";
/// <summary>
/// Gets the name of this plugin.
/// </summary>
public override string Name => "ViewInventory";
/// <summary>
/// Gets the version of this plugin.
/// </summary>
public override Version Version => new Version(1, 0, 0, 0);
/// <summary>
/// Initializes a new instance of the TestPlugin class.
/// This is where you set the plugin's order and perfrom other constructor logic
///初始化TestPlugin类的新实例。
///这是设置插件顺序和性能的地方,来自其他构造函数逻辑
/// </summary>
public ViewInventory(Main game) : base(game)
{
}
/// <summary>
/// Handles plugin initialization.
/// Fired when the server is started and the plugin is being loaded.
/// You may register hooks, perform loading procedures etc here.
///处理插件初始化。
///在服务器启动和插件加载时触发。
///您可以在此处注册挂钩、执行加载过程等。
/// </summary>
public override void Initialize()
{
Commands.ChatCommands.Add(new Command("vi", ViewInvent, "vi", "VI", "Vi", "vI", "v i")
{
HelpText = "输入 /vi 【玩家名】 来查看该玩家的库存\nEnter /vi [player name] to view the player's inventory"
});
Commands.ChatCommands.Add(new Command("vi", ViewInventDisorder, "vid", "VID", "Vid", "vId")
{
HelpText = "输入 /vid 【玩家名】 来查看该玩家的库存,不进行排列\nEnter / vid [player name] to view the player's inventory without sorting"
});
Commands.ChatCommands.Add(new Command("vi", ViewInventText, "vit", "VIT", "Vit", "vIt")
{
HelpText = "输入 /vit 【玩家名】 来查看该玩家的库存,不进行排列\nEnter / vit [player name] to view the player's inventory without sorting"
});
}
/// <summary>
/// Handles plugin disposal logic.
/// *Supposed* to fire when the server shuts down.
/// You should deregister hooks and free all resources here.
///处理插件处理逻辑。
///*Supposed**应该*在服务器关闭时触发。
///您应该取消注册挂钩并释放此处的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
base.Dispose(disposing);
}
//分类查阅指令
private void ViewInvent(CommandArgs args)
{
if (!args.Parameters.Any())
{
args.Player.SendInfoMessage("输入 /vi 【玩家名】 来查看该玩家的库存\nEnter / vi [player name] to view the player's inventory");
return;
}
string name = args.Parameters[0];
if (args.Parameters.Count > 1)
{
name = string.Join(" ", args.Parameters);
}
List<TSPlayer> list = TSPlayer.FindByNameOrID(name);
if (list.Any())
{
foreach (var li in list)
{
StringBuilder sb = new StringBuilder();
string inventory = GetItemsString(li.TPlayer.inventory, NetItem.InventorySlots);
string armor = GetItemsString(li.TPlayer.armor, NetItem.ArmorSlots);
string dyestuff = GetItemsString(li.TPlayer.dye, NetItem.DyeSlots);
string misc = GetItemsString(li.TPlayer.miscEquips, NetItem.MiscEquipSlots);
string miscDye = GetItemsString(li.TPlayer.miscDyes, NetItem.MiscDyeSlots);
string trash = string.Format("【[i/s{0}:{1}]】 ", li.TPlayer.trashItem.stack, li.TPlayer.trashItem.netID);
string pig = GetItemsFromChestString(li.TPlayer.bank, NetItem.PiggySlots);
string safe = GetItemsFromChestString(li.TPlayer.bank2, NetItem.SafeSlots);
string forge = GetItemsFromChestString(li.TPlayer.bank3, NetItem.ForgeSlots);
string vault = GetItemsFromChestString(li.TPlayer.bank4, NetItem.VoidSlots);
sb.AppendLine("玩家 【" + li.Name + "】 的所有库存如下:");
if (inventory.Length > 0 && inventory != null && inventory != "")
{
sb.AppendLine("背包:");
sb.AppendLine(FormatArrangement(inventory, 20, " "));
}
if (armor.Length > 0 && armor != null && armor != "")
{
sb.AppendLine("盔甲 + 饰品 + 时装:");
sb.AppendLine(armor);
}
if (dyestuff.Length > 0 && dyestuff != null && dyestuff != "")
{
sb.AppendLine("染料:");
sb.AppendLine(dyestuff);
}
if (misc.Length > 0 && misc != null && misc != "")
{
sb.AppendLine("宠物 + 矿车 + 坐骑 + 钩爪:");
sb.AppendLine(misc);
}
if (miscDye.Length > 0 && miscDye != null && miscDye != "")
{
sb.AppendLine("宠物 矿车 坐骑 钩爪 染料:");
sb.AppendLine(miscDye);
}
if (trash != "【[i/s0:0]】 ")
{
sb.AppendLine("垃圾桶:");
sb.AppendLine(trash);
}
if (pig.Length > 0 && pig != null && pig != "")
{
sb.AppendLine("猪猪储蓄罐:");
sb.AppendLine(FormatArrangement(pig, 20, " "));
}
if (safe.Length > 0 && safe != null && safe != "")
{
sb.AppendLine("保险箱:");
sb.AppendLine(FormatArrangement(safe, 20, " "));
}
if (forge.Length > 0 && forge != null && forge != "")
{
sb.AppendLine("护卫熔炉:");
sb.AppendLine(FormatArrangement(forge, 20, " "));
}
if (vault.Length > 0 && vault != null && vault != "")
{
sb.AppendLine("虚空金库:");
sb.AppendLine(FormatArrangement(vault, 20, " "));
}
if (sb.Length > 0 && sb != null && sb.ToString() != "")
args.Player.SendMessage(sb.ToString(), TextColor());
else
args.Player.SendInfoMessage("没有任何东西");
}
}
else
{
args.Player.SendInfoMessage("所查询玩家不在线,正在查询离线数据");
string offAll = GetOfflinePlayerInv(TShock.DB, name);
offAll = FormatArrangement(offAll, 35);
if (offAll != "")
{
args.Player.SendMessage("玩家 【" + name + "】 的所有库存如下:" + "\n" + offAll, TextColor());
}
else
{
args.Player.SendInfoMessage("该玩家不存在!");
}
}
}
//不分类查阅指令
private void ViewInventDisorder(CommandArgs args)
{
if (!args.Parameters.Any())
{
args.Player.SendInfoMessage("输入 /vid 【玩家名】 来查看该玩家的库存,不进行排列\nEnter / vid [player name] to view the player's inventory without sorting");
return;
}
string name = args.Parameters[0];
if (args.Parameters.Count > 1)
{
name = string.Join(" ", args.Parameters);
}
List<TSPlayer> list = TSPlayer.FindByNameOrID(name);
if (list.Any())
{
foreach (var li in list)
{
string inventory = GetItemsString(li.TPlayer.inventory, NetItem.InventorySlots);
string armor = GetItemsString(li.TPlayer.armor, NetItem.ArmorSlots);
string dyestuff = GetItemsString(li.TPlayer.dye, NetItem.DyeSlots);
string misc = GetItemsString(li.TPlayer.miscEquips, NetItem.MiscEquipSlots);
string miscDye = GetItemsString(li.TPlayer.miscDyes, NetItem.MiscDyeSlots);
string trash = string.Format("【[i/s{0}:{1}]】 ", li.TPlayer.trashItem.stack, li.TPlayer.trashItem.netID);
string pig = GetItemsFromChestString(li.TPlayer.bank, NetItem.PiggySlots);
string safe = GetItemsFromChestString(li.TPlayer.bank2, NetItem.SafeSlots);
string forge = GetItemsFromChestString(li.TPlayer.bank3, NetItem.ForgeSlots);
string vault = GetItemsFromChestString(li.TPlayer.bank4, NetItem.VoidSlots);
if (trash == "【[i/s0:0]】 ")
trash = "";
string all = inventory + armor + dyestuff + misc + misc + miscDye + trash + pig + safe + forge + vault;
all = FormatArrangement(all, 35);
if (all != "")
{
args.Player.SendMessage("玩家 【" + li.Name + "】 的所有库存如下:\n" + all, TextColor());
}
else
args.Player.SendInfoMessage("没有任何东西");
}
}
else
{
args.Player.SendInfoMessage("所查询玩家不在线,正在查询离线数据");
string offAll = GetOfflinePlayerInv(TShock.DB, name);
offAll = FormatArrangement(offAll, 35);
if (offAll != "")
{
args.Player.SendMessage("玩家 【" + name + "】 的所有库存如下:" + "\n" + offAll, TextColor());
}
else
{
args.Player.SendInfoMessage("该玩家不存在!");
}
}
}
//返回文本查阅背包
private void ViewInventText(CommandArgs args)
{
if (!args.Parameters.Any())
{
args.Player.SendInfoMessage("输入 /vit 【玩家名】 来查看该玩家的库存\nEnter / vit [player name] to view the player's inventory");
return;
}
string name = args.Parameters[0];
if (args.Parameters.Count > 1)
{
name = string.Join(" ", args.Parameters);
}
List<TSPlayer> list = TSPlayer.FindByNameOrID(name);
if (list.Any())
{
foreach (var li in list)
{
StringBuilder sb = new StringBuilder();
string inventory = GetItemsString(li.TPlayer.inventory, NetItem.InventorySlots,1);
string armor = GetItemsString(li.TPlayer.armor, NetItem.ArmorSlots,1);
string dyestuff = GetItemsString(li.TPlayer.dye, NetItem.DyeSlots,1);
string misc = GetItemsString(li.TPlayer.miscEquips, NetItem.MiscEquipSlots,1);
string miscDye = GetItemsString(li.TPlayer.miscDyes, NetItem.MiscDyeSlots,1);
string trash = $" [{Lang.prefix[li.TPlayer.trashItem.prefix].Value}.{li.TPlayer.trashItem.Name}:{li.TPlayer.trashItem.stack}] ";
string pig = GetItemsFromChestString(li.TPlayer.bank, NetItem.PiggySlots,1);
string safe = GetItemsFromChestString(li.TPlayer.bank2, NetItem.SafeSlots,1);
string forge = GetItemsFromChestString(li.TPlayer.bank3, NetItem.ForgeSlots,1);
string vault = GetItemsFromChestString(li.TPlayer.bank4, NetItem.VoidSlots,1);
sb.AppendLine("玩家 【" + li.Name + "】 的所有库存如下:");
if (inventory.Length > 0 && inventory != null && inventory != "")
{
sb.AppendLine("背包:");
sb.AppendLine(FormatArrangement(inventory, 20, " "));
}
if (armor.Length > 0 && armor != null && armor != "")
{
sb.AppendLine("盔甲 + 饰品 + 时装:");
sb.AppendLine(armor);
}
if (dyestuff.Length > 0 && dyestuff != null && dyestuff != "")
{
sb.AppendLine("染料:");
sb.AppendLine(dyestuff);
}
if (misc.Length > 0 && misc != null && misc != "")
{
sb.AppendLine("宠物 + 矿车 + 坐骑 + 钩爪:");
sb.AppendLine(misc);
}
if (miscDye.Length > 0 && miscDye != null && miscDye != "")
{
sb.AppendLine("宠物 矿车 坐骑 钩爪 染料:");
sb.AppendLine(miscDye);
}
if (trash != " [.:0] ")
{
sb.AppendLine("垃圾桶:");
sb.AppendLine(trash);
}
if (pig.Length > 0 && pig != null && pig != "")
{
sb.AppendLine("猪猪储蓄罐:");
sb.AppendLine(FormatArrangement(pig, 20, " "));
}
if (safe.Length > 0 && safe != null && safe != "")
{
sb.AppendLine("保险箱:");
sb.AppendLine(FormatArrangement(safe, 20, " "));
}
if (forge.Length > 0 && forge != null && forge != "")
{
sb.AppendLine("护卫熔炉:");
sb.AppendLine(FormatArrangement(forge, 20, " "));
}
if (vault.Length > 0 && vault != null && vault != "")
{
sb.AppendLine("虚空金库:");
sb.AppendLine(FormatArrangement(vault, 20, " "));
}
if (sb.Length > 0 && sb != null && sb.ToString() != "")
args.Player.SendMessage(sb.ToString(), TextColor());
else
args.Player.SendInfoMessage("没有任何东西");
}
}
else
{
args.Player.SendInfoMessage("所查询玩家不在线,正在查询离线数据");
string offAll = GetOfflinePlayerInv(TShock.DB, name, 1);
offAll = FormatArrangement(offAll, 35);
if (offAll != "")
{
args.Player.SendMessage("玩家 【" + name + "】 的所有库存如下:" + "\n" + offAll, TextColor());
}
else
{
args.Player.SendInfoMessage("该玩家不存在!");
}
}
}
//返回玩家身上物品的字符串
public static string GetItemsString(Item[] items, int slots, int Model = 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < slots; i++)
{
Item item = items[i];
if (Model == 0 && !item.IsAir)
{
if (item.prefix != 0)
sb.Append(string.Format("【[i/p{0}:{1}]】 ", item.prefix, item.netID));
else
sb.Append(string.Format("【[i/s{0}:{1}]】 ", item.stack, item.netID));
}
if (Model == 1 && !item.IsAir)
{
if (item.prefix != 0)
sb.Append($"[{Lang.prefix[item.prefix].Value}.{item.Name}]");
else
sb.Append($"[{item.Name}:{item.stack}]");
}
}
return sb.ToString();
}
//返回玩家猪猪等储蓄物内的物品的字符串
public static string GetItemsFromChestString(Chest chest, int slots, int Model = 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < slots; i++)
{
Item item = chest.item[i];
if (Model == 0 && !item.IsAir)
{
if (item.prefix != 0)
sb.Append(string.Format("【[i/p{0}:{1}]】 ", item.prefix, item.netID));
else
sb.Append(string.Format("【[i/s{0}:{1}]】 ", item.stack, item.netID));
}
if (Model == 1 && !item.IsAir)
{
if (item.prefix != 0)
sb.Append($"[{Lang.prefix[item.prefix].Value}.{item.Name}]");
else
sb.Append($"[{item.Name}:{item.stack}]");
}
}
return sb.ToString();
}
//查询离线玩家sqlite查询
public static string GetOfflinePlayerInv(IDbConnection db, string plrName, int Model = 0)
{
int userAccountID = TShock.UserAccounts.GetUserAccountID(plrName);
string result;
using (QueryResult queryResult = DbExt.QueryReader(db, "SELECT * FROM tsCharacter WHERE Account=" + userAccountID))
{
if (queryResult.Read())
{
List<NetItem> list = queryResult.Get<string>("Inventory").Split(new char[] { '~' }).Select(new Func<string, NetItem>(NetItem.Parse)).ToList<NetItem>();
if (list.Count < NetItem.MaxInventory)
{
list.InsertRange(67, new NetItem[2]);
list.InsertRange(77, new NetItem[2]);
list.InsertRange(87, new NetItem[2]);
list.AddRange(new NetItem[NetItem.MaxInventory - list.Count]);
}
result = GetItemsString(list.ToArray(), list.Count, Model);
}
else
{
result = "";
}
}
return result;
}
//返回离线玩家身上的字符串
public static string GetItemsString(NetItem[] items, int slots, int Model = 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < slots; i++)
{
NetItem item = items[i];
if (Model == 0 && item.NetId != 0)
{
if (item.PrefixId != 0)
sb.Append(string.Format("【[i/p{0}:{1}]】 ", item.PrefixId, item.NetId));
else
sb.Append(string.Format("【[i/s{0}:{1}]】 ", item.Stack, item.NetId));
}
if (Model == 1 && item.NetId != 0)
{
if (item.PrefixId != 0)
sb.Append($"[{Lang.prefix[item.PrefixId].Value}.{Lang.GetItemName(item.NetId)}]");
else
sb.Append($"[{Lang.GetItemName(item.NetId)}:{item.Stack}]");
}
}
return sb.ToString();
}
//给出一个字符串和每行几个物品数,返回排列好的字符串
public static string FormatArrangement(string str, int num, string block = "")
{
if (!string.IsNullOrWhiteSpace(str))
{
List<string> ls = str.Split(' ').ToList();
for (int i = 0; i < ls.Count; i++)
{
if ((i + 1) % (num + 1) == 0)
{
ls.Insert(i, "\n");
}
}
if (block == "")
return string.Join(block, ls);
else
{
StringBuilder sb = new StringBuilder();
foreach (string s in ls)
{
if (s != "\n")
{
sb.Append(s);
sb.Append(block);
}
else
{
sb.AppendLine();
}
}
return sb.ToString();
}
}
else
{
return "";
}
}
//返回颜色
public static Color TextColor()
{
int r, g, b;
r = Main.rand.Next(60, 255);
g = Main.rand.Next(60, 255);
b = Main.rand.Next(60, 255);
return new Color(r, g, b);
}
}
}