-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecipeHelper.cs
351 lines (320 loc) · 16.3 KB
/
RecipeHelper.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Collections.Generic;
namespace BrightExistence.SimpleTools
{
public static class RecipeHelper
{
/// <summary>
/// Attempts to remove an existing recipe from the server's database.
/// </summary>
/// <param name="recName">Name of recipe.</param>
/// <returns>True if recipe was removed, False if recipe was not found or removal was not successful.</returns>
public static bool tryRemoveRecipe (string recName, string NAMESPACE = null)
{
try
{
Recipe Rec;
if (RecipeStorage.TryGetRecipe(recName, out Rec))
{
Pipliz.Log.Write("{0}: Recipe {1} found, attempting to remove.", NAMESPACE == null ? "" : NAMESPACE, Rec.Name);
RecipeStorage.Recipes.Remove(recName);
Recipe Rec2;
if (!RecipeStorage.TryGetRecipe(recName, out Rec2))
{
Pipliz.Log.Write("{0}: Recipe {1} successfully removed", NAMESPACE == null ? "" : NAMESPACE, Rec.Name);
return true;
}
else
{
Pipliz.Log.Write("{0}: Recipe {1} removal failed for unknown reason.", NAMESPACE == null ? "" : NAMESPACE, Rec.Name);
return false;
}
}
else
{
Pipliz.Log.Write("{0}: Recipe {1} not found.", NAMESPACE == null ? "" : NAMESPACE, recName);
return false;
}
}
catch (Exception)
{
Pipliz.Log.Write("{0}: tryRemoveRecipe has reached an exception.");
return false;
}
}
}
public class SimpleRecipe
{
/// <summary>
/// Name of Recipe, excluding prefixs. Ex: myRecipe instead of myHandle.myMod.myRecipe
/// </summary>
public string Name = "New Recipe";
/// <summary>
/// Local copy of mod NAMESPACE.
/// </summary>
protected string NAMESPACE;
/// <summary>
/// An InventoryItem list containing the items the user recieves when this recipe is completed. May be ignored if the constructor
/// which takes a SimpleItem object is used.
/// </summary>
protected List<InventoryItem> realResults = new List<InventoryItem>();
/// <summary>
/// Items references are added in the form of shells so they can be evaluated at the right time into actual InventoryItem objects.
/// </summary>
public List<ItemShell> Results = new List<ItemShell>();
/// <summary>
/// Items references are added in the form of shells so they can be evaluated at the right time into actual InventoryItem objects.
/// </summary>
public List<ItemShell> Requirements = new List<ItemShell>();
/// <summary>
/// An InventoryItem list containing the items necessary to complete this recipe.
/// </summary>
protected List<InventoryItem> realRequirements = new List<InventoryItem>();
/// <summary>
/// The limitType, a.k.a. NPCTypeKey is essentially a group of recipes associated with a block and an NPC. Ex: pipliz.crafter
/// </summary>
public string limitType { get; set; }
/// <summary>
/// The default limit at which an NPC will stop crafting this recipe.
/// </summary>
public int defaultLimit = 1;
/// <summary>
/// The default priority of this recipe vs other recipes of the same limitType when crafted by an NPC.
/// </summary>
public int defaultPriority = 0;
/// <summary>
/// True if this recipe must be researched to be available, otherwise false.
/// </summary>
public bool isOptional = false;
/// <summary>
/// Set to true if you want addRecipeToLimitType() to create a copy of this recipe and add it to the list of recipes the players
/// themselves can craft.
/// </summary>
public bool userCraftable = false;
/// <summary>
/// Names what recipes, if any, this recipe is intended to replace. The named recipes will be deleted from the server's
/// database before this recipe is added. Use when replacing vanilla recipes.
/// </summary>
public List<string> Replaces = new List<string>();
/// <summary>
/// Set to false if you want this recipe, and SimpleResearchable items that know about and depend upon it, to NOT be registered.
/// </summary>
public bool enabled = true;
/// <summary>
/// Automatically generated recipe key with limit type prefix. Ex: 'recipeLimit.myRecipe'. To get the player
/// crafted recipe key, use "player." + Name
/// </summary>
public string fullName
{
get
{
if (limitType != null) return limitType + "." + Name;
else return Name;
}
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="strName">Name of recipe excluding any prefixes. Ex: myRecipe NOT myHandle.myMod.myRecipe</param>
/// <param name="strLimitType">The limitType, a.k.a. NPCTypeKey is essentially a group of recipes associated with a block and an NPC. Ex: pipliz.crafter</param>
public SimpleRecipe(string strName, string strNAMESPACE = null, string strLimitType = null)
{
this.Name = strName == null ? "NewRecipe" : strName;
NAMESPACE = strNAMESPACE;
this.limitType = strLimitType;
Pipliz.Log.Write("{0}: Initialized Recipe {1} (it is not yet registered.)", NAMESPACE == null ? "" : NAMESPACE, this.Name);
try
{
if (!Variables.Recipes.Contains(this)) Variables.Recipes.Add(this);
}
catch (Exception)
{
Pipliz.Log.Write("{0} : WARNING : Recipe {1} could not be automatically added to auto-load list. Make sure you explicityly added it.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
if (strLimitType == null) userCraftable = true;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="Item">A SimpleItem object holding a type which is the intended result of this recipe.</param>
/// <param name="strLimitType">The limitType, a.k.a. NPCTypeKey is essentially a group of recipes associated with a block and an NPC. Ex: pipliz.crafter</param>
public SimpleRecipe(SimpleItem Item, string strLimitType = null)
{
if (Item == null || Item.Name == null || Item.Name.Length < 1)
{
throw new ArgumentException(NAMESPACE == null ? "" : NAMESPACE + ": Simple recipe cannot initialize when given a null Item or an Item with a Name of less than one character.");
}
else
{
limitType = strLimitType;
this.NAMESPACE = Item.NAMESPACE;
this.Name = Item.Name;
addResult(Item);
Pipliz.Log.Write("{0}: Initialized Recipe {1} (it is not yet registered.)", NAMESPACE == null ? "" : NAMESPACE, Name);
try
{
if (!Variables.Recipes.Contains(this)) Variables.Recipes.Add(this);
}
catch (Exception)
{
Pipliz.Log.Write("{0} : WARNING : Recipe {1} could not be automatically added to auto-load list. Make sure you explicityly added it.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
if (strLimitType == null) userCraftable = true;
}
}
/// <summary>
/// Adds an item required by this recipe using a string key.
/// </summary>
/// <param name="itemKey">a valid (string) item key</param>
/// <param name="amount">number of specified item that is required</param>
public void addRequirement (string itemKey, int amount = 1)
{
if (itemKey == null || itemKey.Length < 1)
{
Pipliz.Log.Write("{0} WARNING: Recipe {1}'s addRequirement() method was called but was given a null or invalid item key.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
else
{
Requirements.Add(new ItemShell(itemKey, amount));
}
}
/// <summary>
/// Adds an item required by this recipe using a valid SimpleItem reference.
/// </summary>
/// <param name="requiredItem">Instantiated SimpleItem object.</param>
/// <param name="amount">Number of items required.</param>
public void addRequirement (SimpleItem requiredItem, int amount = 1)
{
if (requiredItem != null && requiredItem.Name.Length > 0)
{
Requirements.Add(new ItemShell(requiredItem, amount));
if (!requiredItem.enabled) this.enabled = false;
}
else
{
Pipliz.Log.Write("{0} WARNING: Recipe {1}'s addRequirement() method was called but was given a null or invalid SimpleItem object.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
}
/// <summary>
/// Adds an item that results from this recipe by a string key.
/// </summary>
/// <param name="itemKey">a valid (string) item key</param>
/// <param name="amount">number of specified item that results</param>
public void addResult (string itemKey, int amount = 1)
{
if (itemKey == null || itemKey.Length < 1)
{
Pipliz.Log.Write("{0} WARNING: Recipe {1}'s addResult() method was called but was given a null or invalid item key.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
else
{
Results.Add(new ItemShell(itemKey, amount));
}
}
/// <summary>
/// Adds an item required by this recipe using a valid SimpleItem reference.
/// </summary>
/// <param name="requiredItem">Instantiated SimpleItem object.</param>
/// <param name="amount">Number of items required.</param>
public void addResult(SimpleItem resultItem, int amount = 1)
{
if (resultItem != null && resultItem.Name.Length > 0)
{
Results.Add(new ItemShell(resultItem, amount));
if (!resultItem.enabled) this.enabled = false;
}
else
{
Pipliz.Log.Write("{0} WARNING: Recipe {1}'s addResult() method was called but was given a null or invalid SimpleItem object.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
}
/// <summary>
/// Does all the work of adding this recipe to the server's database. Should be called in the AfterItemTypesDefined callback.
/// </summary>
public void addRecipeToLimitType()
{
if (enabled)
{
try
{
// First remove any recipes we are replacing.
foreach (string deleteMe in Replaces)
{
Pipliz.Log.Write("{0}: Recipe {1} is marked as replacing {2}, attempting to comply.", NAMESPACE == null ? "" : NAMESPACE, this.Name, deleteMe);
RecipeHelper.tryRemoveRecipe(deleteMe);
}
// Convert shell references into actual InventoryItem objects.
foreach (ItemShell I in Results)
{
if (Variables.itemsMaster == null)
{
Pipliz.Log.WriteError("{0}.SimpleRecipe.addRecipeToLimitType() has reached a critical error: 'Variables.itemsMaster' is not yet available. Recipe: {1}", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
else
{
string useKey = I.asSimpleItem == null ? I.strItemkey : I.asSimpleItem.ID;
if (Variables.itemsMaster.ContainsKey(useKey))
{
realResults.Add(new InventoryItem(useKey, I.intAmount));
}
else
{
Pipliz.Log.WriteError("{0}: A problem occurred adding recipe RESULT {1} to recipe {2}, the item key was not found.", NAMESPACE == null ? "" : NAMESPACE, I.strItemkey, this.Name);
}
}
}
foreach (ItemShell I in Requirements)
{
if (Variables.itemsMaster == null)
{
Pipliz.Log.WriteError("{0}.SimpleRecipe.addRecipeToLimitType() has reached a critical error: 'Variables.itemsMaster' is not yet available. Recipe: {1}", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
else
{
string useKey = I.asSimpleItem == null ? I.strItemkey : I.asSimpleItem.ID;
if (Variables.itemsMaster.ContainsKey(useKey))
{
realRequirements.Add(new InventoryItem(useKey, I.intAmount));
}
else
{
Pipliz.Log.WriteError("{0}: A problem occurred adding recipe REQUIREMENT {1} to recipe {2}, the item key was not found.", NAMESPACE == null ? "" : NAMESPACE, I.strItemkey, this.Name);
}
}
}
// Build actual Recipe object.
Recipe thisRecipe = new Recipe(this.fullName, this.realRequirements, this.realResults, this.defaultLimit, this.isOptional, this.defaultPriority);
// Commence registering it.
Pipliz.Log.Write("{0}: Attempting to register recipe {1}", NAMESPACE == null ? "" : NAMESPACE, thisRecipe.Name);
if (this.limitType != null)
{
if (isOptional)
{
Pipliz.Log.Write("{0}: Attempting to register optional limit type recipe {1}", NAMESPACE == null ? "" : NAMESPACE, thisRecipe.Name);
RecipeStorage.AddOptionalLimitTypeRecipe(limitType, thisRecipe);
}
else
{
Pipliz.Log.Write("{0}: Attempting to register default limit type recipe {1}", NAMESPACE == null ? "" : NAMESPACE, thisRecipe.Name);
RecipeStorage.AddDefaultLimitTypeRecipe(limitType, thisRecipe);
}
}
if (userCraftable)
{
Recipe playerRecipe = new Recipe("player." + this.Name, this.realRequirements, this.realResults, this.defaultLimit, this.isOptional);
Pipliz.Log.Write("{0}: Attempting to register default player type recipe {1}", NAMESPACE == null ? "" : NAMESPACE, playerRecipe.Name);
RecipePlayer.AddDefaultRecipe(playerRecipe);
}
}
catch (Exception ex)
{
Pipliz.Log.WriteError("{0}: Error adding recipe: {1}", NAMESPACE == null ? "" : NAMESPACE, ex.Message);
}
}
else
{
Pipliz.Log.Write("{0}: Recipe {1} has been disabled and will NOT be registered.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
}
}
}