forked from thegreyham/Valheim.CookingSkill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookingSkill.cs
More file actions
329 lines (261 loc) · 14.4 KB
/
CookingSkill.cs
File metadata and controls
329 lines (261 loc) · 14.4 KB
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
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Pipakin.SkillInjectorMod;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using UnityEngine;
namespace CookingSkill
{
[BepInPlugin(PluginGUID, PluginName, PluginVersion)]
[BepInDependency("com.pipakin.SkillInjectorMod")]
public class CookingSkill : BaseUnityPlugin
{
public const string PluginGUID = "thegreyham.valheim.CookingSkill";
public const string PluginName = "Cooking Skill";
public const string PluginVersion = "1.1.2";
private static Harmony harmony;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<bool> modEnabled;
private static ConfigEntry<float> configCookingStationXPIncrease;
private static ConfigEntry<float> configCauldronXPIncrease;
private static ConfigEntry<float> configFermenterXPIncrease;
private static ConfigEntry<float> configFoodHealthMulitplier;
private static ConfigEntry<float> configFoodStaminaMulitplier;
private static ConfigEntry<float> configFoodDurationMulitplier;
const int COOKING_SKILL_ID = 483; // Nexus mod id :)
private static Dictionary<string, Texture2D> cachedTextures = new Dictionary<string, Texture2D>();
private void Awake()
{
nexusID = Config.Bind<int>("General", "NexusID", 483, "NexusMods ID for updates.");
modEnabled = Config.Bind<bool>("General", "Enabled", true, "Enable the mod.");
configCookingStationXPIncrease = Config.Bind<float>("Cooking Skill XP", "CookingStationXP", 1f, "Cooking skill xp gained when using the Cooking Station.");
configCauldronXPIncrease = Config.Bind<float>("Cooking Skill XP", "CauldronXP", 2f, "Cooking skill xp gained when using the Cauldron.");
configFermenterXPIncrease = Config.Bind<float>("Cooking Skill XP", "FermenterXP", 6f, "Cooking skill xp gained when fermenting mead.");
configFoodHealthMulitplier = Config.Bind<float>("Food Effects", "HealthMultiplier", 0.5f, "Buff to Health given when consuming food per Cooking Skill Level. 1f = +1% / Level");
configFoodStaminaMulitplier = Config.Bind<float>("Food Effects", "StaminaMultiplier", 0.5f, "Buff to Stamina given when consuming food per Cooking Skill Level. 1f = +1% / Level");
configFoodDurationMulitplier = Config.Bind<float>("Food Effects", "DurationMultiplier", 1f, "Buff to Food Duration when consuming food per Cooking Skill Level. 1f = +1% / Level");
if (!modEnabled.Value)
return;
harmony = new Harmony(PluginGUID);
harmony.PatchAll(Assembly.GetExecutingAssembly());
if (SkillInjector.GetSkillDef((Skills.SkillType)COOKING_SKILL_ID) == null)
SkillInjector.RegisterNewSkill(COOKING_SKILL_ID, "Cooking", "Improves Health and Stamina buffs from consuming food", 1.0f, LoadCustomTexture("meat_cooked.png"), Skills.SkillType.Knives);
}
private void OnDestroy()
{
harmony.UnpatchSelf();
}
private static void Log(object msg)
{
Debug.Log($"[{PluginName}] {msg.ToString()}");
}
private static void Warn(object msg)
{
Debug.LogWarning($"[{PluginName}] {msg.ToString()}");
}
private static void LogError(object msg)
{
Debug.LogError($"[{PluginName}] {msg.ToString()}");
}
private static Sprite LoadCustomTexture(string filename)
{
string filepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "assets", filename);
if (File.Exists(filepath))
return Sprite.Create(LoadTexture(filepath), new Rect(0.0f, 0.0f, 64f, 64f), Vector2.zero);
LogError($"Unable to load skill icon! Make sure you place the {filename} file in the 'Valheim/BepInEx/plugins/assets/' directory!");
return null;
}
private static Texture2D LoadTexture(string filepath)
{
if (cachedTextures.ContainsKey(filepath))
return cachedTextures[filepath];
Texture2D texture2D = new Texture2D(0, 0);
ImageConversion.LoadImage(texture2D, File.ReadAllBytes(filepath));
return texture2D;
}
// ==================================================================== //
// COOKING STATION PATCHES //
// ==================================================================== //
#region Cooking Station Patches
// increase cooking skill when placing an item on the cooking station
[HarmonyPatch(typeof(CookingStation), "UseItem")]
internal class Patch_CookingStation_UseItem
{
static void Postfix(ref bool __result, Humanoid user)
{
if (__result)
{
((Player)user).RaiseSkill((Skills.SkillType)COOKING_SKILL_ID, configCookingStationXPIncrease.Value * 0.25f);
//Log($"[Add Item to Cook Station] Increase Cooking Skill by {configCookingStationXPIncrease.Value * 0.25f}");
}
}
}
// increase cooking skill when removing a successful cooked item from cooking station
[HarmonyPatch(typeof(CookingStation), "Interact")]
internal class Patch_CookingStation_Interact
{
static void Prefix(ref CookingStation __instance, ref ZNetView ___m_nview, Humanoid user, bool hold)
{
if (hold)
return;
Traverse t_cookingStation = Traverse.Create(__instance);
ZDO zdo = ___m_nview.GetZDO();
for (int slot = 0; slot < __instance.m_slots.Length; ++slot)
{
string itemName = zdo.GetString(nameof(slot) + slot);
bool isItemDone = t_cookingStation.Method("IsItemDone", itemName).GetValue<bool>();
if (itemName != "" && itemName != __instance.m_overCookedItem.name && isItemDone)
{
((Player)user).RaiseSkill((Skills.SkillType)COOKING_SKILL_ID, configCookingStationXPIncrease.Value * 0.75f);
//Log($"[Removed Cooked Item from Cook Station] Increase Cooking Skill by {configCookingStationXPIncrease.Value * 0.75f}");
break;
}
}
}
}
#endregion
// ==================================================================== //
// CAULDRON PATCHES //
// ==================================================================== //
#region Cauldron Patches
// increase cooking skill when making food in the cauldron
[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
internal class Patch_InventoryGui_DoCrafting
{
static void Prefix(ref InventoryGui __instance, ref Recipe ___m_craftRecipe, Player player)
{
if (___m_craftRecipe == null)
return;
bool isCauldronRecipe = ___m_craftRecipe.m_craftingStation?.m_name == "$piece_cauldron";
bool haveRequirements = player.HaveRequirements(___m_craftRecipe, false, 1) || player.NoCostCheat();
if (!isCauldronRecipe || !haveRequirements)
return;
player.RaiseSkill((Skills.SkillType)COOKING_SKILL_ID, configCauldronXPIncrease.Value);
//Log($"[Cooked Item on Cauldron] Increase Cooking Skill by {configCauldronXPIncrease.Value}");
}
}
#endregion
// ==================================================================== //
// FERMENTER PATCHES //
// ==================================================================== //
#region Fermenter Patches
// increase cooking skill when placing a mead base into the fermenter
[HarmonyPatch(typeof(Fermenter), "AddItem")]
internal class Patch_Fermenter_AddItem
{
static void Postfix(ref bool __result, Humanoid user)
{
if (__result)
{
((Player)user).RaiseSkill((Skills.SkillType)COOKING_SKILL_ID, configFermenterXPIncrease.Value * 0.5f);
//Log($"[Add Item to Fermenter] Increase Cooking Skill by {configFermenterXPIncrease.Value * 0.5f}");
}
}
}
// increase cooking skill when removing a fermented mead from the fermenter
[HarmonyPatch(typeof(Fermenter), "Interact")]
internal class Patch_Fermenter_Interact
{
static void Prefix(ref Fermenter __instance, Humanoid user, bool hold)
{
if (hold || !PrivateArea.CheckAccess(__instance.transform.position))
return;
int status = Traverse.Create(__instance).Method("GetStatus").GetValue<int>();
if (status == 3) // 3 is the enum value for Ready
{
((Player)user).RaiseSkill((Skills.SkillType)COOKING_SKILL_ID, configFermenterXPIncrease.Value * 0.5f);
//Log($"[Removed Item from Fermenter] Increase Cooking Skill by {configFermenterXPIncrease.Value * 0.5f}");
}
}
}
#endregion
// ==================================================================== //
// FOOD BUFF PATCHES //
// ==================================================================== //
#region Health & Stamina Food Buff Patches
// All Food will gain a % increase in HP & Stamina per level
[HarmonyPatch(typeof(Player), "EatFood")]
internal class Patch_Player_EatFood
{
static void Prefix(ref Player __instance, ref ItemDrop.ItemData item, ref float[] __state)
{
if (configFoodHealthMulitplier.Value == 0f && configFoodStaminaMulitplier.Value == 0f)
return;
if (!Traverse.Create(__instance).Method("CanEat", item, false).GetValue<bool>())
return;
float skillLevel = __instance.GetSkillFactor((Skills.SkillType)COOKING_SKILL_ID);
float healthSkillModifier = 1f + (configFoodHealthMulitplier.Value * skillLevel);
float staminaSkillModifier = 1f + (configFoodStaminaMulitplier.Value * skillLevel);
float durationSkillModifier = 1f + (configFoodDurationMulitplier.Value * skillLevel);
__state = new float[] { item.m_shared.m_food, item.m_shared.m_foodStamina };
item.m_shared.m_food *= healthSkillModifier;
item.m_shared.m_foodStamina *= staminaSkillModifier;
float newBurnTime = ((int)(item.m_shared.m_foodBurnTime * durationSkillModifier * 100)) / 100f;
Log($"Cooking Skill buffed {item.m_dropPrefab.name}:\nHealth: {__state[0]} -> {item.m_shared.m_food}\nStamina: {__state[1]} -> {item.m_shared.m_foodStamina}\nDuration: {item.m_shared.m_foodBurnTime} sec -> {newBurnTime} sec");
}
static void Postfix(ref ItemDrop.ItemData item, float[] __state)
{
if (configFoodHealthMulitplier.Value == 0f && configFoodStaminaMulitplier.Value == 0f)
return;
if (__state == null || __state.Length == 0)
return;
item.m_shared.m_food = __state[0];
item.m_shared.m_foodStamina = __state[1];
}
}
#endregion
#region Food Duration Buff Patches
[HarmonyPatch(typeof(Player), "UpdateFood")]
internal class Patch_Player_UpdateFood
{
private struct FoodState
{
public Player.Food food;
public float originalBurnTime;
public FoodState(ref Player.Food _food, float _originalBurnTime)
{
this.food = _food;
this.originalBurnTime = _originalBurnTime;
}
}
static void Prefix(ref Player __instance, ref bool forceUpdate, ref List<Player.Food> ___m_foods, ref Dictionary<string, FoodState> __state)
{
if (forceUpdate || configFoodDurationMulitplier.Value == 0f)
return;
__state = new Dictionary<string, FoodState>(); ;
float skillLevel = __instance.GetSkillFactor((Skills.SkillType)COOKING_SKILL_ID);
float durationSkillModifier = 1f + (configFoodDurationMulitplier.Value * skillLevel);
for (int i = 0; i < ___m_foods.Count; i++)
{
Player.Food food = ___m_foods[i];
float newBurnTime = food.m_item.m_shared.m_foodBurnTime * durationSkillModifier;
__state.Add(food.m_name, new FoodState(ref food, food.m_item.m_shared.m_foodBurnTime));
___m_foods[i].m_item.m_shared.m_foodBurnTime = newBurnTime;
}
}
static void Postfix(ref List<Player.Food> ___m_foods, ref Dictionary<string, FoodState> __state)
{
if (configFoodDurationMulitplier.Value == 0f)
return;
if (__state == null || __state.Count == 0)
return;
List<string> eatenFoodNames = new List<string>();
for (int i = 0; i < ___m_foods.Count; i++)
{
eatenFoodNames.Add(___m_foods[i].m_name);
if (__state.ContainsKey(___m_foods[i].m_name))
___m_foods[i].m_item.m_shared.m_foodBurnTime = __state[___m_foods[i].m_name].originalBurnTime;
else
Warn($"Player.UpdateFood().Postfix :: __state did not contain {___m_foods[i].m_name}.");
}
// Reset burnTime of expired food
foreach (var stateItem in __state)
if (!eatenFoodNames.Contains(stateItem.Key))
stateItem.Value.food.m_item.m_shared.m_foodBurnTime = stateItem.Value.originalBurnTime;
}
}
#endregion
}
}