mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-24 05:10:42 -04:00
In SinglePlayer, you should be able to use commands without /client. (E.g. /cuboid will act the same as /client cuboid)
This commit is contained in:
parent
db9698c331
commit
a8f7e2b0a7
@ -246,7 +246,7 @@
|
|||||||
<Compile Include="Network\Utils\FixedBufferStream.cs" />
|
<Compile Include="Network\Utils\FixedBufferStream.cs" />
|
||||||
<Compile Include="Network\Utils\GZipHeaderReader.cs" />
|
<Compile Include="Network\Utils\GZipHeaderReader.cs" />
|
||||||
<Compile Include="Network\NetworkProcessor.WoM.cs" />
|
<Compile Include="Network\NetworkProcessor.WoM.cs" />
|
||||||
<Compile Include="Commands\CommandManager.cs" />
|
<Compile Include="Commands\CommandList.cs" />
|
||||||
<Compile Include="Commands\CommandReader.cs" />
|
<Compile Include="Commands\CommandReader.cs" />
|
||||||
<Compile Include="Network\Utils\NetWriter.cs" />
|
<Compile Include="Network\Utils\NetWriter.cs" />
|
||||||
<Compile Include="Math\AABB.cs" />
|
<Compile Include="Math\AABB.cs" />
|
||||||
|
@ -4,11 +4,15 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace ClassicalSharp.Commands {
|
namespace ClassicalSharp.Commands {
|
||||||
|
|
||||||
public class CommandManager : IGameComponent {
|
public class CommandList : IGameComponent {
|
||||||
|
|
||||||
public static bool IsCommandPrefix( string input ) {
|
const string prefix = "/client";
|
||||||
return Utils.CaselessStarts( input, "/client " ) ||
|
public bool IsCommandPrefix( string input ) {
|
||||||
Utils.CaselessEquals( input, "/client" );
|
if( game.Network.IsSinglePlayer && Utils.CaselessStarts( input, "/" ) )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return Utils.CaselessStarts( input, prefix + " " )
|
||||||
|
|| Utils.CaselessEquals( input, prefix );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Game game;
|
protected Game game;
|
||||||
@ -21,10 +25,9 @@ namespace ClassicalSharp.Commands {
|
|||||||
Register( new InfoCommand() );
|
Register( new InfoCommand() );
|
||||||
Register( new RenderTypeCommand() );
|
Register( new RenderTypeCommand() );
|
||||||
|
|
||||||
if( game.Network.IsSinglePlayer ) {
|
if( !game.Network.IsSinglePlayer ) return;
|
||||||
Register( new ModelCommand() );
|
Register( new ModelCommand() );
|
||||||
Register( new CuboidCommand() );
|
Register( new CuboidCommand() );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Ready( Game game ) { }
|
public void Ready( Game game ) { }
|
||||||
@ -42,37 +45,41 @@ namespace ClassicalSharp.Commands {
|
|||||||
RegisteredCommands.Add( command );
|
RegisteredCommands.Add( command );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Command GetMatch( string commandName ) {
|
public Command GetMatch( string cmdName ) {
|
||||||
Command match = null;
|
Command match = null;
|
||||||
for( int i = 0; i < RegisteredCommands.Count; i++ ) {
|
for( int i = 0; i < RegisteredCommands.Count; i++ ) {
|
||||||
Command cmd = RegisteredCommands[i];
|
Command cmd = RegisteredCommands[i];
|
||||||
if( !Utils.CaselessStarts( cmd.Name, commandName ) ) continue;
|
if( !Utils.CaselessStarts( cmd.Name, cmdName ) ) continue;
|
||||||
|
|
||||||
if( match != null ) {
|
if( match != null ) {
|
||||||
game.Chat.Add( "&e/client: Multiple commands found that start with: \"&f" + commandName + "&e\"." );
|
game.Chat.Add( "&e/client: Multiple commands found that start with: \"&f" + cmdName + "&e\"." );
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
match = cmd;
|
match = cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( match == null )
|
if( match == null )
|
||||||
game.Chat.Add( "&e/client: Unrecognised command: \"&f" + commandName + "&e\"." );
|
game.Chat.Add( "&e/client: Unrecognised command: \"&f" + cmdName + "&e\"." );
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Execute( string text ) {
|
public void Execute( string text ) {
|
||||||
|
if( Utils.CaselessStarts( text, prefix ) ) {
|
||||||
|
text = text.Substring( prefix.Length ).TrimStart( ' ' );
|
||||||
|
text = "/" + text;
|
||||||
|
}
|
||||||
|
|
||||||
CommandReader reader = new CommandReader( text );
|
CommandReader reader = new CommandReader( text );
|
||||||
if( reader.TotalArgs == 0 ) {
|
string cmdName = reader.Next();
|
||||||
|
if( cmdName == null ) {
|
||||||
game.Chat.Add( "&eList of client commands:" );
|
game.Chat.Add( "&eList of client commands:" );
|
||||||
PrintDefinedCommands( game );
|
PrintDefinedCommands( game );
|
||||||
game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" );
|
game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string commandName = reader.Next();
|
|
||||||
Command cmd = GetMatch( commandName );
|
Command cmd = GetMatch( cmdName );
|
||||||
if( cmd != null ) {
|
if( cmd != null ) cmd.Execute( reader );
|
||||||
cmd.Execute( reader );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PrintDefinedCommands( Game game ) {
|
public void PrintDefinedCommands( Game game ) {
|
@ -8,7 +8,6 @@ namespace ClassicalSharp.Commands {
|
|||||||
public class CommandReader {
|
public class CommandReader {
|
||||||
|
|
||||||
string rawInput;
|
string rawInput;
|
||||||
int firstArgOffset;
|
|
||||||
int curOffset;
|
int curOffset;
|
||||||
|
|
||||||
/// <summary> Returns the next argument, or null if there are no more arguments left. </summary>
|
/// <summary> Returns the next argument, or null if there are no more arguments left. </summary>
|
||||||
@ -33,29 +32,6 @@ namespace ClassicalSharp.Commands {
|
|||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Attempts to parse the next argument as a 32-bit integer. </summary>
|
|
||||||
public bool NextInt( out int value ) {
|
|
||||||
return Int32.TryParse( Next(), out value );
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Attempts to parse the next argument as a 32-bit floating point number. </summary>
|
|
||||||
public bool NextFloat( out float value ) {
|
|
||||||
return Single.TryParse( Next(), out value );
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Attempts to parse the next argument as a 6 digit hex number. </summary>
|
|
||||||
/// <remarks> #xxxxxx or xxxxxx are accepted. </remarks>
|
|
||||||
public bool NextHexColour( out FastColour value ) {
|
|
||||||
return FastColour.TryParse( Next(), out value );
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Attempts to parse the next argument using the specified parsing function. </summary>
|
|
||||||
public bool NextOf<T>( out T value, TryParseFunc<T> parser ) {
|
|
||||||
bool success = parser( Next(), out value );
|
|
||||||
if( !success ) value = default( T );
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MoveNext() {
|
bool MoveNext() {
|
||||||
if( curOffset >= rawInput.Length ) return false;
|
if( curOffset >= rawInput.Length ) return false;
|
||||||
int next = rawInput.IndexOf( ' ', curOffset );
|
int next = rawInput.IndexOf( ' ', curOffset );
|
||||||
@ -66,41 +42,9 @@ namespace ClassicalSharp.Commands {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Total number of arguments yet to be processed. </summary>
|
|
||||||
public int RemainingArgs {
|
|
||||||
get { return CountArgsFrom( curOffset ); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Total number of arguments provided by the user. </summary>
|
|
||||||
public int TotalArgs {
|
|
||||||
get { return CountArgsFrom( firstArgOffset ); }
|
|
||||||
}
|
|
||||||
|
|
||||||
int CountArgsFrom( int startOffset ) {
|
|
||||||
int count = 0;
|
|
||||||
int pos = curOffset;
|
|
||||||
curOffset = startOffset;
|
|
||||||
while( MoveNext() ) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
curOffset = pos;
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Rewinds the internal state back to the first argument. </summary>
|
|
||||||
public void Reset() {
|
|
||||||
curOffset = firstArgOffset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandReader( string input ) {
|
public CommandReader( string input ) {
|
||||||
rawInput = input.TrimEnd( ' ' );
|
rawInput = input.TrimEnd( ' ' );
|
||||||
// Check that the string has at least one argument - the command name
|
curOffset = 1; // skip start / for the ocmmand
|
||||||
int firstSpaceIndex = rawInput.IndexOf( ' ' );
|
|
||||||
if( firstSpaceIndex < 0 ) {
|
|
||||||
firstSpaceIndex = rawInput.Length - 1;
|
|
||||||
}
|
|
||||||
firstArgOffset = firstSpaceIndex + 1;
|
|
||||||
curOffset = firstArgOffset;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ namespace ClassicalSharp.Commands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override void Execute( CommandReader reader ) {
|
public override void Execute( CommandReader reader ) {
|
||||||
game.CommandManager.PrintDefinedCommands( game );
|
game.CommandList.PrintDefinedCommands( game );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,10 +38,10 @@ namespace ClassicalSharp.Commands {
|
|||||||
string cmdName = reader.Next();
|
string cmdName = reader.Next();
|
||||||
if( cmdName == null ) {
|
if( cmdName == null ) {
|
||||||
game.Chat.Add( "&eList of client commands:" );
|
game.Chat.Add( "&eList of client commands:" );
|
||||||
game.CommandManager.PrintDefinedCommands( game );
|
game.CommandList.PrintDefinedCommands( game );
|
||||||
game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" );
|
game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" );
|
||||||
} else {
|
} else {
|
||||||
Command cmd = game.CommandManager.GetMatch( cmdName );
|
Command cmd = game.CommandList.GetMatch( cmdName );
|
||||||
if( cmd == null ) return;
|
if( cmd == null ) return;
|
||||||
string[] help = cmd.Help;
|
string[] help = cmd.Help;
|
||||||
for( int i = 0; i < help.Length; i++ )
|
for( int i = 0; i < help.Length; i++ )
|
||||||
|
@ -34,8 +34,8 @@ namespace ClassicalSharp {
|
|||||||
text = text.TrimEnd( trimChars );
|
text = text.TrimEnd( trimChars );
|
||||||
if( String.IsNullOrEmpty( text ) ) return;
|
if( String.IsNullOrEmpty( text ) ) return;
|
||||||
|
|
||||||
if( CommandManager.IsCommandPrefix( text ) ) {
|
if( game.CommandList.IsCommandPrefix( text ) ) {
|
||||||
game.CommandManager.Execute( text );
|
game.CommandList.Execute( text );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
game.Network.SendChat( text, partial );
|
game.Network.SendChat( text, partial );
|
||||||
|
@ -89,7 +89,7 @@ namespace ClassicalSharp {
|
|||||||
UpdateProjection();
|
UpdateProjection();
|
||||||
|
|
||||||
Gui = AddComponent( new GuiInterface( this ) );
|
Gui = AddComponent( new GuiInterface( this ) );
|
||||||
CommandManager = AddComponent( new CommandManager() );
|
CommandList = AddComponent( new CommandList() );
|
||||||
SelectionManager = AddComponent( new SelectionManager() );
|
SelectionManager = AddComponent( new SelectionManager() );
|
||||||
WeatherRenderer = AddComponent( new WeatherRenderer() );
|
WeatherRenderer = AddComponent( new WeatherRenderer() );
|
||||||
HeldBlockRenderer = AddComponent( new HeldBlockRenderer() );
|
HeldBlockRenderer = AddComponent( new HeldBlockRenderer() );
|
||||||
|
@ -101,7 +101,7 @@ namespace ClassicalSharp {
|
|||||||
public IDrawer2D Drawer2D;
|
public IDrawer2D Drawer2D;
|
||||||
public GuiInterface Gui;
|
public GuiInterface Gui;
|
||||||
|
|
||||||
public CommandManager CommandManager;
|
public CommandList CommandList;
|
||||||
public SelectionManager SelectionManager;
|
public SelectionManager SelectionManager;
|
||||||
public ParticleManager ParticleManager;
|
public ParticleManager ParticleManager;
|
||||||
public PickedPosRenderer Picking;
|
public PickedPosRenderer Picking;
|
||||||
|
@ -20,7 +20,6 @@ namespace ClassicalSharp {
|
|||||||
public delegate TResult Func<T1, TResult>( T1 arg1 );
|
public delegate TResult Func<T1, TResult>( T1 arg1 );
|
||||||
public delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2 );
|
public delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2 );
|
||||||
public delegate TResult Func<T1, T2, T3, TResult>( T1 arg1, T2 arg2, T3 arg3 );
|
public delegate TResult Func<T1, T2, T3, TResult>( T1 arg1, T2 arg2, T3 arg3 );
|
||||||
public delegate bool TryParseFunc<T>( string s, out T value );
|
|
||||||
// ################################################################
|
// ################################################################
|
||||||
|
|
||||||
public static partial class Utils {
|
public static partial class Utils {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user