|
| 1 | +namespace EssentialsPlugin.ChatHandlers |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Linq; |
| 6 | + using SEModAPIInternal.API.Common; |
| 7 | + using Utility; |
| 8 | + |
| 9 | + public class HandleHelp : ChatHandlerBase |
| 10 | + { |
| 11 | + public override string GetHelp( ) |
| 12 | + { |
| 13 | + return "If you're here, I can't help you."; |
| 14 | + } |
| 15 | + |
| 16 | + public override string GetCommandText( ) |
| 17 | + { |
| 18 | + return "/help"; |
| 19 | + } |
| 20 | + |
| 21 | + public override bool AllowedInConsole( ) |
| 22 | + { |
| 23 | + return true; |
| 24 | + } |
| 25 | + |
| 26 | + public override bool HandleCommand( ulong userId, string[] words ) |
| 27 | + { |
| 28 | + if (userId == 0) |
| 29 | + HandleCommand( userId, words ); |
| 30 | + else if (( words.Length > 0 ) && ( words[0] == "chat" )) |
| 31 | + HandleHelpCommand( userId, words.Skip( 1 ).ToArray( ) ); |
| 32 | + else |
| 33 | + HandleHelpDialog( userId, words ); |
| 34 | + |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// This function displays available help for all the functionality of this plugin |
| 40 | + /// </summary> |
| 41 | + /// <param name="remoteUserId"></param> |
| 42 | + /// <param name="commandParts"></param> |
| 43 | + private void HandleHelpCommand( ulong remoteUserId, IReadOnlyCollection<string> commandParts ) |
| 44 | + { |
| 45 | + if (commandParts.Count == 0) |
| 46 | + { |
| 47 | + List<string> commands = new List<string>( ); |
| 48 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 49 | + if (handler.GetMultipleCommandText( ).Length < 1) |
| 50 | + { |
| 51 | + string commandBase = handler.GetCommandText( ).Split( new[] {" "}, StringSplitOptions.RemoveEmptyEntries ).First( ); |
| 52 | + if (!commands.Contains( commandBase ) && !handler.IsClientOnly( ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 53 | + commands.Add( commandBase ); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + foreach (string cmd in handler.GetMultipleCommandText( )) |
| 58 | + { |
| 59 | + string commandBase = cmd.Split( new[] {" "}, StringSplitOptions.RemoveEmptyEntries ).First( ); |
| 60 | + if (!commands.Contains( commandBase ) && !handler.IsClientOnly( ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 61 | + commands.Add( commandBase ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + string commandList = string.Join( ", ", commands ); |
| 66 | + string info = $"Dedicated Server Essentials v{Essentials.Instance.Version}. Available Commands: {commandList}"; |
| 67 | + Communication.SendPrivateInformation( remoteUserId, info ); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + string helpTarget = string.Join( " ", commandParts ); |
| 72 | + bool found = false; |
| 73 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 74 | + if (handler.GetMultipleCommandText( ).Length < 1) |
| 75 | + { |
| 76 | + if (string.Equals( handler.GetCommandText( ), helpTarget, StringComparison.CurrentCultureIgnoreCase )) |
| 77 | + { |
| 78 | + Communication.SendPrivateInformation( remoteUserId, handler.GetHelp( ) ); |
| 79 | + found = true; |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + foreach (string cmd in handler.GetMultipleCommandText( )) |
| 85 | + if (string.Equals( cmd, helpTarget, StringComparison.CurrentCultureIgnoreCase )) |
| 86 | + { |
| 87 | + Communication.SendPrivateInformation( remoteUserId, handler.GetHelp( ) ); |
| 88 | + found = true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (!found) |
| 93 | + { |
| 94 | + List<string> helpTopics = new List<string>( ); |
| 95 | + |
| 96 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 97 | + { |
| 98 | + // Again, cleanup to one function |
| 99 | + string[] multipleCommandText = handler.GetMultipleCommandText( ); |
| 100 | + if (multipleCommandText.Length == 0) |
| 101 | + { |
| 102 | + if (handler.GetCommandText( ).ToLower( ).StartsWith( helpTarget.ToLower( ) ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 103 | + helpTopics.Add( handler.GetCommandText( ).ToLower( ).Replace( helpTarget.ToLower( ), string.Empty ) ); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + foreach (string cmd in multipleCommandText) |
| 108 | + if (cmd.ToLower( ).StartsWith( helpTarget.ToLower( ) ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 109 | + helpTopics.Add( cmd.ToLower( ).Replace( helpTarget.ToLower( ), string.Empty ) ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (helpTopics.Any( )) |
| 114 | + { |
| 115 | + Communication.SendPrivateInformation( remoteUserId, $"Help topics for command '{helpTarget.ToLower( )}': {string.Join( ",", helpTopics.ToArray( ) )}" ); |
| 116 | + found = true; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (!found) |
| 121 | + Communication.SendPrivateInformation( remoteUserId, "Unknown command" ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// This function displays available help for all the functionality of this plugin in a dialog window |
| 127 | + /// </summary> |
| 128 | + /// <param name="remoteUserId"></param> |
| 129 | + /// <param name="commandParts"></param> |
| 130 | + private void HandleHelpDialog( ulong remoteUserId, IReadOnlyCollection<string> commandParts ) |
| 131 | + { |
| 132 | + if (commandParts.Count == 0) |
| 133 | + { |
| 134 | + List<string> commands = new List<string>( ); |
| 135 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 136 | + if (handler.GetMultipleCommandText( ).Length < 1) |
| 137 | + { |
| 138 | + string commandBase = handler.GetCommandText( ).Split( new[] {" "}, StringSplitOptions.RemoveEmptyEntries ).First( ); |
| 139 | + if (!commands.Contains( commandBase ) && !handler.IsClientOnly( ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 140 | + commands.Add( commandBase ); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + foreach (string cmd in handler.GetMultipleCommandText( )) |
| 145 | + { |
| 146 | + string commandBase = cmd.Split( new[] {" "}, StringSplitOptions.RemoveEmptyEntries ).First( ); |
| 147 | + if (!commands.Contains( commandBase ) && !handler.IsClientOnly( ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 148 | + commands.Add( commandBase ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + string commandList = string.Join( ", ", commands ); |
| 153 | + //take our list of commands, put line breaks between all the entries and stuff it into a dialog winow |
| 154 | + |
| 155 | + Communication.DisplayDialog( remoteUserId, "Help", "Available commands", commandList + "{0}||Type '/help dialog <command>' for more info.", "close" ); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + string helpTarget = string.Join( " ", commandParts); |
| 160 | + bool found = false; |
| 161 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 162 | + if (handler.GetMultipleCommandText( ).Length < 1) |
| 163 | + { |
| 164 | + if (string.Equals( handler.GetCommandText( ), helpTarget, StringComparison.CurrentCultureIgnoreCase )) |
| 165 | + { |
| 166 | + Communication.DisplayDialog( remoteUserId, handler.GetHelpDialog( ) ); |
| 167 | + found = true; |
| 168 | + } |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + foreach (string cmd in handler.GetMultipleCommandText( )) |
| 173 | + if (string.Equals( cmd, helpTarget, StringComparison.CurrentCultureIgnoreCase )) |
| 174 | + { |
| 175 | + Communication.DisplayDialog( remoteUserId, handler.GetHelpDialog( ) ); |
| 176 | + found = true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (!found) |
| 181 | + { |
| 182 | + List<string> helpTopics = new List<string>( ); |
| 183 | + |
| 184 | + foreach (ChatHandlerBase handler in Essentials.ChatHandlers) |
| 185 | + { |
| 186 | + // Again, cleanup to one function |
| 187 | + string[] multipleCommandText = handler.GetMultipleCommandText( ); |
| 188 | + if (multipleCommandText.Length == 0) |
| 189 | + { |
| 190 | + if (handler.GetCommandText( ).ToLower( ).StartsWith( helpTarget.ToLower( ) ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 191 | + helpTopics.Add( handler.GetCommandText( ).ToLower( ).Replace( helpTarget.ToLower( ), string.Empty ) ); |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + foreach (string cmd in multipleCommandText) |
| 196 | + if (cmd.ToLower( ).StartsWith( helpTarget.ToLower( ) ) && ( !handler.IsAdminCommand( ) || ( handler.IsAdminCommand( ) && ( PlayerManager.Instance.IsUserAdmin( remoteUserId ) || ( remoteUserId == 0 ) ) ) )) |
| 197 | + helpTopics.Add( cmd.ToLower( ).Replace( helpTarget.ToLower( ), string.Empty ) ); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if (helpTopics.Any( )) |
| 202 | + { |
| 203 | + Communication.DisplayDialog( remoteUserId, "Help", helpTarget.ToLower( ), string.Format( "Help topics for command '{0}': {1}", helpTarget.ToLower( ), string.Join( ",", helpTopics.ToArray( ) ) ) ); |
| 204 | + found = true; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (!found) |
| 209 | + Communication.SendPrivateInformation( remoteUserId, "Unknown command" ); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments