From 97d79391002eb96e2c1ef52d28fd22f479c0016a Mon Sep 17 00:00:00 2001 From: Shiroiame-Kusu Date: Mon, 11 Sep 2023 11:39:01 +0800 Subject: [PATCH] Ver x.x.x: I'm boring. --- Serein/Universal/Base/Motd/Motdje.cs | 2 +- Serein/Universal/Core/Server/ServerManager.cs | 8 +-- Serein/Universal/Utils/PropertyOperation.cs | 6 +- .../WPF/Windows/Pages/Server/Download.xaml.cs | 62 +++++++------------ .../Windows/Pages/Server/PlayerList.xaml.cs | 33 +++++----- .../Windows/Pages/Server/Properties.xaml.cs | 24 +++---- 6 files changed, 61 insertions(+), 74 deletions(-) diff --git a/Serein/Universal/Base/Motd/Motdje.cs b/Serein/Universal/Base/Motd/Motdje.cs index 021f8dc2..39c3874c 100644 --- a/Serein/Universal/Base/Motd/Motdje.cs +++ b/Serein/Universal/Base/Motd/Motdje.cs @@ -115,7 +115,7 @@ internal override void Get() MotdjePacket.Packet packet = JsonConvert.DeserializeObject(Origin) ?? throw new ArgumentNullException(); JObject PacketData = JObject.Parse(Origin); - PlayerListData = PacketData["players"].ToObject(); + PlayerListData = PacketData["players"]?.ToObject(); IsSuccessful = true; OnlinePlayer = packet.Players.Online; MaxPlayer = packet.Players.Max; diff --git a/Serein/Universal/Core/Server/ServerManager.cs b/Serein/Universal/Core/Server/ServerManager.cs index 0b67a964..f81e512c 100644 --- a/Serein/Universal/Core/Server/ServerManager.cs +++ b/Serein/Universal/Core/Server/ServerManager.cs @@ -202,9 +202,9 @@ public static bool Start(bool quiet) defaultJava.UseShellExecute = false; defaultJava.CreateNoWindow = true; - Process pr = Process.Start(defaultJava); - JavaVersion = pr.StandardError.ReadLine().Split(' ')[2].Replace("\"", ""); - JavaVersionNumber = int.Parse(JavaVersion.Substring(0, 2)); + Process? pr = Process.Start(defaultJava); + JavaVersion = pr?.StandardError?.ReadLine()?.Split(' ')[2].Replace("\"", ""); + JavaVersionNumber = int.Parse(JavaVersion?.Substring(0, 2)); } catch (Exception ex) @@ -677,7 +677,7 @@ public static void UpdateInfo() /// 杩愯鏃堕棿 public static string Time => Status && _serverProcess is not null ? (DateTime.Now - _serverProcess.StartTime).ToCustomString() : string.Empty; - public static ProcessStartInfo StartInfo { get; private set; } + public static ProcessStartInfo? StartInfo { get; private set; } public static string? CurrentJavaPath { get; private set; } /// diff --git a/Serein/Universal/Utils/PropertyOperation.cs b/Serein/Universal/Utils/PropertyOperation.cs index 0a9c93cc..5ca06acd 100644 --- a/Serein/Universal/Utils/PropertyOperation.cs +++ b/Serein/Universal/Utils/PropertyOperation.cs @@ -27,7 +27,7 @@ public PropertyOperation(string fileName) /// /// 键 /// 值 - public override void Add(object key, object value) + public override void Add(object key, object? value) { base.Add(key, value); list.Add(key); @@ -62,7 +62,7 @@ private void Load(string filePath) string bufLine = string.Empty; bool hasSep; bool precedingBackslash; - using (StreamReader sr = new StreamReader(filePath)) + using (StreamReader? sr = new StreamReader(filePath)) { while (sr.Peek() >= 0) { @@ -132,7 +132,7 @@ private void Load(string filePath) /// 要保存的文件的路径 public void Save() { - string filePath = this.fileName; + string? filePath = this.fileName; if (File.Exists(filePath)) { File.Delete(filePath); diff --git a/Serein/WPF/Windows/Pages/Server/Download.xaml.cs b/Serein/WPF/Windows/Pages/Server/Download.xaml.cs index 32a15f38..1c8caf52 100644 --- a/Serein/WPF/Windows/Pages/Server/Download.xaml.cs +++ b/Serein/WPF/Windows/Pages/Server/Download.xaml.cs @@ -28,7 +28,6 @@ using System.Threading; using System.Windows.Threading; using System.Timers; -using System.Collections.Generic; namespace Serein.Windows.Pages.Server { @@ -42,21 +41,21 @@ public partial class Download : UiPage public static string? DownloadAPI = "https://download.fastmirror.net/download/"; public static string? VersionAPI; public static string? VersionAPIStatus; - public JObject VersionAPIDataPrase; + public JObject? VersionAPIDataPrase; public static string? DetailedAPI; public static string? DetailedAPIStatus; - public JObject DetailedAPIDataPrase; - public static int DownloadableServerNumber {get; set; } - public static int DownloadableServerVersion { get; set; } - public static string ServerPath; + public JObject? DetailedAPIDataPrase; + public static int? DownloadableServerNumber {get; set; } + public static int? DownloadableServerVersion { get; set; } + public static string? ServerPath; private double b; - public string DownloadUnit { get; private set; } + public string? DownloadUnit { get; private set; } public static int a { get; set;} - public string CurrentServerPath { get; private set; } - public string DownloadFileURL { get; private set; } - public static string ResponseStatus { get; private set; } - public static string ResponseData { get; private set; } + public string? CurrentServerPath { get; private set; } + public string? DownloadFileURL { get; private set; } + public static string? ResponseStatus { get; private set; } + public static string? ResponseData { get; private set; } public static bool isDownloadFinished = true; @@ -96,9 +95,9 @@ private async void ServerDownload(object sender, RoutedEventArgs e) DownloadFileURL = DownloadAPI + ServerName + "/" + ServerVersion + "/" + ServerDownloadCoreVersion.SelectedItem; } } - catch (Exception ex) + catch { - DownloadFileURL = DownloadAPI + ServerName + "/" + ServerVersion + "/" + DetailedAPIDataPrase["data"]["builds"][0]["core_version"]; + DownloadFileURL = DownloadAPI + ServerName + "/" + ServerVersion + "/" + DetailedAPIDataPrase?["data"]?["builds"]?[0]?["core_version"]; } var DownloadStatus = DownloadFile(DownloadFileURL, CurrentServerPath + "\\server.jar"); if (DownloadStatus == true) @@ -122,7 +121,7 @@ private async void ServerDownload(object sender, RoutedEventArgs e) { DownloadButton.IsEnabled = true; } - }catch(Exception ex) + }catch { } @@ -157,13 +156,13 @@ private async void LoadAPIInfo() } VersionAPIDataPrase = JObject.Parse(VersionAPI); - DownloadableServerNumber = VersionAPIDataPrase["data"].Count(); + DownloadableServerNumber = VersionAPIDataPrase?["data"]?.Count(); for(int i = 0; i < DownloadableServerNumber; i++) { - ServerDownloadName.Items.Add(VersionAPIDataPrase["data"][i]["name"]); + ServerDownloadName.Items.Add(VersionAPIDataPrase?["data"]?[i]?["name"]); } } - catch(Exception ex){ + catch{ } @@ -208,31 +207,17 @@ private void ServerDownloadName_SelectionChanged(object sender, SelectionChanged if (i >= 0) { - DownloadableServerVersion = VersionAPIDataPrase["data"][i]["mc_versions"].Count(); + DownloadableServerVersion = VersionAPIDataPrase?["data"]?[i]?["mc_versions"]?.Count(); } else { i = 0; - DownloadableServerVersion = VersionAPIDataPrase["data"][i]["mc_versions"].Count(); + DownloadableServerVersion = VersionAPIDataPrase?["data"]?[i]?["mc_versions"]?.Count(); } for (int i2 = 0; i2 < DownloadableServerVersion; i2++) { - ServerDownloadVersion.Items.Add(VersionAPIDataPrase["data"][i]["mc_versions"][i2]); - } - /*DownloadButton.IsEnabled = false; - var i = ServerDownloadName.SelectedIndex; - JObject APIDataPrase = JObject.Parse(APIResult); - DownloadableServerVersion = APIDataPrase["data"][i]["mc_versions"].Count(); - ServerDownloadVersion.Items.Clear(); - ServerDownloadCoreVersion.Items.Clear(); - for (int i2 = 0;i2 < DownloadableServerVersion; i2++) - { - ServerDownloadVersion.Items.Add(APIDataPrase["data"][i]["mc_versions"][i2]); + ServerDownloadVersion.Items.Add(VersionAPIDataPrase?["data"]?[i]?["mc_versions"]?[i2]); } - - DownloadButton.IsEnabled = false; - CoreVersion.Visibility = Visibility.Collapsed; - ServerDownloadVersion.IsEnabled = true;*/ } private async void ServerDownloadVersion_SelectionChanged(object sender, SelectionChangedEventArgs e) @@ -261,8 +246,7 @@ private async void ServerDownloadVersion_SelectionChanged(object sender, Selecti DetailedAPIDataPrase = JObject.Parse(DetailedAPI); DownloadButton.IsEnabled = true; } - }catch(Exception ex) - { + }catch{ } } @@ -286,7 +270,7 @@ private void Refresh_Click(object sender, RoutedEventArgs e) LoadAPIInfo(); } - private async void FetchDetail_Click(object sender, RoutedEventArgs e) + private void FetchDetail_Click(object sender, RoutedEventArgs e) { try { @@ -305,7 +289,7 @@ public bool DownloadFile(string URL, string filename) try { isDownloadFinished = false; - HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL); + HttpWebRequest Myrq = (HttpWebRequest)WebRequest.Create(URL); Myrq.Referer = "https://www.fastmirror.net/"; Myrq.Host = "download.fastmirror.net"; Myrq.Headers.Add("Origin", "https://www.fastmirror.net"); @@ -391,7 +375,7 @@ public static void DoEvents() try { Dispatcher.PushFrame(frame); } catch (InvalidOperationException) { } } - private static object ExitFrames(object frame) + private static object? ExitFrames(object frame) { ((DispatcherFrame)frame).Continue = false; return null; diff --git a/Serein/WPF/Windows/Pages/Server/PlayerList.xaml.cs b/Serein/WPF/Windows/Pages/Server/PlayerList.xaml.cs index 51297f8d..8d53de3d 100644 --- a/Serein/WPF/Windows/Pages/Server/PlayerList.xaml.cs +++ b/Serein/WPF/Windows/Pages/Server/PlayerList.xaml.cs @@ -27,10 +27,10 @@ namespace Serein.Windows.Pages.Server /// public partial class PlayerList : UiPage { - public static JObject? PlayerListData = Motd.PlayerListData; + public static JObject PlayerListData = Motd.PlayerListData; public static int CurrentSelectedIndex; //public bool isPlayerListNotNull = true; - public List items { get; set; } + public List? items { get; set; } public PlayerList() { @@ -55,7 +55,7 @@ private void refresh() PlayerListData = Motd.PlayerListData; try { - PlayerListItems = PlayerListData["sample"].ToString(); + PlayerListItems = PlayerListData?["sample"]?.ToString(); } catch { @@ -66,10 +66,10 @@ private void refresh() { if (!string.IsNullOrEmpty(PlayerListItems)) { - for (int a = 0; a < PlayerListData["sample"].Count(); a++) + for (int a = 0; a < PlayerListData?["sample"]?.Count(); a++) { - string? Username = PlayerListData["sample"][a]["name"].ToString(); - string? UUID = PlayerListData["sample"][a]["id"].ToString(); + string? Username = PlayerListData["sample"]?[a]?["name"]?.ToString(); + string? UUID = PlayerListData["sample"]?[a]?["id"]?.ToString(); string UsernameFilter = Username + @"\[\/(.*?)\]"; string IPFilter = @"\[\/(.*?)\]"; @@ -137,7 +137,7 @@ private void GamemodeSurvival_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -152,7 +152,7 @@ private void GamemodeCreative_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -167,7 +167,7 @@ private void GamemodeAdventure_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -182,7 +182,7 @@ private void GamemodeSpectator_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -197,7 +197,7 @@ private void SetOperator_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -212,7 +212,7 @@ private void DeleteOperator_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -227,7 +227,7 @@ private void KickPlayer_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -242,7 +242,7 @@ private void BanPlayer_Click(object sender, RoutedEventArgs e) string? username = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; //username = items[-1].Username; } catch @@ -255,9 +255,11 @@ private void BanPlayer_Click(object sender, RoutedEventArgs e) private void BanPlayerIP_Click(object sender, RoutedEventArgs e) { string? username = null; + string? IP = null; try { - username = items[CurrentSelectedIndex].Username; + username = items?[CurrentSelectedIndex].Username; + IP = items?[CurrentSelectedIndex].IP; //username = items[-1].Username; } catch @@ -265,6 +267,7 @@ private void BanPlayerIP_Click(object sender, RoutedEventArgs e) return; } ServerManager.InputCommand("ipban " + username); + ServerManager.InputCommand("ipban " + IP); } private void PlayerListView_MouseDown(object sender, MouseButtonEventArgs e) diff --git a/Serein/WPF/Windows/Pages/Server/Properties.xaml.cs b/Serein/WPF/Windows/Pages/Server/Properties.xaml.cs index 1a56763c..25479146 100644 --- a/Serein/WPF/Windows/Pages/Server/Properties.xaml.cs +++ b/Serein/WPF/Windows/Pages/Server/Properties.xaml.cs @@ -47,7 +47,7 @@ public Properties() ReloadinBackground(); } } - catch (Exception ex) + catch { } @@ -244,26 +244,26 @@ private void LoadProperties() default: throw new Exception(); } - SERVER_SEED.Text = PropertiesOperation["level-seed"].ToString(); - SERVER_PORT.Text = PropertiesOperation["server-port"].ToString(); + SERVER_SEED.Text = PropertiesOperation["level-seed"]?.ToString(); + SERVER_PORT.Text = PropertiesOperation["server-port"]?.ToString(); try { - if(int.Parse(PropertiesOperation["server-port"].ToString()) != 0) + if(int.Parse(PropertiesOperation["server-port"]?.ToString()) != 0) { - Global.Settings.Server.Port = int.Parse(PropertiesOperation["server-port"].ToString()); + Global.Settings.Server.Port = int.Parse(PropertiesOperation["server-port"]?.ToString()); } } catch { } - SPAWNPOINT_PROTECT.Text = PropertiesOperation["spawn-protection"].ToString(); - MAINWORLD_NAME.Text = PropertiesOperation["level-name"].ToString(); - WORLD_BORDER.Text = PropertiesOperation["max-world-size"].ToString(); - SERVER_MAXPLAYER.Text = PropertiesOperation["max-players"].ToString(); - SERVER_MOTD.Text = PropertiesOperation["motd"].ToString(); - SERVER_VIEWDISTANCE.Text = PropertiesOperation["view-distance"].ToString(); - SERVER_SIMULATEDISTANCE.Text = PropertiesOperation["simulation-distance"].ToString(); + SPAWNPOINT_PROTECT.Text = PropertiesOperation["spawn-protection"]?.ToString(); + MAINWORLD_NAME.Text = PropertiesOperation["level-name"]?.ToString(); + WORLD_BORDER.Text = PropertiesOperation["max-world-size"]?.ToString(); + SERVER_MAXPLAYER.Text = PropertiesOperation["max-players"]?.ToString(); + SERVER_MOTD.Text = PropertiesOperation["motd"]?.ToString(); + SERVER_VIEWDISTANCE.Text = PropertiesOperation["view-distance"]?.ToString(); + SERVER_SIMULATEDISTANCE.Text = PropertiesOperation["simulation-distance"]?.ToString();