forked from JavidPack/RecipeBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecipeBrowserPlayer.cs
283 lines (262 loc) · 9.83 KB
/
RecipeBrowserPlayer.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
using Microsoft.Xna.Framework;
using RecipeBrowser.UIElements;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameInput;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;
namespace RecipeBrowser
{
// Helper class for saving and loading recipes.
// During load, recipes that no longer match ingredients or tiles will be forgotten.
internal static class RecipeIO
{
// Unused slim TagCompound for just the definition of an item.
//public static TagCompound SaveItemDefinition(Item item)
//{
// var tag = new TagCompound();
// if (item.type <= 0)
// return tag;
// if (item.modItem == null)
// {
// tag.Set("mod", "Terraria");
// tag.Set("id", item.netID);
// }
// else
// {
// tag.Set("mod", item.modItem.mod.Name);
// tag.Set("name", item.modItem.Name);
// tag.Set("data", item.modItem.Save());
// }
// return tag;
//}
// Necessary? Tile needed?
public static TagCompound Save(Recipe recipe)
{
return new TagCompound
{
["createItem"] = ItemIO.Save(recipe.createItem),
["requiredItem"] = recipe.requiredItem.Where(x => !x.IsAir).Select(ItemIO.Save).ToList(),
};
}
// Returns -1 for missing recipe, index of recipe otherwise
public static int Load(TagCompound tag)
{
Item createItem = ItemIO.Load(tag.Get<TagCompound>("createItem"));
List<Item> requiredItems = tag.GetList<TagCompound>("requiredItem").Select(ItemIO.Load).ToList();
for (int i = 0; i < Recipe.numRecipes; i++)
{
Recipe recipe = Main.recipe[i];
if (recipe.createItem.type == createItem.type)
{
HashSet<int> tagIngredients = new HashSet<int>(requiredItems.Where(x => !x.IsAir).Select(x => x.type));
HashSet<int> recipeIngredients = new HashSet<int>(recipe.requiredItem.Where(x => !x.IsAir).Select(x => x.type));
if (tagIngredients.SetEquals(recipeIngredients))
return i;
}
}
return -1;
}
}
internal class RecipeBrowserPlayer : ModPlayer
{
internal List<int> favoritedRecipes;
// For now, reset on enter world. Could remember later if needed.
static internal bool[] seenTiles;
// TODO: Remember hitNPCs? Implement just like seenTiles and reset each session?
public override void Initialize()
{
favoritedRecipes = new List<int>();
}
public override void SaveData(TagCompound tag)
{
tag["StarredRecipes"] = favoritedRecipes.Select(x => RecipeIO.Save(Main.recipe[x])).ToList();
}
public override void LoadData(TagCompound tag)
{
favoritedRecipes = tag.GetList<TagCompound>("StarredRecipes").Select(RecipeIO.Load).Where(x => x > -1).ToList();
}
// Only happens on local client/SP
public override void OnEnterWorld()
{
seenTiles = new bool[TileLoader.TileCount];
Point center = Player.Center.ToTileCoordinates();
for (int i = center.X - 100; i < center.X + 100; i++)
{
for (int j = center.Y - 100; j < center.Y + 100; j++)
{
if (WorldGen.InWorld(i, j) && Main.tile[i, j] != null && !seenTiles[Main.tile[i, j].TileType]) {
int Tile = Main.tile[i, j].TileType;
List<int> adjTiles = Utilities.PopulateAdjTilesForTile(Tile);
foreach (var tile in adjTiles) {
seenTiles[tile] = true;
}
}
}
}
// All crafting tile items in inventory also count as "seen"
foreach (var item in Player.inventory) {
if (item.active) {
RecipeBrowserUI.instance.ItemReceived(item);
}
}
RecipeBrowserUI.instance.favoritePanelUpdateNeeded = true;
RecipeBrowserUI.instance.ShowFavoritePanel = favoritedRecipes.Count > 0 && RecipeBrowserUI.instance.HideUnlessInventoryToggle.CurrentState == 0;
RecipeCatalogueUI.instance.updateNeeded = true;
if (RecipeCatalogueUI.instance.recipeSlots.Count > 0)
{
RecipeBrowserUI.instance.UpdateFavoritedPanel();
}
SharedUI.instance.updateNeeded = true; // Added for creative mode filter hiding.
}
// Called on other clients when a player leaves.
public override void PlayerDisconnect()
{
// When a player leaves, trigger an update to get rid of Favorited Recipe entries.
if(!Main.dedServ)
RecipeBrowserUI.instance.favoritePanelUpdateNeeded = true;
}
public override void SyncPlayer(int toWho, int fromWho, bool newPlayer)
{
SendFavoritedRecipes(toWho, fromWho, true);
}
public override void CopyClientState(ModPlayer clientClone)
{
RecipeBrowserPlayer clone = clientClone as RecipeBrowserPlayer;
clone.favoritedRecipes.Clear();
clone.favoritedRecipes.AddRange(favoritedRecipes);
}
public override void SendClientChanges(ModPlayer clientPlayer)
{
RecipeBrowserPlayer clone = clientPlayer as RecipeBrowserPlayer;
if (!favoritedRecipes.SequenceEqual(clone.favoritedRecipes))
{
SendFavoritedRecipes(-1, Player.whoAmI);
}
}
public void SendFavoritedRecipes(int toWho, int fromWho, bool syncPlayer = false)
{
ModPacket packet = Mod.GetPacket();
packet.Write((byte)MessageType.SendFavoritedRecipes);
packet.Write((byte)Player.whoAmI);
packet.Write((bool)syncPlayer); // prevents duplicate sends when normal syncPlayer is happening.
packet.Write(favoritedRecipes.Count);
foreach (var recipeIndex in favoritedRecipes)
{
packet.Write(recipeIndex);
}
packet.Send(toWho, fromWho);
}
// These triggers should work in autopause and aren't related to Player actions, so we can use UpdateUI to call them.
//public void ProcessTriggersButAlways(TriggersSet triggersSet)
// 0.6.1.6: hm, seemed to have backfired. Investigate why 0.6.1.5 approach would miss keypresses.
public override void ProcessTriggers(TriggersSet triggersSet)
{
//if (!RecipeBrowser.instance.CheatSheetLoaded)
{
if (RecipeBrowser.instance.ToggleRecipeBrowserHotKey.JustPressed)
{
RecipeBrowserUI.instance.ShowRecipeBrowser = !RecipeBrowserUI.instance.ShowRecipeBrowser;
// Debug assistance, allows for reinitializing RecipeBrowserUI
//if (!RecipeBrowserUI.instance.ShowRecipeBrowser)
//{
// RecipeBrowserUI.instance.RemoveAllChildren();
// var isInitializedFieldInfo = typeof(Terraria.UI.UIElement).GetField("_isInitialized", BindingFlags.Instance | BindingFlags.NonPublic);
// isInitializedFieldInfo.SetValue(RecipeBrowserUI.instance, false);
// RecipeBrowserUI.instance.Activate();
//}
}
if (RecipeBrowser.instance.QueryHoveredItemHotKey.JustPressed)
{
// Debug assistance, manually clear craftPath calculations
//foreach (var slot in RecipeCatalogueUI.instance.recipeSlots)
//{
// slot.craftPathsNeeded = false;
// slot.craftPathsCalculated = false;
// slot.craftPaths = null;
//}
//RecipeCatalogueUI.instance.updateNeeded = true;
//RecipeCatalogueUI.instance.InvalidateExtendedCraft();
if (!Main.HoverItem.IsAir)
{
// Query item on "Any Iron Bar", should I uncheck "ignore recipe groups"?, can check _nameoverride to see.
bool shouldShowRecipeBrowser = true;
if (RecipeBrowserUI.instance.CurrentPanel == RecipeBrowserUI.RecipeCatalogue)
{
if (Main.HoverItem.type == RecipeCatalogueUI.instance.queryItem.item?.type)
shouldShowRecipeBrowser = !RecipeBrowserUI.instance.ShowRecipeBrowser;
else
RecipeCatalogueUI.instance.queryItem.ReplaceWithFake(Main.HoverItem.type);
}
else if (RecipeBrowserUI.instance.CurrentPanel == RecipeBrowserUI.Craft)
{
if (Main.HoverItem.type == CraftUI.instance.recipeResultItemSlot.item?.type)
shouldShowRecipeBrowser = !RecipeBrowserUI.instance.ShowRecipeBrowser;
else
CraftUI.instance.SetItem(Main.HoverItem.type);
}
else if (RecipeBrowserUI.instance.CurrentPanel == RecipeBrowserUI.ItemCatalogue)
{
ItemCatalogueUI.instance.itemGrid.Goto(delegate (UIElement element) {
UIItemCatalogueItemSlot itemSlot = element as UIItemCatalogueItemSlot;
if (itemSlot != null && itemSlot.itemType == Main.HoverItem.type) {
ItemCatalogueUI.instance.SetItem(itemSlot);
return true;
}
return false;
}, true);
}
else if (RecipeBrowserUI.instance.CurrentPanel == RecipeBrowserUI.Bestiary)
{
if (Main.HoverItem.type == BestiaryUI.instance.queryItem.item?.type)
shouldShowRecipeBrowser = !RecipeBrowserUI.instance.ShowRecipeBrowser;
else
BestiaryUI.instance.queryItem.ReplaceWithFake(Main.HoverItem.type);
}
RecipeBrowserUI.instance.ShowRecipeBrowser = shouldShowRecipeBrowser;
}
}
if (RecipeBrowser.instance.ToggleFavoritedPanelHotKey.JustPressed) {
RecipeBrowserUI.instance.ShowFavoritePanel = !RecipeBrowserUI.instance.ShowFavoritePanel;
if (!RecipeBrowserUI.instance.ShowFavoritePanel)
RecipeBrowserUI.instance.ForceHideFavoritePanel = true;
else {
RecipeBrowserUI.instance.ForceShowFavoritePanel = true;
}
RecipeBrowserUI.instance.favoritePanelUpdateNeeded = true;
}
}
}
public override void PreUpdateBuffs()
{
if (Main.myPlayer == Player.whoAmI && seenTiles != null)
{
if (!Main.playerInventory && WorldGen.InWorld((int)Player.position.X / 16, (int)Player.position.Y / 16, 10))
Main.LocalPlayer.AdjTiles();
for (int i = 0; i < seenTiles.Length; i++)
{
if (Player.adjTile[i] && !seenTiles[i]) // could move to Player_AdjTiles_Patcher, nah.
{
//Main.NewText("Seen " + Utilities.GetTileName(i));
seenTiles[i] = true;
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
}
}
}
}
public override void ModifyDrawInfo(ref PlayerDrawSet drawInfo) {
if (drawInfo.drawPlayer == UIArmorSetCatalogueItemSlot.drawPlayer) {
drawInfo.colorArmorHead = Color.White;
drawInfo.colorArmorBody = Color.White;
drawInfo.colorArmorLegs = Color.White;
//drawInfo.upperArmorColor = Color.White;
//drawInfo.middleArmorColor = Color.White;
//drawInfo.lowerArmorColor = Color.White;
}
}
}
}