Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 17, 2017
1 parent 0b472e2 commit c44dec3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 41 deletions.
10 changes: 5 additions & 5 deletions fCraft/Commands/InfoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ static void ServerInfoHandler( Player player, CommandReader cmd ) {
Environment.Version );
}

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

Expand Down Expand Up @@ -1322,7 +1322,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
string output = "";
if (param.StartsWith("*") && param.EndsWith("*")) {
foreach (CommandDescriptor item in items) {
if (item.Name.ToLower().Contains(param.ToLower().Trim('*'))) {
if (item.Name.CaselessContains(param.Trim('*'))) {
output += item.MinRank.Color + item.Name + "&S, ";
}
}
Expand All @@ -1335,7 +1335,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
return;
} else if (param.EndsWith("*")) {
foreach (CommandDescriptor item in items) {
if (item.Name.CaselessStarts(param.ToLower().Trim('*'))) {
if (item.Name.CaselessStarts(param.Trim('*'))) {
output += item.MinRank.Color + item.Name + "&S, ";
}
}
Expand All @@ -1348,7 +1348,7 @@ private static void CommandsHandler(Player player, CommandReader cmd) {
return;
} else if (param.StartsWith("*")) {
foreach (CommandDescriptor item in items) {
if (item.Name.CaselessEnds(param.ToLower().Trim('*'))) {
if (item.Name.CaselessEnds(param.Trim('*'))) {
output += item.MinRank.Color + item.Name + "&S, ";
}
}
Expand Down
9 changes: 5 additions & 4 deletions fCraft/Commands/WorldCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ static void WorldsHandler(Player player, CommandReader cmd) {
} else if (param.EndsWith("*") && param.StartsWith("*")) {
listName = "worlds containing \"" + param.ToLower().Replace("*", "") + "\"";
extraParam = param.ToLower();
worlds = WorldManager.Worlds.Where(w => w.Name.ToLower().Contains(param.ToLower().Replace("*", ""))).ToArray();
worlds = WorldManager.Worlds.Where(w => w.Name.CaselessContains(param.Replace("*", ""))).ToArray();
} else if (param.EndsWith("*")) {
listName = "worlds starting with \"" + param.ToLower().Replace("*", "") + "\"";
extraParam = param.ToLower();
Expand Down Expand Up @@ -3504,14 +3504,15 @@ static void MWList(Player player, CommandReader cmd) {
WorldManager.Worlds.Where( w =>
w.Name.StartsWith("PW_") && w.AccessSecurity.Check(player.Info) &&
!w.Name.StartsWith(mapName)).ToArray();
if (own.Any()) {

if (own.Length > 0) {
player.Message("Your personal worlds: {0}", own.JoinToClassyString());
}
if (others.Any()) {
if (others.Length > 0) {
player.Message("Player personal worlds you have access to: {0}",
others.JoinToClassyString());
}
if (!own.Any() && !others.Any()) {
if (own.Length == 0 && others.Length == 0) {
player.Message("You do not have access to any personal worlds.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Commands/ZoneCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ static void ZoneListHandler( Player player, CommandReader cmd ) {
player.Message( " Type &H/ZInfo ZoneName&S for details." );

if (player.IsSuper || !player.Supports(CpeExt.SelectionCuboid)) return;
if (showZones.ToLower() !="yes") return;
if (!showZones.CaselessEquals("yes")) return;

HighlightZoneArgs args = new HighlightZoneArgs() { Player = player, Zones = zones };
Scheduler.NewTask(HighlightZones, args)
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Network/Player.Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ bool LoginSequence()
return false;
}

if (string.IsNullOrEmpty(ClientName) || !ClientName.ToLower().Contains("classicalsharp")) {
if (string.IsNullOrEmpty(ClientName) || !ClientName.CaselessContains("classicalsharp")) {
Message("&bIt is recommended that you use the ClassicalSharp client!");
Message("&9http://123dmwm.tk/cs &bredirects to the official download.");
}
Expand Down
4 changes: 2 additions & 2 deletions fCraft/Player/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static bool SendGlobal([NotNull] Player player, [NotNull] string rawMessa
}

foreach (ChatFilter Swear in ChatFilter.Filters) {
if (rawMessage.ToLower().Contains(Swear.Word.ToLower())) {
if (rawMessage.CaselessContains(Swear.Word)) {
rawMessage = rawMessage.ReplaceString(Swear.Word, Swear.Replacement, StringComparison.InvariantCultureIgnoreCase);
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public static void getUrls(string rawMessage) {
using (var reader = new StreamReader(responseStream, encoding))
fullUrl = reader.ReadToEnd();
}
if (!fullUrl.ToLower().Contains(match.ToString().ToLower()) && !match.ToString().ToLower().Contains(fullUrl.ToLower())) {
if (!fullUrl.CaselessContains(match.ToString()) && !match.ToString().CaselessContains(fullUrl)) {
fullUrls.Add(fullUrl);
}
}
Expand Down
27 changes: 0 additions & 27 deletions fCraft/Player/Replace.cs

This file was deleted.

18 changes: 18 additions & 0 deletions fCraft/System/Utils/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ public static bool CaselessEnds(this string a, string b) {
public static bool CaselessContains(this string a, string b) {
return a.IndexOf(b, StringComparison.OrdinalIgnoreCase) >= 0;
}

public static string ReplaceString(this string str, string oldValue, string newValue, StringComparison comparison) {
StringBuilder sb = new StringBuilder();

int previousIndex = 0;
int index = str.IndexOf(oldValue, comparison);
while (index != -1) {
sb.Append(str.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;

previousIndex = index;
index = str.IndexOf(oldValue, index, comparison);
}
sb.Append(str.Substring(previousIndex));

return sb.ToString();
}
}


Expand Down
1 change: 0 additions & 1 deletion fCraft/fCraft.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
<Compile Include="Player\ChatFilter.cs" />
<Compile Include="Player\PlayerHacks.cs" />
<Compile Include="Player\Report.cs" />
<Compile Include="Player\Replace.cs" />
<Compile Include="Player\SearchOptions.cs" />
<Compile Include="Player\SecurityController.cs" />
<Compile Include="Plugins\Plugin.cs" />
Expand Down

0 comments on commit c44dec3

Please sign in to comment.