Clean up
This commit is contained in:
@ -1,96 +1,101 @@
|
||||
using RageCoop.Core.Scripting;
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using RageCoop.Core.Scripting;
|
||||
|
||||
namespace RageCoop.Server.Scripting
|
||||
namespace RageCoop.Server.Scripting;
|
||||
|
||||
internal class BaseScript : ServerScript
|
||||
{
|
||||
internal class BaseScript : ServerScript
|
||||
private readonly Server Server;
|
||||
|
||||
public BaseScript(Server server)
|
||||
{
|
||||
private readonly Server Server;
|
||||
public BaseScript(Server server) { Server = server; }
|
||||
public override void OnStart()
|
||||
{
|
||||
API.RegisterCustomEventHandler(CustomEvents.NativeResponse, NativeResponse);
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnVehicleDeleted, (e) =>
|
||||
{
|
||||
API.Entities.RemoveVehicle((int)e.Args[0]);
|
||||
});
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnPedDeleted, (e) =>
|
||||
{
|
||||
API.Entities.RemovePed((int)e.Args[0]);
|
||||
});
|
||||
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, (e) =>
|
||||
{
|
||||
if (Server.Settings.WeatherTimeSync)
|
||||
{
|
||||
if (e.Client != API.Host) { e.Client.SendCustomEvent(CustomEvents.IsHost, false); return; }
|
||||
Server = server;
|
||||
}
|
||||
|
||||
foreach (var c in API.GetAllClients().Values)
|
||||
{
|
||||
if (c == e.Client)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
c.SendCustomEventQueued(CustomEvents.WeatherTimeSync, e.Args);
|
||||
}
|
||||
public override void OnStart()
|
||||
{
|
||||
API.RegisterCustomEventHandler(CustomEvents.NativeResponse, NativeResponse);
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnVehicleDeleted,
|
||||
e => { API.Entities.RemoveVehicle((int)e.Args[0]); });
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnPedDeleted, e => { API.Entities.RemovePed((int)e.Args[0]); });
|
||||
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, e =>
|
||||
{
|
||||
if (Server.Settings.WeatherTimeSync)
|
||||
{
|
||||
if (e.Client != API.Host)
|
||||
{
|
||||
e.Client.SendCustomEvent(CustomEvents.IsHost, false);
|
||||
return;
|
||||
}
|
||||
});
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied, (e) =>
|
||||
{
|
||||
API.SendCustomEventQueued(API.GetAllClients().Values.Where(x => x != e.Client).ToList(), CustomEvents.OnPlayerDied, e.Client.Username);
|
||||
});
|
||||
API.Events.OnChatMessage += (s, e) =>
|
||||
Server.Logger?.Info((e.Client?.Username ?? e.ClaimedSender ?? "Unknown") + ": " + e.Message);
|
||||
}
|
||||
public override void OnStop()
|
||||
{
|
||||
}
|
||||
public static void SetAutoRespawn(Client c, bool toggle)
|
||||
{
|
||||
c.SendCustomEvent(CustomEvents.SetAutoRespawn, toggle);
|
||||
}
|
||||
public void SetNameTag(Client c, bool toggle)
|
||||
{
|
||||
foreach (var other in API.GetAllClients().Values)
|
||||
{
|
||||
if (c == other) { continue; }
|
||||
other.SendCustomEvent(CustomEvents.SetDisplayNameTag, c.Player.ID, toggle);
|
||||
}
|
||||
}
|
||||
public void SendServerPropsTo(List<ServerProp> objects, List<Client> clients = null)
|
||||
{
|
||||
foreach (var obj in objects)
|
||||
{
|
||||
API.SendCustomEventQueued(clients, CustomEvents.ServerPropSync, obj.ID, obj.Model, obj.Position, obj.Rotation);
|
||||
}
|
||||
}
|
||||
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
|
||||
{
|
||||
foreach (var obj in objects)
|
||||
{
|
||||
API.SendCustomEventQueued(clients, CustomEvents.ServerBlipSync, obj.ID, (ushort)obj.Sprite, (byte)obj.Color, obj.Scale, obj.Position, obj.Rotation, obj.Name);
|
||||
}
|
||||
}
|
||||
|
||||
private void NativeResponse(CustomEventReceivedArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
int id = (int)e.Args[0];
|
||||
lock (e.Client.Callbacks)
|
||||
foreach (var c in API.GetAllClients().Values)
|
||||
{
|
||||
if (e.Client.Callbacks.TryGetValue(id, out Action<object> callback))
|
||||
{
|
||||
callback(e.Args[1]);
|
||||
e.Client.Callbacks.Remove(id);
|
||||
}
|
||||
if (c == e.Client) continue;
|
||||
c.SendCustomEventQueued(CustomEvents.WeatherTimeSync, e.Args);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
});
|
||||
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied,
|
||||
e =>
|
||||
{
|
||||
API.Logger.Error("Failed to parse NativeResponse");
|
||||
API.Logger.Error(ex);
|
||||
}
|
||||
API.SendCustomEventQueued(API.GetAllClients().Values.Where(x => x != e.Client).ToList(),
|
||||
CustomEvents.OnPlayerDied, e.Client.Username);
|
||||
});
|
||||
API.Events.OnChatMessage += (s, e) =>
|
||||
Server.Logger?.Info((e.Client?.Username ?? e.ClaimedSender ?? "Unknown") + ": " + e.Message);
|
||||
}
|
||||
|
||||
public override void OnStop()
|
||||
{
|
||||
}
|
||||
|
||||
public static void SetAutoRespawn(Client c, bool toggle)
|
||||
{
|
||||
c.SendCustomEvent(CustomEvents.SetAutoRespawn, toggle);
|
||||
}
|
||||
|
||||
public void SetNameTag(Client c, bool toggle)
|
||||
{
|
||||
foreach (var other in API.GetAllClients().Values)
|
||||
{
|
||||
if (c == other) continue;
|
||||
other.SendCustomEvent(CustomEvents.SetDisplayNameTag, c.Player.ID, toggle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendServerPropsTo(List<ServerProp> objects, List<Client> clients = null)
|
||||
{
|
||||
foreach (var obj in objects)
|
||||
API.SendCustomEventQueued(clients, CustomEvents.ServerPropSync, obj.ID, obj.Model, obj.Position,
|
||||
obj.Rotation);
|
||||
}
|
||||
|
||||
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
|
||||
{
|
||||
foreach (var obj in objects)
|
||||
API.SendCustomEventQueued(clients, CustomEvents.ServerBlipSync, obj.ID, (ushort)obj.Sprite, (byte)obj.Color,
|
||||
obj.Scale, obj.Position, obj.Rotation, obj.Name);
|
||||
}
|
||||
|
||||
private void NativeResponse(CustomEventReceivedArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = (int)e.Args[0];
|
||||
lock (e.Client.Callbacks)
|
||||
{
|
||||
if (e.Client.Callbacks.TryGetValue(id, out var callback))
|
||||
{
|
||||
callback(e.Args[1]);
|
||||
e.Client.Callbacks.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
API.Logger.Error("Failed to parse NativeResponse");
|
||||
API.Logger.Error(ex);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user