-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiOptionCommand.cs
More file actions
61 lines (54 loc) · 1.92 KB
/
Copy pathMultiOptionCommand.cs
File metadata and controls
61 lines (54 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using ChatCommandAPI.Utils;
namespace ChatCommandAPI;
public abstract class MultiOptionCommand<T> : Command
where T : Enum
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor
protected MultiOptionCommand()
{
var values = Enum.GetValues(typeof(T));
if (values.Length == 0)
throw new ArgumentException(
$"Invalid enum type {typeof(T).AssemblyQualifiedName} (contains no values)",
nameof(T)
);
}
public sealed override string[] Syntax
{
get
{
var values = Enum.GetValues(typeof(T));
return values.Length <= 5 ? [$"{{ {string.Join(" | ", values)} }}"] : ["[value]"];
}
}
/// <summary>
/// The current value of the command
/// </summary>
/// <remarks>Use override to point this to a static variable (with your preferred default value)</remarks>
public virtual T CurrentValue { get; set; }
public sealed override void Invoke(string args)
{
if (string.IsNullOrWhiteSpace(args))
{
var values = Enum.GetNames(typeof(T));
Chat.Print(
$"{typeof(T).Name} values:\n<indent=15%>{string.Join('\n', values)}</indent>\n{Name} value: <color=#{(Enum.IsDefined(typeof(T), CurrentValue) ? "00ffff" : "ff0000")}>{CurrentValue}</color>"
);
return;
}
if (!Enum.TryParse(typeof(T), args, out var result))
throw new InvalidArgumentsException();
var oldValue = CurrentValue;
CurrentValue = (T)result;
Changed(oldValue);
}
/// <summary>
/// The callback when the user changes the value
/// </summary>
/// <param name="oldValue">The value before the change</param>
protected virtual void Changed(T oldValue)
{
Chat.Print($"{Name} changed to {CurrentValue}");
}
}