Ragemp-roleplay-skript/Weapons.cs
2019-04-16 13:59:07 +02:00

92 lines
No EOL
3.7 KiB
C#

using System.Collections.Generic;
using System.IO;
using GrandTheftMultiplayer.Server.Elements;
using GrandTheftMultiplayer.Server.API;
using GrandTheftMultiplayer.Server.Constant;
using GrandTheftMultiplayer.Shared;
using Newtonsoft.Json;
namespace SaveWeapons
{
public class SaveWeapons : Script
{
public class CWeaponData
{
public int Ammo { get; set; }
public WeaponTint Tint { get; set; }
public string Components { get; set; }
}
const string DIR_NAME = "playerWeapons/";
public SaveWeapons()
{
API.onResourceStart += SaveWeapons_DirCheck;
API.onPlayerWeaponSwitch += onWeaponSwitch;
}
public void onWeaponSwitch(Client entity, WeaponHash oldWeapon)
{
string oldweaponstring = API.getPlayerCurrentWeapon(entity).ToString();
if(oldweaponstring == "WEAPON_PISTOL_MK2" || oldweaponstring == "WEAPON_SMG_MK2" || oldweaponstring == "WEAPON_ASSAULTRIFLE_MK2" || oldweaponstring == "WEAPON_CARBINERIFLE_MK2" || oldweaponstring == "WEAPON_COMBATMG_MK2" || oldweaponstring == "WEAPON_HEAVYSNIPER_MK2"){
API.removePlayerWeapon(entity,API.getPlayerCurrentWeapon(entity));
}
}
public void SaveWeapons_DirCheck()
{
if (!Directory.Exists(DIR_NAME)) Directory.CreateDirectory(DIR_NAME);
}
public static string GetFileName(string socialClubName)
{
return DIR_NAME + socialClubName + ".json";
}
public static void Save(Client player, API api)
{
if (!Directory.Exists(DIR_NAME)) Directory.CreateDirectory(DIR_NAME);
string fileName = GetFileName(player.socialClubName);
Dictionary<WeaponHash, CWeaponData> weaponData = new Dictionary<WeaponHash, CWeaponData>();
foreach (WeaponHash wepHash in api.getPlayerWeapons(player))
{
weaponData.Add(wepHash, new CWeaponData { Ammo = api.getPlayerWeaponAmmo(player, wepHash), Tint = api.getPlayerWeaponTint(player, wepHash), Components = JsonConvert.SerializeObject(api.getPlayerWeaponComponents(player, wepHash)) });
}
string jsonData = JsonConvert.SerializeObject(weaponData, Formatting.Indented);
File.WriteAllText(fileName, jsonData);
}
public static void Load(Client player, API api)
{
string fileName = GetFileName(player.socialClubName);
if (File.Exists(fileName))
{
string jsonData = File.ReadAllText(fileName);
Dictionary<WeaponHash, CWeaponData> weaponData = JsonConvert.DeserializeObject<Dictionary<WeaponHash, CWeaponData>>(jsonData);
foreach (KeyValuePair<WeaponHash, CWeaponData> weapon in weaponData)
{
if(!(weapon.Value.Ammo > 9999))
{
api.givePlayerWeapon(player, weapon.Key, weapon.Value.Ammo, false, true);
api.setPlayerWeaponTint(player, weapon.Key, weapon.Value.Tint);
List<WeaponComponent> weaponMods = JsonConvert.DeserializeObject<List<WeaponComponent>>(weapon.Value.Components);
foreach (WeaponComponent compID in weaponMods) api.givePlayerWeaponComponent(player, weapon.Key, compID);
} else {
API.shared.consoleOutput("Currupt Weapon prevented from loading! User: " + player.name);
}
}
}
else
{
api.consoleOutput("[SaveWeapons_Load] " + fileName + " doesn't exist!");
}
}
}
}