Skip to content

Commit 293e2ad

Browse files
authored
Merge pull request #533 from TomGrobbe/feature/serverEventRefactors
Server Event Refactors
2 parents 75c2d86 + d8afc29 commit 293e2ad

File tree

10 files changed

+371
-242
lines changed

10 files changed

+371
-242
lines changed

SharedClasses/PermissionsManager.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public enum Permission
2828
OPTeleport,
2929
OPWaypoint,
3030
OPSpectate,
31+
OPSendMessage,
3132
OPIdentifiers,
3233
OPSummon,
3334
OPKill,
@@ -385,9 +386,16 @@ public enum Permission
385386
/// </summary>
386387
/// <param name="permission"></param>
387388
/// <param name="source"></param>
388-
/// <param name="checkAnyway">if true, then the permissions will be checked even if they aren't setup yet.</param>
389389
/// <returns></returns>
390390
public static bool IsAllowed(Permission permission, Player source) => IsAllowedServer(permission, source);
391+
392+
/// <summary>
393+
/// Public function to check if a permission is allowed.
394+
/// </summary>
395+
/// <param name="permission"></param>
396+
/// <param name="playerHandle"></param>
397+
/// <returns></returns>
398+
public static bool IsAllowed(Permission permission, string playerHandle) => IsAllowedServer(permission, playerHandle);
391399
#endif
392400

393401
#if CLIENT
@@ -456,11 +464,23 @@ private static bool IsAllowedServer(Permission permission, Player source)
456464
return false;
457465
}
458466

459-
if (IsPlayerAceAllowed(source.Handle, GetAceName(permission)))
467+
return IsAllowedServer(permission, source.Handle);
468+
}
469+
470+
/// <summary>
471+
/// Checks if the player is allowed that specific permission.
472+
/// </summary>
473+
/// <param name="permission"></param>
474+
/// <param name="playerHandle"></param>
475+
/// <returns></returns>
476+
private static bool IsAllowedServer(Permission permission, string playerHandle)
477+
{
478+
if (!DoesPlayerExist(playerHandle))
460479
{
461-
return true;
480+
return false;
462481
}
463-
return false;
482+
483+
return IsPlayerAceAllowed(playerHandle, GetAceName(permission));
464484
}
465485
#endif
466486

vMenu/CommonFunctions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,13 @@ public static async void CommitSuicide()
966966
/// Summon player.
967967
/// </summary>
968968
/// <param name="player"></param>
969-
public static void SummonPlayer(IPlayer player) => TriggerServerEvent("vMenu:SummonPlayer", player.ServerId);
969+
public static void SummonPlayer(IPlayer player)
970+
{
971+
Vehicle currentVehicle = GetVehicle();
972+
int numberOfSeats = currentVehicle is not null ? GetVehicleModelNumberOfSeats(currentVehicle.Model) : 0;
973+
974+
TriggerServerEvent("vMenu:SummonPlayer", player.ServerId, numberOfSeats);
975+
}
970976
#endregion
971977

972978
#region Spectate function
@@ -3293,10 +3299,6 @@ public static async void PrivateMessage(string source, string message, bool sent
32933299

32943300
if (MainMenu.MiscSettingsMenu == null || MainMenu.MiscSettingsMenu.MiscDisablePrivateMessages)
32953301
{
3296-
if (!(sent && source == Game.Player.ServerId.ToString()))
3297-
{
3298-
TriggerServerEvent("vMenu:PmsDisabled", source);
3299-
}
33003302
return;
33013303
}
33023304

vMenu/EventManager.cs

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ public EventManager()
4040
EventHandlers.Add("vMenu:SetAddons", new Action(SetConfigOptions)); // DEPRECATED: Backwards-compatible event handler; use 'vMenu:SetConfigOptions' instead
4141
EventHandlers.Add("vMenu:SetConfigOptions", new Action(SetConfigOptions));
4242
EventHandlers.Add("vMenu:SetPermissions", new Action<string>(MainMenu.SetPermissions));
43-
EventHandlers.Add("vMenu:GoToPlayer", new Action<string>(SummonPlayer));
4443
EventHandlers.Add("vMenu:KillMe", new Action<string>(KillMe));
4544
EventHandlers.Add("vMenu:Notify", new Action<string>(NotifyPlayer));
4645
EventHandlers.Add("vMenu:SetClouds", new Action<float, string>(SetClouds));
4746
EventHandlers.Add("vMenu:GoodBye", new Action(GoodBye));
4847
EventHandlers.Add("vMenu:SetBanList", new Action<string>(UpdateBanList));
49-
EventHandlers.Add("vMenu:ClearArea", new Action<float, float, float>(ClearAreaNearPos));
48+
EventHandlers.Add("vMenu:ClearArea", new Action<Vector3>(ClearAreaNearPos));
5049
EventHandlers.Add("vMenu:updatePedDecors", new Action(UpdatePedDecors));
5150
EventHandlers.Add("playerSpawned", new Action(SetAppearanceOnFirstSpawn));
52-
EventHandlers.Add("vMenu:GetOutOfCar", new Action<int, int>(GetOutOfCar));
5351
EventHandlers.Add("vMenu:PrivateMessage", new Action<string, string>(PrivateMessage));
5452
EventHandlers.Add("vMenu:UpdateTeleportLocations", new Action<string>(UpdateTeleportLocations));
5553

@@ -353,92 +351,14 @@ private void KillMe(string sourceName)
353351
SetEntityHealth(Game.PlayerPed.Handle, 0);
354352
}
355353

356-
/// <summary>
357-
/// Teleport to the specified player.
358-
/// </summary>
359-
/// <param name="targetPlayer"></param>
360-
private async void SummonPlayer(string targetPlayer)
361-
{
362-
// ensure the player list is requested in case of Infinity
363-
MainMenu.PlayersList.RequestPlayerList();
364-
await MainMenu.PlayersList.WaitRequested();
365-
366-
var player = MainMenu.PlayersList.FirstOrDefault(a => a.ServerId == int.Parse(targetPlayer));
367-
368-
if (player != null)
369-
{
370-
_ = TeleportToPlayer(player);
371-
}
372-
}
373-
374354
/// <summary>
375355
/// Clear the area around the provided x, y, z coordinates. Clears everything like (destroyed) objects, peds, (ai) vehicles, etc.
376356
/// Also restores broken streetlights, etc.
377357
/// </summary>
378358
/// <param name="x"></param>
379359
/// <param name="y"></param>
380360
/// <param name="z"></param>
381-
private void ClearAreaNearPos(float x, float y, float z)
382-
{
383-
ClearAreaOfEverything(x, y, z, 100f, false, false, false, false);
384-
}
385-
386-
/// <summary>
387-
/// Kicks the current player from the specified vehicle if they're inside and don't own the vehicle themselves.
388-
/// </summary>
389-
/// <param name="vehNetId"></param>
390-
/// <param name="vehicleOwnedBy"></param>
391-
private async void GetOutOfCar(int vehNetId, int vehicleOwnedBy)
392-
{
393-
if (NetworkDoesNetworkIdExist(vehNetId))
394-
{
395-
var veh = NetToVeh(vehNetId);
396-
if (DoesEntityExist(veh))
397-
{
398-
var vehicle = new Vehicle(veh);
399-
400-
if (vehicle == null || !vehicle.Exists())
401-
{
402-
return;
403-
}
404-
405-
if (Game.PlayerPed.IsInVehicle(vehicle) && vehicleOwnedBy != Game.Player.ServerId)
406-
{
407-
if (!vehicle.IsStopped)
408-
{
409-
Notify.Alert("The owner of this vehicle is reclaiming their personal vehicle. You will be kicked from this vehicle in about 10 seconds. Stop the vehicle now to avoid taking damage.", false, true);
410-
}
411-
412-
// Wait for the vehicle to come to a stop, or 10 seconds, whichever is faster.
413-
var timer = GetGameTimer();
414-
while (vehicle != null && vehicle.Exists() && !vehicle.IsStopped)
415-
{
416-
await Delay(0);
417-
if (GetGameTimer() - timer > (10 * 1000)) // 10 second timeout
418-
{
419-
break;
420-
}
421-
}
422-
423-
// just to make sure they're actually still inside the vehicle and the vehicle still exists.
424-
if (vehicle != null && vehicle.Exists() && Game.PlayerPed.IsInVehicle(vehicle))
425-
{
426-
// Make the ped jump out because the car isn't stopped yet.
427-
if (!vehicle.IsStopped)
428-
{
429-
Notify.Info("You were warned, now you'll have to suffer the consequences!");
430-
TaskLeaveVehicle(Game.PlayerPed.Handle, vehicle.Handle, 4160);
431-
}
432-
// Make the ped exit gently.
433-
else
434-
{
435-
TaskLeaveVehicle(Game.PlayerPed.Handle, vehicle.Handle, 0);
436-
}
437-
}
438-
}
439-
}
440-
}
441-
}
361+
private void ClearAreaNearPos(Vector3 position) => ClearAreaOfEverything(position.X, position.Y, position.Z, 100f, false, false, false, false);
442362

443363
/// <summary>
444364
/// Updates ped decorators for the clothing animation when players have joined.

vMenu/MainMenu.cs

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public MainMenu()
120120
}
121121
#endregion
122122
#region keymapping
123-
string KeyMappingID = String.IsNullOrWhiteSpace(GetSettingsString(Setting.vmenu_keymapping_id)) ? "Default" : GetSettingsString(Setting.vmenu_keymapping_id);
123+
string KeyMappingID = GetKeyMappingId();
124124
RegisterCommand($"vMenu:{KeyMappingID}:NoClip", new Action<dynamic, List<dynamic>, string>((dynamic source, List<dynamic> args, string rawCommand) =>
125125
{
126126
if (IsAllowed(Permission.NoClip))
@@ -144,20 +144,6 @@ public MainMenu()
144144
}
145145
}
146146
}), false);
147-
RegisterCommand($"vMenu:{KeyMappingID}:MenuToggle", new Action<dynamic, List<dynamic>, string>((dynamic source, List<dynamic> args, string rawCommand) =>
148-
{
149-
if (MenuEnabled)
150-
{
151-
if (!MenuController.IsAnyMenuOpen())
152-
{
153-
Menu.OpenMenu();
154-
}
155-
else
156-
{
157-
MenuController.CloseAllMenus();
158-
}
159-
}
160-
}), false);
161147

162148
if (!(GetSettingsString(Setting.vmenu_noclip_toggle_key) == null))
163149
{
@@ -340,29 +326,13 @@ public MainMenu()
340326
// Clear all previous pause menu info/brief messages on resource start.
341327
ClearBrief();
342328

343-
// Request the permissions data from the server.
344-
TriggerServerEvent("vMenu:RequestPermissions");
345-
346-
// Request server state from the server.
347-
TriggerServerEvent("vMenu:RequestServerState");
348-
}
349-
350-
#region Infinity bits
351-
[EventHandler("vMenu:SetServerState")]
352-
public void SetServerState(IDictionary<string, object> data)
353-
{
354-
if (data.TryGetValue("IsInfinity", out var isInfinity))
329+
if (GlobalState.Get("vmenu_onesync") ?? false)
355330
{
356-
if (isInfinity is bool isInfinityBool)
357-
{
358-
if (isInfinityBool)
359-
{
360-
PlayersList = new InfinityPlayerList(Players);
361-
}
362-
}
331+
PlayersList = new InfinityPlayerList(Players);
363332
}
364333
}
365334

335+
#region Infinity bits
366336
[EventHandler("vMenu:ReceivePlayerList")]
367337
public void ReceivedPlayerList(IList<object> players)
368338
{
@@ -540,6 +510,21 @@ static bool canUseMenu()
540510
StatSetFloat((uint)GetHashKey("MP0_PLAYER_MENTAL_STATE"), 0f, true); // Mental State
541511
}
542512

513+
RegisterCommand($"vMenu:{GetKeyMappingId()}:MenuToggle", new Action<dynamic, List<dynamic>, string>((dynamic source, List<dynamic> args, string rawCommand) =>
514+
{
515+
if (MenuEnabled)
516+
{
517+
if (!MenuController.IsAnyMenuOpen())
518+
{
519+
Menu.OpenMenu();
520+
}
521+
else
522+
{
523+
MenuController.CloseAllMenus();
524+
}
525+
}
526+
}), false);
527+
543528
TriggerEvent("vMenu:SetupTickFunctions");
544529
}
545530

@@ -920,5 +905,9 @@ private static void CreateSubmenus()
920905
}
921906
}
922907
#endregion
908+
909+
#region Utilities
910+
private static string GetKeyMappingId() => string.IsNullOrWhiteSpace(GetSettingsString(Setting.vmenu_keymapping_id)) ? "Default" : GetSettingsString(Setting.vmenu_keymapping_id);
911+
#endregion
923912
}
924913
}

vMenu/menus/MiscSettings.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ public class MiscSettings
5050
public bool RestorePlayerWeapons { get; private set; } = UserDefaults.MiscRestorePlayerWeapons;
5151
public bool DrawTimeOnScreen { get; internal set; } = UserDefaults.MiscShowTime;
5252
public bool MiscRightAlignMenu { get; private set; } = UserDefaults.MiscRightAlignMenu;
53-
public bool MiscDisablePrivateMessages { get; private set; } = UserDefaults.MiscDisablePrivateMessages;
53+
private bool _disablePrivateMessages;
54+
public bool MiscDisablePrivateMessages
55+
{
56+
get => _disablePrivateMessages;
57+
set
58+
{
59+
_disablePrivateMessages = value;
60+
Game.Player.State.Set("vmenu_pms_disabled", value, true);
61+
}
62+
}
5463
public bool MiscDisableControllerSupport { get; private set; } = UserDefaults.MiscDisableControllerSupport;
5564

5665
internal bool TimecycleEnabled { get; private set; } = false;
@@ -70,6 +79,12 @@ public class MiscSettings
7079

7180
internal static List<vMenuShared.ConfigManager.TeleportLocation> TpLocations = new();
7281

82+
public MiscSettings()
83+
{
84+
// Sets statebag when resource starts
85+
MiscDisablePrivateMessages = UserDefaults.MiscDisablePrivateMessages;
86+
}
87+
7388
/// <summary>
7489
/// Creates the menu.
7590
/// </summary>
@@ -469,8 +484,7 @@ private void CreateMenu()
469484
{
470485
if (item == clearArea)
471486
{
472-
var pos = Game.PlayerPed.Position;
473-
BaseScript.TriggerServerEvent("vMenu:ClearArea", pos.X, pos.Y, pos.Z);
487+
BaseScript.TriggerServerEvent("vMenu:ClearArea");
474488
}
475489
};
476490

vMenu/menus/OnlinePlayers.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ private void CreateMenu()
5252
var ban = new MenuItem("~r~Ban Player Permanently", "Ban this player permanently from the server. Are you sure you want to do this? You can specify the ban reason after clicking this button.");
5353
var tempban = new MenuItem("~r~Ban Player Temporarily", "Give this player a tempban of up to 30 days (max). You can specify duration and ban reason after clicking this button.");
5454

55-
// always allowed
56-
playerMenu.AddMenuItem(sendMessage);
57-
// permissions specific
55+
if (IsAllowed(Permission.OPSendMessage))
56+
{
57+
playerMenu.AddMenuItem(sendMessage);
58+
}
5859
if (IsAllowed(Permission.OPTeleport))
5960
{
6061
playerMenu.AddMenuItem(teleport);
@@ -111,6 +112,12 @@ private void CreateMenu()
111112
// send message
112113
if (item == sendMessage)
113114
{
115+
if (currentPlayer.Handle == Game.Player.Handle)
116+
{
117+
Notify.Error("You cannot message yourself!");
118+
return;
119+
}
120+
114121
if (MainMenu.MiscSettingsMenu != null && !MainMenu.MiscSettingsMenu.MiscDisablePrivateMessages)
115122
{
116123
var message = await GetUserInput($"Private Message To {currentPlayer.Name}", 200);
@@ -245,6 +252,7 @@ private void CreateMenu()
245252
}
246253
else if (item == printIdentifiers)
247254
{
255+
// TODO: Replace callback function
248256
Func<string, string> CallbackFunction = (data) =>
249257
{
250258
Debug.WriteLine(data);

vMenu/menus/PersonalVehicle.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,11 @@ private void CreateMenu()
280280
{
281281
if (item == kickAllPassengers)
282282
{
283-
if (CurrentPersonalVehicle.Occupants.Count() > 0 && CurrentPersonalVehicle.Occupants.Any(p => p != Game.PlayerPed))
283+
Ped[] occupants = CurrentPersonalVehicle.Occupants;
284+
285+
if (occupants.Count() > 0 && occupants.Any(p => p != Game.PlayerPed && p.IsPlayer))
284286
{
285-
var netId = VehToNet(CurrentPersonalVehicle.Handle);
286-
TriggerServerEvent("vMenu:GetOutOfCar", netId, Game.Player.ServerId);
287+
TriggerServerEvent("vMenu:GetOutOfCar", CurrentPersonalVehicle.NetworkId);
287288
}
288289
else
289290
{

vMenu/menus/WeatherOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ private void CreateMenu()
143143
}
144144
else if (item == snowEnabled)
145145
{
146+
if (EventManager.GetServerWeather is "XMAS" or "SNOWLIGHT" or "SNOW" or "BLIZZARD")
147+
{
148+
Notify.Custom($"Snow effects cannot be disabled when weather is ~y~{EventManager.GetServerWeather}~s~.");
149+
return;
150+
}
151+
146152
Notify.Custom($"Snow effects will now be forced {(_checked ? "~g~enabled" : "~r~disabled")}~s~.");
147153
UpdateServerWeather(EventManager.GetServerWeather, EventManager.DynamicWeatherEnabled, _checked);
148154
}

0 commit comments

Comments
 (0)