Skip to content

Commit c44dec3

Browse files
Minor code cleanup
1 parent 0b472e2 commit c44dec3

File tree

8 files changed

+32
-41
lines changed

8 files changed

+32
-41
lines changed

fCraft/Commands/InfoCommands.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,8 @@ static void ServerInfoHandler( Player player, CommandReader cmd ) {
692692
Environment.Version );
693693
}
694694

695-
double bytesReceivedRate = Server.Players.Aggregate( 0d, ( i, p ) => i + p.BytesReceivedRate );
696-
double bytesSentRate = Server.Players.Aggregate( 0d, ( i, p ) => i + p.BytesSentRate );
695+
double bytesReceivedRate = Server.Players.Sum( p => p.BytesReceivedRate );
696+
double bytesSentRate = Server.Players.Sum( p => p.BytesSentRate );
697697
player.Message( " Bandwidth: {0:0.0} KB/s up, {1:0.0} KB/s down",
698698
bytesSentRate / 1000, bytesReceivedRate / 1000 );
699699

@@ -1322,7 +1322,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
13221322
string output = "";
13231323
if (param.StartsWith("*") && param.EndsWith("*")) {
13241324
foreach (CommandDescriptor item in items) {
1325-
if (item.Name.ToLower().Contains(param.ToLower().Trim('*'))) {
1325+
if (item.Name.CaselessContains(param.Trim('*'))) {
13261326
output += item.MinRank.Color + item.Name + "&S, ";
13271327
}
13281328
}
@@ -1335,7 +1335,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
13351335
return;
13361336
} else if (param.EndsWith("*")) {
13371337
foreach (CommandDescriptor item in items) {
1338-
if (item.Name.CaselessStarts(param.ToLower().Trim('*'))) {
1338+
if (item.Name.CaselessStarts(param.Trim('*'))) {
13391339
output += item.MinRank.Color + item.Name + "&S, ";
13401340
}
13411341
}
@@ -1348,7 +1348,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
13481348
return;
13491349
} else if (param.StartsWith("*")) {
13501350
foreach (CommandDescriptor item in items) {
1351-
if (item.Name.CaselessEnds(param.ToLower().Trim('*'))) {
1351+
if (item.Name.CaselessEnds(param.Trim('*'))) {
13521352
output += item.MinRank.Color + item.Name + "&S, ";
13531353
}
13541354
}

fCraft/Commands/WorldCommands.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ static void WorldsHandler(Player player, CommandReader cmd) {
17111711
} else if (param.EndsWith("*") && param.StartsWith("*")) {
17121712
listName = "worlds containing \"" + param.ToLower().Replace("*", "") + "\"";
17131713
extraParam = param.ToLower();
1714-
worlds = WorldManager.Worlds.Where(w => w.Name.ToLower().Contains(param.ToLower().Replace("*", ""))).ToArray();
1714+
worlds = WorldManager.Worlds.Where(w => w.Name.CaselessContains(param.Replace("*", ""))).ToArray();
17151715
} else if (param.EndsWith("*")) {
17161716
listName = "worlds starting with \"" + param.ToLower().Replace("*", "") + "\"";
17171717
extraParam = param.ToLower();
@@ -3504,14 +3504,15 @@ static void MWList(Player player, CommandReader cmd) {
35043504
WorldManager.Worlds.Where( w =>
35053505
w.Name.StartsWith("PW_") && w.AccessSecurity.Check(player.Info) &&
35063506
!w.Name.StartsWith(mapName)).ToArray();
3507-
if (own.Any()) {
3507+
3508+
if (own.Length > 0) {
35083509
player.Message("Your personal worlds: {0}", own.JoinToClassyString());
35093510
}
3510-
if (others.Any()) {
3511+
if (others.Length > 0) {
35113512
player.Message("Player personal worlds you have access to: {0}",
35123513
others.JoinToClassyString());
35133514
}
3514-
if (!own.Any() && !others.Any()) {
3515+
if (own.Length == 0 && others.Length == 0) {
35153516
player.Message("You do not have access to any personal worlds.");
35163517
}
35173518
}

fCraft/Commands/ZoneCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ static void ZoneListHandler( Player player, CommandReader cmd ) {
696696
player.Message( " Type &H/ZInfo ZoneName&S for details." );
697697

698698
if (player.IsSuper || !player.Supports(CpeExt.SelectionCuboid)) return;
699-
if (showZones.ToLower() !="yes") return;
699+
if (!showZones.CaselessEquals("yes")) return;
700700

701701
HighlightZoneArgs args = new HighlightZoneArgs() { Player = player, Zones = zones };
702702
Scheduler.NewTask(HighlightZones, args)

fCraft/Network/Player.Networking.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ bool LoginSequence()
770770
return false;
771771
}
772772

773-
if (string.IsNullOrEmpty(ClientName) || !ClientName.ToLower().Contains("classicalsharp")) {
773+
if (string.IsNullOrEmpty(ClientName) || !ClientName.CaselessContains("classicalsharp")) {
774774
Message("&bIt is recommended that you use the ClassicalSharp client!");
775775
Message("&9http://123dmwm.tk/cs &bredirects to the official download.");
776776
}

fCraft/Player/Chat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static bool SendGlobal([NotNull] Player player, [NotNull] string rawMessa
4343
}
4444

4545
foreach (ChatFilter Swear in ChatFilter.Filters) {
46-
if (rawMessage.ToLower().Contains(Swear.Word.ToLower())) {
46+
if (rawMessage.CaselessContains(Swear.Word)) {
4747
rawMessage = rawMessage.ReplaceString(Swear.Word, Swear.Replacement, StringComparison.InvariantCultureIgnoreCase);
4848
}
4949
}
@@ -113,7 +113,7 @@ public static void getUrls(string rawMessage) {
113113
using (var reader = new StreamReader(responseStream, encoding))
114114
fullUrl = reader.ReadToEnd();
115115
}
116-
if (!fullUrl.ToLower().Contains(match.ToString().ToLower()) && !match.ToString().ToLower().Contains(fullUrl.ToLower())) {
116+
if (!fullUrl.CaselessContains(match.ToString()) && !match.ToString().CaselessContains(fullUrl)) {
117117
fullUrls.Add(fullUrl);
118118
}
119119
}

fCraft/Player/Replace.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

fCraft/System/Utils/ExtensionMethods.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ public static bool CaselessEnds(this string a, string b) {
222222
public static bool CaselessContains(this string a, string b) {
223223
return a.IndexOf(b, StringComparison.OrdinalIgnoreCase) >= 0;
224224
}
225+
226+
public static string ReplaceString(this string str, string oldValue, string newValue, StringComparison comparison) {
227+
StringBuilder sb = new StringBuilder();
228+
229+
int previousIndex = 0;
230+
int index = str.IndexOf(oldValue, comparison);
231+
while (index != -1) {
232+
sb.Append(str.Substring(previousIndex, index - previousIndex));
233+
sb.Append(newValue);
234+
index += oldValue.Length;
235+
236+
previousIndex = index;
237+
index = str.IndexOf(oldValue, index, comparison);
238+
}
239+
sb.Append(str.Substring(previousIndex));
240+
241+
return sb.ToString();
242+
}
225243
}
226244

227245

fCraft/fCraft.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@
159159
<Compile Include="Player\ChatFilter.cs" />
160160
<Compile Include="Player\PlayerHacks.cs" />
161161
<Compile Include="Player\Report.cs" />
162-
<Compile Include="Player\Replace.cs" />
163162
<Compile Include="Player\SearchOptions.cs" />
164163
<Compile Include="Player\SecurityController.cs" />
165164
<Compile Include="Plugins\Plugin.cs" />

0 commit comments

Comments
 (0)