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