This repository was archived by the owner on Dec 8, 2025. It is now read-only.
forked from AqlaSolutions/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStoragePlayer.cs
More file actions
288 lines (245 loc) · 7.85 KB
/
StoragePlayer.cs
File metadata and controls
288 lines (245 loc) · 7.85 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
using System.Collections.Generic;
using System.Linq;
using MagicStorageExtra.Components;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;
namespace MagicStorageExtra
{
public class StoragePlayer : ModPlayer
{
private readonly ItemTypeOrderedSet _craftedRecipes = new ItemTypeOrderedSet("CraftedRecipes");
private readonly ItemTypeOrderedSet _hiddenRecipes = new ItemTypeOrderedSet("HiddenItems");
private TEStorageHeart _latestAccessedStorage;
public bool remoteAccess;
private Point16 storageAccess = new Point16(-1, -1);
public int timeSinceOpen = 1;
public override bool CloneNewInstances => false;
public IEnumerable<Item> HiddenRecipes => _hiddenRecipes.Items;
public IEnumerable<Item> CraftedRecipes => _craftedRecipes.Items;
public ItemTypeOrderedSet FavoritedRecipes { get; } = new ItemTypeOrderedSet("FavoritedRecipes");
public ItemTypeOrderedSet SeenRecipes { get; } = new ItemTypeOrderedSet("SeenRecipes");
public ItemTypeOrderedSet TestedRecipes { get; } = new ItemTypeOrderedSet("TestedRecipes");
public ItemTypeOrderedSet AsKnownRecipes { get; } = new ItemTypeOrderedSet("AsKnownRecipes");
public TEStorageHeart LatestAccessedStorage =>
_latestAccessedStorage != null && _latestAccessedStorage.IsAlive ? _latestAccessedStorage : null;
public bool IsRecipeHidden(Item item) => _hiddenRecipes.Contains(item);
public bool AddToHiddenRecipes(Item item) => _hiddenRecipes.Add(item);
public bool RemoveFromHiddenRecipes(Item item) => _hiddenRecipes.Remove(item);
public bool AddToCraftedRecipes(Item item) => _craftedRecipes.Add(item);
public override TagCompound Save()
{
var c = new TagCompound();
_hiddenRecipes.Save(c);
_craftedRecipes.Save(c);
FavoritedRecipes.Save(c);
SeenRecipes.Save(c);
TestedRecipes.Save(c);
AsKnownRecipes.Save(c);
return c;
}
public override void Load(TagCompound tag)
{
_hiddenRecipes.Load(tag);
_craftedRecipes.Load(tag);
FavoritedRecipes.Load(tag);
SeenRecipes.Load(tag);
TestedRecipes.Load(tag);
AsKnownRecipes.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.talkNPC = -1;
Main.playerInventory = true;
timeSinceOpen++;
}
if (storageAccess.X >= 0 && storageAccess.Y >= 0 && (player.chest != -1 || !Main.playerInventory || player.sign > -1 || player.talkNPC > -1))
{
CloseStorage();
lock (BlockRecipes.activeLock)
{
Recipe.FindRecipes();
}
}
else if (storageAccess.X >= 0 && storageAccess.Y >= 0)
{
Main.HidePlayerCraftingMenu = true;
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))
{
Main.PlaySound(SoundID.MenuClose);
CloseStorage();
lock (BlockRecipes.activeLock)
{
Recipe.FindRecipes();
}
}
else if (!(TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].type) is StorageAccess))
{
Main.PlaySound(SoundID.MenuClose);
CloseStorage();
lock (BlockRecipes.activeLock)
{
Recipe.FindRecipes();
}
}
}
}
public void OpenStorage(Point16 point, bool remote = false)
{
storageAccess = point;
remoteAccess = remote;
_latestAccessedStorage = GetStorageHeart();
if (MagicStorageConfig.UseConfigFilter && CraftingGUI.recipeButtons != null)
{
CraftingGUI.recipeButtons.Choice = MagicStorageConfig.ShowAllRecipes ? 1 : 0;
}
if (MagicStorageConfig.ClearSearchText)
{
StorageGUI.searchBar?.Reset();
CraftingGUI.searchBar?.Reset();
}
StorageGUI.RefreshItems();
}
public void CloseStorage()
{
storageAccess = new Point16(-1, -1);
Main.blockInput = false;
}
public Point16 ViewingStorage() => storageAccess;
public static void GetItem(Item item, bool toMouse)
{
Player player = Main.LocalPlayer;
if (toMouse && Main.playerInventory && Main.mouseItem.IsAir)
{
Main.mouseItem = item;
item = new Item();
}
else 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)
{
item = player.GetItem(Main.myPlayer, item, false, true);
if (!item.IsAir)
player.QuickSpawnClonedItem(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;
if (StorageCrafting())
{
if (false)
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
GetCraftingAccess().TryDepositStation(item);
}
else
{
NetHelper.SendDepositStation(GetCraftingAccess().ID, item);
item.SetDefaults(0, true);
}
}
}
else
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
GetStorageHeart().DepositItem(item);
}
else
{
NetHelper.SendDeposit(GetStorageHeart().ID, item);
item.SetDefaults(0, true);
}
}
if (item.type != oldType || item.stack != oldStack)
{
Main.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 is null)
return null;
int tileType = tile.type;
ModTile modTile = TileLoader.GetTile(tileType);
return (modTile as StorageAccess)?.GetHeart(storageAccess.X, storageAccess.Y);
}
public TECraftingAccess GetCraftingAccess()
{
if (storageAccess.X < 0 || storageAccess.Y < 0 || !TileEntity.ByPosition.ContainsKey(storageAccess))
return null;
return TileEntity.ByPosition[storageAccess] as TECraftingAccess;
}
public bool StorageCrafting()
{
if (storageAccess.X < 0 || storageAccess.Y < 0)
return false;
Tile tile = Main.tile[storageAccess.X, storageAccess.Y];
return tile != null && tile.type == ModContent.TileType<CraftingAccess>();
}
public static bool IsStorageCrafting() => Main.LocalPlayer.GetModPlayer<StoragePlayer>().StorageCrafting();
public override void ModifyHitByNPC(NPC npc, ref int damage, ref bool crit)
{
foreach (Item item in player.inventory.Concat(player.armor).Concat(player.dye).Concat(player.miscDyes).Concat(player.miscEquips))
if (item != null && !item.IsAir && CraftingGUI.IsTestItem(item))
{
damage *= 5;
break;
}
}
public override bool CanHitPvp(Item item, Player target)
{
if (CraftingGUI.IsTestItem(item))
return false;
return base.CanHitPvp(item, target);
}
public override void OnRespawn(Player player)
{
foreach (Item item in player.inventory.Concat(player.armor).Concat(player.dye).Concat(player.miscDyes).Concat(player.miscEquips))
if (item != null && !item.IsAir && CraftingGUI.IsTestItem(item))
item.TurnToAir();
{
Item item = player.trashItem;
if (item != null && !item.IsAir && CraftingGUI.IsTestItem(item))
item.TurnToAir();
}
base.OnRespawn(player);
}
}
}