Files

98 lines
3.6 KiB
C#
Raw Permalink Normal View History

2022-09-08 12:41:56 -07:00
using RageCoop.Core.Scripting;
using System;
2022-06-22 14:18:20 +08:00
using System.Collections.Generic;
using System.Linq;
namespace RageCoop.Server.Scripting
{
2022-09-08 12:41:56 -07:00
internal class BaseScript : ServerScript
2022-06-22 14:18:20 +08:00
{
2022-07-12 17:10:16 +08:00
private readonly Server Server;
2022-09-08 12:41:56 -07:00
public BaseScript(Server server) { Server = server; }
2022-06-22 14:18:20 +08:00
public override void OnStart()
{
2022-06-23 09:46:38 +08:00
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]);
});
2022-07-10 16:13:08 +08:00
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, (e) =>
{
2022-07-12 17:10:16 +08:00
if (Server.Settings.WeatherTimeSync)
2022-07-10 16:13:08 +08:00
{
2022-08-21 12:37:37 +08:00
if (e.Client != API.Host) { e.Client.SendCustomEvent(CustomEvents.IsHost, false); return; }
2022-07-12 17:10:16 +08:00
foreach (var c in API.GetAllClients().Values)
2022-07-10 16:13:08 +08:00
{
2022-09-08 12:41:56 -07:00
if (c == e.Client)
2022-07-12 17:10:16 +08:00
{
continue;
}
c.SendCustomEventQueued(CustomEvents.WeatherTimeSync, e.Args);
2022-07-10 16:13:08 +08:00
}
}
});
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied, (e) =>
{
2023-10-06 23:46:08 -03:00
API.SendCustomEventQueued(API.GetAllClients().Values.Where(x => x != e.Client).ToList(), CustomEvents.OnPlayerDied, e.Args);
});
2022-09-08 12:41:56 -07:00
API.Events.OnChatMessage += (s, e) =>
Server.Logger?.Info((e.Client?.Username ?? e.ClaimedSender ?? "Unknown") + ": " + e.Message);
2022-06-22 14:18:20 +08:00
}
public override void OnStop()
{
}
2022-09-08 12:41:56 -07:00
public static void SetAutoRespawn(Client c, bool toggle)
2022-06-22 14:18:20 +08:00
{
2022-09-08 12:41:56 -07:00
c.SendCustomEvent(CustomEvents.SetAutoRespawn, toggle);
2022-06-22 14:18:20 +08:00
}
public void SetNameTag(Client c, bool toggle)
{
2022-09-08 12:41:56 -07:00
foreach (var other in API.GetAllClients().Values)
{
2022-09-08 12:41:56 -07:00
if (c == other) { continue; }
other.SendCustomEvent(CustomEvents.SetDisplayNameTag, c.Player.ID, toggle);
}
}
2022-09-08 12:41:56 -07:00
public void SendServerPropsTo(List<ServerProp> objects, List<Client> clients = null)
2022-07-02 17:14:56 +08:00
{
2022-09-08 12:41:56 -07:00
foreach (var obj in objects)
2022-07-02 17:14:56 +08:00
{
2022-09-08 12:41:56 -07:00
API.SendCustomEventQueued(clients, CustomEvents.ServerPropSync, obj.ID, obj.Model, obj.Position, obj.Rotation);
2022-07-02 17:14:56 +08:00
}
}
2022-07-03 15:28:28 +08:00
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
{
foreach (var obj in objects)
{
2022-09-08 12:41:56 -07:00
API.SendCustomEventQueued(clients, CustomEvents.ServerBlipSync, obj.ID, (ushort)obj.Sprite, (byte)obj.Color, obj.Scale, obj.Position, obj.Rotation, obj.Name);
2022-07-03 15:28:28 +08:00
}
}
2022-09-08 12:41:56 -07:00
private void NativeResponse(CustomEventReceivedArgs e)
2022-06-23 09:46:38 +08:00
{
try
{
int id = (int)e.Args[0];
2022-07-29 16:17:33 +08:00
lock (e.Client.Callbacks)
2022-06-23 09:46:38 +08:00
{
2022-09-08 12:41:56 -07:00
if (e.Client.Callbacks.TryGetValue(id, out Action<object> callback))
2022-06-23 09:46:38 +08:00
{
callback(e.Args[1]);
2022-07-29 16:17:33 +08:00
e.Client.Callbacks.Remove(id);
2022-06-23 09:46:38 +08:00
}
}
}
catch (Exception ex)
{
2022-07-01 13:54:18 +08:00
API.Logger.Error("Failed to parse NativeResponse");
API.Logger.Error(ex);
2022-06-23 09:46:38 +08:00
}
}
2022-06-22 14:18:20 +08:00
}
}