Skip to content

Commit

Permalink
Merge pull request #169 from jpw1991/124-repair-pylon-behaviour
Browse files Browse the repository at this point in the history
2.5.10: improve repair pylon behaviour; upgrade to jotunn 1.11.2
  • Loading branch information
jpw1991 authored Mar 30, 2023
2 parents 9769b7a + 85fa3f6 commit a952a71
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ChebsNecromancy/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class BasePlugin : BaseUnityPlugin
{
public const string PluginGuid = "com.chebgonaz.ChebsNecromancy";
public const string PluginName = "ChebsNecromancy";
public const string PluginVersion = "2.5.9";
public const string PluginVersion = "2.5.11";
private const string ConfigFileName = PluginGuid + ".cfg";
private static readonly string ConfigFileFullPath = Path.Combine(Paths.ConfigPath, ConfigFileName);

Expand Down
8 changes: 4 additions & 4 deletions ChebsNecromancy/ChebsNecromancy.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\JotunnLib.2.11.0\build\JotunnLib.props" Condition="Exists('..\packages\JotunnLib.2.11.0\build\JotunnLib.props')" />
<Import Project="..\packages\JotunnLib.2.11.2\build\JotunnLib.props" Condition="Exists('..\packages\JotunnLib.2.11.2\build\JotunnLib.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -58,8 +58,8 @@
<Reference Include="assembly_valheim_">
<HintPath>libs\assembly_valheim_publicized.dll</HintPath>
</Reference>
<Reference Include="Jotunn, Version=2.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\JotunnLib.2.11.0\lib\net462\Jotunn.dll</HintPath>
<Reference Include="Jotunn, Version=2.11.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\JotunnLib.2.11.2\lib\net462\Jotunn.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.dll</HintPath>
Expand Down Expand Up @@ -257,6 +257,6 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\JotunnLib.2.11.0\build\JotunnLib.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\JotunnLib.2.11.0\build\JotunnLib.props'))" />
<Error Condition="!Exists('..\packages\JotunnLib.2.11.2\build\JotunnLib.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\JotunnLib.2.11.2\build\JotunnLib.props'))" />
</Target>
</Project>
5 changes: 3 additions & 2 deletions ChebsNecromancy/CustomPrefabs/OrbOfBeckoningProjectile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ private void Start()
if (!TryGetComponent(out Projectile projectile)
|| !projectile.m_owner.TryGetComponent(out player))
{
Logger.LogError($"{name}'s projectile.owner has no player component! NPC using this weapon?");
// In this case, the item is likely being wielded by an NPC. So return prematurely and don't worry
// about the code.
return;
}

// make all minions that belong to the player follow the ball
List<Character> allCharacters = new();
Character.GetCharactersInRange(player.transform.position, SkeletonWand.SkeletonSetFollowRange.Value, allCharacters);
Expand Down
16 changes: 15 additions & 1 deletion ChebsNecromancy/Minions/NeckroGathererMinion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ private void Update()
{
if (ZNet.instance == null
|| !(Time.time > lastUpdate)) return;

// Some users get null object exceptions inside the neckro's Update method. IDK why exactly that would be.
// Mod conflicts? Don't know. So to mitigate this, just be extra careful about nulls and abort if anything
// is null.
if (container == null && !TryGetComponent(out container))
{
Logger.LogError("Neckro container is null and cannot be retrieved!");
return;
}
if (_monsterAI == null && !TryGetComponent(out _monsterAI))
{
Logger.LogError("Neckro MonsterAI is null and cannot be retrieved!");
return;
}

bool canPick = LookForNearbyItems();
if (canPick)
Expand Down Expand Up @@ -140,7 +154,7 @@ private void Update()
{
DepositItems();
}
}
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions ChebsNecromancy/Package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ You can find the github [here](https://github.com/jpw1991/chebs-necromancy).

Date | Version | Notes
--- | --- | ---
28/03/2023 | 2.5.11 | don't log error if NPC is using the Orb of Beckoning
28/03/2023 | 2.5.10 | improve repair pylon behaviour; upgrade to jotunn 1.11.2; make neckro update more null resistant
27/03/2023 | 2.5.9 | fix a null object exception for structure friendly-fire code; ships ignore minion impact damage; fix problem of duplicate resource drops on death if crate is enabled
25/03/2023 | 2.5.8 | miners pop an entire rock/node in one whack because completely mining the fragments seems impossible
25/03/2023 | 2.5.7 | permit multiple miners to whack same rock; overhaul rock whacking logic for lumberjacks and miners; remove collision on draugr heads
Expand Down
4 changes: 2 additions & 2 deletions ChebsNecromancy/Package/manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "ChebsNecromancy",
"description": "Cheb's Necromancy adds Necromancy to Valheim via craftable wands and structures. Minions will follow you, guard your base, and perform menial tasks.",
"version_number": "2.5.9",
"version_number": "2.5.11",
"website_url": "https://github.com/jpw1991/chebs-necromancy",
"dependencies": [
"ValheimModding-Jotunn-2.11.0"
"ValheimModding-Jotunn-2.11.2"
]
}
73 changes: 46 additions & 27 deletions ChebsNecromancy/Structures/RepairPylon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx.Configuration;
using ChebsNecromancy.Common;
Expand All @@ -12,7 +13,7 @@ internal class RepairPylon : Structure
public static ConfigEntry<float> SightRadius;
public static ConfigEntry<float> RepairUpdateInterval, FuelConsumedPerPointOfDamage, RepairWoodWhen, RepairOtherWhen;
public static ConfigEntry<int> RepairContainerWidth, RepairContainerHeight;
public static ConfigEntry<string> Fuels;
public static ConfigEntry<string> Fuels, AlwaysRepair;

private readonly int pieceMask = LayerMask.GetMask("piece");

Expand Down Expand Up @@ -73,6 +74,10 @@ public static void CreateConfigs(BasePlugin plugin)
"How low a non-wood structure's health must drop in order for it to be repaired. Set to 0 to repair regardless of damage.",
null, true);

AlwaysRepair = plugin.ModConfig(ChebsRecipeConfig.ObjectName, "AlwaysRepair", "piece_sharpstakes,piece_dvergr_sharpstakes",
"These prefabs are always repaired no matter their damage and ignore the RepairWoodWhen/RepairOtherWhen thresholds. This is good for defensive things like stakes which should always be kept at maximum health. Please use a comma-delimited list of prefab names.",
null, true);

Fuels = plugin.ModConfig(ChebsRecipeConfig.ObjectName, "Fuels", "Resin,GreydwarfEye,Pukeberries",
"The items that are consumed as fuel when repairing. Please use a comma-delimited list of prefab names.",
null, true);
Expand Down Expand Up @@ -110,44 +115,53 @@ IEnumerator LookForPieces()
if (RepairDamage(wearNTear))
{
var player = Player.m_localPlayer;

// show repair text if player is near the pylon
if (Vector3.Distance(player.transform.position, transform.position) < 5)
if (player != null)
{
Chat.instance.SetNpcText(gameObject, Vector3.up, 5f, 2f, "",
$"Repairing {wearNTear.gameObject.name} ({(healthPercent*100).ToString("0.##")}%)...", false);
}
// show repair text if player is near the pylon
if (Vector3.Distance(player.transform.position, transform.position) < 5)
{
Chat.instance.SetNpcText(gameObject, Vector3.up, 5f, 2f, "",
$"Repairing {wearNTear.gameObject.name} ({(healthPercent*100).ToString("0.##")}%)...", false);
}

// make the hammer sound and puff of smoke etc. if the player is nearby the thing being repaired
var distance = Vector3.Distance(player.transform.position, wearNTear.transform.position);
if (distance < 20)
{
var localPiece = wearNTear.m_piece;
if (localPiece is not null)
// make the hammer sound and puff of smoke etc. if the player is nearby the thing being repaired
var distance = Vector3.Distance(player.transform.position, wearNTear.transform.position);
if (distance < 20)
{
var localPieceTransform = localPiece.transform;
localPiece.m_placeEffect.Create(localPieceTransform.position,
localPieceTransform.rotation);
var localPiece = wearNTear.m_piece;
if (localPiece is not null)
{
var localPieceTransform = localPiece.transform;
localPiece.m_placeEffect.Create(localPieceTransform.position,
localPieceTransform.rotation);
}
}
}

yield return new WaitForSeconds(1);
}

yield return new WaitForSeconds(1);
}
}
}

private bool RepairDamage(WearNTear wearNTear)
{
if (wearNTear.GetHealthPercentage() >= 1f) return false;
if (wearNTear.m_materialType is WearNTear.MaterialType.Wood or WearNTear.MaterialType.HardWood)
{
if (RepairWoodWhen.Value != 0.0f && wearNTear.GetHealthPercentage() >= RepairWoodWhen.Value) return false;
}
else

var alwaysRepairNames = AlwaysRepair.Value.Split(',');
// wearNTear.name could be "piece_sharpstakes(Clone)" and alwaysRepairName could be "piece_sharpstakes"
var alwaysRepair = alwaysRepairNames.Any(alwaysRepairName => wearNTear.name.Contains(alwaysRepairName));

if (!alwaysRepair)
{
if (RepairOtherWhen.Value != 0.0f && wearNTear.GetHealthPercentage() >= RepairOtherWhen.Value) return false;
if (wearNTear.m_materialType is WearNTear.MaterialType.Wood or WearNTear.MaterialType.HardWood)
{
if (RepairWoodWhen.Value != 0.0f && wearNTear.GetHealthPercentage() >= RepairWoodWhen.Value) return false;
}
else
{
if (RepairOtherWhen.Value != 0.0f && wearNTear.GetHealthPercentage() >= RepairOtherWhen.Value) return false;
}
}

var consumedFuel = ConsumeFuel(wearNTear);
Expand Down Expand Up @@ -238,13 +252,18 @@ private List<WearNTear> PiecesInRange()
if (nearbyColliders.Length < 1) return null;

var result = new List<WearNTear>();
//var repairPylonSkipped = new List<string>();
foreach (var nearbyCollider in nearbyColliders)
{
var wearAndTear = nearbyCollider.GetComponentInParent<WearNTear>();
if (wearAndTear == null) continue;
if (!wearAndTear.m_nview.IsValid()) continue;
if (wearAndTear == null || !wearAndTear.m_nview.IsValid())
{
//repairPylonSkipped.Add($"{nearbyCollider.name}");
continue;
}
result.Add(wearAndTear);
}
//Jotunn.Logger.LogInfo($"Skipped {string.Join(",", repairPylonSkipped)}");

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion ChebsNecromancy/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HarmonyX" version="2.10.1" targetFramework="net48" />
<package id="JotunnLib" version="2.11.0" targetFramework="net48" />
<package id="JotunnLib" version="2.11.2" targetFramework="net48" />
<package id="Mono.Cecil" version="0.11.4" targetFramework="net462" />
<package id="MonoMod" version="22.7.31.1" targetFramework="net48" />
<package id="MonoMod.RuntimeDetour" version="22.7.31.1" targetFramework="net48" />
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ You can find the github [here](https://github.com/jpw1991/chebs-necromancy).

Date | Version | Notes
--- | --- | ---
28/03/2023 | 2.5.11 | don't log error if NPC is using the Orb of Beckoning
28/03/2023 | 2.5.10 | improve repair pylon behaviour; upgrade to jotunn 1.11.2; make neckro update more null resistant
27/03/2023 | 2.5.9 | fix a null object exception for structure friendly-fire code; ships ignore minion impact damage; fix problem of duplicate resource drops on death if crate is enabled
25/03/2023 | 2.5.8 | miners pop an entire rock/node in one whack because completely mining the fragments seems impossible
25/03/2023 | 2.5.7 | permit multiple miners to whack same rock; overhaul rock whacking logic for lumberjacks and miners; remove collision on draugr heads
Expand Down

0 comments on commit a952a71

Please sign in to comment.