Use CaselessEquals or CaselessStarts in Utils class.

This commit is contained in:
UnknownShadow200 2015-06-08 11:41:45 +10:00
parent 3b5bb87341
commit f31af21aeb
3 changed files with 29 additions and 22 deletions

View File

@ -6,7 +6,7 @@ namespace ClassicalSharp.Commands {
public class CommandManager {
public static bool IsCommandPrefix( string input ) {
return input.StartsWith( "/client", StringComparison.OrdinalIgnoreCase );
return Utils.CaselessStarts( input, "/client" );
}
public Game Window;
@ -25,7 +25,7 @@ namespace ClassicalSharp.Commands {
void RegisterCommand( Command command ) {
command.Window = Window;
foreach( Command cmd in RegisteredCommands ) {
if( command.Name.Equals( cmd.Name, StringComparison.OrdinalIgnoreCase ) ) {
if( Utils.CaselessEquals( cmd.Name, command.Name ) ) {
throw new InvalidOperationException( "Another command already has name : " + command.Name );
}
}
@ -36,7 +36,7 @@ namespace ClassicalSharp.Commands {
bool matchFound = false;
Command matchingCommand = null;
foreach( Command cmd in RegisteredCommands ) {
if( cmd.Name.StartsWith( commandName, StringComparison.OrdinalIgnoreCase ) ) {
if( Utils.CaselessStarts( cmd.Name, commandName ) ) {
if( matchFound ) {
Window.AddChat( "&e/client: Multiple commands found that start with: \"&f" + commandName + "&e\"." );
return null;

View File

@ -54,7 +54,7 @@ namespace ClassicalSharp.Commands {
return new [] {
"&a/client env [property] [value]",
"&bproperties: &eskycol, fogcol, cloudscol, cloudsspeed,",
" &esuncol, shadowcol",
" &esuncol, shadowcol, weather",
"&bcol properties: &evalue must be a hex colour code (either #RRGGBB or RRGGBB).",
"&bcloudsspeed: &evalue must be a decimal number. (e.g. 0.5, 1, 4.5).",
"&eIf no args are given, the current env variables will be displayed.",
@ -68,19 +68,19 @@ namespace ClassicalSharp.Commands {
Window.AddChat( "Fog colour: " + Window.Map.FogCol.ToRGBHexString() );
Window.AddChat( "Clouds colour: " + Window.Map.CloudsCol.ToRGBHexString() );
Window.AddChat( "Sky colour: " + Window.Map.SkyCol.ToRGBHexString() );
} else if( property.Equals( "skycol", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "skycol" ) ) {
ReadHexColourAnd( reader, c => Window.Map.SetSkyColour( c ) );
} else if( property.Equals( "fogcol", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "fogcol" ) ) {
ReadHexColourAnd( reader, c => Window.Map.SetFogColour( c ) );
} else if( property.Equals( "cloudscol", StringComparison.OrdinalIgnoreCase ) ||
property.Equals( "cloudcol", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "cloudscol" )
|| Utils.CaselessEquals( property, "cloudcol" ) ) {
ReadHexColourAnd( reader, c => Window.Map.SetCloudsColour( c ) );
} else if( property.Equals( "suncol", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "suncol" ) ) {
ReadHexColourAnd( reader, c => Window.Map.SetSunlight( c ) );
} else if( property.Equals( "shadowcol", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "shadowcol" ) ) {
ReadHexColourAnd( reader, c => Window.Map.SetShadowlight( c ) );
} else if( property.Equals( "cloudsspeed", StringComparison.OrdinalIgnoreCase ) ||
property.Equals( "cloudspeed", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "cloudsspeed" )
|| Utils.CaselessEquals( property, "cloudspeed" ) ) {
float speed;
if( !reader.NextFloat( out speed ) ) {
Window.AddChat( "&e/client env: &cInvalid clouds speed." );
@ -90,7 +90,7 @@ namespace ClassicalSharp.Commands {
env.CloudsSpeed = speed;
}
}
} else if( property.Equals( "weather", StringComparison.OrdinalIgnoreCase ) ) {
} else if( Utils.CaselessEquals( property, "weather" ) ) {
int weather;
if( !reader.NextInt( out weather ) || weather < 0 || weather > 2 ) {
Window.AddChat( "&e/client env: &cInvalid weather." );
@ -123,8 +123,7 @@ namespace ClassicalSharp.Commands {
"&eDisplays the help (if available) for the given command.",
};
}
}
}
public override void Execute( CommandReader reader ) {
string commandName = reader.Next();
@ -161,21 +160,21 @@ namespace ClassicalSharp.Commands {
string property = reader.Next();
if( property == null ) {
Window.AddChat( "&e/client info: &cYou didn't specify a property." );
} else if( property == "pos" ) {
} else if( Utils.CaselessEquals( property, "pos" ) ) {
Window.AddChat( "Feet: " + Window.LocalPlayer.Position );
Window.AddChat( "Eye: " + Window.LocalPlayer.EyePosition );
} else if( property == "targetpos" ) {
} else if( Utils.CaselessEquals( property, "targetpos" ) ) {
PickedPos pos = Window.SelectedPos;
if( !pos.Valid ) {
Window.AddChat( "Currently not targeting a block" );
} else {
Window.AddChat( "Currently targeting: " + pos.BlockPos );
}
} else if( property == "dimensions" ) {
} else if( Utils.CaselessEquals( property, "dimensions" ) ) {
Window.AddChat( "map width: " + Window.Map.Width );
Window.AddChat( "map height: " + Window.Map.Height );
Window.AddChat( "map length: " + Window.Map.Length );
} else if( property == "jumpheight" ) {
} else if( Utils.CaselessEquals( property, "jumpheight" ) ) {
float jumpHeight = Window.LocalPlayer.JumpHeight;
Window.AddChat( jumpHeight.ToString( "F2" ) + " blocks" );
} else {
@ -205,13 +204,13 @@ namespace ClassicalSharp.Commands {
string property = reader.Next();
if( property == null ) {
Window.AddChat( "&e/client rendertype: &cYou didn't specify a new render type." );
} else if( property == "legacyfast" ) {
} else if( Utils.CaselessEquals( property, "legacyfast" ) ) {
SetNewRenderType( true, true, true );
Window.AddChat( "&e/client rendertype: &fRender type is now fast legacy." );
} else if( property == "legacy" ) {
} else if( Utils.CaselessEquals( property, "legacy" ) ) {
SetNewRenderType( true, false, true );
Window.AddChat( "&e/client rendertype: &fRender type is now legacy." );
} else if( property == "normal" ) {
} else if( Utils.CaselessEquals( property, "normal" ) ) {
SetNewRenderType( false, false, false );
Window.AddChat( "&e/client rendertype: &fRender type is now normal." );
}

View File

@ -61,6 +61,14 @@ namespace ClassicalSharp {
return new String( output, 0, usedChars );
}
public static bool CaselessEquals( string a, string b ) {
return a.Equals( b, StringComparison.OrdinalIgnoreCase );
}
public static bool CaselessStarts( string a, string b ) {
return a.StartsWith( b, StringComparison.OrdinalIgnoreCase );
}
public static string ToHexString( byte[] array ) {
int len = array.Length;
char[] hexadecimal = new char[len * 2];