Jump to content

Recommended Posts

Posted

Hi All,

I am a little new to modding with Vintage Story but I am currently just trying to get it so that client side when the player presses CTRL they take just 1 damage. It's just so that I can explore how this all works.

I currently have all the key press working but when I call into `OnHurt` or `ReceiveDamage` animations and such happens but the health bar is not updated to reflect anything. Looking into these methods it doesn't look like it would do that, but the problem is I have no idea where this happens.

Is anyone able to help me out?

Regards,

Migo

Posted (edited)

I managed to figure it out on my own.

If anyone else is interested here is the code:

private ICoreClientAPI coreClientAPI;

public override void Start(ICoreAPI api)
{
    Mod.Logger.Notification("Hello from template mod: " + api.Side);
}

public override void StartServerSide(ICoreServerAPI api)
{
    api.Network.RegisterChannel("migodamage")
        .RegisterMessageType(typeof(DamagePlayerPacket));

    api.Network.RegisterChannel("migoheal")
        .RegisterMessageType(typeof(DamagePlayerPacket));

    api.Network.GetChannel("migodamage").SetMessageHandler<DamagePlayerPacket>(OnDamagePlayerPacket);
    api.Network.GetChannel("migoheal").SetMessageHandler<DamagePlayerPacket>(OnHealPlayerPacket);
}

public override void StartClientSide(ICoreClientAPI api)
{
    coreClientAPI = api;

    coreClientAPI.Network.RegisterChannel("migodamage")
        .RegisterMessageType(typeof(DamagePlayerPacket));

    coreClientAPI.Network.RegisterChannel("migoheal")
        .RegisterMessageType(typeof(DamagePlayerPacket));

    api.Event.KeyDown += Event_HealSelf;
}

private void Event_HealSelf(KeyEvent e)
{
    if (e.CtrlPressed)
    {
        var packet = new DamagePlayerPacket { DamageAmount = 4 };
        coreClientAPI.Network.GetChannel("migodamage").SendPacket(packet);
        Mod.Logger.Notification("Damage packet sent to server.");
    }
    else if (e.ShiftPressed)
    {
        var packet = new DamagePlayerPacket { DamageAmount = 4 };
        coreClientAPI.Network.GetChannel("migoheal").SendPacket(packet);
        Mod.Logger.Notification("Heal packet sent to server.");
    }
}

private void OnDamagePlayerPacket(IServerPlayer fromPlayer, DamagePlayerPacket packet)
{
    var entity = fromPlayer.Entity;

    var damageSource = new DamageSource
    {
        Source = EnumDamageSource.Internal,
        Type = EnumDamageType.Injury
    };

    entity.ReceiveDamage(damageSource, packet.DamageAmount);
    entity.OnHurt(damageSource, packet.DamageAmount);

    float currentHealth = entity.WatchedAttributes.GetOrAddTreeAttribute("health").GetFloat("currenthealth");

    Mod.Logger.Notification($"Damaged player {fromPlayer.PlayerName} for {packet.DamageAmount} HP. Health: {currentHealth}");
}

private void OnHealPlayerPacket(IServerPlayer fromPlayer, DamagePlayerPacket packet)
{
    var entity = fromPlayer.Entity;

    var damageSource = new DamageSource
    {
        Source = EnumDamageSource.Internal,
        Type = EnumDamageType.Heal
    };

    entity.ReceiveDamage(damageSource, packet.DamageAmount);
    entity.OnHurt(damageSource, packet.DamageAmount);

    float currentHealth = entity.WatchedAttributes.GetOrAddTreeAttribute("health").GetFloat("currenthealth");

    Mod.Logger.Notification($"Healed player {fromPlayer.PlayerName} for {packet.DamageAmount} HP. Health: {currentHealth}");
}

[ProtoContract]
public class DamagePlayerPacket
{
    [ProtoMember(1)]
    public int DamageAmount;
}

 

Edited by Migo
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.