-
Notifications
You must be signed in to change notification settings - Fork 0
/
GravebagPlugin.cs
362 lines (303 loc) · 15.7 KB
/
GravebagPlugin.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
using System.Reflection;
using Terraria.DataStructures;
using TShockAPI.DB;
namespace Gravebags {
[ApiVersion(2, 1)]
public class GravebagPlugin : TerrariaPlugin {
// Dictionary<itemIndex, gravebag>
private Dictionary<int, Gravebag> gravebags = new Dictionary<int, Gravebag>();
// Dictionary<accountID, bagIndex>
private Dictionary<int, int> gravebagsNearPlayer = new Dictionary<int, int>();
public GravebagManager dbManager;
static readonly int TotalSlots = NetItem.InventorySlots + NetItem.ArmorSlots
+ NetItem.DyeSlots + NetItem.MiscEquipSlots + NetItem.MiscDyeSlots;
static readonly int CursorSlotIndex = NetItem.InventorySlots - 1;
#region Info
public override string Name => "Gravebags";
public override string Author => "PotatoCider";
public override string Description => "Gravebags plugin for Mediumcore playthrough.";
public override Version Version => Assembly.GetExecutingAssembly().GetName().Version;
#endregion
#region Initialize
public override void Initialize() {
dbManager = new GravebagManager(TShock.DB);
Commands.ChatCommands.Add(new Command(PickupBagCommand, "pickupbag", "pickup", "pick", "pb", "b"));
ServerApi.Hooks.GamePostInitialize.Register(this, OnGamePostInitialize);
GetDataHandlers.KillMe.Register(OnKillMe);
GetDataHandlers.PlayerUpdate.Register(OnPlayerUpdate);
}
#endregion
#region Dispose
protected override void Dispose(bool disposing) {
if (disposing) {
ServerApi.Hooks.GamePostInitialize.Deregister(this, OnGamePostInitialize);
GetDataHandlers.KillMe.UnRegister(OnKillMe);
GetDataHandlers.PlayerUpdate.UnRegister(OnPlayerUpdate);
}
base.Dispose(disposing);
}
#endregion
public GravebagPlugin(Main game)
: base(game) {
Order = 1;
}
#region Commands
void PickupBagCommand(CommandArgs args) {
int accountID = args.Player.Account.ID;
if (gravebagsNearPlayer.TryGetValue(accountID, out int itemIndex)) {
PickupGravebag(args.Player, itemIndex);
} else {
args.Player.SendInfoMessage("[Gravebags] There are no bags around you to pick up.");
}
}
#endregion
#region Handlers
void OnGamePostInitialize(EventArgs args) {
TShock.Log.ConsoleDebug("[Gravebags] Post Initialize");
List<Gravebag> bags = dbManager.GetAllGravebags(Main.worldID);
foreach (Gravebag bag in bags) {
SpawnGravebag(bag);
}
}
void OnKillMe(object _, GetDataHandlers.KillMeEventArgs args) {
Player player = args.Player.TPlayer;
if (player.difficulty != PlayerDifficultyID.MediumCore
&& player.difficulty != PlayerDifficultyID.Hardcore) return;
List<NetItem> inventory = new List<NetItem>(TotalSlots);
inventory.AddRange(player.inventory.Select(item => (NetItem)item));
inventory.AddRange(player.armor.Select(item => (NetItem)item));
inventory.AddRange(player.dye.Select(item => (NetItem)item));
inventory.AddRange(player.miscEquips.Select(item => (NetItem)item));
inventory.AddRange(player.miscDyes.Select(item => (NetItem)item));
// suppress item drops
int i = 0;
bool anyItemDropped = false;
foreach (NetItem item in inventory) {
if (IsEmptyOrIgnoredItem(item)) continue;
anyItemDropped = true;
int last = i;
do {
Item floorItem = Main.item[i];
if (floorItem.active &&
floorItem.netID == item.NetId &&
floorItem.stack == item.Stack &&
floorItem.prefix == item.PrefixId) {
Main.item[i].netDefaults(ItemID.None);
Main.item[i].active = false;
TSPlayer.All.SendData(PacketTypes.ItemDrop, null, i);
break;
}
i = (i + 1) % 400;
if (i == last) {
TShock.Log.ConsoleError("[Gravebags] Unable to suppress non-existent item drop ({0} {1} {2})",
item.NetId, item.Stack, item.PrefixId);
}
} while (i != last);
}
if (!anyItemDropped) return;
Gravebag bag = dbManager.PersistGravebag(Main.worldID, args.Player.Account.ID, args.Player.TPlayer.position, inventory, (NetItem)player.trashItem);
SpawnGravebag(bag);
}
void OnPlayerUpdate(object _, GetDataHandlers.PlayerUpdateEventArgs args) {
CheckGravebags(args.Player);
}
#endregion
int SpawnGravebag(Gravebag bag) {
int itemID = Item.NewItem(new EntitySource_DebugCommand(), bag.position, Vector2.Zero, ItemID.CultistBossBag);
gravebags[itemID] = bag;
// Prevent players from picking up unobtainable boss bag
Main.item[itemID].playerIndexTheItemIsReservedFor = TSPlayer.Server.Index;
Main.item[itemID].keepTime = int.MaxValue;
TSPlayer.All.SendData(PacketTypes.ItemOwner, null, itemID);
TShock.Log.ConsoleDebug("[Gravebags] Spawn {0} item {1} pos ({2} {3})", bag.ID, itemID, bag.position.X / 16.0, bag.position.Y / 16.0);
return itemID;
}
void CheckGravebags(TSPlayer player) {
int accountID = player.Account.ID;
if (player.Dead) return;
foreach (int bagIndex in gravebags.Keys.ToList()) {
Gravebag bag = gravebags[bagIndex];
float distance = bag.position.Distance(player.TPlayer.position);
// ignore players further than 50 blocks away
if (distance > 800.0) continue;
Item floorItem = Main.item[bagIndex];
// respawn bag if not present
if (!floorItem.active || floorItem.netID != ItemID.CultistBossBag) {
gravebags.Remove(bagIndex);
SpawnGravebag(bag);
continue;
}
// persist updated position
if (floorItem.position.Distance(bag.position) > 16.0 && floorItem.velocity.Equals(Vector2.Zero)) {
bag.position = floorItem.position;
dbManager.UpdateGravebagPosition(bag.ID, bag.position);
TShock.Log.ConsoleDebug("[Gravebags] Update Position {0} item {1} pos ({2} {3})",
bag.ID, bagIndex, bag.position.X / 16.0, bag.position.Y / 16.0);
}
// sync bag position
player.SendData(PacketTypes.ItemDrop, null, bagIndex);
// check within 3 blocks
if (distance <= 48.0) {
bool bagRemoved = false;
if (accountID == bag.accountID) {
bagRemoved = PickupGravebag(player, bagIndex);
} else if (!gravebagsNearPlayer.TryGetValue(accountID, out int index) || index != bagIndex) {
UserAccount bagOwner = TShock.UserAccounts.GetUserAccountByID(bag.accountID);
player.SendInfoMessage("[Gravebags] {0}'s gravebag. \"/b\" to pick up.", bagOwner.Name);
}
if (!bagRemoved) gravebagsNearPlayer[accountID] = bagIndex;
} else if (gravebagsNearPlayer.TryGetValue(accountID, out int index) && index == bagIndex) {
gravebagsNearPlayer.Remove(accountID);
}
}
}
bool PickupGravebag(TSPlayer player, int bagIndex) {
Gravebag bag = gravebags[bagIndex];
List<int> pickUpItems = new List<int>();
int pickedUp = 0;
if (Main.ServerSideCharacter) {
pickedUp += FillSSCInventory(player, bag, pickUpItems);
} else {
AppendNonSSCPickupItems(bag, pickUpItems);
}
// Give remaining items
int overflowCount = 0;
if (pickUpItems.Count > 0) {
foreach (int i in pickUpItems) {
Item bagItem = NetItemToItem(bag.inventory[i]);
// Fill item server-side first, since item is not immediately updated server-side. This simulates
// the item entering the players inventory, so that we can calculate whether the player can take more.
if (player.TPlayer.ItemSpace(bagItem).CanTakeItem) {
Item leftoverItem = player.TPlayer.GetItem(-1, bagItem.Clone(), GetItemSettings.PickupItemFromWorld);
int itemIndex = Item.NewItem(new EntitySource_DebugCommand(), player.TPlayer.position, Vector2.Zero,
bagItem.netID, bagItem.stack - leftoverItem.stack, true, bagItem.prefix, true);
Main.item[itemIndex].playerIndexTheItemIsReservedFor = player.Index;
player.SendData(PacketTypes.ItemDrop, null, itemIndex, 1);
player.SendData(PacketTypes.ItemOwner, null, itemIndex);
bag.inventory[i] = (NetItem)leftoverItem;
pickedUp++;
}
if (bag.inventory[i].Stack > 0) overflowCount++;
}
}
// Handle trash item
if (Main.ServerSideCharacter && !IsEmptyOrIgnoredItem(bag.trashItem)) {
player.TPlayer.trashItem = NetItemToItem(bag.trashItem);
bag.trashItem = new NetItem();
player.SendData(PacketTypes.PlayerSlot, null, player.Index, NetItem.TrashIndex.Item1, bag.trashItem.PrefixId);
pickedUp++;
}
if (pickedUp > 0) {
player.SendInfoMessage("[Gravebags] You picked up {0} item{1}. {2} item{3} left.",
pickedUp, pickedUp == 1 ? "" : "s", overflowCount, overflowCount == 1 ? "" : "s");
TShock.Log.ConsoleDebug("[Gravebags] Pick up {0} item {1} pickUp {2} count {3} overflow {4}", bag.ID, bagIndex, pickUpItems.Count, pickedUp, overflowCount);
if (overflowCount > 0) dbManager.UpdateGravebagInventory(bag.ID, bag.inventory, bag.trashItem);
}
if (overflowCount > 0) return false;
TShock.Log.ConsoleDebug("[Gravebags] Delete {0} item {1}", bag.ID, bagIndex);
gravebags.Remove(bagIndex);
dbManager.RemoveGravebag(bag.ID);
foreach (var kv in gravebagsNearPlayer.Where(kv => kv.Value == bagIndex).ToList()) {
gravebagsNearPlayer.Remove(kv.Key);
}
Main.item[bagIndex].active = false;
Main.item[bagIndex].keepTime = 0;
TSPlayer.All.SendData(PacketTypes.ItemDrop, null, bagIndex);
return true;
}
#region Helpers
int FillSSCInventory(TSPlayer player, Gravebag bag, List<int> pickUpItems) {
int pickedUp = 0;
for (int i = 0; i < TotalSlots; i++) {
Item bagItem = NetItemToItem(bag.inventory[i]);
if (IsEmptyOrIgnoredItem(bagItem)) continue;
GetSubInventoryIndex(player.TPlayer, i, out Item[] subInv, out int index);
// Checks made to auto-fill inventory slot:
// - SSC must be enabled
// - Current inventory slot is empty or ignored item
// - Current inventory slot is not cursor slot
// - Bag accessory does not clash with other accessories on player
if (IsEmptyOrIgnoredItem(subInv[index]) && index != CursorSlotIndex
&& !(ReferenceEquals(subInv, player.TPlayer.armor) && Terraria.UI.ItemSlot.AccCheck(subInv, bagItem, index))) {
subInv[index] = bagItem;
player.SendData(PacketTypes.PlayerSlot, null, player.Index, i, bagItem.prefix);
bag.inventory[i] = new NetItem();
pickedUp++;
} else {
pickUpItems.Add(i);
}
}
return pickedUp;
}
void AppendNonSSCPickupItems(Gravebag bag, List<int> pickUpItems) {
// Hotbar
for (int i = NetItem.InventoryIndex.Item1; i < NetItem.InventoryIndex.Item1 + 10; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// Armor (non vanity)
for (int i = NetItem.ArmorIndex.Item1; i < NetItem.ArmorIndex.Item1 + 10; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// MiscEquips
for (int i = NetItem.MiscEquipIndex.Item1; i < NetItem.MiscEquipIndex.Item2; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// Inventory
for (int i = NetItem.InventoryIndex.Item1 + 10; i < NetItem.InventoryIndex.Item2; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// Armor (vanity)
for (int i = NetItem.ArmorIndex.Item1 + 10; i < NetItem.ArmorIndex.Item2; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// Dyes
for (int i = NetItem.DyeIndex.Item1; i < NetItem.DyeIndex.Item2; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
// MiscDyes
for (int i = NetItem.MiscDyeIndex.Item1; i < NetItem.MiscDyeIndex.Item2; i++) {
if (!IsEmptyOrIgnoredItem(bag.inventory[i])) pickUpItems.Add(i);
}
}
void GetSubInventoryIndex(Player player, int i, out Item[] subInv, out int index) {
if (i < NetItem.InventoryIndex.Item2) {
index = i - NetItem.InventoryIndex.Item1;
subInv = player.inventory;
} else if (i < NetItem.ArmorIndex.Item2) {
index = i - NetItem.ArmorIndex.Item1;
subInv = player.armor;
} else if (i < NetItem.DyeIndex.Item2) {
index = i - NetItem.DyeIndex.Item1;
subInv = player.dye;
} else if (i < NetItem.MiscEquipIndex.Item2) {
index = i - NetItem.MiscEquipIndex.Item1;
subInv = player.miscEquips;
} else if (i < NetItem.MiscDyeIndex.Item2) {
index = i - NetItem.MiscDyeIndex.Item1;
subInv = player.miscDyes;
} else {
throw new ArgumentOutOfRangeException("i", "Index out of range");
}
}
bool IsEmptyOrIgnoredItem(Item item) { return IsEmptyOrIgnoredItem((NetItem)item); }
bool IsEmptyOrIgnoredItem(NetItem item) {
return item.NetId == ItemID.None ||
item.NetId == ItemID.CopperShortsword ||
item.NetId == ItemID.CopperPickaxe ||
item.NetId == ItemID.CopperAxe;
}
/// <summary>
/// Converts a NetItem to an Item.
/// </summary>
/// <param name="netItem"></param>
/// <returns>A new instance of an Item.</returns>
Item NetItemToItem(NetItem netItem) {
Item item = new Item();
item.netDefaults(netItem.NetId);
item.stack = netItem.Stack;
item.prefix = netItem.PrefixId;
return item;
}
#endregion
}
}