-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathCommandUsage.java
More file actions
86 lines (76 loc) · 2.63 KB
/
Copy pathCommandUsage.java
File metadata and controls
86 lines (76 loc) · 2.63 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ch.njol.skript.command;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.util.Utils;
import net.kyori.adventure.text.Component;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.text.TextComponentParser;
/**
* Holds info about the usage of a command.
* TODO: replace with record when java 17
* @deprecated There is no direct replacement for this class.
*/
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public class CommandUsage {
/**
* A dynamic usage message that can contain expressions.
*/
private final VariableString usage;
/**
* A fallback usage message that can be used in non-event environments,
* like when registering the Bukkit command.
*/
private final String defaultUsage;
/**
* @param usage The dynamic usage message, can contain expressions.
* @param defaultUsage A fallback usage message for use in non-event environments.
*/
public CommandUsage(@Nullable VariableString usage, String defaultUsage) {
if (usage == null) {
// Manually escape quotes. This is not a good solution, as it doesn't handle many other issues, like % in
// commands, but in lieu of re-writing the argument parser and command logic completely, I believe this is
// a decent stop-gap measure for using " in commands.
defaultUsage = VariableString.quote(defaultUsage);
usage = VariableString.newInstance(defaultUsage);
assert usage != null;
}
this.usage = usage;
this.defaultUsage = defaultUsage;
}
/**
* @return The usage message as a {@link VariableString}.
*/
public VariableString getRawUsage() {
return usage;
}
/**
* Get the usage message without an event to evaluate it.
* @return The evaluated usage message.
*/
public String getUsage() {
return getUsage(null);
}
/**
* @param event The event used to evaluate the usage message.
* @return The evaluated usage message.
*/
public String getUsage(@Nullable Event event) {
if (event != null || usage.isSimple())
return usage.toString(event);
return defaultUsage;
}
/**
* @param event The event used to evaluate the usage message.
* @return The evaluated usage message as an Adventure {@link Component} with
* Skript color tags ({@code <red>}, {@code &c}, etc.) parsed into real
* chat formatting via {@link TextComponentParser#parse(Object)}. Command
* usage strings are server-controlled, so unsafe tags are appropriate.
*/
public Component getUsageComponent(@Nullable Event event) {
return TextComponentParser.instance().parse(getUsage(event));
}
@Override
public String toString() {
return getUsage();
}
}