commit c50584b6b1b24b327d670bb5f7f7021f68bbdc84 Author: LabodiDavid Date: Sun Aug 23 17:38:34 2026 +0200 Initial release - v1.0.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9aa4887 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +.vs/ +bin/ +obj/ +dist/ + +*.user +*.suo +*.userosscache +*.sln.docstates + +[Bb]uild/ +[Dd]ebug/ +[Rr]elease/ + +*.dll +*.pdb + +packages/ + +.idea/ \ No newline at end of file diff --git a/AutoRefillFires.csproj b/AutoRefillFires.csproj new file mode 100644 index 0000000..7601e28 --- /dev/null +++ b/AutoRefillFires.csproj @@ -0,0 +1,108 @@ + + + + net472 + AutoRefillFires + AutoRefillFires + latest + + 1.0.0 + + D:\SteamLibrary\steamapps\common\Valheim + Default + + $(APPDATA)\r2modmanPlus-local\Valheim\profiles\$(R2ProfileName) + $(R2BasePath)\BepInEx + $(BepInExPath)\plugins\AutoRefillFires + + + + + $(BepInExPath)\core\BepInEx.dll + false + + + + $(BepInExPath)\core\0Harmony.dll + false + + + + $(ValheimPath)\valheim_Data\Managed\assembly_valheim.dll + false + + + + $(ValheimPath)\valheim_Data\Managed\UnityEngine.dll + false + + + + $(ValheimPath)\valheim_Data\Managed\UnityEngine.CoreModule.dll + false + + + + + + <_Parameter1>PluginVersion + <_Parameter2>$(PluginVersion) + + + + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)dist\AutoRefillFires + $(PackageRoot)\plugins\AutoRefillFires + $(SolutionDir)dist\AutoRefillFires-$(PluginVersion).zip + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AutoRefillFires.slnx b/AutoRefillFires.slnx new file mode 100644 index 0000000..fa0e537 --- /dev/null +++ b/AutoRefillFires.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Plugin.cs b/Plugin.cs new file mode 100644 index 0000000..ad5073f --- /dev/null +++ b/Plugin.cs @@ -0,0 +1,384 @@ +using BepInEx; +using BepInEx.Configuration; +using UnityEngine; + +namespace AutoRefillFires +{ + + [BepInPlugin( + Plugin.PluginGuid, + Plugin.PluginName, + Plugin.PluginVersion + )] + public class Plugin : BaseUnityPlugin + { + public const string PluginGuid = "simplifydave.autorefillfires"; + public const string PluginName = "Auto Refill Fires"; + public const string PluginVersion = VersionInfo.Version; + + private ConfigEntry _radius; + private ConfigEntry _checkInterval; + private ConfigEntry _refillBelowPercent; + + private ConfigEntry _useNearbyContainers; + private ConfigEntry _containerRadius; + + private ConfigEntry _refillToMax; + private ConfigEntry _refillAmount; + private ConfigEntry _fillCampfires; + private ConfigEntry _fillHearths; + private ConfigEntry _fillStandingTorches; + private ConfigEntry _fillWallTorches; + private ConfigEntry _fillBraziers; + private ConfigEntry _fillOtherFireplaces; + + private float _nextCheckTime; + + private void Awake() + { + _radius = Config.Bind( + "General", + "Radius", + 20f, + "Refill fireplaces within this radius around the player." + ); + + _checkInterval = Config.Bind( + "General", + "CheckInterval", + 3f, + "How often fireplaces are checked, in seconds." + ); + + _refillBelowPercent = Config.Bind( + "General", + "RefillBelowPercent", + 0.75f, + "Refill when fuel drops below this percentage." + ); + + _refillToMax = Config.Bind( + "General", + "RefillToMax", + true, + "If true, refill fireplaces to maximum fuel. If false, add only RefillAmount fuel." + ); + + _refillAmount = Config.Bind( + "General", + "RefillAmount", + 5, + "Amount of fuel to add when RefillToMax is false." + ); + + _useNearbyContainers = Config.Bind( + "Containers", + "UseNearbyContainers", + false, + "Allow plugin to take fuel from nearby containers." + ); + + _containerRadius = Config.Bind( + "Containers", + "ContainerRadius", + 10f, + "Maximum distance from the fireplace to search for containers." + ); + + _fillCampfires = Config.Bind( + "Fireplace Types", + "FillCampfires", + true, + "Automatically refill campfires." + ); + + _fillHearths = Config.Bind( + "Fireplace Types", + "FillHearths", + true, + "Automatically refill hearths." + ); + + _fillStandingTorches = Config.Bind( + "Fireplace Types", + "FillStandingTorches", + true, + "Automatically refill all standing torches, including colored variants." + ); + + _fillWallTorches = Config.Bind( + "Fireplace Types", + "FillWallTorches", + true, + "Automatically refill all wall torches, including colored variants." + ); + + _fillBraziers = Config.Bind( + "Fireplace Types", + "FillBraziers", + true, + "Automatically refill braziers." + ); + + _fillOtherFireplaces = Config.Bind( + "Fireplace Types", + "FillOtherFireplaces", + true, + "Automatically refill unknown or modded Fireplace-based objects." + ); + + Logger.LogInfo("Auto Refill Fires loaded!"); + } + + private void Update() + { + if (Time.time < _nextCheckTime) + return; + + _nextCheckTime = Time.time + _checkInterval.Value; + + Player player = Player.m_localPlayer; + + if (player == null) + return; + + RefillNearbyFireplaces(player); + } + + private bool ShouldRefillFireplace(Fireplace fireplace) + { + string objectName = fireplace.gameObject.name + .Replace("(Clone)", "") + .ToLowerInvariant(); + + // Campfire + if (objectName.Contains("firepit")) + return _fillCampfires.Value; + + // Hearth + if (objectName.Contains("hearth")) + return _fillHearths.Value; + + // All standing / ground torches, including colored variants + if (objectName.Contains("groundtorch")) + return _fillStandingTorches.Value; + + // All wall torches, including colored variants + if (objectName.Contains("walltorch")) + return _fillWallTorches.Value; + + // Braziers + if (objectName.Contains("brazier")) + return _fillBraziers.Value; + + // Unknown / modded Fireplace-based objects + return _fillOtherFireplaces.Value; + } + + private void RefillNearbyFireplaces(Player player) + { + Fireplace[] fireplaces = + Object.FindObjectsByType( + FindObjectsInactive.Exclude, + FindObjectsSortMode.None + ); + + foreach (Fireplace fireplace in fireplaces) + { + if (fireplace == null) + continue; + + float distance = Vector3.Distance( + player.transform.position, + fireplace.transform.position + ); + + if (distance > _radius.Value) + continue; + + TryRefillFireplace(player, fireplace); + } + } + + private void TryRefillFireplace(Player player, Fireplace fireplace) + { + if (!ShouldRefillFireplace(fireplace)) + return; + ZNetView nview = fireplace.GetComponent(); + + if (nview == null) + nview = fireplace.GetComponentInParent(); + + if (nview == null) + return; + + if (!nview.IsValid()) + return; + + ZDO zdo = nview.GetZDO(); + + if (zdo == null) + return; + + if (fireplace.m_fuelItem == null) + return; + + float currentFuel = zdo.GetFloat(ZDOVars.s_fuel, 0f); + float maxFuel = fireplace.m_maxFuel; + + if (maxFuel <= 0f) + return; + + float fuelPercent = currentFuel / maxFuel; + + if (fuelPercent >= _refillBelowPercent.Value) + return; + + string fuelName = + fireplace.m_fuelItem.m_itemData.m_shared.m_name; + + int missingFuel = + Mathf.CeilToInt(maxFuel - currentFuel); + + if (missingFuel <= 0) + return; + + int requestedFuel; + + if (_refillToMax.Value) + { + requestedFuel = missingFuel; + } + else + { + requestedFuel = Mathf.Min( + Mathf.Max(_refillAmount.Value, 0), + missingFuel + ); + } + + if (requestedFuel <= 0) + return; + + int fuelAdded = 0; + + for (int i = 0; i < requestedFuel; i++) + { + if (!TryConsumeFuel( + player, + fireplace, + fuelName + )) + { + break; + } + + nview.InvokeRPC( + "RPC_AddFuel", + new object[0] + ); + + fuelAdded++; + } + + if (fuelAdded > 0) + { + Logger.LogInfo( + $"Refilled {fireplace.name}: " + + $"{currentFuel:0.0}/{maxFuel:0.0}, " + + $"fuel={fuelName}, " + + $"added={fuelAdded}, " + + $"mode={(_refillToMax.Value ? "max" : "fixed")}" + ); + } + } + + private bool TryConsumeFuel(Player player, Fireplace fireplace, string fuelName) + { + Inventory playerInventory = player.GetInventory(); + + if (playerInventory.CountItems(fuelName) > 0) + { + playerInventory.RemoveItem(fuelName, 1); + + Logger.LogDebug( + $"Fuel {fuelName} taken from player inventory." + ); + + return true; + } + + if (!_useNearbyContainers.Value) + return false; + + return TryConsumeFuelFromNearbyContainer( + fireplace, + fuelName + ); + } + + private bool TryConsumeFuelFromNearbyContainer( + Fireplace fireplace, + string fuelName) + { + Container[] containers = + Object.FindObjectsByType( + FindObjectsInactive.Exclude, + FindObjectsSortMode.None + ); + + Container closestContainer = null; + float closestDistance = float.MaxValue; + + foreach (Container container in containers) + { + if (container == null) + continue; + + float distance = Vector3.Distance( + fireplace.transform.position, + container.transform.position + ); + + if (distance > _containerRadius.Value) + continue; + + Inventory inventory = container.GetInventory(); + + if (inventory == null) + continue; + + if (inventory.CountItems(fuelName) <= 0) + continue; + + if (distance < closestDistance) + { + closestDistance = distance; + closestContainer = container; + } + } + + if (closestContainer == null) + return false; + + Inventory chestInventory = + closestContainer.GetInventory(); + + if (chestInventory == null) + return false; + + if (chestInventory.CountItems(fuelName) <= 0) + return false; + + chestInventory.RemoveItem(fuelName, 1); + + Logger.LogInfo( + $"Fuel {fuelName} taken from container " + + $"{closestContainer.name} " + + $"({closestDistance:0.0}m)" + ); + + return true; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d08be6a --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Auto Refill Fires + +A lightweight BepInEx mod for Valheim that automatically refills nearby fireplaces and torches. + +## Features + +- Automatically refills nearby campfires +- Automatically refills hearths +- Automatically refills standing torches, including colored variants +- Automatically refills wall torches +- Automatically refills braziers +- Uses the correct fuel type automatically +- Can consume fuel from the player's inventory +- Optional support for nearby containers +- Configurable refill radius +- Configurable refill threshold +- Refill to maximum or by a fixed amount +- Individual enable/disable options for fireplace types + +## Requirements + +- Valheim +- BepInExPack Valheim + +## Configuration + +The configuration file is generated after launching the game once with the mod installed. + +`BepInEx/config/simplifydave.autorefillfires.cfg` + +## Installation + +Install using r2modman / Thunderstore Mod Manager, or manually place `AutoRefillFires.dll` inside: + +`BepInEx/plugins/AutoRefillFires/` + +## License + +MIT \ No newline at end of file diff --git a/Thunderstore/CHANGELOG.md b/Thunderstore/CHANGELOG.md new file mode 100644 index 0000000..337eeda --- /dev/null +++ b/Thunderstore/CHANGELOG.md @@ -0,0 +1,20 @@ +# Changelog + +## 1.0.0 + +Initial release. + +### Features + +- Automatic campfire refilling +- Automatic hearth refilling +- Automatic standing torch refilling +- Automatic wall torch refilling +- Automatic brazier refilling +- Supports colored torch variants +- Automatically detects the required fuel type +- Optional nearby container fuel usage +- Configurable refill radius +- Configurable refill threshold +- Refill-to-max or fixed refill amount +- Individual fireplace type toggles \ No newline at end of file diff --git a/Thunderstore/README.md b/Thunderstore/README.md new file mode 100644 index 0000000..d08be6a --- /dev/null +++ b/Thunderstore/README.md @@ -0,0 +1,39 @@ +# Auto Refill Fires + +A lightweight BepInEx mod for Valheim that automatically refills nearby fireplaces and torches. + +## Features + +- Automatically refills nearby campfires +- Automatically refills hearths +- Automatically refills standing torches, including colored variants +- Automatically refills wall torches +- Automatically refills braziers +- Uses the correct fuel type automatically +- Can consume fuel from the player's inventory +- Optional support for nearby containers +- Configurable refill radius +- Configurable refill threshold +- Refill to maximum or by a fixed amount +- Individual enable/disable options for fireplace types + +## Requirements + +- Valheim +- BepInExPack Valheim + +## Configuration + +The configuration file is generated after launching the game once with the mod installed. + +`BepInEx/config/simplifydave.autorefillfires.cfg` + +## Installation + +Install using r2modman / Thunderstore Mod Manager, or manually place `AutoRefillFires.dll` inside: + +`BepInEx/plugins/AutoRefillFires/` + +## License + +MIT \ No newline at end of file diff --git a/Thunderstore/icon.png b/Thunderstore/icon.png new file mode 100644 index 0000000..180a760 Binary files /dev/null and b/Thunderstore/icon.png differ diff --git a/Thunderstore/manifest.json b/Thunderstore/manifest.json new file mode 100644 index 0000000..6f3bd79 --- /dev/null +++ b/Thunderstore/manifest.json @@ -0,0 +1,9 @@ +{ + "name": "AutoRefillFires", + "version_number": "__VERSION__", + "website_url": "https://github.com/LabodiDavid/AutoRefillFires", + "description": "Automatically refills nearby campfires, hearths and torches using fuel from the player inventory or optionally nearby containers.", + "dependencies": [ + "denikson-BepInExPack_Valheim-5.4.2333" + ] +} \ No newline at end of file