Skip to content

Commit c3ee50e

Browse files
More testing and adjustments
1 parent 8606b5b commit c3ee50e

3 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/main/java/ch/njol/skript/expressions/ExprCommandInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bukkit.command.Command;
1313
import org.bukkit.command.CommandMap;
1414
import org.bukkit.command.PluginCommand;
15+
import org.bukkit.command.PluginIdentifiableCommand;
1516
import org.bukkit.command.defaults.BukkitCommand;
1617
import org.bukkit.event.Event;
1718
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
@@ -62,8 +63,8 @@ private enum InfoType {
6263
PERMISSION(Command::getPermission),
6364
PERMISSION_MESSAGE(Command::getPermissionMessage),
6465
PLUGIN(command -> {
65-
if (command instanceof PluginCommand) {
66-
return ((PluginCommand) command).getPlugin().getName();
66+
if (command instanceof PluginIdentifiableCommand pluginCommand) {
67+
return pluginCommand.getPlugin().getName();
6768
} else if (command instanceof BukkitCommand) {
6869
return "Bukkit";
6970
} else if (command.getClass().getPackage().getName().startsWith("org.spigot")) {

src/main/java/org/skriptlang/skript/bukkit/command/elements/structures/util/SubCommandEntryData.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ protected List<String> getValue(String value) {
8484
}
8585
})
8686
.addEntry("description", null, true)
87-
// TODO this was previously a VariableString. need to handle that
88-
.addEntry("usage", null, true)
87+
.addEntryData(new VariableStringEntryData("usage", null, true))
8988
.addEntry("prefix", null, true)
9089
.addEntry("permission", null, true)
9190
.addEntryData(new KeyValueEntryData<ExecutableBy>("executable by", null, true) {
@@ -188,10 +187,16 @@ public SubCommandEntryData(String key, boolean optional, boolean multiple) {
188187
}
189188

190189
// command usage
191-
String usage = entryContainer.getOptional("usage", String.class, false);
192-
if (!isRoot && usage != null) {
193-
Skript.error("Only the root of a command may have a usage.");
194-
return null;
190+
String usage = null;
191+
VariableString variableUsage = entryContainer.getOptional("usage", VariableString.class, false);
192+
if (variableUsage != null) {
193+
if (!isRoot) {
194+
Skript.error("Only the root of a command may have a usage.");
195+
return null;
196+
}
197+
if (variableUsage.isSimple()) {
198+
usage = variableUsage.toString(null);
199+
}
195200
}
196201

197202
// command prefix (custom namespace)
@@ -319,7 +324,6 @@ public SubCommandEntryData(String key, boolean optional, boolean multiple) {
319324
}
320325

321326
// attach subcommand pieces
322-
// TODO verify error behavior...
323327
List<ArgumentBuilder<CommandSourceStack, ?>> subcommands =
324328
entryContainer.getAll("subcommand", Result.class, false).stream()
325329
.flatMap(result -> result.arguments().stream())
@@ -398,7 +402,6 @@ public boolean canCreateWith(Node node) {
398402

399403
// this is intentionally placed AFTER iterating over the children
400404
// for conflicting command arguments, the argument at this level should be preferred over a subcommand's argument
401-
// TODO there is actually more complexity here to handle
402405
// it is not guaranteed to conflict if two arguments are at the same level
403406
// e.g. Player-then-Number conflicts but Number-then-Player doesn't
404407
if (commandElement.isLeaf()) {

src/test/skript/tests/bukkit/command/StructCommand.sk

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ using local variable type hints
33
options:
44
command: skriptcommand
55

6-
# expect error for invalid executable by for subcommand
6+
# expect error for missing trigger given no subcommands
77
parse:
88
results: {-StructCommand::entries::1::*}
99
code:
1010
command /entries_test_1:
11+
12+
# expect error for invalid executable by for subcommand
13+
parse:
14+
results: {-StructCommand::entries::2::*}
15+
code:
16+
command /entries_test_2:
1117
executable by: players
1218
trigger:
1319
stop
@@ -16,6 +22,13 @@ parse:
1622
trigger:
1723
stop
1824

25+
# expect error for invalid subcommand and subsequently missing trigger (given no valid subcommands)
26+
parse:
27+
results: {-StructCommand::entries::3::*}
28+
code:
29+
command /entries_test_3:
30+
subcommand test:
31+
1932
# expect error for duplicate argument names
2033
parse:
2134
results: {-StructCommand::args::1::*}
@@ -64,6 +77,14 @@ parse:
6477
trigger:
6578
set {_a::*} to {_x::*} in lowercase
6679

80+
command /structcommand:
81+
aliases: /structcommand2
82+
description: This is the description
83+
usage: "This is the usage"
84+
permission: command.structcommand
85+
trigger:
86+
stop
87+
6788
command /test-subcommand <number>:
6889
subcommand <number>:
6990
trigger:
@@ -96,7 +117,10 @@ command /commandtest <string="player">:
96117

97118
test "StructCommand":
98119
# parsing
99-
assert {-StructCommand::entries::1::*} is "It is not possible to restrict execution to the console as the parent command is only executable by players."
120+
set {_error_missing_trigger} to "You must have a 'trigger' entry if there are no subcommands!"
121+
assert {-StructCommand::entries::1::*} is {_error_missing_trigger}
122+
assert {-StructCommand::entries::2::*} is "It is not possible to restrict execution to the console as the parent command is only executable by players."
123+
assert {-StructCommand::entries::3::*} is {_error_missing_trigger} and {_error_missing_trigger}
100124
assert {-StructCommand::args::1::*} is "The argument name 'x' was already used."
101125
assert {-StructCommand::args::2::*} is "text arguments do not support minimum or maximum values."
102126
assert {-StructCommand::args::3::*} is not set
@@ -105,6 +129,15 @@ test "StructCommand":
105129
assert {-StructCommand::hints::1::*} is "Expected variable '{_x}' to be a text, but it is a number" with "Hint failed (%{StructCommand::hints::1::*}%)"
106130
assert {-StructCommand::hints::2::*} is "Expected variable '{_x::*}' to be a text, but it is a number" with "Hint failed (%{StructCommand::hints::2::*}%)"
107131

132+
# entry validation
133+
assert the main command name of the command "structcommand" is "structcommand"
134+
assert the description of the command "structcommand" is "This is the description"
135+
assert the label of the command "structcommand" is "structcommand"
136+
#assert the usage of the command "structcommand" is "This is the usage"
137+
assert the aliases of the command "structcommand" are "structcommand2" and "skript:structcommand2"
138+
#assert the permission of the command "structcommand" is "skript.structcommand"
139+
assert the plugin of the command "structcommand" is "Skript"
140+
108141
# execution
109142
execute command "test-subcommand 10"
110143
assert {-test-subcommand} is 10 with "root command execution failed"

0 commit comments

Comments
 (0)