RustTip

OnPlayerAttack

OxidePlayer
object OnPlayerAttack(BaseMelee baseMelee, HitInfo info)
object OnPlayerAttack(BasePlayer basePlayer, HitInfo hitInfo)
Method changed — check — the game method differs from the one the hook is recorded for in Oxide. The hook is still called, but its place may have shifted — worth checking after an update.

Parameters

baseMelee
BaseMelee
info
HitInfo

Return value

objectReturn a value of the method's result type and the game returns it immediately; null continues as usual.

Example

object — you can affect the game: return a value to change or cancel the action.

private object OnPlayerAttack(BaseMelee baseMelee, HitInfo info)
{
    Puts("OnPlayerAttack");

    // change nothing — the game continues as usual
    return null;

    // to affect the game:
    // return null; // null — the game continues as usual
}

Works in both Oxide and Carbon: Carbon loads Oxide plugins as is.

Injection point 1

Rust.opj (Oxide)
void BaseMelee.DoAttackShared(HitInfo)

Passes: BaseMelee baseMelee, HitInfo info

WorksReturnBehavior: ExitWhenValidTypeArgumentBehavior: UseArgumentString (this, a0)Assembly-CSharp.dll

Where it is called (this branch's game code)

public virtual void DoAttackShared(HitInfo info)
{
	if (Interface.CallHook("OnPlayerAttack", this, info) != null)
	{
		return;
	}
	GetAttackStats(info);
	if (info.HitEntity != null)
	{
		using (TimeWarning.New("OnAttacked", 50))
		{
// …

Injection point 2

Rust.opj (Oxide)
void BasePlayer.OnProjectileAttack(BaseEntity.RPCMessage)

Passes: BasePlayer basePlayer, HitInfo hitInfo

Method changed — checkReturnBehavior: ExitWhenValidTypeArgumentBehavior: UseArgumentString (this, l0.hitInfo)Assembly-CSharp.dll

Where it is called (this branch's game code)

// …
}
else
{
	float num26 = hitEntity.PenetrationResistance(hitInfo) / hitInfo.ProjectilePrefab.penetrationPower;
	firedProjectile.integrity = Mathf.Clamp01(firedProjectile.integrity - num26);
}
if (flag6)
{
	if (Interface.CallHook("OnPlayerAttack", this, local0.hitInfo) != null)
	{
		return;
	}
	stats.Add(firedProjectile.itemMod.category + "_hit_" + hitEntity.Categorize(), 1);
	if (((firedProjectile.weaponPrefab is Food && firedProjectile.weaponPrefab.ShortPrefabName.Contains("pie")) & flag) && hitPlayer != this && !hitPlayer.IsNpc)
	{
		GiveAchievement("PIE_HIT_PLAYER");
	}
// …

Sources: Rust.opj from OxideMod/Oxide.Rust (MIT) and hook metadata of the Carbon documentation (MIT). Method code is decompiled from this branch's server.