-
Notifications
You must be signed in to change notification settings - Fork 9
/
KeyChanger.cs
359 lines (318 loc) · 11.8 KB
/
KeyChanger.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Localization;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.DB;
namespace KeyChanger
{
[ApiVersion(2, 1)]
public class KeyChanger : TerrariaPlugin
{
public override string Author => "Enerdy";
public static Config Config { get; private set; }
public override string Description => "SBPlanet KeyChanger System: Exchanges special chest keys by their correspondent items.";
public KeyTypes?[] Exchanging { get; private set; }
public override string Name => "KeyChanger";
public override Version Version => System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
public KeyChanger(Main game) : base(game) { }
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.GameInitialize.Deregister(this, onInitialize);
ServerApi.Hooks.NetGetData.Deregister(this, onGetData);
ServerApi.Hooks.ServerLeave.Deregister(this, onLeave);
}
}
public override void Initialize()
{
ServerApi.Hooks.GameInitialize.Register(this, onInitialize);
ServerApi.Hooks.NetGetData.Register(this, onGetData, -1);
ServerApi.Hooks.ServerLeave.Register(this, onLeave);
}
private void onGetData(GetDataEventArgs e)
{
if (Config.UseSSC || e.Handled ||e.MsgID != PacketTypes.ItemDrop || TShock.Players[e.Msg.whoAmI] == null
|| !TShock.Players[e.Msg.whoAmI].Active || Exchanging[e.Msg.whoAmI] == null)
{
return;
}
using (var reader = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)))
{
int id = reader.ReadInt16();
if (id == 400) // 400 = new Item
{
reader.ReadSingle(); // positionX
reader.ReadSingle(); // positionY
reader.ReadSingle(); // velocityX
reader.ReadSingle(); // positionY
short stack = reader.ReadInt16();
reader.ReadByte(); // prefix
reader.ReadByte(); // no delay..?
int netID = reader.ReadInt16();
if (netID == (int)Exchanging[e.Msg.whoAmI])
{
// Check if the player has available slots and warn them if they do not
if (!TShock.Players[e.Msg.whoAmI].InventorySlotAvailable)
{
TShock.Players[e.Msg.whoAmI].SendWarningMessage("Make sure you have at least one available inventory slot to perform this exchange.");
return;
}
Key key = Utils.LoadKey(Exchanging[e.Msg.whoAmI].Value);
if (Config.EnableRegionExchanges)
{
Region region;
if (Config.MarketMode)
region = TShock.Regions.GetRegionByName(Config.MarketRegion);
else
region = key.Region;
// Checks if the player is inside the region
if (!region.InArea((int)TShock.Players[e.Msg.whoAmI].X, (int)TShock.Players[e.Msg.whoAmI].Y))
{
return;
}
}
// Cancel the drop
TShock.Players[e.Msg.whoAmI].SendData(PacketTypes.ItemDrop, "", id);
// If the item is stackable, give them the same amount of in return; otherwise, return the excess
Random rand = new Random();
Item give = key.Items[rand.Next(0, key.Items.Count)];
if (give.maxStack >= stack)
{
TShock.Players[e.Msg.whoAmI].GiveItem(give.netID, give.Name, give.width, give.height, stack);
Item take = TShock.Utils.GetItemById((int)key.Type);
TShock.Players[e.Msg.whoAmI].SendSuccessMessage($"Exchanged {stack} {take.Name}(s) for {stack} {give.Name}(s)!");
}
else
{
TShock.Players[e.Msg.whoAmI].GiveItem(give.netID, give.Name, give.width, give.height, 1);
Item take = TShock.Utils.GetItemById((int)key.Type);
TShock.Players[e.Msg.whoAmI].SendSuccessMessage($"Exchanged a {take.Name} for 1 {give.Name}!");
TShock.Players[e.Msg.whoAmI].GiveItem(take.netID, take.Name, take.width, take.height, stack - 1);
TShock.Players[e.Msg.whoAmI].SendSuccessMessage("Returned the excess keys.");
}
Exchanging[e.Msg.whoAmI] = null;
e.Handled = true;
}
}
}
}
private void onInitialize(EventArgs e)
{
Config = Config.Read();
//This is the main command, which branches to everything the plugin can do, by checking the first parameter a player inputs
Commands.ChatCommands.Add(new Command(new List<string>() { "key.change", "key.reload", "key.mode" }, KeyChange, "key")
{
HelpDesc = new[]
{
$"{Commands.Specifier}key - Shows plugin info.",
$"{Commands.Specifier}key change <type> - Exchanges a key of the input type.",
$"{Commands.Specifier}key list - Shows a list of available keys and items.",
$"{Commands.Specifier}key mode <mode> - Changes exchange mode.",
$"{Commands.Specifier}key reload - Reloads the config file.",
"If an exchange fails, make sure your inventory has free slots."
}
});
Exchanging = new KeyTypes?[Main.maxNetPlayers];
}
private void onLeave(LeaveEventArgs e)
{
if (e.Who >= 0 || e.Who < Main.maxNetPlayers)
{
Exchanging[e.Who] = null;
}
}
private void KeyChange(CommandArgs args)
{
TSPlayer ply = args.Player;
// SSC check to alert users
if (!Main.ServerSideCharacter)
{
ply.SendWarningMessage("[Warning] This plugin will not work properly with ServerSideCharacters disabled.");
}
if (args.Parameters.Count < 1)
{
// Plugin Info
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
ply.SendMessage($"KeyChanger (v{version}) by Enerdy", Color.SkyBlue);
ply.SendMessage("Description: Changes special chest keys into their specific items", Color.SkyBlue);
ply.SendMessage($"Syntax: {Commands.Specifier}key <list/mode/change/reload> [type]", Color.SkyBlue);
ply.SendMessage($"Type {Commands.Specifier}help key for more info", Color.SkyBlue);
}
else if (args.Parameters[0].ToLower() == "change" && args.Parameters.Count == 1)
{
ply.SendErrorMessage("Invalid syntax! Proper syntax: {0}key change <type>", Commands.Specifier);
}
else if (args.Parameters.Count > 0)
{
string cmd = args.Parameters[0].ToLower();
switch (cmd)
{
case "change":
// Prevents cast from the server console
if (!ply.RealPlayer)
{
ply.SendErrorMessage("You must use this command in-game.");
return;
}
if (!ply.HasPermission("key.change"))
{
ply.SendErrorMessage("You do not have access to this command.");
break;
}
KeyTypes type;
if (!Enum.TryParse(args.Parameters[1].ToLowerInvariant(), true, out type))
{
ply.SendErrorMessage("Invalid key type! Available types: " + String.Join(", ",
Config.EnableTempleKey ? "temple" : null,
Config.EnableJungleKey ? "jungle" : null,
Config.EnableCorruptionKey ? "corruption" : null,
Config.EnableCrimsonKey ? "crimson" : null,
Config.EnableHallowedKey ? "hallowed" : null,
Config.EnableFrozenKey ? "frozen" : null));
return;
}
Key key = Utils.LoadKey(type);
// Verifies whether the key has been enabled
if (!key.Enabled)
{
ply.SendInfoMessage("The selected key is disabled.");
return;
}
if (!Config.UseSSC)
{
// Begin the exchange, expect the player to drop the key
Exchanging[args.Player.Index] = type;
ply.SendInfoMessage($"Drop (hold & right-click) any number of {key.Name} keys to proceed.");
return;
}
// Checks if the player carries the necessary key
var lookup = ply.TPlayer.inventory.FirstOrDefault(i => i.netID == (int)key.Type);
if (lookup == null)
{
ply.SendErrorMessage("Make sure you carry the selected key in your inventory.");
return;
}
if (Config.EnableRegionExchanges)
{
Region region;
if (Config.MarketMode)
region = TShock.Regions.GetRegionByName(Config.MarketRegion);
else
region = key.Region;
// Checks if the required region is set to null
if (region == null)
{
ply.SendInfoMessage("No valid region was set for this key.");
return;
}
// Checks if the player is inside the region
if (!region.InArea(args.Player.TileX, args.Player.TileY))
{
ply.SendErrorMessage("You are not in a valid region to make this exchange.");
return;
}
}
Item item;
for (int i = 0; i < 50; i++)
{
item = ply.TPlayer.inventory[i];
// Loops through the player's inventory
if (item.netID == (int)key.Type)
{
// Found the item, checking for available slots
if (item.stack == 1 || ply.InventorySlotAvailable)
{
ply.TPlayer.inventory[i].stack--;
NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, NetworkText.Empty, ply.Index, i);
Random rand = new Random();
Item give = key.Items[rand.Next(0, key.Items.Count)];
ply.GiveItem(give.netID, give.Name, give.width, give.height, 1);
Item take = TShock.Utils.GetItemById((int)key.Type);
ply.SendSuccessMessage("Exchanged a {0} for 1 {1}!", take.Name, give.Name);
return;
}
// Sent if neither of the above conditions were fulfilled.
ply.SendErrorMessage("Make sure you have at least one available inventory slot.");
return;
}
}
break;
case "reload":
{
if (!ply.HasPermission("key.reload"))
{
ply.SendErrorMessage("You do not have access to this command.");
break;
}
Config = Config.Read();
ply.SendSuccessMessage("KeyChangerConfig.json reloaded successfully.");
break;
}
case "list":
{
ply.SendMessage("Temple Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Temple).Items.Select(i => i.Name)), Color.Goldenrod);
ply.SendMessage("Jungle Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Jungle).Items.Select(i => i.Name)), Color.Goldenrod);
ply.SendMessage("Corruption Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Corruption).Items.Select(i => i.Name)), Color.Goldenrod);
ply.SendMessage("Crimson Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Crimson).Items.Select(i => i.Name)), Color.Goldenrod);
ply.SendMessage("Hallowed Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Hallowed).Items.Select(i => i.Name)), Color.Goldenrod);
ply.SendMessage("Frozen Key - " + String.Join(", ", Utils.LoadKey(KeyTypes.Frozen).Items.Select(i => i.Name)), Color.Goldenrod);
break;
}
case "mode":
{
if (!ply.HasPermission("key.mode"))
{
ply.SendErrorMessage("You do not have access to this command.");
break;
}
if (args.Parameters.Count < 2)
{
ply.SendErrorMessage("Invalid syntax! Proper syntax: {0}key mode <normal/region/market>", Commands.Specifier);
break;
}
string query = args.Parameters[1].ToLower();
if (query == "normal")
{
Config.EnableRegionExchanges = false;
ply.SendSuccessMessage("Exchange mode set to normal (exchange everywhere).");
}
else if (query == "region")
{
Config.EnableRegionExchanges = true;
Config.MarketMode = false;
ply.SendSuccessMessage("Exchange mode set to region (a region for each type).");
}
else if (query == "market")
{
Config.EnableRegionExchanges = true;
Config.MarketMode = true;
ply.SendSuccessMessage("Exchange mode set to market (one region for every type).");
}
else
{
ply.SendErrorMessage("Invalid syntax! Proper syntax: {0}key mode <normal/region/market>", Commands.Specifier);
return;
}
Config.Write();
break;
}
default:
{
ply.SendErrorMessage(Utils.ErrorMessage(ply));
break;
}
}
}
else
{
ply.SendErrorMessage(Utils.ErrorMessage(ply));
}
}
}
}