Jump to content

C# Bridge - GetWeapons not implemented?


Tamasu

Recommended Posts

Hello,

I got a new problem - yet again.

At the moment im trying to save the weapons of the player into the database. But my WeaponHash array from the player is ALWAYS null, even though I have the weapons ingame.
My console says WARNING: "GetPlayerWeapons" function is not implemented yet. So is it impossible to do it with C#, because the bridge dosen't has it implemented yet?
Maybe should I try to get it from ClientSide? via javaScript? I dont think that this is really safe.

Code:

[Command("addweapon", "Usage; /addweapon [WeaponName]", GreedyArg = true)]
public void CmdAddWeapon(Client sender, string weaponName) // Command to add Weapons to a player
{
    WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(weaponName);
    NAPI.Player.GivePlayerWeapon(sender, weaponHash, 1000);
    NAPI.Chat.SendChatMessageToPlayer(sender, "added: " + weaponHash + " to you");
    NAPI.Util.ConsoleOutput("Player: " + sender.Name + "@" + sender.Address + " spawned weapon: " + weaponHash);
}

public void SaveWorld() // Command to save everything, works for cars!
{
    foreach (var p in NAPI.Pools.GetAllPlayers())
    {
        p.SendChatMessage("Saving you..!");
        SaveCharacterInfo(p);
    }
    foreach (var v in NAPI.Pools.GetAllVehicles())
    {
        SaveVehicle(v);
    }
}

public void SaveCharacterInfo(Client player) // Saves the character position and Weapon-Inventory
{
    Character character = GetCharacter(GetAccountBySocialClub(player.SocialClubName));
    CharacterInfo characterInfo = new CharacterInfo();
    characterInfo.Position = player.Position;
    WeaponHash[] playerWeapons = NAPI.Player.GetPlayerWeapons(player);
    foreach (WeaponHash weapon in playerWeapons)
    {
        characterInfo.WeaponInventory.Add(new CharacterWeapon(weapon, NAPI.Player.GetPlayerWeaponAmmo(player, weapon)));
    }
    character.CharacterInfo = NAPI.Util.ToJson(characterInfo);
    using (var dbContext = new DefaultDbContext())
    {
        dbContext.Characters.Update(character);
        dbContext.SaveChanges();
    }
}

    class CharacterWeapon // List at CharacterInfo so every weapon has its own ammo
    {
        public WeaponHash WeaponHash { get; set; }
        public int Ammo { get; set; }
        public WeaponComponent WeaponComponent { get; set; }
        public WeaponTint WeaponTint { get; set; }

        public CharacterWeapon(WeaponHash weaponHash, int ammo)
        {
            WeaponHash = weaponHash;
            Ammo = ammo;
        }
    }
}

Is there a way to fix it or is it just impossible to do it with C#?

I've got the newest C# Bridge ( 0.3.7.0)
and Bootstrapper (0.3.7.0)

Im looking forward to your answers.

Tamasu

Link to comment
Share on other sites

Hey,

since I am not home right now I cant give you some example code. Just save the weapon and ammo everytime a weapon action occurs. For e.g. buying a weapon. Since it happens as a function you easily can save that weapon into DB. Giving it, dropping it, selling it. All of these are functions you can use to store the action into DB. Atleast this is what I did. 

 

Greetings Avoid

Link to comment
Share on other sites

Hi Avoid,

thanks for your reply.

Could you share an code example later? I would like to see how other people managed to make it work.

Also isn't this very database heavy? Or is this not much of a problem?

Tamasu

 

Edit:

So I managed to save the weapons in my players inventory. Everytime when a weapon gets spawned it directly writes it into the database and when connecting im reading this data. this works perfectly.

But how can I access ammo now? Seems like the method NAPI.Player.GetPlayerWeaponAmmo(sender, WeaponHash) isnt returning an ammoNumber; it always returns 0.
How can I track the users ammo? Check everytime he shoots and -1 his ammo? I think that would be very unperformanced.

I would love to hear any tips or see an alternative code.
 

        [ServerEvent(Event.PlayerWeaponSwitch)]
        public void OnPlayerWeaponSwitch(Client sender, WeaponHash oldWeaponHash, WeaponHash newWeaponHash)
        {
            SaveWeapons(sender, GetCharacter(GetAccountBySocialClub(sender.SocialClubName)), new CharacterWeapon(oldWeaponHash, NAPI.Player.GetPlayerWeaponAmmo(sender, oldWeaponHash)));
        }

        public void SaveWeapons(Client sender, Character character, CharacterWeapon characterWeapon)
        {
            CharacterInfo currentInfo = NAPI.Util.FromJson<CharacterInfo>(character.CharacterInfo);
            if (currentInfo.WeaponInventory.Count == 0)
            {
            }
            else
            {
                CharacterInfo characterInfo = NAPI.Util.FromJson<CharacterInfo>(character.CharacterInfo);
                List<CharacterWeapon> updatedList = new List<CharacterWeapon>();
                foreach (var weapon in characterInfo.WeaponInventory)
                {
                    updatedList.Add(new CharacterWeapon(weapon.WeaponHash, weapon.Ammo));
                }
                characterInfo.WeaponInventory = updatedList;
                character.CharacterInfo = NAPI.Util.ToJson(characterInfo);
                SetCharacter(sender, character);
            }
        }
              
        public void SetCharacter(Client sender, Character character)
        {
            using (var dbContext = new DefaultDbContext())
            {
                dbContext.Characters.Update(character);
                dbContext.SaveChanges();
                NAPI.Util.ConsoleOutput(sender.SocialClubName + " got updated in database");
            }
        }              
              

 

Tamasu

Edited by Tamasu
// Edit 17.10.2019 21:00
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...