tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModRecipe.cs
Go to the documentation of this file.
1using System;
2using Terraria.ID;
4
5namespace Terraria.ModLoader
6{
10 public class ModRecipe : Recipe
11 {
12 public readonly Mod mod;
13 private int numIngredients = 0;
14 private int numTiles = 0;
15
19 public int RecipeIndex {
20 get;
21 private set;
22 }
23
28 public ModRecipe(Mod mod) {
29 if (!RecipeHooks.setupRecipes)
30 throw new RecipeException("A ModRecipe can only be created inside recipe related methods");
31 this.mod = mod;
32 }
33
39 public void SetResult(int itemID, int stack = 1) {
40 this.createItem.SetDefaults(itemID, false);
41 this.createItem.stack = stack;
42 }
43
51 public void SetResult(Mod mod, string itemName, int stack = 1) {
52 if (mod == null) {
53 mod = this.mod;
54 }
55 int type = mod.ItemType(itemName);
56 if (type == 0) {
57 string message = "The item " + itemName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
58 message += "If you are trying to use a vanilla item, try removing the first argument.";
59 throw new RecipeException(message);
60 }
61 this.SetResult(type, stack);
62 }
63
69 public void SetResult(ModItem item, int stack = 1) {
70 this.SetResult(item.item.type, stack);
71 }
72
78 public void AddIngredient(int itemID, int stack = 1) {
79 if (numIngredients == 14)
80 throw new RecipeException("Recipe already has maximum number of ingredients. 14 is the max.");
81 this.requiredItem[numIngredients].SetDefaults(itemID, false);
82 this.requiredItem[numIngredients].stack = stack;
84 }
85
93 public void AddIngredient(Mod mod, string itemName, int stack = 1) {
94 if (mod == null) {
95 mod = this.mod;
96 }
97 int type = mod.ItemType(itemName);
98 if (type == 0) {
99 string message = "The item " + itemName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
100 message += "If you are trying to use a vanilla item, try removing the first argument.";
101 throw new RecipeException(message);
102 }
103 this.AddIngredient(type, stack);
104 }
105
111 public void AddIngredient(ModItem item, int stack = 1) {
112 this.AddIngredient(item.item.type, stack);
113 }
114
121 public void AddRecipeGroup(string name, int stack = 1) {
122 if (!RecipeGroup.recipeGroupIDs.ContainsKey(name)) {
123 throw new RecipeException("A recipe group with the name " + name + " does not exist.");
124 }
125 int id = RecipeGroup.recipeGroupIDs[name];
126 RecipeGroup rec = RecipeGroup.recipeGroups[id];
127 AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
128 acceptedGroups.Add(id);
129 }
130
137 public void AddRecipeGroup(int recipeGroupID, int stack = 1)
138 {
139 if (!RecipeGroup.recipeGroups.ContainsKey(recipeGroupID)) {
140 throw new RecipeException("A recipe group with the ID " + recipeGroupID + " does not exist.");
141 }
142 RecipeGroup rec = RecipeGroup.recipeGroups[recipeGroupID];
143 AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
144 acceptedGroups.Add(recipeGroupID);
145 }
146
152 public void AddTile(int tileID) {
153 if (numTiles == 14)
154 throw new RecipeException("Recipe already has maximum number of tiles. 14 is the max.");
155 if (tileID < 0 || tileID >= TileLoader.TileCount) {
156 throw new RecipeException("No tile has ID " + tileID);
157 }
158 this.requiredTile[numTiles] = tileID;
159 numTiles++;
160 }
161
168 public void AddTile(Mod mod, string tileName) {
169 if (mod == null) {
170 mod = this.mod;
171 }
172 int type = mod.TileType(tileName);
173 if (type == 0) {
174 string message = "The tile " + tileName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
175 message += "If you are trying to use a vanilla tile, try using ModRecipe.AddTile(tileID).";
176 throw new RecipeException(message);
177 }
178 this.AddTile(type);
179 }
180
185 public void AddTile(ModTile tile) {
186 this.AddTile(tile.Type);
187 }
188
193 public virtual bool RecipeAvailable() {
194 return true;
195 }
196
201 public virtual void OnCraft(Item item) {
202 }
203
204 //in Terraria.Recipe.Create before alchemy table check add
205 // ModRecipe modRecipe = this as ModRecipe;
206 // if(modRecipe != null) { num = modRecipe.ConsumeItem(item.type, item.stack); }
213 public virtual int ConsumeItem(int type, int numRequired) {
214 return numRequired;
215 }
216
221 public void AddRecipe() {
222 if (this.createItem == null || this.createItem.type == 0) {
223 throw new RecipeException("A recipe without any result has been added.");
224 }
225 if (this.numIngredients > 14 || this.numTiles > 14) {
226 throw new RecipeException("A recipe with either too many tiles or too many ingredients has been added. 14 is the maximum amount.");
227 }
228 for (int k = 0; k < Recipe.maxRequirements; k++) {
229 if (this.requiredTile[k] == TileID.Bottles) {
230 this.alchemy = true;
231 break;
232 }
233 }
234 if (Recipe.numRecipes >= Recipe.maxRecipes) {
235 Recipe.maxRecipes += 500;
236 Array.Resize(ref Main.recipe, Recipe.maxRecipes);
237 Array.Resize(ref Main.availableRecipe, Recipe.maxRecipes);
238 Array.Resize(ref Main.availableRecipeY, Recipe.maxRecipes);
239 for (int k = Recipe.numRecipes; k < Recipe.maxRecipes; k++) {
240 Main.recipe[k] = new Recipe();
241 Main.availableRecipeY[k] = 65f * k;
242 }
243 }
244 Main.recipe[Recipe.numRecipes] = this;
245 this.RecipeIndex = Recipe.numRecipes;
246 mod.recipes.Add(this);
247 Recipe.numRecipes++;
248 }
249 }
250}
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:25
int TileType(string name)
Gets the type of the ModTile of this mod with the given name. Returns 0 if no ModTile with the given ...
int ItemType(string name)
Gets the internal ID / type of the ModItem corresponding to the name. Returns 0 if no ModItem with th...
This class serves as a place for you to place all your properties and hooks for each item....
Definition: ModItem.cs:17
Item item
The item object that this ModItem controls.
Definition: ModItem.cs:26
This class extends Terraria.Recipe, meaning you can use it in a similar manner to vanilla recipes....
Definition: ModRecipe.cs:11
int RecipeIndex
The index of the recipe in the Main.recipe array.
Definition: ModRecipe.cs:19
void AddRecipeGroup(string name, int stack=1)
Adds a recipe group ingredient to this recipe with the given RecipeGroup name and stack size....
Definition: ModRecipe.cs:121
void AddIngredient(int itemID, int stack=1)
Adds an ingredient to this recipe with the given item type and stack size. Ex: recipe....
Definition: ModRecipe.cs:78
virtual int ConsumeItem(int type, int numRequired)
Allows you to determine how many of a certain ingredient is consumed when this recipe is used....
Definition: ModRecipe.cs:213
void AddTile(ModTile tile)
Adds a required crafting station to this recipe of the given type of tile.
Definition: ModRecipe.cs:185
void AddTile(int tileID)
Adds a required crafting station with the given tile type to this recipe. Ex: recipe....
Definition: ModRecipe.cs:152
void AddIngredient(ModItem item, int stack=1)
Adds an ingredient to this recipe of the given type of item and stack size.
Definition: ModRecipe.cs:111
void SetResult(ModItem item, int stack=1)
Sets the result of this recipe to the given type of item and stack size. Useful in ModItem....
Definition: ModRecipe.cs:69
void AddRecipeGroup(int recipeGroupID, int stack=1)
Adds a recipe group ingredient to this recipe with the given RecipeGroupID and stack size....
Definition: ModRecipe.cs:137
void AddIngredient(Mod mod, string itemName, int stack=1)
Adds an ingredient to this recipe with the given item name from the given mod, and with the given sta...
Definition: ModRecipe.cs:93
virtual bool RecipeAvailable()
Whether or not the conditions are met for this recipe to be available for the player to use....
Definition: ModRecipe.cs:193
virtual void OnCraft(Item item)
Allows you to make anything happen when the player uses this recipe. The item parameter is the item ...
Definition: ModRecipe.cs:201
void SetResult(Mod mod, string itemName, int stack=1)
Sets the result of this recipe with the given item name from the given mod, and with the given stack ...
Definition: ModRecipe.cs:51
void AddRecipe()
Adds this recipe to the game. Call this after you have finished setting the result,...
Definition: ModRecipe.cs:221
ModRecipe(Mod mod)
Constructor
Definition: ModRecipe.cs:28
void SetResult(int itemID, int stack=1)
Sets the result of this recipe with the given item type and stack size.
Definition: ModRecipe.cs:39
void AddTile(Mod mod, string tileName)
Adds a required crafting station to this recipe with the given tile name from the given mod....
Definition: ModRecipe.cs:168
This class represents a type of tile that can be added by a mod. Only one instance of this class will...
Definition: ModTile.cs:13
ushort Type
The internal ID of this type of tile.
Definition: ModTile.cs:33
This is where all ModRecipe and GlobalRecipe hooks are gathered and called.
Definition: RecipeHooks.cs:10
This serves as the central class from which tile-related functions are supported and carried out.
Definition: TileLoader.cs:15
@ Environment
Sandstorm, Hell, Above surface during Eclipse, Space