Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. /*
  2. *
  3. * LICENCE : 2017
  4. * THIS SCRIPT CAN ONLY BE DOWNLOADED AND EDITED AT https://github.com/winject/NoCarJack
  5. *
  6. */
  7.  
  8. using System;
  9. using System.Drawing;
  10. using CitizenFX;
  11. using CitizenFX.Core;
  12. using CitizenFX.Core.Native;
  13. using System.Collections.Generic;
  14. using System.Windows.Forms;
  15. using System.Threading.Tasks;
  16. using NoCarJack;
  17. using Screen = CitizenFX.Core.UI.Screen;
  18.  
  19. namespace NoCarJack
  20. {
  21. public class NoCarJack : BaseScript
  22. {
  23. /*const string FILENAME = "NoCarJack.net.dll";
  24. const string VERSION = "1.2.1";
  25. const string AUTHOR = "winject";
  26. const string CONFIG_PATH = "NoCarJack.ini";*/
  27.  
  28. bool SKIP = false;
  29. bool LOCK = false;
  30. Timer unlockTimer = new Timer(0);
  31. Timer skipTimer = new Timer(1);
  32. Timer updateTimer = new Timer(2);
  33.  
  34. const int maxVehicles = 10;
  35. List<int> vehicleHistory = new List<int>(maxVehicles);
  36. Vehicle lastVeh = null;
  37. Vehicle targetVeh = null;
  38.  
  39. public NoCarJack()
  40. {
  41. EventHandlers["nocarjack:skipThisFrame"] += new Action<int, int>(Skip);
  42. EventHandlers["nocarjack:addOwnedVehicle"] += new Action<int, int>(Add);
  43. EventHandlers["nocarjack:removeOwnedVehicle"] += new Action<int, int>(Remove);
  44. base.Tick += OnTick;
  45. }
  46.  
  47. /// <summary>
  48. /// Disable the vehicle check temporarily for this player.
  49. /// Allows the player to get in any vehicle, must be triggered server-side!
  50. /// </summary>
  51. /// <param name="playerId">Targetted player ID</param>
  52. /// <param name="time">Time left before vehicle checking continues again </param>
  53. private void Skip(int playerId, int time)
  54. {
  55. int id = Game.Player.ServerId;
  56. if(playerId == id)
  57. {
  58. SKIP = true;
  59. skipTimer.Limit = time;
  60. }
  61. }
  62.  
  63. /// <summary>
  64. /// Add an owned vehicle to the vehicle history list which basically allows the player to get in this networked car
  65. /// </summary>
  66. /// <param name="playerId">Targetted player ID</param>
  67. /// <param name="networkVehicleID">Must be retrieved with NETWORK_GET_NETWORK_ID_FROM_ENTITY, works only with MISSION_ENTITY</param>
  68. private void Add(int playerId, int vehNetworkID)
  69. {
  70. int id = Game.Player.ServerId;
  71. if(playerId == id)
  72. {
  73. if(!vehicleHistory.Contains(vehNetworkID) && vehNetworkID != 0 && vehicleHistory.Count < maxVehicles)
  74. {
  75. vehicleHistory.Add(vehNetworkID);
  76. }
  77. }
  78. }
  79.  
  80. /// <summary>
  81. /// Removes the selected vehicle from the vehicle history list
  82. /// </summary>
  83. /// <param name="playerId"></param>
  84. /// <param name="networkVehicleID"></param>
  85. private void Remove(int playerId, int vehNetworkID)
  86. {
  87. int id = Game.Player.ServerId;
  88. if (playerId == id)
  89. {
  90. if (vehicleHistory.Contains(vehNetworkID) && vehNetworkID != 0)
  91. {
  92. vehicleHistory.Remove(vehNetworkID);
  93. }
  94. }
  95. }
  96.  
  97. /// <summary>
  98. /// Remove detroyed vehicles since storing them doesn't make any sense
  99. /// </summary>
  100. /// <param name="refreshTime">Let the CPU breathe</param>
  101. private void Update(int refreshTime = 6000)
  102. {
  103. if (updateTimer.Expired)
  104. {
  105. if (vehicleHistory.Count > 0)
  106. {
  107. for (int i = vehicleHistory.Count - 1; i >= 0; i--)
  108. {
  109. Vehicle tempVeh = new Vehicle(Function.Call<int>(Hash.NETWORK_GET_ENTITY_FROM_NETWORK_ID, vehicleHistory[i]));
  110. if (tempVeh.Exists() && tempVeh.IsDead)
  111. {
  112. tempVeh.IsPersistent = false;
  113. vehicleHistory.Remove(vehicleHistory[i]);
  114. }
  115. }
  116. updateTimer.Limit = refreshTime;
  117. }
  118. }
  119. }
  120.  
  121. private void DebugData()
  122. {
  123. Debug.Write("--------------------------\n");
  124. for(int e = 0; e < vehicleHistory.Count; e++)
  125. {
  126. Debug.Write(string.Format("Element : {0} \n ID : {1} \n", e, vehicleHistory[e]));
  127. }
  128. Debug.Write(string.Format(" Proceeded count : {0} \n --------------------------\n", vehicleHistory.Count));
  129. }
  130.  
  131. /// <summary>
  132. /// Main loop
  133. /// </summary>
  134. /// <returns></returns>
  135. private async Task OnTick()
  136. {
  137. Update();
  138.  
  139. if(SKIP && skipTimer.Expired)
  140. {
  141. SKIP = false;
  142. }
  143.  
  144.  
  145. if(Game.PlayerPed.Exists() && Game.PlayerPed.IsAlive && Game.PlayerPed.CurrentVehicle != null)
  146. {
  147. if(Game.PlayerPed.CurrentVehicle.Driver != null)
  148. {
  149. if (Game.PlayerPed.CurrentVehicle.Driver.Handle == Game.PlayerPed.Handle)
  150. {
  151. lastVeh = Game.PlayerPed.CurrentVehicle;
  152. Add(Game.Player.ServerId, lastVeh.GetNetworkID());
  153. }
  154. }
  155. }
  156.  
  157. if (Game.PlayerPed.VehicleTryingToEnter != null && Game.PlayerPed.VehicleTryingToEnter.Exists() && !LOCK && !SKIP)
  158. {
  159. //Check if vehicle is unlocked and free to be entered
  160. if (Function.Call<int>(Hash.GET_VEHICLE_DOOR_LOCK_STATUS, Game.PlayerPed.VehicleTryingToEnter) != 2 && Function.Call<int>(Hash.GET_VEHICLE_DOOR_LOCK_STATUS, Game.PlayerPed.VehicleTryingToEnter) != 10)
  161. {
  162. targetVeh = Game.PlayerPed.VehicleTryingToEnter;
  163. if(IsVehicleInHistory(targetVeh.GetNetworkID()))
  164. {
  165. return;
  166. }
  167. if (!Game.PlayerPed.IsLucky(30))
  168. {
  169. if (targetVeh.HasDriver())
  170. {
  171. if (!IsPedInPlayerList(targetVeh.Driver)) //avoid disabling another's player vehicle
  172. {
  173. Game.PlayerPed.Task.ClearAll();
  174. targetVeh.LockStatus = VehicleLockStatus.CannotBeTriedToEnter;
  175. Function.Call(Hash.SET_VEHICLE_UNDRIVEABLE, targetVeh, true);
  176. targetVeh.IsEngineRunning = true;
  177. if (targetVeh.IsPersistent) targetVeh.IsPersistent = false;
  178. }
  179. else
  180. {
  181.  
  182. }
  183. }
  184. else
  185. {
  186. if (!IsVehiclePlayerListLastVehicle(targetVeh))
  187. {
  188. if(lastVeh != null)
  189. {
  190. if(lastVeh.Handle != targetVeh.Handle)
  191. {
  192. Game.PlayerPed.Task.ClearAll();
  193. targetVeh.LockStatus = VehicleLockStatus.CannotBeTriedToEnter;
  194. Function.Call(Hash.SET_VEHICLE_UNDRIVEABLE, targetVeh, true);
  195. if (targetVeh.IsPersistent) targetVeh.IsPersistent = false;
  196. }
  197. else
  198. {
  199.  
  200. }
  201. }
  202. else
  203. {
  204. Game.PlayerPed.Task.ClearAll();
  205. targetVeh.LockStatus = VehicleLockStatus.CannotBeTriedToEnter;
  206. Function.Call(Hash.SET_VEHICLE_UNDRIVEABLE, targetVeh, true);
  207. if (targetVeh.IsPersistent) targetVeh.IsPersistent = false;
  208. }
  209. }
  210. else
  211. {
  212.  
  213. }
  214. }
  215. }
  216. else
  217. {
  218.  
  219. }
  220. }
  221. unlockTimer.Limit = 500;
  222. LOCK = true;
  223. }
  224.  
  225. if(unlockTimer.Expired && LOCK)
  226. {
  227. if(Game.PlayerPed.VehicleTryingToEnter != null)
  228. {
  229. if(targetVeh.Handle == Game.PlayerPed.VehicleTryingToEnter.Handle)
  230. {
  231. unlockTimer.Limit = 1000;
  232. }
  233. else
  234. {
  235. LOCK = !LOCK;
  236. }
  237. }
  238. else
  239. {
  240. LOCK = !LOCK;
  241. }
  242. }
  243. await Task.FromResult(0);
  244. }
  245.  
  246. /// <summary>
  247. /// Checks whether the specified ped belongs to a player's ped
  248. /// </summary>
  249. /// <param name="ped"></param>
  250. /// <returns></returns>
  251. private bool IsPedInPlayerList(Ped ped)
  252. {
  253. PlayerList list = base.Players;
  254. foreach (Player p in list)
  255. {
  256. if(p.Character == ped)
  257. {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263.  
  264. /// <summary>
  265. /// Checks whether the specified vehicle belongs to another player
  266. /// </summary>
  267. /// <param name="veh"></param>
  268. /// <returns></returns>
  269. private bool IsVehiclePlayerListLastVehicle(Vehicle veh)
  270. {
  271. PlayerList list = base.Players;
  272. foreach (Player p in list)
  273. {
  274. if (p.Handle != Game.PlayerPed.Handle)
  275. {
  276. if (p.Character.LastVehicle != null)
  277. {
  278. if (p.Character.LastVehicle.Handle == veh.Handle)
  279. {
  280. //CitizenFX.Core.UI.Screen.ShowNotification("~r~PRIVATE VEHICLE");
  281. return true;
  282. }
  283. }
  284. }
  285. }
  286. //CitizenFX.Core.UI.Screen.ShowNotification("~g~PUBLIC VEHICLE");
  287. return false;
  288. }
  289.  
  290. /// <summary>
  291. /// Checks if a vehicle already exists as a newtork registered vehicle
  292. /// </summary>
  293. /// <param name="veh"></param>
  294. /// <returns></returns>
  295. private bool IsVehicleInHistory(int vehNetworkID)
  296. {
  297. for(int i = vehicleHistory.Count - 1; i >= 0; i--) // Allows the list to be looped even if it gets changed at runtime
  298. {
  299. if (vehicleHistory[i] == vehNetworkID)
  300. {
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement