i have set everything up and ive found the code i want to alter using ILSpy. first question, is it necessary to be able to reference the method i want to alter with HarmonyPatch? if so is there any way to set private methods to public scope? or can i just not mod methods that has a private accessor?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using HarmonyLib;
using UnityEngine;
namespace ValheimIronMod
{
[BepInPlugin("patrick.ValheimIronMod", "valheim iron mod", "1.0.0")]
[BepInProcess("valheim.exe")]
public class ValheimIronMod : BaseUnityPlugin
{
private readonly Harmony harmony = new Harmony("patrick.ValheimIronMod");
void Awake()
{
harmony.PatchAll();
}
[HarmonyPatch(typeof(MineRock5), nameof(MineRock5.DamageArea))]
class MineRock5_Patch
{
//static void Prefix(ref float ___m_jumpForce)
//{
// Debug.Log($"Jump Force: {___m_jumpForce}");
// ___m_jumpForce = 15;
// Debug.Log($"Modified Jump Force: {___m_jumpForce}");
//}
}
}
}
also, how do i alter methods in valheim? ive seen a couple of tutorial videos on youtube but they only cover changing a public variables value in the game not alter a method MineRock5 => DamageArea(int hitAreaIndex, HitData hit) this is the method as is vanilla that i want to alter
private bool DamageArea(int hitAreaIndex, HitData hit)
{
ZLog.Log((object)("hit mine rock " + hitAreaIndex));
HitArea hitArea = GetHitArea(hitAreaIndex);
if (hitArea == null)
{
ZLog.Log((object)("Missing hit area " + hitAreaIndex));
return false;
}
LoadHealth();
if (hitArea.m_health <= 0f)
{
ZLog.Log((object)"Already destroyed");
return false;
}
hit.ApplyResistance(m_damageModifiers, out var significantModifier);
float totalDamage = hit.GetTotalDamage();
if (hit.m_toolTier < m_minToolTier)
{
DamageText.instance.ShowText(DamageText.TextType.TooHard, hit.m_point, 0f);
return false;
}
DamageText.instance.ShowText(significantModifier, hit.m_point, totalDamage);
if (totalDamage <= 0f)
{
return false;
}
hitArea.m_health -= totalDamage;
SaveHealth();
m_hitEffect.Create(hit.m_point, Quaternion.identity);
Player closestPlayer = Player.GetClosestPlayer(hit.m_point, 10f);
if ((bool)closestPlayer)
{
closestPlayer.AddNoise(100f);
}
if (hitArea.m_health <= 0f)
{
m_nview.InvokeRPC(ZNetView.Everybody, "SetAreaHealth", hitAreaIndex, hitArea.m_health);
m_destroyedEffect.Create(hit.m_point, Quaternion.identity);
foreach (GameObject drop in m_dropItems.GetDropList())
{
Vector3 position = hit.m_point + Random.insideUnitSphere * 0.3f;
Object.Instantiate(drop, position, Quaternion.identity);
}
if (AllDestroyed())
{
m_nview.Destroy();
}
return true;
}
return false;
}also using this class in the alterations for the method above ObjectDB => GetItemPrefab(string name) inside that method i want to alter one of the loops a little
var objectDB = new ObjectDB(); // i want to add this line
var ironOre = objectDB.GetItemPrefab("IronScrap"); // i want to add this line
foreach (GameObject drop in m_dropItems.GetDropList())
{
Vector3 position = hit.m_point + Random.insideUnitSphere * 0.3f;
var percentageDiceRoll = Random.Range(0,100); // i want to add this line
if(percentageDiceRoll <= 20) Object.Instantiate(ironOre, position, Quaternion.identity); // i want to add this line
Object.Instantiate(drop, position, Quaternion.identity);
}but how can i do this?