forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoragePlayer.cs
339 lines (269 loc) · 9.81 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
using MagicStorage.Common.Systems;
using MagicStorage.Components;
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;
protected override bool CloneNewInstances => false;
public ItemTypeOrderedSet HiddenRecipes { get; } = new("HiddenItems");
public ItemTypeOrderedSet FavoritedRecipes { get; } = new("FavoritedRecipes");
public override void SaveData(TagCompound tag)
{
HiddenRecipes.Save(tag);
FavoritedRecipes.Save(tag);
}
public override void LoadData(TagCompound tag)
{
HiddenRecipes.Load(tag);
FavoritedRecipes.Load(tag);
}
public override void OnEnterWorld(Player player) {
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);
}
}
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 && MagicUI.craftingUI.GetPage<CraftingUIState.RecipesPage>("Crafting") is CraftingUIState.RecipesPage page)
{
page.recipeButtons.Choice = MagicStorageConfig.ShowAllRecipes
? 1 //Show all recipes
: 0; //Show available recipes
page.recipeButtons.OnChanged();
}
if (MagicStorageConfig.ClearSearchText)
{
MagicUI.storageUI?.GetPage<StorageUIState.StoragePage>("Storage")?.searchBar?.Reset();
MagicUI.craftingUI?.GetPage<CraftingUIState.RecipesPage>("Crafting")?.searchBar?.Reset();
}
StorageGUI.RefreshItems();
}
//Intended to only be used with StorageHeartAccessWrapper
internal void OpenStorageUnsafely(Point16 point) {
storageAccess = point;
remoteAccess = true;
StorageGUI.RefreshItems();
}
public void CloseStorage()
{
CloseStorageUnsafely();
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;
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 (ItemCombining.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.QuickSpawnClonedItem(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);
StorageGUI.needRefresh = true;
}
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 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 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();
}
}
}
}