Skip to content

Commit fdbba04

Browse files
committed
Reworked CompoundRecipe
1 parent 33f79fe commit fdbba04

File tree

4 files changed

+43
-66
lines changed

4 files changed

+43
-66
lines changed

CompoundRecipe.cs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,57 @@
22
using System.Linq;
33
using Terraria;
44
using Terraria.ModLoader;
5+
using Terraria.ID;
56

67
namespace RecursiveCraft
78
{
8-
public class CompoundRecipe
9+
public class CompoundRecipe : ModRecipe
910
{
10-
public Recipe CurrentRecipe;
11-
public Dictionary<int, int> DropItems;
1211
public RecipeInfo RecipeInfo;
1312
public Recipe OverridenRecipe;
1413
public int RecipeId;
1514

16-
public CompoundRecipe(int recipeId, RecipeInfo recipeInfo)
15+
public CompoundRecipe(Mod mod) : base(mod)
16+
{
17+
}
18+
19+
public void Apply(int recipeId, RecipeInfo recipeInfo)
1720
{
1821
RecipeId = recipeId;
1922
OverridenRecipe = Main.recipe[recipeId];
2023

21-
if (recipeInfo.UsedItems.Count > Recipe.maxRequirements)
22-
Recipe.maxRequirements = recipeInfo.UsedItems.Count;//This may be a bit bigger than the needed value
23-
24-
CurrentRecipe = new Recipe
24+
if (recipeInfo.UsedItems.Count > maxRequirements)
2525
{
26-
createItem = OverridenRecipe.createItem
27-
};
28-
DropItems = new Dictionary<int, int>();
26+
maxRequirements = recipeInfo.UsedItems.Count; //This may be a bit bigger than the needed value
27+
requiredItem = new Item[maxRequirements];
28+
for (int j = 0; j < maxRequirements; j++) requiredItem[j] = new Item();
29+
}
30+
31+
createItem = createItem = OverridenRecipe.createItem;
2932
RecipeInfo = recipeInfo;
3033

31-
List<KeyValuePair<int, int>> keyValuePairs = recipeInfo.UsedItems.ToList();
34+
List<KeyValuePair<int, int>> keyValuePairs = RecipeInfo.UsedItems.ToList();
3235
int i = 0;
3336
foreach (KeyValuePair<int, int> keyValuePair in keyValuePairs.Where(keyValuePair => keyValuePair.Value > 0))
3437
{
35-
CurrentRecipe.requiredItem[i] = new Item();
36-
CurrentRecipe.requiredItem[i].SetDefaults(keyValuePair.Key);
37-
CurrentRecipe.requiredItem[i].stack = keyValuePair.Value;
38+
requiredItem[i].SetDefaults(keyValuePair.Key);
39+
requiredItem[i].stack = keyValuePair.Value;
3840
++i;
3941
}
42+
43+
for (; i < maxRequirements; i++) requiredItem[i].type = ItemID.None;
4044
}
4145

42-
public void BeforeCraft()
46+
public override int ConsumeItem(int type, int numRequired)
4347
{
44-
List<KeyValuePair<int, int>> keyValuePairs = RecipeInfo.TrueUsedItems.ToList();
45-
int i = 0;
46-
foreach (KeyValuePair<int, int> keyValuePair in keyValuePairs)
47-
if (keyValuePair.Value < 0)
48-
{
49-
DropItems.Add(keyValuePair.Key, -keyValuePair.Value);
50-
}
51-
else
52-
{
53-
CurrentRecipe.requiredItem[i] = new Item();
54-
CurrentRecipe.requiredItem[i].SetDefaults(keyValuePair.Key);
55-
CurrentRecipe.requiredItem[i].stack = keyValuePair.Value;
56-
++i;
57-
}
58-
59-
for (; i < RecipeInfo.UsedItems.Count; i++)
60-
{
61-
CurrentRecipe.requiredItem[i].stack = 0;
62-
}
48+
return RecipeInfo.TrueUsedItems.TryGetValue(type, out numRequired) ? numRequired : 0;
6349
}
6450

65-
public void OnCraft()
51+
public override void OnCraft(Item item)
6652
{
67-
foreach (KeyValuePair<int, int> keyValuePair in DropItems)
68-
Main.player[Main.myPlayer].QuickSpawnItem(keyValuePair.Key, keyValuePair.Value);
53+
List<KeyValuePair<int, int>> keyValuePairs = RecipeInfo.TrueUsedItems.ToList();
54+
foreach (KeyValuePair<int, int> keyValuePair in keyValuePairs.Where(keyValuePair => keyValuePair.Value < 0))
55+
Main.LocalPlayer.QuickSpawnItem(keyValuePair.Key, -keyValuePair.Value);
6956

7057
foreach (KeyValuePair<Recipe, int> keyValuePair in RecipeInfo.RecipeUsed)
7158
for (int i = 0; i < keyValuePair.Value; i++)

RecursiveCraft.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RecursiveCraft : Mod
1717
{
1818
public static Dictionary<int, List<Recipe>> RecipeByResult;
1919
public static Dictionary<Recipe, RecipeInfo> RecipeCache;
20-
public static CompoundRecipe CurrentCompound;
20+
public static CompoundRecipe CompoundRecipe;
2121

2222
public static int DepthSearch;
2323
public static bool InventoryWasOpen;
@@ -28,7 +28,6 @@ public override void Load()
2828
ILRecipe.FindRecipes += ApplyRecursiveSearch;
2929
OnMain.DrawInventory += EditFocusRecipe;
3030
OnMain.Update += ApplyKey;
31-
On.Terraria.Recipe.Create += CraftCompoundRecipe;
3231
RecipeByResult = new Dictionary<int, List<Recipe>>();
3332
RecipeCache = new Dictionary<Recipe, RecipeInfo>();
3433

@@ -46,18 +45,19 @@ public override void Unload()
4645
ILRecipe.FindRecipes -= ApplyRecursiveSearch;
4746
OnMain.DrawInventory -= EditFocusRecipe;
4847
OnMain.Update -= ApplyKey;
49-
On.Terraria.Recipe.Create -= CraftCompoundRecipe;
5048
RecipeByResult = null;
5149
RecipeCache = null;
5250
Hotkeys = null;
5351

54-
if (CurrentCompound != null)
55-
Main.recipe[CurrentCompound.RecipeId] = CurrentCompound.OverridenRecipe;
56-
CurrentCompound = null;
52+
if (CompoundRecipe.OverridenRecipe != null)
53+
Main.recipe[CompoundRecipe.RecipeId] = CompoundRecipe.OverridenRecipe;
54+
CompoundRecipe = null;
5755
}
5856

5957
public override void PostAddRecipes()
6058
{
59+
CompoundRecipe = new CompoundRecipe(this);
60+
6161
foreach (Recipe recipe in Main.recipe)
6262
{
6363
int type = recipe.createItem.type;
@@ -71,21 +71,6 @@ public override void PostAddRecipes()
7171
}
7272
}
7373

74-
public static void CraftCompoundRecipe(On.Terraria.Recipe.orig_Create orig, Recipe self)
75-
{
76-
if (CurrentCompound != null && self == CurrentCompound.CurrentRecipe)
77-
{
78-
CurrentCompound.BeforeCraft();
79-
orig(self);
80-
CurrentCompound.OnCraft();
81-
Recipe.FindRecipes();
82-
}
83-
else
84-
{
85-
orig(self);
86-
}
87-
}
88-
8974
public void ApplyKey(OnMain.orig_Update orig, Main self, GameTime gameTime)
9075
{
9176
if (InventoryWasOpen != Main.playerInventory)
@@ -131,17 +116,17 @@ public void ApplyKey(OnMain.orig_Update orig, Main self, GameTime gameTime)
131116

132117
public static void EditFocusRecipe(OnMain.orig_DrawInventory orig, Main self)
133118
{
134-
if (CurrentCompound != null) Main.recipe[CurrentCompound.RecipeId] = CurrentCompound.OverridenRecipe;
119+
if (CompoundRecipe.OverridenRecipe != null) Main.recipe[CompoundRecipe.RecipeId] = CompoundRecipe.OverridenRecipe;
135120
int i = Main.availableRecipe[Main.focusRecipe];
136121
Recipe recipe = Main.recipe[i];
137122
if (RecipeCache.TryGetValue(recipe, out RecipeInfo recipeInfo))
138123
{
139-
CurrentCompound = new CompoundRecipe(i, recipeInfo);
140-
Main.recipe[i] = CurrentCompound.CurrentRecipe;
124+
CompoundRecipe.Apply(i, recipeInfo);
125+
Main.recipe[i] = CompoundRecipe;
141126
}
142127
else
143128
{
144-
CurrentCompound = null;
129+
CompoundRecipe.OverridenRecipe = null;
145130
}
146131

147132
orig(self);
@@ -179,7 +164,9 @@ public static void RecursiveSearch(Dictionary<int, int> inventory)
179164
CraftingSource craftingSource = new PlayerAsCraftingSource();
180165
for (int n = 0; n < Recipe.maxRecipes && Main.recipe[n].createItem.type != ItemID.None; n++)
181166
{
182-
Recipe recipe = CurrentCompound?.RecipeId == n ? CurrentCompound.OverridenRecipe : Main.recipe[n];
167+
Recipe recipe = Main.recipe[n];
168+
if (recipe is CompoundRecipe compoundRecipe)
169+
recipe = compoundRecipe.OverridenRecipe;
183170
RecipeInfo recipeInfo = FindIngredientsForRecipe(inventory, craftingSource, recipe);
184171
if (recipeInfo != null)
185172
{
@@ -258,7 +245,7 @@ public static int AmountOfDoableRecipe(ref Dictionary<int, int> inventoryToUse,
258245
int trueIngredientsNeeded = trueTimeCraft * ingredient.stack;
259246
if (recipe.alchemy && craftingSource.AlchemyTable)
260247
for (int i = 0; i < trueTimeCraft; i++)
261-
if(Main.rand.Next(3) == 0)
248+
if (Main.rand.Next(3) == 0)
262249
trueIngredientsNeeded -= ingredient.stack;
263250

264251
#region UseIngredients

build.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
displayName = Recursive Craft
22
author = Gaelyte
3-
version = 1.2
3+
version = 1.3
44
homepage = https://github.com/Ishigh1/RecursiveCraft

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.3
2+
* Changed the implementation of Compoundrecipe
3+
14
# 1.2
25
* Fixed an issue with recipes needing more than 15 ingredients
36
* Fixed an issue with OnCraft not being called

0 commit comments

Comments
 (0)