ServerScript update
This commit is contained in:
@ -9,7 +9,7 @@ namespace FirstGameMode
|
|||||||
[Command("hello")]
|
[Command("hello")]
|
||||||
public static void HelloCommand(CommandContext ctx)
|
public static void HelloCommand(CommandContext ctx)
|
||||||
{
|
{
|
||||||
ServerScript.SendChatMessageToPlayer(ctx.Player.Username, "Hello " + ctx.Player.Username + " :)");
|
API.SendChatMessageToPlayer(ctx.Player.Username, "Hello " + ctx.Player.Username + " :)");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("inrange")]
|
[Command("inrange")]
|
||||||
@ -17,18 +17,18 @@ namespace FirstGameMode
|
|||||||
{
|
{
|
||||||
if (ctx.Player.Ped.IsInRangeOf(new LVector3(0f, 0f, 75f), 7f))
|
if (ctx.Player.Ped.IsInRangeOf(new LVector3(0f, 0f, 75f), 7f))
|
||||||
{
|
{
|
||||||
ServerScript.SendChatMessageToPlayer(ctx.Player.Username, "You are in range! :)");
|
API.SendChatMessageToPlayer(ctx.Player.Username, "You are in range! :)");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ServerScript.SendChatMessageToPlayer(ctx.Player.Username, "You are not in range! :(");
|
API.SendChatMessageToPlayer(ctx.Player.Username, "You are not in range! :(");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("online")]
|
[Command("online")]
|
||||||
public static void OnlineCommand(CommandContext ctx)
|
public static void OnlineCommand(CommandContext ctx)
|
||||||
{
|
{
|
||||||
ServerScript.SendChatMessageToPlayer(ctx.Player.Username, ServerScript.GetAllPlayersCount() + " player online!");
|
API.SendChatMessageToPlayer(ctx.Player.Username, API.GetAllPlayersCount() + " player online!");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("kick")]
|
[Command("kick")]
|
||||||
@ -36,11 +36,11 @@ namespace FirstGameMode
|
|||||||
{
|
{
|
||||||
if (ctx.Args.Length < 2)
|
if (ctx.Args.Length < 2)
|
||||||
{
|
{
|
||||||
ServerScript.SendChatMessageToPlayer(ctx.Player.Username, "Please use \"/kick <USERNAME> <REASON>\"");
|
API.SendChatMessageToPlayer(ctx.Player.Username, "Please use \"/kick <USERNAME> <REASON>\"");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerScript.KickPlayerByUsername(ctx.Args[0], ctx.Args.Skip(1).ToArray());
|
API.KickPlayerByUsername(ctx.Args[0], ctx.Args.Skip(1).ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
using CoopServer;
|
using System.ComponentModel;
|
||||||
using CoopServer.Entities;
|
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
|
|
||||||
|
using CoopServer;
|
||||||
|
using CoopServer.Entities;
|
||||||
|
|
||||||
namespace FirstGameMode
|
namespace FirstGameMode
|
||||||
{
|
{
|
||||||
public class Main : ServerScript
|
public class Main : ServerScript
|
||||||
@ -9,39 +11,45 @@ namespace FirstGameMode
|
|||||||
private static readonly Timer RunningSinceTimer = new() { Interval = 1000 };
|
private static readonly Timer RunningSinceTimer = new() { Interval = 1000 };
|
||||||
private static int RunningSince = 0;
|
private static int RunningSince = 0;
|
||||||
|
|
||||||
public override void Start()
|
public Main()
|
||||||
{
|
{
|
||||||
RunningSinceTimer.Start();
|
RunningSinceTimer.Start();
|
||||||
RunningSinceTimer.Elapsed += new ElapsedEventHandler((sender, e) => RunningSince += 1);
|
RunningSinceTimer.Elapsed += new ElapsedEventHandler((sender, e) => RunningSince += 1);
|
||||||
|
|
||||||
RegisterCommand("running", RunningCommand);
|
API.OnPlayerConnected += OnPlayerConnected;
|
||||||
RegisterCommands<Commands>();
|
API.OnPlayerDisconnected += OnPlayerDisconnected;
|
||||||
|
API.OnChatMessage += OnChatMessage;
|
||||||
|
|
||||||
|
API.RegisterCommand("running", RunningCommand);
|
||||||
|
API.RegisterCommands<Commands>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RunningCommand(CommandContext ctx)
|
public static void RunningCommand(CommandContext ctx)
|
||||||
{
|
{
|
||||||
SendChatMessageToPlayer(ctx.Player.Username, "Server has been running for: " + RunningSince + " seconds!");
|
API.SendChatMessageToPlayer(ctx.Player.Username, "Server has been running for: " + RunningSince + " seconds!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnPlayerConnect(EntitiesPlayer client)
|
public static void OnPlayerConnected(EntitiesPlayer client)
|
||||||
{
|
{
|
||||||
SendChatMessageToAll("Player " + client.Username + " connected!");
|
API.SendChatMessageToAll("Player " + client.Username + " connected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnPlayerDisconnect(EntitiesPlayer player, string reason)
|
public static void OnPlayerDisconnected(EntitiesPlayer player)
|
||||||
{
|
{
|
||||||
SendChatMessageToAll(player.Username + " left the server, reason: " + reason);
|
API.SendChatMessageToAll("Player " + player.Username + " disconnected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool OnChatMessage(string username, string message)
|
public static void OnChatMessage(string username, string message, CancelEventArgs e)
|
||||||
{
|
{
|
||||||
|
e.Cancel = true;
|
||||||
|
|
||||||
if (message.StartsWith("EASTEREGG"))
|
if (message.StartsWith("EASTEREGG"))
|
||||||
{
|
{
|
||||||
SendChatMessageToPlayer(username, "You found the EASTEREGG! *-*");
|
API.SendChatMessageToPlayer(username, "You found the EASTEREGG! *-*");
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
API.SendChatMessageToAll(message, username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,7 +152,7 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
GameMode.Start();
|
GameMode.API.InvokeStart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -476,7 +476,7 @@ namespace CoopServer
|
|||||||
|
|
||||||
if (GameMode != null)
|
if (GameMode != null)
|
||||||
{
|
{
|
||||||
GameMode.OnPlayerConnect(Players[packet.Player]);
|
GameMode.API.InvokePlayerConnect(Players[packet.Player]);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<NetConnection> playerList = Util.FilterAllLocal(local);
|
List<NetConnection> playerList = Util.FilterAllLocal(local);
|
||||||
@ -518,7 +518,7 @@ namespace CoopServer
|
|||||||
{
|
{
|
||||||
if (GameMode != null)
|
if (GameMode != null)
|
||||||
{
|
{
|
||||||
GameMode.OnPlayerDisconnect(Players[packet.Player], reason);
|
GameMode.API.InvokePlayerDisconnect(Players[packet.Player], reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<NetConnection> playerList = Util.FilterAllLocal(packet.Player);
|
List<NetConnection> playerList = Util.FilterAllLocal(packet.Player);
|
||||||
@ -644,22 +644,24 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string username = packet.Username;
|
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
|
||||||
|
if (userConnection == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
packet = new()
|
outgoingMessage = MainNetServer.CreateMessage();
|
||||||
|
new ChatMessagePacket()
|
||||||
{
|
{
|
||||||
Username = "Server",
|
Username = "Server",
|
||||||
Message = "Command not found!"
|
Message = "Command not found!"
|
||||||
};
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||||
|
MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, 0);
|
||||||
outgoingMessage = MainNetServer.CreateMessage();
|
|
||||||
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
|
||||||
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections.Find(con => con.RemoteUniqueIdentifier == Players.First(x => x.Value.Username == username).Key), NetDeliveryMethod.ReliableOrdered, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (GameMode.OnChatMessage(packet.Username, packet.Message))
|
else if (GameMode.API.InvokeChatMessage(packet.Username, packet.Message))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,53 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
using Lidgren.Network;
|
using Lidgren.Network;
|
||||||
|
|
||||||
namespace CoopServer
|
namespace CoopServer
|
||||||
{
|
{
|
||||||
public class ServerScript
|
public abstract class ServerScript
|
||||||
{
|
{
|
||||||
public virtual void Start() { }
|
public API API = new();
|
||||||
|
|
||||||
public virtual void OnPlayerConnect(Entities.EntitiesPlayer player)
|
|
||||||
{
|
|
||||||
Logging.Info("New player [" + player.SocialClubName + " | " + player.Username + "] connected!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void OnPlayerDisconnect(Entities.EntitiesPlayer player, string reason)
|
public class API
|
||||||
{
|
{
|
||||||
Logging.Info(player.Username + " left the server, reason: " + reason);
|
#region DELEGATES
|
||||||
|
public delegate void ChatEvent(string username, string message, CancelEventArgs cancel);
|
||||||
|
public delegate void PlayerEvent(Entities.EntitiesPlayer player);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region EVENTS
|
||||||
|
public event EventHandler OnStart;
|
||||||
|
public event ChatEvent OnChatMessage;
|
||||||
|
public event PlayerEvent OnPlayerConnected;
|
||||||
|
public event PlayerEvent OnPlayerDisconnected;
|
||||||
|
|
||||||
|
internal void InvokeStart()
|
||||||
|
{
|
||||||
|
OnStart?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool OnChatMessage(string username, string message)
|
internal void InvokePlayerConnect(Entities.EntitiesPlayer player)
|
||||||
{
|
{
|
||||||
return false;
|
OnPlayerConnected?.Invoke(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void InvokePlayerDisconnect(Entities.EntitiesPlayer player, string reason)
|
||||||
|
{
|
||||||
|
OnPlayerDisconnected?.Invoke(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal bool InvokeChatMessage(string username, string message)
|
||||||
|
{
|
||||||
|
var args = new CancelEventArgs(false);
|
||||||
|
OnChatMessage?.Invoke(username, message, args);
|
||||||
|
return args.Cancel;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region FUNCTIONS
|
||||||
public static List<long> GetAllConnections()
|
public static List<long> GetAllConnections()
|
||||||
{
|
{
|
||||||
List<long> result = new();
|
List<long> result = new();
|
||||||
@ -121,6 +145,7 @@ namespace CoopServer
|
|||||||
{
|
{
|
||||||
Server.RegisterCommands<T>();
|
Server.RegisterCommands<T>();
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Command
|
public class Command
|
||||||
|
Reference in New Issue
Block a user