From 75c8a8ced449b91e26783ad61fa39c51b238bcef Mon Sep 17 00:00:00 2001
From: xuyuwtu <3082068984@qq.com>
Date: Thu, 29 Jan 2026 13:50:09 +0800
Subject: [PATCH] Use better code
---
TShockAPI/Bouncer.cs | 2 +-
TShockAPI/CLI/CommandLineParser.cs | 3 +-
TShockAPI/CLI/FlagSet.cs | 5 +--
TShockAPI/Commands.cs | 35 +++++++++----------
TShockAPI/DB/BanManager.cs | 4 +--
TShockAPI/DB/Queries/MysqlQueryBuilder.cs | 2 +-
TShockAPI/DB/Queries/PostgresQueryBuilder.cs | 4 +--
TShockAPI/DB/Queries/SqliteQueryBuilder.cs | 4 +--
TShockAPI/DB/RegionManager.cs | 5 ++-
TShockAPI/DB/UserManager.cs | 4 +--
TShockAPI/Extensions/LinqExt.cs | 4 +--
TShockAPI/GeoIPCountry.cs | 2 +-
TShockAPI/Group.cs | 4 +--
.../NetModules/CreativePowerHandler.cs | 4 +--
.../NetModules/NetModulePacketHandler.cs | 4 +--
TShockAPI/Localization/EnglishLanguage.cs | 9 ++---
TShockAPI/Modules/ModuleManager.cs | 2 +-
TShockAPI/Modules/ReduceConsoleSpam.cs | 2 +-
TShockAPI/NetItem.cs | 3 +-
TShockAPI/PaginationTools.cs | 6 ++--
TShockAPI/Permissions.cs | 2 +-
TShockAPI/PlayerData.cs | 8 ++---
TShockAPI/Rest/Rest.cs | 8 ++---
TShockAPI/Rest/RestManager.cs | 12 +++----
TShockAPI/Rest/RestObject.cs | 3 +-
TShockAPI/Rest/RestVerbs.cs | 3 +-
TShockAPI/Rest/SecureRest.cs | 6 +---
TShockAPI/SaveManager.cs | 4 +--
TShockAPI/TSPlayer.cs | 12 +++----
TShockAPI/TShock.cs | 6 ++--
TShockAPI/UpdateManager.cs | 2 +-
TShockAPI/Utils.cs | 8 ++---
32 files changed, 77 insertions(+), 105 deletions(-)
diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs
index 6f5a538f..94328f57 100644
--- a/TShockAPI/Bouncer.cs
+++ b/TShockAPI/Bouncer.cs
@@ -767,7 +767,7 @@ namespace TShockAPI
return;
}
}
- else if (action == EditAction.PlaceTile && (projectileCreatesTile.ContainsKey(lastKilledProj) && editData == projectileCreatesTile[lastKilledProj]))
+ else if (action == EditAction.PlaceTile && projectileCreatesTile.TryGetValue(lastKilledProj, out int value) && editData == value)
{
args.Player.LastKilledProjectile = 0;
}
diff --git a/TShockAPI/CLI/CommandLineParser.cs b/TShockAPI/CLI/CommandLineParser.cs
index d62917fb..6a80ff06 100644
--- a/TShockAPI/CLI/CommandLineParser.cs
+++ b/TShockAPI/CLI/CommandLineParser.cs
@@ -147,12 +147,11 @@ namespace TShockAPI.CLI
///
public T Get(FlagSet flags)
{
- if (!_results.ContainsKey(flags))
+ if (!_results.TryGetValue(flags, out object result))
{
return default(T);
}
- object result = _results[flags];
Type t = typeof(T);
if (t == typeof(string))
diff --git a/TShockAPI/CLI/FlagSet.cs b/TShockAPI/CLI/FlagSet.cs
index ed559f3d..1a1c187f 100644
--- a/TShockAPI/CLI/FlagSet.cs
+++ b/TShockAPI/CLI/FlagSet.cs
@@ -43,10 +43,7 @@ namespace TShockAPI.CLI
/// Flags represented by this FlagSet
public FlagSet(params string[] flags)
{
- if (flags == null)
- {
- throw new ArgumentNullException(nameof(flags));
- }
+ ArgumentNullException.ThrowIfNull(flags);
_flags = flags.Select(f => f.ToLowerInvariant());
}
diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs
index 6191e171..682b4ff8 100644
--- a/TShockAPI/Commands.cs
+++ b/TShockAPI/Commands.cs
@@ -113,8 +113,7 @@ namespace TShockAPI
get { return commandDelegate; }
set
{
- if (value == null)
- throw new ArgumentNullException();
+ ArgumentNullException.ThrowIfNull(value);
commandDelegate = value;
}
@@ -134,10 +133,9 @@ namespace TShockAPI
public Command(CommandDelegate cmd, params string[] names)
{
- if (cmd == null)
- throw new ArgumentNullException("cmd");
+ ArgumentNullException.ThrowIfNull(cmd);
if (names == null || names.Length < 1)
- throw new ArgumentException("names");
+ throw new ArgumentException("is null or empty", nameof(names));
AllowServer = true;
CommandDelegate = cmd;
@@ -691,11 +689,10 @@ namespace TShockAPI
if (Hooks.PlayerHooks.OnPlayerCommand(player, cmdName, cmdText, args, ref cmds, cmdPrefix))
return true;
- if (cmds.Count() == 0)
+ if (!cmds.Any())
{
- if (player.AwaitingResponse.ContainsKey(cmdName))
+ if (player.AwaitingResponse.TryGetValue(cmdName, out Action