forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoragePlayer.cs
More file actions
292 lines (235 loc) · 8.14 KB
/
StoragePlayer.cs
File metadata and controls
292 lines (235 loc) · 8.14 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
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
using MagicStorage.Components;
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 int timeSinceOpen = 1;
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 UpdateDead()
{
if (Player.whoAmI == Main.myPlayer)
CloseStorage();
}
public override void ResetEffects()
{
if (Player.whoAmI != Main.myPlayer)
return;
if (timeSinceOpen < 1)
{
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);
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 (TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].TileType) is not StorageAccess)
{
SoundEngine.PlaySound(SoundID.MenuClose);
CloseStorage();
Recipe.FindRecipes();
}
}
}
public void OpenStorage(Point16 point, bool remote = false)
{
storageAccess = point;
remoteAccess = remote;
if (MagicStorageConfig.UseConfigFilter && CraftingGUI.recipeButtons is not null)
{
CraftingGUI.recipeButtons.Choice = MagicStorageConfig.ShowAllRecipes ? 1 : 0;
}
if (MagicStorageConfig.ClearSearchText)
{
StorageGUI.searchBar?.Reset();
CraftingGUI.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()
{
if (StorageEnvironment()) {
NetHelper.ClientRequestForceCraftingGUIRefresh();
EnvironmentGUI.currentAccess = null;
}
storageAccess = Point16.NegativeOne;
Main.blockInput = false;
}
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)
{
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.RefreshItems();
}
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();
}
}
}
}