forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoragePlayer.cs
217 lines (205 loc) · 6.94 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
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.DataStructures;
using Terraria.ModLoader;
using Terraria.UI;
using MagicStorage.Components;
namespace MagicStorage
{
public class StoragePlayer : ModPlayer
{
public int timeSinceOpen = 1;
private Point16 storageAccess = new Point16(-1, -1);
public bool remoteAccess = false;
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();
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.tileRangeX || playerX > storageAccess.X + Player.tileRangeX + 1 || playerY < storageAccess.Y - Player.tileRangeY || playerY > storageAccess.Y + Player.tileRangeY + 1))
{
Main.PlaySound(11, -1, -1, 1);
CloseStorage();
Recipe.FindRecipes();
}
else if (!(TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].type) is StorageAccess))
{
Main.PlaySound(11, -1, -1, 1);
CloseStorage();
Recipe.FindRecipes();
}
}
}
public void OpenStorage(Point16 point, bool remote = false)
{
storageAccess = point;
remoteAccess = remote;
StorageGUI.RefreshItems();
}
public void CloseStorage()
{
storageAccess = new Point16(-1, -1);
Main.blockInput = false;
if (StorageGUI.searchBar != null)
{
StorageGUI.searchBar.Reset();
}
if (StorageGUI.searchBar2 != null)
{
StorageGUI.searchBar2.Reset();
}
if (CraftingGUI.searchBar != null)
{
CraftingGUI.searchBar.Reset();
}
if (CraftingGUI.searchBar2 != null)
{
CraftingGUI.searchBar2.Reset();
}
}
public Point16 ViewingStorage()
{
return storageAccess;
}
public static void GetItem(Item item, bool toMouse)
{
Player player = Main.player[Main.myPlayer];
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 (Main.netMode == 0)
{
GetCraftingAccess().TryDepositStation(item);
}
else
{
NetHelper.SendDepositStation(GetCraftingAccess().ID, item);
item.SetDefaults(0, true);
}
}
else
{
if (Main.netMode == 0)
{
GetStorageHeart().DepositItem(item);
}
else
{
NetHelper.SendDeposit(GetStorageHeart().ID, item);
item.SetDefaults(0, true);
}
}
if (item.type != oldType || item.stack != oldStack)
{
Main.PlaySound(7, -1, -1, 1, 1f, 0f);
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 == null)
{
return null;
}
int tileType = tile.type;
ModTile modTile = TileLoader.GetTile(tileType);
if (modTile == null || !(modTile is StorageAccess))
{
return null;
}
return ((StorageAccess)modTile).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 == mod.TileType("CraftingAccess");
}
public static bool IsStorageCrafting()
{
return Main.player[Main.myPlayer].GetModPlayer<StoragePlayer>().StorageCrafting();
}
}
}