Terraria ModLoader  0.11.6.2
A framework for 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 
134  public void AddTile(int tileID) {
135  if (numTiles == 14)
136  throw new RecipeException("Recipe already has maximum number of tiles. 14 is the max.");
137  if (tileID < 0 || tileID >= TileLoader.TileCount) {
138  throw new RecipeException("No tile has ID " + tileID);
139  }
140  this.requiredTile[numTiles] = tileID;
141  numTiles++;
142  }
143 
150  public void AddTile(Mod mod, string tileName) {
151  if (mod == null) {
152  mod = this.mod;
153  }
154  int type = mod.TileType(tileName);
155  if (type == 0) {
156  string message = "The tile " + tileName + " does not exist in the mod " + mod.Name + "." + Environment.NewLine;
157  message += "If you are trying to use a vanilla tile, try using ModRecipe.AddTile(tileID).";
158  throw new RecipeException(message);
159  }
160  this.AddTile(type);
161  }
162 
167  public void AddTile(ModTile tile) {
168  this.AddTile(tile.Type);
169  }
170 
175  public virtual bool RecipeAvailable() {
176  return true;
177  }
178 
183  public virtual void OnCraft(Item item) {
184  }
185 
186  //in Terraria.Recipe.Create before alchemy table check add
187  // ModRecipe modRecipe = this as ModRecipe;
188  // if(modRecipe != null) { num = modRecipe.ConsumeItem(item.type, item.stack); }
195  public virtual int ConsumeItem(int type, int numRequired) {
196  return numRequired;
197  }
198 
203  public void AddRecipe() {
204  if (this.createItem == null || this.createItem.type == 0) {
205  throw new RecipeException("A recipe without any result has been added.");
206  }
207  if (this.numIngredients > 14 || this.numTiles > 14) {
208  throw new RecipeException("A recipe with either too many tiles or too many ingredients has been added. 14 is the max.");
209  }
210  for (int k = 0; k < Recipe.maxRequirements; k++) {
211  if (this.requiredTile[k] == TileID.Bottles) {
212  this.alchemy = true;
213  break;
214  }
215  }
216  if (Recipe.numRecipes >= Recipe.maxRecipes) {
217  Recipe.maxRecipes += 500;
218  Array.Resize(ref Main.recipe, Recipe.maxRecipes);
219  Array.Resize(ref Main.availableRecipe, Recipe.maxRecipes);
220  Array.Resize(ref Main.availableRecipeY, Recipe.maxRecipes);
221  for (int k = Recipe.numRecipes; k < Recipe.maxRecipes; k++) {
222  Main.recipe[k] = new Recipe();
223  Main.availableRecipeY[k] = 65f * k;
224  }
225  }
226  Main.recipe[Recipe.numRecipes] = this;
227  this.RecipeIndex = Recipe.numRecipes;
228  mod.recipes.Add(this);
229  Recipe.numRecipes++;
230  }
231  }
232 }
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:183
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:167
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:134
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:203
virtual bool RecipeAvailable()
Whether or not the conditions are met for this recipe to be available for the player to use...
Definition: ModRecipe.cs:175
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:150
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:195
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