/// <summary> /// Registers a new entity to the server /// </summary> /// <param name="input">A compatible entry</param> /// <exception cref="Exception">Thrown if unknown/unhandable type has been passed</exception> public void Register(params object[] input) { foreach (object item in input) { switch (item) { default: throw new Exception($"Input ({item.GetType().ToString()}) can't be handled by RegisterAsync."); case WorldGenerator generator: Logger.LogDebug($"Registering {generator.Id}..."); WorldGenerators.Add(generator); break; } } }
/// <summary> /// Starts this server /// </summary> /// <returns></returns> public async Task StartServer() { Console.CancelKeyPress += this.Console_CancelKeyPress; Logger.LogMessage($"Launching Obsidian Server v{Version} with ID {Id}"); //Check if MPDM and OM are enabled, if so, we can't handle connections if (Config.MulitplayerDebugMode && Config.OnlineMode) { Logger.LogError("Incompatible Config: Multiplayer debug mode can't be enabled at the same time as online mode since usernames will be overwritten"); StopServer(); return; } Logger.LogMessage($"Loading operator list..."); Operators.Initialize(); Logger.LogMessage("Registering default entities"); RegisterDefault(); Logger.LogMessage($"Loading and Initializing plugins..."); await this.PluginManager.LoadPluginsAsync(this.Logger); if (WorldGenerators.FirstOrDefault(g => g.Id == Config.Generator) is WorldGenerator worldGenerator) { this.WorldGenerator = worldGenerator; } else { this.Logger.LogWarning($"Generator ({Config.Generator}) is unknown. Using default generator"); this.WorldGenerator = new SuperflatGenerator(); } Logger.LogMessage($"World generator set to {this.WorldGenerator.Id} ({this.WorldGenerator.ToString()})"); Logger.LogDebug($"Set start DateTimeOffset for measuring uptime."); this.StartTime = DateTimeOffset.Now; Logger.LogMessage("Starting server backend..."); await Task.Factory.StartNew(async() => { await this.ServerLoop().ConfigureAwait(false); }); if (!this.Config.OnlineMode) { this.Logger.LogMessage($"Server started in offline mode.."); } Logger.LogDebug($"Start listening for new clients"); _tcpListener.Start(); await BlockRegistry.RegisterAll(); while (!_cts.IsCancellationRequested) { var tcp = await _tcpListener.AcceptTcpClientAsync(); Logger.LogDebug($"New connection from client with IP {tcp.Client.RemoteEndPoint.ToString()}"); int newplayerid = Math.Max(0, this.Clients.Count); var clnt = new Client(tcp, this.Config, newplayerid, this); Clients.Add(clnt); await Task.Factory.StartNew(async() => { await clnt.StartConnectionAsync().ConfigureAwait(false); }); } Logger.LogWarning($"Cancellation has been requested. Stopping server..."); }