-
Notifications
You must be signed in to change notification settings - Fork 101
/
StoragePlayer.cs
435 lines (338 loc) · 12.7 KB
/
StoragePlayer.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
using MagicStorage.Common.Systems;
using MagicStorage.Components;
using MagicStorage.CrossMod;
using MagicStorage.UI.States;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;
namespace MagicStorage
{
public class StoragePlayer : ModPlayer
{
public static StoragePlayer LocalPlayer => Main.LocalPlayer.GetModPlayer<StoragePlayer>();
public bool remoteAccess, remoteCrafting;
private Point16 storageAccess = Point16.NegativeOne;
public float portableAccessRangePlayerToPylons;
public int portableAccessRangePylonsToStorage;
public int timeSinceOpen = 1;
internal bool pendingRemoteOpen;
internal int wirelessLatency = -1;
internal const int MaxLatency = 10;
private int? patreon;
// Automaton help tips
internal bool unlockedTip_Mechs, unlockedTip_MoonLord;
internal int automatonHelpTip;
internal const int SAVE_VERSION = 1;
protected override bool CloneNewInstances => false;
public ItemTypeOrderedSet HiddenRecipes { get; } = new("HiddenItems");
public ItemTypeOrderedSet FavoritedRecipes { get; } = new("FavoritedRecipes");
public ItemTypeOrderedSet HiddenShimmerItems { get; } = new("HiddenShimmerItems");
public ItemTypeOrderedSet FavoritedShimmerItems { get; } = new("FavoritedShimmerItems");
public override void SaveData(TagCompound tag)
{
HiddenRecipes.Save(tag);
FavoritedRecipes.Save(tag);
HiddenShimmerItems.Save(tag);
FavoritedShimmerItems.Save(tag);
tag["automaton"] = automatonHelpTip;
BitsByte unlocked = new(unlockedTip_Mechs, unlockedTip_MoonLord);
tag["unlocked"] = (byte)unlocked;
tag["version"] = SAVE_VERSION;
tag["shilling"] = patreon;
}
public override void LoadData(TagCompound tag)
{
HiddenRecipes.Load(tag);
FavoritedRecipes.Load(tag);
HiddenShimmerItems.Load(tag);
FavoritedShimmerItems.Load(tag);
automatonHelpTip = tag.GetInt("version") == SAVE_VERSION ? tag.GetInt("automaton") : 0;
BitsByte unlocked = tag.GetByte("unlocked");
unlocked.Retrieve(ref unlockedTip_Mechs, ref unlockedTip_MoonLord);
if (tag.TryGet("shilling", out int waitCount))
patreon = waitCount >= 0 ? waitCount - 1 : waitCount;
else
patreon = Main.rand.Next(3, 8);
}
public override void OnEnterWorld() {
if (MagicStorageMod.UsingPrivateBeta) {
Main.NewTextMultiline("Thank you for helping test a private beta for Magic Storage!\n" +
"Do note that using this private beta build will cause a ton of text to be printed to the chat (when the config is enabled) and to your log files.",
c: Color.LightBlue);
}
if (patreon == 0) {
Main.NewTextMultiline("Thank you for playing Magic Storage!\n" +
"Please consider supporting the development of Magic Storage by becoming a patron for the absoluteAquarian Patreon.\n" +
"Patrons will gain early access to future updates of Magic Storage, plus other benefits.\n" +
"[c/dddd00:NOTE:] This message will only be displayed once per player.",
c: Color.LightBlue);
}
}
public override void PlayerDisconnect() {
GetStorageHeart()?.UnlockOnCurrentClient();
}
public override void UpdateDead()
{
if (Player.whoAmI == Main.myPlayer)
CloseStorage();
}
public override void ResetEffects()
{
if (wirelessLatency >= 0)
wirelessLatency--;
if (Player.whoAmI != Main.myPlayer)
return;
if (timeSinceOpen < 1 && !Main.autoPause && storageAccess.X >= 0 && storageAccess.Y >= 0)
{
Player.SetTalkNPC(-1);
Main.playerInventory = true;
timeSinceOpen++;
}
if (storageAccess.X >= 0 && storageAccess.Y >= 0 && (Player.chest != -1 || !Main.playerInventory || Player.sign > -1 || Player.talkNPC > -1))
{
CloseStorage();
Recipe.FindRecipes();
}
else if (storageAccess.X >= 0 && storageAccess.Y >= 0)
{
int playerX = (int)(Player.Center.X / 16f);
int playerY = (int)(Player.Center.Y / 16f);
var modTile = TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].TileType);
if (!remoteAccess &&
(playerX < storageAccess.X - Player.lastTileRangeX ||
playerX > storageAccess.X + Player.lastTileRangeX + 1 ||
playerY < storageAccess.Y - Player.lastTileRangeY ||
playerY > storageAccess.Y + Player.lastTileRangeY + 1))
{
SoundEngine.PlaySound(SoundID.MenuClose);
CloseStorage();
Recipe.FindRecipes();
}
else if (modTile is not StorageAccess || (remoteAccess && remoteCrafting && modTile is not CraftingAccess) || (remoteAccess && !Items.PortableAccess.PlayerCanBeRemotelyConnectedToStorage(Player, storageAccess)))
{
SoundEngine.PlaySound(SoundID.MenuClose);
CloseStorage();
Recipe.FindRecipes();
}
}
}
public void OpenStorage(Point16 point, bool remote = false)
{
storageAccess = point;
remoteAccess = remote;
Main.playerInventory = true;
MagicUI.OpenUI();
if (MagicStorageConfig.UseConfigFilter) {
if (MagicUI.craftingUI.GetDefaultPage() is CraftingUIState.RecipesPage page) {
page.recipeButtons.Choice = MagicStorageConfig.ShowAllRecipes
? 1 //Show all recipes
: 0; //Show available recipes
page.recipeButtons.OnChanged();
}
if (MagicUI.decraftingUI.GetDefaultPage() is DecraftingUIState.ShimmeringPage decraftingPage) {
decraftingPage.recipeButtons.Choice = MagicStorageConfig.ShowAllRecipes
? 1 //Show all recipes
: 0; //Show available recipes
decraftingPage.recipeButtons.OnChanged();
}
}
if (MagicStorageConfig.ClearSearchText)
{
MagicUI.storageUI.ResetSearchBars();
MagicUI.craftingUI.ResetSearchBars();
MagicUI.environmentUI.ResetSearchBars();
MagicUI.decraftingUI.ResetSearchBars();
}
MagicUI.SetRefresh(forceFullRefresh: true);
GetStorageHeart()?.LockOnCurrentClient();
}
//Intended to only be used with StorageHeartAccessWrapper
internal void OpenStorageUnsafely(Point16 point) {
storageAccess = point;
remoteAccess = true;
MagicUI.RefreshItems();
}
public void CloseStorage()
{
GetStorageHeart()?.UnlockOnCurrentClient();
CloseStorageUnsafely();
Main.LocalPlayer.tileEntityAnchor.Clear();
remoteAccess = false;
remoteCrafting = false;
portableAccessRangePlayerToPylons = 0;
portableAccessRangePylonsToStorage = 0;
}
//Intended to only be used with PortableAccess
internal void CloseStorageUnsafely() {
if (StorageEnvironment()) {
NetHelper.ClientRequestForceCraftingGUIRefresh();
EnvironmentGUI.currentAccess = null;
}
storageAccess = Point16.NegativeOne;
Main.blockInput = false;
wirelessLatency = -1;
MagicUI.CloseUI();
}
public Point16 ViewingStorage() => storageAccess;
public static void GetItem(IEntitySource source, Item item, bool toMouse)
{
Player player = Main.LocalPlayer;
if (toMouse && Main.playerInventory && Main.mouseItem.IsAir)
{
Main.mouseItem = item;
return;
}
if (toMouse && Main.playerInventory && Main.mouseItem.type == item.type)
{
int total = Main.mouseItem.stack + item.stack;
if (total > Main.mouseItem.maxStack)
total = Main.mouseItem.maxStack;
int difference = total - Main.mouseItem.stack;
if (difference > 0)
Utility.CallOnStackHooks(Main.mouseItem, item, difference);
Main.mouseItem.stack = total;
item.stack -= difference;
}
if (item.IsAir)
return;
item = player.GetItem(Main.myPlayer, item, GetItemSettings.InventoryEntityToPlayerInventorySettings);
if (item.IsAir)
return;
if (Main.mouseItem.IsAir)
{
Main.mouseItem = item;
return;
}
if (StorageAggregator.CanCombineItems(Main.mouseItem, item) && Main.mouseItem.stack + item.stack < Main.mouseItem.maxStack)
{
Utility.CallOnStackHooks(Main.mouseItem, item, item.stack);
Main.mouseItem.stack += item.stack;
return;
}
player.QuickSpawnItem(source, item, item.stack);
}
public override bool ShiftClickSlot(Item[] inventory, int context, int slot)
{
if (context != ItemSlot.Context.InventoryItem && context != ItemSlot.Context.InventoryCoin && context != ItemSlot.Context.InventoryAmmo)
return false;
if (storageAccess.X < 0 || storageAccess.Y < 0)
return false;
Item item = inventory[slot];
if (item.favorited || item.IsAir)
return false;
int oldType = item.type;
int oldStack = item.stack;
GetStorageHeart().TryDeposit(item);
if (item.type != oldType || item.stack != oldStack)
{
SoundEngine.PlaySound(SoundID.Grab);
int[] types = new int[] {
item.type,
oldType
};
if (MagicUI.uiInterface.CurrentState is not null)
MagicUI.SetNextCollectionsToRefresh(types);
}
return true;
}
public TEStorageHeart GetStorageHeart()
{
if (storageAccess.X < 0 || storageAccess.Y < 0)
return null;
Tile tile = Main.tile[storageAccess.X, storageAccess.Y];
if (!tile.HasTile)
return null;
ModTile modTile = TileLoader.GetTile(tile.TileType);
return (modTile as StorageAccess)?.GetHeart(storageAccess.X, storageAccess.Y);
}
public TECraftingAccess GetCraftingAccess()
{
if (storageAccess.X < 0 || storageAccess.Y < 0)
return null;
if (TileEntity.ByPosition.TryGetValue(storageAccess, out TileEntity te))
return te as TECraftingAccess;
return null;
}
public TEDecraftingAccess GetDecraftingAccess()
{
if (storageAccess.X < 0 || storageAccess.Y < 0)
return null;
if (TileEntity.ByPosition.TryGetValue(storageAccess, out TileEntity te))
return te as TEDecraftingAccess;
return null;
}
public static bool IsStorageCraftingOrDecrafting() => IsStorageCrafting() || IsStorageDecrafting();
public bool StorageCrafting()
{
if (storageAccess.X < 0 || storageAccess.Y < 0)
return false;
Tile tile = Main.tile[storageAccess.X, storageAccess.Y];
return tile.HasTile && tile.TileType == ModContent.TileType<CraftingAccess>();
}
public static bool IsStorageCrafting() => StoragePlayer.LocalPlayer.StorageCrafting();
public bool StorageEnvironment() {
if (storageAccess.X < 0 || storageAccess.Y < 0)
return false;
Tile tile = Main.tile[storageAccess.X, storageAccess.Y];
return tile.HasTile && tile.TileType == ModContent.TileType<EnvironmentAccess>();
}
public static bool IsStorageEnvironment() => StoragePlayer.LocalPlayer.StorageEnvironment();
public bool StorageDecrafting() {
if (storageAccess.X < 0 || storageAccess.Y < 0)
return false;
Tile tile = Main.tile[storageAccess.X, storageAccess.Y];
return tile.HasTile && tile.TileType == ModContent.TileType<DecraftingAccess>();
}
public static bool IsStorageDecrafting() => StoragePlayer.LocalPlayer.StorageDecrafting();
public class StorageHeartAccessWrapper {
public Point16 Storage { get; private set; }
public Point16 HeartLocation { get; private set; }
public bool Valid => Storage.X >= 0 && Storage.Y >= 0 && HeartLocation.X >= 0 && HeartLocation.Y >= 0;
public TEStorageHeart Heart => Valid && TileEntity.ByPosition.TryGetValue(HeartLocation, out TileEntity te) && te is TEStorageHeart heart ? heart : null;
private Point16 oldPosition = Point16.NegativeOne;
private bool oldRemote = false, oldCrafting = false;
public StorageHeartAccessWrapper(TEStorageHeart heart) {
Storage = HeartLocation = heart?.Position ?? Point16.NegativeOne;
}
public StorageHeartAccessWrapper(TEStorageCenter center) {
Storage = center?.Position ?? Point16.NegativeOne;
HeartLocation = center?.GetHeart()?.Position ?? Point16.NegativeOne;
}
public StorageHeartAccessWrapper(TECraftingAccess crafting) {
Storage = crafting?.Position ?? Point16.NegativeOne;
TEStorageHeart newHeart = crafting is not null ? ModContent.GetInstance<CraftingAccess>().GetHeart(crafting.Position.X, crafting.Position.Y) : null;
HeartLocation = newHeart?.Position ?? Point16.NegativeOne;
}
public IDisposable OpenStorage() {
oldPosition = LocalPlayer.ViewingStorage();
oldRemote = LocalPlayer.remoteAccess;
oldCrafting = LocalPlayer.remoteCrafting;
LocalPlayer.OpenStorageUnsafely(Storage);
return new Disposable(this);
}
public void CloseStorage() {
if (Storage != Point16.NegativeOne && oldPosition != Point16.NegativeOne) {
LocalPlayer.OpenStorageUnsafely(oldPosition);
LocalPlayer.remoteAccess = oldRemote;
LocalPlayer.remoteCrafting = oldCrafting;
Storage = Point16.NegativeOne;
HeartLocation = Point16.NegativeOne;
oldPosition = Point16.NegativeOne;
oldRemote = false;
oldCrafting = false;
}
}
private class Disposable : IDisposable {
public readonly StorageHeartAccessWrapper wrapper;
public Disposable(StorageHeartAccessWrapper wrapper) => this.wrapper = wrapper;
public void Dispose() => wrapper.CloseStorage();
}
}
}
}