8000 Adds function argument tag tests · CommandAPI/CommandAPI@aeeb91d · GitHub
[go: up one dir, main page]

Skip to content

Commit aeeb91d

Browse files
committed
Adds function argument tag tests
1 parent b797e8e commit aeeb91d

File tree

4 files changed

+98
-2
lines changed
  • commandapi-platforms/commandapi-bukkit

4 files changed

+98
-2
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/wrappers/SimpleFunctionWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* A wrapper class for Minecraft 1.12's functions
3434
*/
35-
public class SimpleFunctionWrapper implements Keyed {
35+
public class SimpleFunctionWrapper implements Keyed {
3636

3737
final NamespacedKey minecraftKey;
3838
final ToIntFunction<Object> functionExecutor; //What the function does

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-impl-1.19.4/src/main/java/dev/jorel/commandapi/test/MockNMS.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ public void addFunction(NamespacedKey key, List<String> commands) {
518518
this.functions.put(resourceLocation, CommandFunction.fromLines(resourceLocation, Brigadier.getCommandDispatcher(), css, commands));
519519
}
520520

521+
@SuppressWarnings("unchecked")
522+
@Override
523+
public void addTag(NamespacedKey key, List<List<String>> commands) {
524+
ResourceLocation resourceLocation = new ResourceLocation(key.toString());
525+
CommandSourceStack css = getBrigadierSourceFromCommandSender(new BukkitPlayer(Bukkit.getOnlinePlayers().iterator().next()));
526+
527+
List<CommandFunction> tagFunctions = new ArrayList<>();
528+
for(List<String> functionCommands : commands) {
529+
tagFunctions.add(CommandFunction.fromLines(resourceLocation, Brigadier.getCommandDispatcher(), css, functionCommands));
530+
}
531+
this.tags.put(resourceLocation, tagFunctions);
532+
}
533+
521534
@Override
522535
public org.bukkit.advancement.Advancement addAdvancement(NamespacedKey key) {
523536
advancementDataWorld.advancements.advancements.put(new ResourceLocation(key.toString()),

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-impl/src/main/java/dev/jorel/commandapi/test/MockPlatform.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public static <T> T getFieldAs(Class<?> className, String fieldName, Object inst
165165

166166
public abstract org.bukkit.advancement.Advancement addAdvancement(NamespacedKey key);
167167

168-
public void addFunction(NamespacedKey key, List<String> commands) {}; // TODO: Implenent for other NMS
168+
public void addFunction(NamespacedKey key, List<String> commands) {}; // TODO: Implement for other NMS
169+
public void addTag(NamespacedKey key, List<List<String>> commands) {}; // TODO: Implement for other NMS
169170

170171
/**
171172
* Converts 1.16.5 and below potion effect names to NamespacedKey names. For

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentFunctionTests.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,57 @@ void executionTestWithFunctionArgument() {
8484
assertNoMoreResults(results);
8585
assertNoMoreResults(sayResults);
8686
}
87+
88+
@Test
89+
void executionTestWithFunctionArgumentTag() {
90+
Mut<FunctionWrapper[]> results = Mut.of();
91+
Mut<String> sayResults = Mut.of();
92+
93+
new CommandAPICommand("test")
94+
.withArguments(new FunctionArgument("function"))
95+
.executesPlayer((player, args) -> {
96+
results.set((FunctionWrapper[]) args.get("function"));
97+
})
98+
.register();
99+
100+
new CommandAPICommand("mysay")
101+
.withArguments(new GreedyStringArgument("message"))
102+
.executesPlayer((player, args) -> {
103+
sayResults.set(args.getUnchecked("message"));
104+
})
105+
.register();
106+
107+
PlayerMock player = server.addPlayer();
108+
109+
// Declare our functions on the server
110+
MockPlatform.getInstance().addTag(new NamespacedKey("ns", "mytag"), List.of(
111+
List.of("mysay hi", "mysay bye"),
112+
List.of("mysay hello", "mysay world")
113+
));
114+
115+
// Run the /test command
116+
server.dispatchCommand(player, "test #ns:mytag");
117+
118+
// Check that the FunctionArgument has one entry and it hasn't run the /mysay
119+
// command
120+
FunctionWrapper[] result = results.get();
121+
assertEquals(2, result.length);
122+
assertNoMoreResults(sayResults);
123+
124+
// Run the function (which should run the /mysay command)
125+
for(FunctionWrapper wrapper : result) {
126+
wrapper.run();
127+
}
128+
129+
// Check that /mysay was run successfully...
130+
assertEquals("hi", sayResults.get());
131+
assertEquals("bye", sayResults.get());
132+
assertEquals("hello", sayResults.get());
133+
assertEquals("world", sayResults.get());
134+
135+
assertNoMoreResults(results);
136+
assertNoMoreResults(sayResults);
137+
}
87138

88139
/********************
89140
* Suggestion tests *
@@ -112,4 +163,35 @@ void suggestionTestWithFunctionArgument() {
112163
assertEquals(List.of("mynamespace:myotherfunc", "ns:myfunc"), server.getSuggestions(player, "test "));
113164
}
114165

166+
@Test
167+
void suggestionTestWithFunctionArgumentTag() {
168+
new CommandAPICommand("test")
169+
.withArguments(new FunctionArgument("function"))
170+
.executesPlayer(P_EXEC)
171+
.register();
172+
173+
new CommandAPICommand("mysay")
174+
.withArguments(new GreedyStringArgument("message"))
175+
.executesPlayer(P_EXEC)
176+
.register();
177+
178+
PlayerMock player = server.addPlayer();
179+
180+
// Declare our functions on the server
181+
MockPlatform.getInstance().addFunction(new NamespacedKey("ns", "myfunc"), List.of("mysay hi"));
182+
MockPlatform.getInstance().addFunction(new NamespacedKey("mynamespace", "myotherfunc"), List.of("mysay bye"));
183+
MockPlatform.getInstance().addTag(new NamespacedKey("ns", "mytag"), List.of(
184+
List.of("mysay hi", "mysay bye"),
185+
List.of("mysay hello", "mysay world")
186+
));
187+
MockPlatform.getInstance().addTag(new NamespacedKey("namespace", "myothertag"), List.of(
188+
List.of("mysay hi", "mysay bye"),
189+
List.of("mysay hello", "mysay world")
190+
));
191+
192+
// /test
193+
// Should suggest #namespace:myothertag, #ns:mytag, mynamespace:myotherfunc and ns:myfunc
194+
assertEquals(List.of("#namespace:myothertag", "#ns:mytag", "mynamespace:myotherfunc", "ns:myfunc"), server.getSuggestions(player, "test "));
195+
}
196+
115197
}

0 commit comments

Comments
 (0)
0