-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCommand.cs
More file actions
225 lines (195 loc) · 9.3 KB
/
RemoveCommand.cs
File metadata and controls
225 lines (195 loc) · 9.3 KB
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
using TShockAPI;
namespace ItemSearch
{
public static class RemoveCommands
{
public static void RemoveOnline(CommandArgs args)
{
var cfg = Configuration.Reload();
// 新用法:/is del player <玩家名/*all> <物品名称/ID> [数量,不填=全部]
if (args.Parameters.Count < 4)
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 用法: /is del player <玩家名/*all> <物品名称/ID> [数量]");
return;
}
string target = args.Parameters[2];
string itemInput = args.Parameters[3];
int amount = 0; // 0表示全部移除
if (args.Parameters.Count > 4 && !int.TryParse(args.Parameters[4], out amount))
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 数量必须是数字");
return;
}
// 解析物品
if (!ItemUtils.TryParseItem(itemInput, out int itemId, out string itemName))
{
if (!string.IsNullOrEmpty(itemName))
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 找到多个匹配: {itemName}");
else
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 未找到物品: {itemInput}");
return;
}
bool all = target == "*all*";
if (!all)
{
var plr = TShock.Players.FirstOrDefault(p => p?.Active == true &&
p.Name.Equals(target, StringComparison.OrdinalIgnoreCase));
if (plr == null)
{
args.Player.SendErrorMessage(string.Format(cfg.Msg.PlayerOffline, target));
return;
}
int removed = PlayerInventoryUtils.RemoveFromOnline(plr, itemId, amount);
string msg = amount == 0 ? "全部" : $"{removed}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从 {plr.Name} 移除 {ItemUtils.FormatItem(itemId, itemName)} x{msg}");
}
else
{
_ = Task.Run(async () =>
{
int totalPlayers = 0;
int totalRemoved = 0;
var players = TShock.Players.Where(p => p?.Active == true).ToList();
foreach (var plr in players)
{
int removed = PlayerInventoryUtils.RemoveFromOnline(plr, itemId, amount);
if (removed > 0)
{
totalPlayers++;
totalRemoved += removed;
plr.SendInfoMessage($"管理员移除了你的 {ItemUtils.FormatItem(itemId, itemName)} x{removed}");
}
}
string msg = amount == 0 ? "全部" : $"{totalRemoved}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从 {totalPlayers}位在线玩家 移除 {msg} {itemName}");
});
}
}
public static void RemoveChests(CommandArgs args)
{
var cfg = Configuration.Reload();
// 新用法:/is delchest <箱子ID/*all> <物品名称/ID> [数量,不填=全部]
if (args.Parameters.Count < 3)
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 用法: /is delchest <箱子ID/*all> <物品名称/ID> [数量]");
return;
}
string idStr = args.Parameters[1];
string itemInput = args.Parameters[2];
int amount = 0;
if (args.Parameters.Count > 3 && !int.TryParse(args.Parameters[3], out amount))
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 数量必须是数字");
return;
}
// 解析物品
if (!ItemUtils.TryParseItem(itemInput, out int itemId, out string itemName))
{
if (!string.IsNullOrEmpty(itemName))
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 找到多个匹配: {itemName}");
else
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 未找到物品: {itemInput}");
return;
}
if (idStr == "*all*")
{
var progress = new Progress<(int current, int total)>(p =>
{
if (p.current % 100 == 0)
args.Player.SendInfoMessage($"[c/00FFFF:物品搜索] 处理中... {p.current}/{p.total}");
});
int perTick = cfg.Perf.ChestsPerTick;
if (amount == 0) perTick = 100; // 全部移除时加快速度
_ = ChestUtils.RemoveFromAllAsync(itemId, amount, perTick, progress)
.ContinueWith(t =>
{
var (chests, total) = t.Result;
string msg = amount == 0 ? "全部" : $"{total}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从 {chests}个箱子 移除 {msg} {itemName}");
});
}
else if (int.TryParse(idStr, out int chestId))
{
if (chestId < 0 || chestId >= Main.maxChests || Main.chest[chestId] == null)
{
args.Player.SendErrorMessage(string.Format(cfg.Msg.InvalidChest, chestId));
return;
}
int removed = 0;
int remaining = amount;
for (int i = 0; i < ChestUtils.ChestMaxItems; i++)
{
if (Main.chest[chestId].item[i]?.type == itemId)
{
int toRemove = amount == 0 ? Main.chest[chestId].item[i].stack : Math.Min(Main.chest[chestId].item[i].stack, remaining);
ChestUtils.RemoveItem(chestId, i, toRemove, out int rem);
if (amount > 0) remaining -= rem;
removed += rem;
if (amount > 0 && remaining <= 0) break;
}
}
string msg = amount == 0 ? "全部" : $"{removed}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从箱子#{chestId} 移除 {msg} {itemName}");
}
else
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 用法: /is delchest <箱子ID/*all> <物品名称/ID> [数量]");
}
}
public static void RemoveSSC(CommandArgs args)
{
var cfg = Configuration.Reload();
// 新用法:/is delssc <玩家名/*all> <物品名称/ID> [数量,不填=全部]
if (args.Parameters.Count < 3)
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 用法: /is delssc <玩家名/*all> <物品名称/ID> [数量]");
return;
}
string target = args.Parameters[1];
string itemInput = args.Parameters[2];
int amount = 0;
if (args.Parameters.Count > 3 && !int.TryParse(args.Parameters[3], out amount))
{
args.Player.SendErrorMessage("[c/FF0000:物品搜索] 数量必须是数字");
return;
}
if (!PlayerInventoryUtils.IsSSCEnabled())
{
args.Player.SendErrorMessage(cfg.Msg.SSCDisabled);
return;
}
// 解析物品
if (!ItemUtils.TryParseItem(itemInput, out int itemId, out string itemName))
{
if (!string.IsNullOrEmpty(itemName))
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 找到多个匹配: {itemName}");
else
args.Player.SendErrorMessage($"[c/FF0000:物品搜索] 未找到物品: {itemInput}");
return;
}
bool all = target == "*all*";
if (!all)
{
if (!PlayerInventoryUtils.RemoveFromSSC(target, itemId, amount, out int removed))
{
args.Player.SendErrorMessage(string.Format(cfg.Msg.SSCNotFound, target));
return;
}
string msg = amount == 0 ? "全部" : $"{removed}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从离线玩家 {target} 移除 {msg} {itemName}");
}
else
{
var progress = new Progress<string>(msg =>
args.Player.SendInfoMessage($"[c/00FFFF:物品搜索] 处理中: {msg}"));
_ = PlayerInventoryUtils.RemoveFromAllSSCAsync(itemId, amount, progress)
.ContinueWith(t =>
{
var (players, total) = t.Result;
string msg = amount == 0 ? "全部" : $"{total}个";
args.Player.SendSuccessMessage($"[c/00FF00:物品搜索] 已从 {players}位离线玩家 移除 {msg} {itemName}");
});
}
}
}
}