FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Event WeaponDamage on Sync Usage Alltime Cancel

BluePeer opened this issue · comments

The reproduction code below requires 2 people.
Everyone gets a server-side weapon + ammunition.
3 events are registered.
1x Weapon Damage event as sync with WeaponDamageResponse.
1x Weapon Damage Event as Sync IScript with WeaponDamageResponse.
1x Weapon Damage Event as Async IScript Task.

all 3 events should go through Normal and allow the damage to remain unchanged.

If you run the Scripttest.WeaponDamage function individually as Alt.OnWeaponDamage +=.

the log comes 1x, the damage is blocked.

You run the IScript with Scripttest.WeaponDamage individually.

The function is triggered and blocked twice (2x log).

If you run the Async Task variant WeaponDamageb individually.
it work normally. 1x execution no blockage.

using AltV.Net;
using AltV.Net.Async;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Enums;

namespace Test
{
    public class Test : AsyncResource
    {
        public override void OnStart()
        {
            Alt.OnWeaponDamage += Scripttest.WeaponDamage;
            foreach (var p in Alt.GetAllPlayers())
            {
                p.Model = (uint)PedModel.FreemodeMale01;
                p.Spawn(new Position(0, 0, 73));
                p.GiveWeapon(WeaponModel.Pistol, 0, false);
                var winfo = Alt.Core.GetWeaponModelInfo((uint)WeaponModel.Pistol);
                p.SetAmmo(winfo.Value.AmmoTypeHash, 30);
            }
            Alt.Log("server running");
        }

        public override void OnStop()
        {
            Alt.Log("server stopped");
        }
    }
    public class Scripttest : IScript
    {
        [ScriptEvent(ScriptEventType.WeaponDamage)]
        public static WeaponDamageResponse WeaponDamage(IPlayer p, IEntity Target, uint weapon, ushort damage, Position shortOffset, BodyPart part)
        {
            Alt.Log(p.Id + " Sync " + damage + " " + ((IPlayer)Target).Health);
            return new WeaponDamageResponse(true, damage);
        }
        [AsyncScriptEvent(ScriptEventType.WeaponDamage)]
        public async Task WeaponDamageb(IPlayer p, IEntity Target, uint weapon, ushort damage, Position shortOffset, BodyPart part)
        {
            Alt.Log(p.Id + " ASync " + damage + " " + ((IPlayer)Target).Health);
        }
        [ScriptEvent(ScriptEventType.PlayerConnect)]
        public void PlayerConnect(IPlayer p, string reason)
        {
            p.Model = (uint)PedModel.FreemodeMale01;
            p.Spawn(new Position(0,0,73));
            p.GiveWeapon(WeaponModel.Pistol,0,false);
            var winfo = Alt.Core.GetWeaponModelInfo((uint)WeaponModel.Pistol);
            p.SetAmmo(winfo.Value.AmmoTypeHash,30);
        }
        [AsyncScriptEvent(ScriptEventType.PlayerDead)]
        public async Task PlayerDead(IPlayer p, IEntity killer, uint weapon)
        {
            await Task.Delay(3000);
            p.Health = p.MaxHealth;
            p.Spawn(new Position(0, 0, 73));
            p.RemoveAllWeapons(true);
            p.GiveWeapon(WeaponModel.Pistol, 0, false);
            var winfo = Alt.Core.GetWeaponModelInfo((uint)WeaponModel.Pistol);
            p.SetAmmo(winfo.Value.AmmoTypeHash, 30);
        }


        
    }
}

pls check on latest dev its still exists

and pls dont do return new WeaponDamageResponse(true, damage);

do return false or return damage

and pls dont do return new WeaponDamageResponse(true, damage);

do return false or return damage

ill check later this day
but if i only use the damage
it will end in the same

public static implicit operator WeaponDamageResponse(uint val)
{
return new WeaponDamageResponse(notCancel: true, val);
}

and pls dont do return new WeaponDamageResponse(true, damage);
do return false or return damage

ill check later this day but if i only use the damage it will end in the same

public static implicit operator WeaponDamageResponse(uint val) { return new WeaponDamageResponse(notCancel: true, val); }

I know, but its easier for u in ur code

just tested with current dev.

also with the simplified just dmg value return.
But now the score is 1:1, the same incorrect behavior as before
damage is always blocked, the iscript is triggered twice.
So 1:1 no change.

[19:17:02] alt:V Server 16.0-dev16 (dev)
[19:17:02] Starting BluePeer Dev Streams on 91.218.67.182:7722
[19:17:03] Loading resource csharptest
[19:17:03] server running
[19:17:03] Loaded resource csharptest
[19:17:03] Loading resource qa-tools
[19:17:03] Loaded resource qa-tools
[19:17:03] Required server permissions:
[19:17:03] Optional server permissions:
[19:17:03] Starting HTTP server on 91.218.67.182:7722
[19:17:03] Main thread started (ThreadId: 302317)
[19:17:03] EntityStreamer thread started (ThreadId: 302370)
[19:17:03] SyncReceive thread started (ThreadId: 302373)
[19:17:03] SyncSend thread started (ThreadId: 302372)
[19:17:03] Network thread started (ThreadId: 302374)
[19:17:03] Console thread started (ThreadId: 302375)
[19:17:03][Warning] Server started in debug mode
[19:17:40] 2 Sync 543 200
[19:17:40] 2 Sync 543 200
[19:17:40] 2 Sync 543 200
[19:17:40] 2 ASync 543 200
[19:17:43] 2 Sync 25 200
[19:17:43] 2 Sync 25 200
[19:17:43] 2 Sync 25 200
[19:17:43] 2 ASync 25 200
[19:17:46] 2 Sync 25 200
[19:17:46] 2 Sync 25 200
[19:17:46] 2 Sync 25 200
[19:17:46] 2 ASync 25 200
[19:17:51] 2 Sync 25 200
[19:17:51] 2 Sync 25 200
[19:17:51] 2 Sync 25 200
[19:17:51] 2 ASync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 ASync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 Sync 25 200
[19:17:57] 2 ASync 25 200
[19:17:59] 2 Sync 25 200
[19:17:59] 2 Sync 25 200
[19:17:59] 2 Sync 25 200
[19:17:59] 2 ASync 25 200

u got it twice, because u registered it twice

Alt.OnWeaponDamage += Scripttest.WeaponDamage; and
[ScriptEvent(ScriptEventType.WeaponDamage)]

should be fixed with 16.0.7-dev

confirmed
works and no more double execution of ISCRIPT