Skip to content

Commit d0292ec

Browse files
authored
Merge pull request #18 from kylepls/commands
continued work on #5 added #on method to PluginCommand
2 parents 9f4d2c6 + 9150f5a commit d0292ec

File tree

6 files changed

+120
-38
lines changed

6 files changed

+120
-38
lines changed

mcspring-api/mcspring-subcommands/src/main/java/in/kyle/mcspring/subcommands/PluginCommand.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,70 @@ private Optional<Double> tryParseDouble(String intString) {
198198
return Optional.empty();
199199
}
200200
}
201-
201+
202+
protected void callOn(String command, Executors executors, int argSize) {
203+
if(hasExecutablePart()) {
204+
if(parts.get(0).equalsIgnoreCase(command)) {
205+
parts.remove(0);
206+
then(() -> invoke(executors, argSize));
207+
}
208+
} else {
209+
state = State.MISSING_ARG;
210+
}
211+
}
212+
213+
public <A> void on(String command, Executors.O1<A> e) {
214+
callOn(command, e, 1);
215+
}
216+
217+
public <A, B> void on(String command, Executors.O2<A, B> e) {
218+
callOn(command, e, 2);
219+
}
220+
221+
public <A, B, C> void on(String command, Executors.O3<A, B, C> e) {
222+
callOn(command, e, 3);
223+
}
224+
225+
public <A, B, C, D> void on(String command, Executors.O4<A, B, C, D> e) {
226+
callOn(command, e, 4);
227+
}
228+
229+
public <A, B, C, D, E> void on(String command, Executors.O5<A, B, C, D, E> e) {
230+
callOn(command, e, 5);
231+
}
232+
233+
public <A, B, C, D, E, F> void on(String command, Executors.O6<A, B, C, D, E, F> e) {
234+
callOn(command, e, 6);
235+
}
236+
237+
public void on(String command, Executors.O0 executors) {
238+
callOn(command, executors, 0);
239+
}
240+
241+
public <A> void on(String command, Executors.E1<A> e) {
242+
callOn(command, e, 1);
243+
}
244+
245+
public <A, B> void on(String command, Executors.E2<A, B> e) {
246+
callOn(command, e, 2);
247+
}
248+
249+
public <A, B, C> void on(String command, Executors.E3<A, B, C> e) {
250+
callOn(command, e, 3);
251+
}
252+
253+
public <A, B, C, D> void on(String command, Executors.E4<A, B, C, D> e) {
254+
callOn(command, e, 4);
255+
}
256+
257+
public <A, B, C, D, E> void on(String command, Executors.E5<A, B, C, D, E> e) {
258+
callOn(command, e, 5);
259+
}
260+
261+
public <A, B, C, D, E, F> void on(String command, Executors.E6<A, B, C, D, E, F> e) {
262+
callOn(command, e, 6);
263+
}
264+
202265
public <A> void then(Executors.O0 e) {
203266
then(() -> invoke(e, 0));
204267
}

mcspring-api/mcspring-subcommands/src/main/java/in/kyle/mcspring/subcommands/PluginCommandTabCompletable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public <T> void withMap(Map<String, T> options, Function<String, String> invalid
7272
tabCompletionOptions.addAll(options.keySet());
7373
}
7474
}
75+
76+
@Override
77+
protected void callOn(String command, Executors executors, int argSize) {
78+
tabCompletionOptions.add(command);
79+
super.callOn(command, executors, argSize);
80+
}
7581

7682
@Override
7783
public void otherwise(Supplier<String> supplier) {

mcspring-api/mcspring-subcommands/src/test/java/in/kyle/mcspring/SpringSpigotSupport.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

mcspring-api/mcspring-subcommands/src/test/java/in/kyle/mcspring/subcommands/TestPluginCommand.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ void setup() {
3030
sender.getMessages().subscribe(outputMessages::add);
3131
}
3232

33+
@Test
34+
void testDirectExecutor() {
35+
class Test {
36+
37+
void exec1(PluginCommand command) {
38+
command.on("test", this::handler1);
39+
}
40+
41+
void exec2(PluginCommand command) {
42+
command.withString();
43+
command.on("test2", this::handler2);
44+
}
45+
46+
void handler1(CommandSender sender) {
47+
sender.sendMessage("Hello World");
48+
}
49+
50+
void handler2(CommandSender sender, String string) {
51+
sender.sendMessage(string);
52+
}
53+
}
54+
55+
Test test = new Test();
56+
console.run(sender, "test", test::exec1);
57+
assertThat(outputMessages).containsExactly("Hello World");
58+
outputMessages.clear();
59+
console.run(sender, "hello test2", test::exec2);
60+
assertThat(outputMessages).containsExactly("hello");
61+
}
62+
3363
@Test
3464
void testSenderArg() {
3565
class Test {

mcspring-api/mcspring-subcommands/src/test/java/in/kyle/mcspring/subcommands/TestTabCompletion.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package in.kyle.mcspring.subcommands;
22

3+
import in.kyle.mcspring.test.MCSpringTest;
34
import org.bukkit.command.CommandSender;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
@@ -18,7 +19,7 @@
1819
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.assertj.core.api.Assertions.fail;
2021

21-
@SpringBootTest
22+
@MCSpringTest
2223
public class TestTabCompletion {
2324

2425
@Autowired
@@ -33,6 +34,23 @@ void setup() {
3334
sender.getMessages().subscribe(outputMessages::add);
3435
}
3536

37+
@Test
38+
void testTabWithDirectExecution() {
39+
class Test {
40+
41+
void root(PluginCommand command) {
42+
command.on("test", this::exec);
43+
}
44+
45+
void exec(String string) {
46+
fail("Should not run");
47+
}
48+
}
49+
Test test = new Test();
50+
var completions = tabDiscovery.getCompletions(sender, "", test::root);
51+
assertThat(completions).containsExactly("test");
52+
}
53+
3654
@Test
3755
void testNoTab() {
3856
class Test {

mcspring-build/mcspring-plugin-manager/src/main/java/in/kyle/mcspring/manager/controller/BukkitPluginUnloader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@Component
2727
@RequiredArgsConstructor
2828
@ConditionalOnBean(Plugin.class)
29+
@SuppressWarnings("unchecked")
2930
class BukkitPluginUnloader {
3031

3132
private final PluginManager pluginManager;

0 commit comments

Comments
 (0)