From 6cdc527dbab100eaf42f6b0cd30f1dd3543878da Mon Sep 17 00:00:00 2001 From: mikx Date: Thu, 29 Jan 2026 22:42:34 -0500 Subject: [PATCH] (1.3.0) Individual Item Weight > 'BepInEx\config\mxvalheim.custom_weights.json' --- MxValheim/MxValheim.cs | 46 +++++++++++++++++++++++++++++++++++++- MxValheim/MxValheim.csproj | 8 ++++++- MxValheim/Patch/Items.cs | 33 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 MxValheim/Patch/Items.cs diff --git a/MxValheim/MxValheim.cs b/MxValheim/MxValheim.cs index b7cc7c6..2ae026a 100644 --- a/MxValheim/MxValheim.cs +++ b/MxValheim/MxValheim.cs @@ -1,15 +1,21 @@ using BepInEx; using BepInEx.Configuration; using HarmonyLib; +using System; using System.Collections.Generic; +using System.IO; +using System.Xml; using UnityEngine; +using Newtonsoft.Json; [BepInPlugin(ModGUID, ModName, ModVersion)] public class MxValheimMod : BaseUnityPlugin { + public static MxValheimMod Instance; // Singleton reference + private const string ModGUID = "ovh.mxdev.mxvalheim"; private const string ModName = "MxValheim"; - private const string ModVersion = "1.2.1"; + private const string ModVersion = "1.3.0"; public static ConfigEntry Config_Locked; public static ConfigEntry Config_OreMultiplier; @@ -18,15 +24,53 @@ public class MxValheimMod : BaseUnityPlugin public static ConfigEntry Config_rainDamage; public static ConfigEntry Config_boatSpeed; + private static string WeightConfigPath => Path.Combine(Paths.ConfigPath, "mxvalheim.custom_weights.json"); + public static Dictionary WeightSettings = new Dictionary(); + void Awake() { + Instance = this; + Config_OreMultiplier = Config.Bind("General","OreMultiplier",3,"How many items should drop for every 1 ore/scrap found."); Config_rangeMultiplier = Config.Bind("General", "CraftingRangeMultiplier",2.0f,"Multiplier for the workbench build/crafting range. Default is 2x."); Config_bowDrawSpeedBonusPerLevel = Config.Bind("General", "BowDrawSpeedBonusPercentPerLevel", 1.0f, "Shorten the bow draw speed by this percent for every bow upgrade level."); Config_rainDamage = Config.Bind("General", "RainDamage", true, "Set to true to stop rain damage, false to return to vanilla behavior."); Config_boatSpeed = Config.Bind("General", "BoatSpeedMultiplier", 2.0f, "Your boat/raft will move without wind at a speed multiplied by this value."); + LoadJsonConfig(); + Harmony harmony = new Harmony(ModGUID); harmony.PatchAll(); } + + private void Update() + { + // Only check while in the game world to save resources + if (ObjectDB.instance == null) return; + + + } + + private bool LoadJsonConfig() + { + try + { + if (!File.Exists(WeightConfigPath)) + { + WeightSettings = new Dictionary { { "Wood", 1.0f }, { "Stone", 1.0f } }; + File.WriteAllText(WeightConfigPath, JsonConvert.SerializeObject(WeightSettings, Newtonsoft.Json.Formatting.Indented)); + return true; + } + + string json = File.ReadAllText(WeightConfigPath); + WeightSettings = JsonConvert.DeserializeObject>(json); + Logger.LogInfo($"Successfully parsed {WeightSettings.Count} items."); + return true; + } + catch (Exception ex) + { + Logger.LogWarning($"Could not read JSON (might be busy): {ex.Message}"); + return false; + } + } } \ No newline at end of file diff --git a/MxValheim/MxValheim.csproj b/MxValheim/MxValheim.csproj index e716e67..05b04e3 100644 --- a/MxValheim/MxValheim.csproj +++ b/MxValheim/MxValheim.csproj @@ -44,6 +44,10 @@ E:\SteamLibrary\steamapps\common\Valheim\BepInEx\core\BepInEx.dll + + + E:\SteamLibrary\steamapps\common\Valheim\BepInEx\plugins\Newtonsoft.Json.dll + @@ -52,6 +56,7 @@ + E:\SteamLibrary\steamapps\common\Valheim\Valheim_Data\Managed\UnityEngine.dll @@ -67,12 +72,13 @@ + - + \ No newline at end of file diff --git a/MxValheim/Patch/Items.cs b/MxValheim/Patch/Items.cs new file mode 100644 index 0000000..6958bfa --- /dev/null +++ b/MxValheim/Patch/Items.cs @@ -0,0 +1,33 @@ +using HarmonyLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace MxValheim.Patch +{ + internal class Items + { + [HarmonyPatch(typeof(ObjectDB), nameof(ObjectDB.Awake))] + public static class ObjectDB_Patch + { + public static void Postfix() + { + if (ObjectDB.instance == null) return; + + int count = 0; + foreach (GameObject item in ObjectDB.instance.m_items) + { + ItemDrop itemDrop = item.GetComponent(); + if (itemDrop != null && MxValheimMod.WeightSettings.TryGetValue(item.name, out float newWeight)) + { + itemDrop.m_itemData.m_shared.m_weight = newWeight; + count++; + } + } + } + } + } +}