Skip to content

Commit 0ee29ca

Browse files
committed
Updated Command System.
1 parent 17723a7 commit 0ee29ca

File tree

11 files changed

+238
-1
lines changed

11 files changed

+238
-1
lines changed

src/main/java/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
requires static org.spongepowered.mixin;
1818

1919
requires com.google.gson;
20+
requires guava;
2021

2122
// Config API
2223
exports org.mangorage.mangobotcore.api.config.v1;
@@ -25,6 +26,9 @@
2526
exports org.mangorage.mangobotcore.api.jda.command.v1;
2627
exports org.mangorage.mangobotcore.api.jda.event.v1;
2728

29+
// Command API
30+
exports org.mangorage.mangobotcore.api.command.v1;
31+
2832
// Plugin API
2933
exports org.mangorage.mangobotcore.api.plugin;
3034
exports org.mangorage.mangobotcore.api.plugin.v1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
public interface ICommandContext {
4+
<T> T get(Class<T> tClass);
5+
boolean hasType(Class<?> tClass);
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
import org.mangorage.mangobotcore.internal.command.CommandDispatcher;
4+
5+
public interface ICommandDispatcher {
6+
static ICommandDispatcher create(ICommandResult invalid) {
7+
return new CommandDispatcher(invalid);
8+
}
9+
10+
void register(ICommandNode commandNode);
11+
ICommandNode getCommandNode(String name);
12+
13+
ICommandResult execute(String input, ICommandContext context);
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
import org.mangorage.mangobotcore.api.util.misc.Arguments;
4+
5+
public interface ICommandExecutor {
6+
ICommandResult execute(ICommandContext context, Arguments arguments);
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
import org.mangorage.mangobotcore.internal.command.CommandNodeBuilder;
4+
5+
import java.util.List;
6+
7+
public interface ICommandNode {
8+
static ICommandNodeBuilder create(String name) {
9+
return new CommandNodeBuilder(name);
10+
}
11+
12+
String getName();
13+
String getUsage();
14+
List<ICommandNode> getSubNodes();
15+
16+
ICommandResult execute(ICommandContext commandContext, List<String> arguments);
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
import java.util.function.Function;
4+
5+
public interface ICommandNodeBuilder {
6+
ICommandNodeBuilder usage(String usage);
7+
ICommandNodeBuilder then(ICommandNode node);
8+
ICommandNodeBuilder requires(Function<ICommandContext, Boolean> requiresChecker);
9+
ICommandNodeBuilder executes(ICommandExecutor executor);
10+
11+
ICommandNode build();
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
public interface ICommandResult {
4+
String getResultReason();
5+
}

src/main/java/org/mangorage/mangobotcore/api/jda/command/v1/CommandResult.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
package org.mangorage.mangobotcore.api.jda.command.v1;
2424

2525
import net.dv8tion.jda.api.entities.Message;
26+
import org.mangorage.mangobotcore.api.command.v1.ICommandResult;
2627
import org.mangorage.mangobotcore.api.util.jda.MessageSettings;
2728

2829
import java.util.HashMap;
2930

3031

31-
public record CommandResult(String string) {
32+
public record CommandResult(String string) implements ICommandResult {
3233
private static final MessageSettings DEFAULT_SETTINGS = MessageSettings.create().build();
3334
private static final HashMap<String, CommandResult> CACHE = new HashMap<>();
3435

3536
public static CommandResult of(String content) {
3637
return CACHE.computeIfAbsent(content, CommandResult::new);
3738
}
3839

40+
public static final CommandResult INVALID_COMMAND = of("Invalid Command!");
3941
public static final CommandResult PASS = of(null);
4042
public static final CommandResult FAIL = of("An error occurred while executing this command");
4143
public static final CommandResult NO_PERMISSION = of("You don't have permission to use this command!");
@@ -48,4 +50,9 @@ public void accept(Message message) {
4850
if (string() != null)
4951
DEFAULT_SETTINGS.apply(message.reply(string)).queue();
5052
}
53+
54+
@Override
55+
public String getResultReason() {
56+
return string;
57+
}
5158
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.mangorage.mangobotcore.internal.command;
2+
3+
import org.mangorage.mangobotcore.api.command.v1.ICommandContext;
4+
import org.mangorage.mangobotcore.api.command.v1.ICommandDispatcher;
5+
import org.mangorage.mangobotcore.api.command.v1.ICommandNode;
6+
import org.mangorage.mangobotcore.api.command.v1.ICommandResult;
7+
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public final class CommandDispatcher implements ICommandDispatcher {
13+
private final Map<String, ICommandNode> roots = new HashMap<>();
14+
private final ICommandResult defaultInvalid;
15+
16+
public CommandDispatcher(ICommandResult defaultInvalid) {
17+
this.defaultInvalid = defaultInvalid;
18+
}
19+
20+
@Override
21+
public void register(ICommandNode commandNode) {
22+
roots.put(
23+
commandNode.getName(),
24+
commandNode
25+
);
26+
}
27+
28+
@Override
29+
public ICommandNode getCommandNode(String name) {
30+
return roots.get(name);
31+
}
32+
33+
@Override
34+
public ICommandResult execute(String input, ICommandContext context) {
35+
String[] split = input.trim().split("\\s+");
36+
if (split.length == 0)
37+
return defaultInvalid;
38+
39+
ICommandNode root = roots.get(split[0]);
40+
if (root == null)
41+
return defaultInvalid;
42+
43+
return root.execute(context, List.of(split).subList(1, split.length));
44+
}
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.mangorage.mangobotcore.internal.command;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import org.mangorage.mangobotcore.api.command.v1.ICommandContext;
5+
import org.mangorage.mangobotcore.api.command.v1.ICommandExecutor;
6+
import org.mangorage.mangobotcore.api.command.v1.ICommandNode;
7+
import org.mangorage.mangobotcore.api.command.v1.ICommandResult;
8+
import org.mangorage.mangobotcore.api.jda.command.v1.CommandResult;
9+
import org.mangorage.mangobotcore.api.util.misc.Arguments;
10+
import java.util.*;
11+
import java.util.function.Function;
12+
13+
public final class CommandNode implements ICommandNode {
14+
private final String name;
15+
private final String usage;
16+
private final Function<ICommandContext, Boolean> requiresCheck;
17+
private final Map<String, ICommandNode> children;
18+
private final ICommandExecutor executor;
19+
20+
public CommandNode(String name, String usage, Function<ICommandContext, Boolean> requiresCheck, Map<String, ICommandNode> children, ICommandExecutor executor) {
21+
this.name = name;
22+
this.usage = usage;
23+
this.requiresCheck = requiresCheck;
24+
this.children = ImmutableMap.copyOf(children);
25+
this.executor = executor;
26+
}
27+
28+
@Override
29+
public String getName() {
30+
return name;
31+
}
32+
33+
@Override
34+
public String getUsage() {
35+
return usage;
36+
}
37+
38+
@Override
39+
public List<ICommandNode> getSubNodes() {
40+
return null;
41+
}
42+
43+
public ICommandResult execute(ICommandContext ctx, List<String> args) {
44+
if (!requiresCheck.apply(ctx)) {
45+
return CommandResult.NO_PERMISSION;
46+
}
47+
48+
if (args.isEmpty()) {
49+
if (executor == null)
50+
throw new RuntimeException("No executor for command: " + name);
51+
return executor.execute(ctx, Arguments.empty());
52+
}
53+
54+
String next = args.getFirst();
55+
ICommandNode child = children.get(next);
56+
57+
if (child != null) {
58+
return child.execute(ctx, args.subList(1, args.size()));
59+
} else {
60+
if (executor == null)
61+
return CommandResult.FAIL;
62+
63+
return executor.execute(ctx, Arguments.of(args.toArray(String[]::new)));
64+
}
65+
}
66+
}
67+

0 commit comments

Comments
 (0)