Terraria ModLoader  0.11.7.5
A mod to make and play Terraria mods
ModRecipe.cs
Go to the documentation of this file.
1 using System;
2 using Terraria.ID;
4 
5 namespace 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  this.mod = mod;
30  }
31 
37  public void SetResult(int itemID, int stack = 1) {
38  this.createItem.SetDefaults(itemID, false);
39  this.createItem.stack = stack;
40  }
41 
49  public void SetResult(Mod mod, string itemName, int stack = 1) {
50  if (mod == null) {
51  mod = this.mod;
52  }
53  int type = mod.ItemType(itemName);
54  if (type == 0) {
55  string message = "The item " + itemName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
56  message += "If you are trying to use a vanilla item, try removing the first argument.";
57  throw new RecipeException(message);
58  }
59  this.SetResult(type, stack);
60  }
61 
67  public void SetResult(ModItem item, int stack = 1) {
68  this.SetResult(item.item.type, stack);
69  }
70 
76  public void AddIngredient(int itemID, int stack = 1) {
77  if (numIngredients == 14)
78  throw new RecipeException("Recipe already has maximum number of ingredients. 14 is the max.");
79  this.requiredItem[numIngredients].SetDefaults(itemID, false);
80  this.requiredItem[numIngredients].stack = stack;
81  numIngredients++;
82  }
83 
91  public void AddIngredient(Mod mod, string itemName, int stack = 1) {
92  if (mod == null) {
93  mod = this.mod;
94  }
95  int type = mod.ItemType(itemName);
96  if (type == 0) {
97  string message = "The item " + itemName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
98  message += "If you are trying to use a vanilla item, try removing the first argument.";
99  throw new RecipeException(message);
100  }
101  this.AddIngredient(type, stack);
102  }
103 
109  public void AddIngredient(ModItem item, int stack = 1) {
110  this.AddIngredient(item.item.type, stack);
111  }
112 
119  public void AddRecipeGroup(string name, int stack = 1) {
120  if (!RecipeGroup.recipeGroupIDs.ContainsKey(name)) {
121  throw new RecipeException("A recipe group with the name " + name + " does not exist.");
122  }
123  int id = RecipeGroup.recipeGroupIDs[name];
124  RecipeGroup rec = RecipeGroup.recipeGroups[id];
125  AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
126  acceptedGroups.Add(id);
127  }
128 
135  public void AddRecipeGroup(int recipeGroupID, int stack = 1)
136  {
137  if (!RecipeGroup.recipeGroups.ContainsKey(recipeGroupID)) {
138  throw new RecipeException("A recipe group with the ID " + recipeGroupID + " does not exist.");
139  }
140  RecipeGroup rec = RecipeGroup.recipeGroups[recipeGroupID];
141  AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
142  acceptedGroups.Add(recipeGroupID);
143  }
144 
150  public void AddTile(int tileID) {
151  if (numTiles == 14)
152  throw new RecipeException("Recipe already has maximum number of tiles. 14 is the max.");
153  if (tileID < 0 || tileID >= TileLoader.TileCount) {
154  throw new RecipeException("No tile has ID " + tileID);
155  }
156  this.requiredTile[numTiles] = tileID;
157  numTiles++;
158  }
159 
166  public void AddTile(Mod mod, string tileName) {
167  if (mod == null) {
168  mod = this.mod;
169  }
170  int type = mod.TileType(tileName);
171  if (type == 0) {
172  string message = "The tile " + tileName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
173  message += "If you are trying to use a vanilla tile, try using ModRecipe.AddTile(tileID).";
174  throw new RecipeException(message);
175  }
176  this.AddTile(type);
177  }
178 
183  public void AddTile(ModTile tile) {
184  this.AddTile(tile.Type);
185  }
186 
191  public virtual bool RecipeAvailable() {
192  return true;
193  }
194 
199  public virtual void OnCraft(Item item) {
200  }
201 
202  //in Terraria.Recipe.Create before alchemy table check add
203  // ModRecipe modRecipe = this as ModRecipe;
204  // if(modRecipe != null) { num = modRecipe.ConsumeItem(item.type, item.stack); }
211  public virtual int ConsumeItem(int type, int numRequired) {
212  return numRequired;
213  }
214 
219  public void AddRecipe() {
220  if (this.createItem == null || this.createItem.type == 0) {
221  throw new RecipeException("A recipe without any result has been added.");
222  }
223  if (this.numIngredients > 14 || this.numTiles > 14) {
224  throw new RecipeException("A recipe with either too many tiles or too many ingredients has been added. 14 is the maximum amount.");
225  }
226  for (int k = 0; k < Recipe.maxRequirements; k++) {
227  if (this.requiredTile[k] == TileID.Bottles) {
228  this.alchemy = true;
229  break;
230  }
231  }
232  if (Recipe.numRecipes >= Recipe.maxRecipes) {
233  Recipe.maxRecipes += 500;
234  Array.Resize(ref Main.recipe, Recipe.maxRecipes);
235  Array.Resize(ref Main.availableRecipe, Recipe.maxRecipes);
236  Array.Resize(ref Main.availableRecipeY, Recipe.maxRecipes);
237  for (int k = Recipe.numRecipes; k < Recipe.maxRecipes; k++) {
238  Main.recipe[k] = new Recipe();
239  Main.availableRecipeY[k] = 65f * k;
240  }
241  }
242  Main.recipe[Recipe.numRecipes] = this;
243  this.RecipeIndex = Recipe.numRecipes;
244  mod.recipes.Add(this);
245  Recipe.numRecipes++;
246  }
247  }
248 }
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:199
This class represents a type of tile that can be added by a mod. Only one instance of this class will...
Definition: ModTile.cs:12
void AddTile(ModTile tile)
Adds a required crafting station to this recipe of the given type of tile.
Definition: ModRecipe.cs:183
int ItemType(string name)
Gets the internal ID / type of the ModItem corresponding to the name. Returns 0 if no ModItem with th...
Item item
The item object that this ModItem controls.
Definition: ModItem.cs:26
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 ...
This serves as the central class from which tile-related functions are supported and carried out...
Definition: TileLoader.cs:14
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:91
void AddTile(int tileID)
Adds a required crafting station with the given tile type to this recipe. Ex: recipe.AddTile(TileID.WorkBenches)
Definition: ModRecipe.cs:150
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:49
void AddRecipe()
Adds this recipe to the game. Call this after you have finished setting the result, ingredients, etc.
Definition: ModRecipe.cs:219
virtual bool RecipeAvailable()
Whether or not the conditions are met for this recipe to be available for the player to use...
Definition: ModRecipe.cs:191
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:135
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:166
void SetResult(int itemID, int stack=1)
Sets the result of this recipe with the given item type and stack size.
Definition: ModRecipe.cs:37
ushort Type
The internal ID of this type of tile.
Definition: ModTile.cs:33
Sandstorm, Hell, Above surface during Eclipse, Space
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:211
ModRecipe(Mod mod)
Constructor
Definition: ModRecipe.cs:28
void AddIngredient(int itemID, int stack=1)
Adds an ingredient to this recipe with the given item type and stack size. Ex: recipe.AddIngredient(ItemID.IronAxe)
Definition: ModRecipe.cs:76
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:67
virtual string Name
Stores the name of the mod. This name serves as the mod&#39;s identification, and also helps with saving ...
Definition: Mod.cs:42
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:109
Mod is an abstract class that you will override. It serves as a central place from which the mod&#39;s co...
Definition: Mod.cs:24
This class serves as a place for you to place all your properties and hooks for each item...
Definition: ModItem.cs:16
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:119
This class extends Terraria.Recipe, meaning you can use it in a similar manner to vanilla recipes...
Definition: ModRecipe.cs:10