Use better code

This commit is contained in:
xuyuwtu 2026-01-29 13:50:09 +08:00
parent 608e7a99bc
commit 75c8a8ced4
32 changed files with 77 additions and 105 deletions

View file

@ -347,9 +347,9 @@ namespace TShockAPI.DB
/// <returns></returns>
public Ban GetBanById(int id)
{
if (Bans.ContainsKey(id))
if (Bans.TryGetValue(id, out Ban value))
{
return Bans[id];
return value;
}
using var reader = database.QueryReader("SELECT * FROM PlayerBans WHERE TicketNumber=@0", id);

View file

@ -80,7 +80,7 @@ public class MysqlQueryBuilder : GenericQueryBuilder, IQueryBuilder
return ret + (length is not null ? "({0})".SFormat((int)length) : "");
}
throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
throw new NotImplementedException(Enum.GetName(type));
}
/// <inheritdoc />

View file

@ -43,7 +43,7 @@ public class PostgresQueryBuilder : GenericQueryBuilder
MySqlDbType.Int64 => "BIGINT",
MySqlDbType.DateTime => "TIMESTAMP",
_ => throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type))
_ => throw new NotImplementedException(Enum.GetName(type))
};
/// <inheritdoc />
@ -79,7 +79,7 @@ public class PostgresQueryBuilder : GenericQueryBuilder
.Where(c => c.Unique).Select(c => c.Name)
.ToArray(); // No re-enumeration
return $"CREATE TABLE {EscapeTableName(table.Name)} ({string.Join(", ", columns)} {(uniques.Any() ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : "")})";
return $"CREATE TABLE {EscapeTableName(table.Name)} ({string.Join(", ", columns)} {(uniques.Length != 0 ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : "")})";
}
/// <inheritdoc />

View file

@ -48,7 +48,7 @@ public class SqliteQueryBuilder : GenericQueryBuilder, IQueryBuilder
var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name);
return "CREATE TABLE {0} ({1} {2})".SFormat(EscapeTableName(table.Name),
string.Join(", ", columns),
uniques.Count() > 0 ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : "");
uniques.Any() ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : "");
}
/// <summary>
@ -91,7 +91,7 @@ public class SqliteQueryBuilder : GenericQueryBuilder, IQueryBuilder
return ret;
}
throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
throw new NotImplementedException(Enum.GetName(type));
}
/// <summary>

View file

@ -84,7 +84,7 @@ namespace TShockAPI.DB
string groups = reader.Get<string>("Groups");
int z = reader.Get<int>("Z");
string[] splitids = mergedids.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
string[] splitids = mergedids.Split(',', StringSplitOptions.RemoveEmptyEntries);
Region r = new Region(id, new Rectangle(X1, Y1, width, height), name, owner, Protected != 0, Main.worldID.ToString(), z);
r.SetAllowedGroups(groups);
@ -779,8 +779,7 @@ namespace TShockAPI.DB
foreach (string id in idArr)
{
int i = 0;
if (int.TryParse(id, out i) && i != 0)
if (int.TryParse(id, out var i) && i != 0)
{
idList.Add(i);
}

View file

@ -515,7 +515,7 @@ namespace TShockAPI.DB
if (password.Trim().Length < Math.Max(4, TShock.Config.Settings.MinimumPasswordLength))
{
int minLength = TShock.Config.Settings.MinimumPasswordLength;
throw new ArgumentOutOfRangeException("password", GetString($"Password must be at least {minLength} characters."));
throw new ArgumentOutOfRangeException(nameof(password), GetString($"Password must be at least {minLength} characters."));
}
try
{
@ -536,7 +536,7 @@ namespace TShockAPI.DB
if (password.Trim().Length < Math.Max(4, TShock.Config.Settings.MinimumPasswordLength))
{
int minLength = TShock.Config.Settings.MinimumPasswordLength;
throw new ArgumentOutOfRangeException("password", GetString($"Password must be at least {minLength} characters."));
throw new ArgumentOutOfRangeException(nameof(password), GetString($"Password must be at least {minLength} characters."));
}
Password = BCryptNext::BCrypt.Net.BCrypt.HashPassword(password.Trim(), workFactor);
}