Skip to content

Commit 46d77b5

Browse files
reworked Command
removed CommandExecutor added methods execute and tabComplete to Command made Command keep track of the command name CommandManager now only takes a CommandBuilder, since the built Command will supply the command's name
1 parent 28fa5c8 commit 46d77b5

File tree

7 files changed

+116
-125
lines changed

7 files changed

+116
-125
lines changed

src/main/java/com/datasiqn/commandcore/CommandCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public static CommandCore getInstance() {
173173
command.setExecutor(mainCommand);
174174
command.setTabCompleter(mainCommand);
175175

176-
if (options.createHelpCommand()) instance.commandManager.registerCommand("help", new CommandBuilder()
176+
if (options.createHelpCommand()) instance.commandManager.registerCommand(new CommandBuilder("help")
177177
.description("Shows the help menu")
178178
.then(ArgumentBuilder.argument(ArgumentType.COMMAND, "command")
179179
.executes(context -> {

src/main/java/com/datasiqn/commandcore/MainCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
3939
}
4040
List<String> listArgs = new ArrayList<>(Arrays.asList(args));
4141
listArgs.remove(0);
42-
Result<None, List<String>> output = cmd.getExecutor().execute(CommandCore.createContext(CommandCore.createSource(sender), cmd, args[0], new ListArguments(listArgs)));
42+
Result<None, List<String>> output = cmd.execute(CommandCore.createContext(CommandCore.createSource(sender), cmd, args[0], new ListArguments(listArgs)));
4343
output.ifError(messages -> {
4444
for (String message : messages) sender.sendMessage(ChatColor.RED + message);
4545
sender.sendMessage(ChatColor.GRAY + "Usage(s):");
@@ -65,7 +65,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bu
6565
if (cmd == null || (cmd.getPermissionString() != null && !sender.hasPermission(cmd.getPermissionString()))) return new ArrayList<>();
6666
List<String> listArgs = new ArrayList<>(Arrays.asList(args));
6767
listArgs.remove(0);
68-
TabComplete complete = cmd.getExecutor().getTabComplete(CommandCore.createContext(CommandCore.createSource(sender), cmd, args[0], new ListArguments(listArgs)));
68+
TabComplete complete = cmd.tabComplete(CommandCore.createContext(CommandCore.createSource(sender), cmd, args[0], new ListArguments(listArgs)));
6969
matchingString = complete.getMatchingString();
7070
tabComplete.addAll(complete.values());
7171
}

src/main/java/com/datasiqn/commandcore/command/Command.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
package com.datasiqn.commandcore.command;
22

3+
import com.datasiqn.resultapi.None;
4+
import com.datasiqn.resultapi.Result;
35
import org.jetbrains.annotations.NotNull;
46
import org.jetbrains.annotations.Nullable;
57

8+
import java.util.Collections;
69
import java.util.List;
710

811
/**
912
* Represents a command
1013
*/
1114
public interface Command {
1215
/**
13-
* Gets the executor for this command
14-
* @return The executor
16+
* Gets this command's name
17+
* @return The name
1518
*/
16-
@NotNull CommandExecutor getExecutor();
19+
@NotNull String getName();
20+
21+
/**
22+
* Gets the aliases of this command
23+
* @return The aliases
24+
*/
25+
@NotNull String @NotNull [] getAliases();
26+
27+
/**
28+
* Executes this command executor
29+
* @param context The context in which this command was executed
30+
* @return The result of the command
31+
*/
32+
@NotNull Result<None, List<String>> execute(CommandContext context);
33+
34+
/**
35+
* Gets the tabcomplete for this command executor
36+
* @param context The context in which this command was tab completed
37+
* @return The tab completions
38+
*/
39+
default @NotNull TabComplete tabComplete(CommandContext context) {
40+
// matching string is blank because tab complete is an empty list
41+
return new TabComplete(Collections.emptyList(), "");
42+
}
1743

1844
/**
1945
* Gets the permission for this command, registered in the plugin.yml file
@@ -44,10 +70,4 @@ public interface Command {
4470
* @return The usages
4571
*/
4672
@NotNull List<String> getUsages();
47-
48-
/**
49-
* Gets the aliases of this command
50-
* @return The aliases
51-
*/
52-
@NotNull String[] getAliases();
5373
}

src/main/java/com/datasiqn/commandcore/command/CommandExecutor.java

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

src/main/java/com/datasiqn/commandcore/command/builder/BuilderExecutor.java renamed to src/main/java/com/datasiqn/commandcore/command/builder/BuilderCommand.java

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,53 @@
44
import com.datasiqn.commandcore.argument.ArgumentReader;
55
import com.datasiqn.commandcore.argument.Arguments;
66
import com.datasiqn.commandcore.argument.ListArguments;
7-
import com.datasiqn.commandcore.command.CommandExecutor;
7+
import com.datasiqn.commandcore.command.Command;
88
import com.datasiqn.commandcore.command.TabComplete;
9-
import com.datasiqn.commandcore.command.context.CommandContext;
9+
import com.datasiqn.commandcore.command.CommandContext;
1010
import com.datasiqn.resultapi.None;
1111
import com.datasiqn.resultapi.Result;
1212
import org.bukkit.Bukkit;
1313
import org.bukkit.ChatColor;
1414
import org.jetbrains.annotations.Contract;
1515
import org.jetbrains.annotations.NotNull;
16+
import org.jetbrains.annotations.Nullable;
1617

1718
import java.util.ArrayList;
1819
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Set;
2122
import java.util.function.Consumer;
22-
import java.util.function.Function;
2323

24-
class BuilderExecutor implements CommandExecutor {
24+
class BuilderCommand implements Command {
25+
private final String name;
26+
private final String[] aliases;
27+
private final String description;
28+
private final String permission;
29+
private final List<String> usages;
30+
2531
private final Set<CommandNode<?>> nodes;
2632
private final Consumer<CommandContext> executor;
27-
private final List<Function<CommandContext, Result<None, String>>> requires;
33+
private final List<CommandLink.Require> requires;
34+
35+
public BuilderCommand(@NotNull CommandBuilder commandBuilder, List<String> usages) {
36+
this.name = commandBuilder.name;
37+
this.aliases = commandBuilder.aliases;
38+
this.description = commandBuilder.description;
39+
this.permission = commandBuilder.permission;
40+
this.usages = usages;
41+
this.nodes = commandBuilder.children;
42+
this.executor = commandBuilder.executor;
43+
this.requires = commandBuilder.requires;
44+
}
45+
46+
@Override
47+
public @NotNull String getName() {
48+
return name;
49+
}
2850

29-
BuilderExecutor(Consumer<CommandContext> executor, Set<CommandNode<?>> nodes, List<Function<CommandContext, Result<None, String>>> requires) {
30-
this.nodes = nodes;
31-
this.executor = executor;
32-
this.requires = requires;
51+
@Override
52+
public @NotNull String @NotNull [] getAliases() {
53+
return aliases;
3354
}
3455

3556
@Override
@@ -43,7 +64,7 @@ class BuilderExecutor implements CommandExecutor {
4364
if (size >= 1) {
4465
if (nodes.isEmpty()) return Result.error(Collections.singletonList("Expected no parameters, but got parameters instead"));
4566

46-
CurrentNode current = findCurrentNode(reader);
67+
BuilderCommand.CurrentNode current = findCurrentNode(reader);
4768
Result<CommandNode<?>, List<String>> resultNode = current.node;
4869
if (resultNode.isError()) {
4970
if (current.extraInput) {
@@ -80,7 +101,7 @@ class BuilderExecutor implements CommandExecutor {
80101
}
81102

82103
@Override
83-
public @NotNull TabComplete getTabComplete(@NotNull CommandContext context) {
104+
public @NotNull TabComplete tabComplete(@NotNull CommandContext context) {
84105
Arguments args = context.getArguments();
85106

86107
if (args.size() >= 1) {
@@ -91,7 +112,7 @@ class BuilderExecutor implements CommandExecutor {
91112
String matchingString = args.getString(args.size() - 1);
92113

93114
if (args.size() != 1) {
94-
CurrentNode current = findCurrentNode(reader);
115+
BuilderCommand.CurrentNode current = findCurrentNode(reader);
95116
List<CommandNode<?>> nodeList = current.nodes;
96117
if (nodeList.size() != 0) {
97118
CommandNode<?> node = nodeList.get(nodeList.size() - 1);
@@ -106,15 +127,40 @@ class BuilderExecutor implements CommandExecutor {
106127
}
107128
return new TabComplete(tabcomplete, matchingString);
108129
}
109-
return CommandExecutor.super.getTabComplete(context);
130+
return Command.super.tabComplete(context);
131+
}
132+
133+
@Override
134+
public @Nullable String getPermissionString() {
135+
return permission;
136+
}
137+
138+
@Override
139+
public boolean hasPermission() {
140+
return permission != null;
141+
}
142+
143+
@Override
144+
public @Nullable String getDescription() {
145+
return description;
146+
}
147+
148+
@Override
149+
public boolean hasDescription() {
150+
return description != null;
151+
}
152+
153+
@Override
154+
public @NotNull List<String> getUsages() {
155+
return usages;
110156
}
111157

112158
@Contract("_, _ -> new")
113-
private @NotNull CommandContext buildContext(@NotNull CommandContext context, @NotNull CurrentNode result) {
159+
private @NotNull CommandContext buildContext(@NotNull CommandContext context, @NotNull BuilderCommand.CurrentNode result) {
114160
return CommandCore.createContext(context.getSource(), context.getCommand(), context.getLabel(), new ListArguments(result.args));
115161
}
116162

117-
private @NotNull Result<ApplicableNode, List<String>> checkApplicable(@NotNull ArgumentReader reader, @NotNull Set<CommandNode<?>> nodes) {
163+
private @NotNull Result<BuilderCommand.ApplicableNode, List<String>> checkApplicable(@NotNull ArgumentReader reader, @NotNull Set<CommandNode<?>> nodes) {
118164
List<CommandNode<?>> options = new ArrayList<>();
119165
List<String> exceptions = new ArrayList<>();
120166
if (reader.index() != 0) reader.next();
@@ -131,25 +177,25 @@ class BuilderExecutor implements CommandExecutor {
131177
String arg;
132178
if (reader.atEnd()) arg = reader.splice(beforeIndex);
133179
else arg = reader.splice(beforeIndex, reader.index());
134-
return Result.ok(new ApplicableNode(options.get(0), arg));
180+
return Result.ok(new BuilderCommand.ApplicableNode(options.get(0), arg));
135181
}
136182

137183
@Contract("_ -> new")
138-
private @NotNull BuilderExecutor.CurrentNode findCurrentNode(@NotNull ArgumentReader reader) {
184+
private @NotNull BuilderCommand.CurrentNode findCurrentNode(@NotNull ArgumentReader reader) {
139185
Set<CommandNode<?>> nodeSet = nodes;
140186
List<String> args = new ArrayList<>();
141187
List<CommandNode<?>> nodeList = new ArrayList<>();
142188
CommandNode<?> node = null;
143189
while (!reader.atEnd()) {
144-
if (nodeSet.isEmpty()) return new CurrentNode(Result.error(Collections.emptyList()), nodeList, args, true);
145-
Result<ApplicableNode, List<String>> parseResult = checkApplicable(reader, nodeSet);
190+
if (nodeSet.isEmpty()) return new BuilderCommand.CurrentNode(Result.error(Collections.emptyList()), nodeList, args, true);
191+
Result<BuilderCommand.ApplicableNode, List<String>> parseResult = checkApplicable(reader, nodeSet);
146192
if (parseResult.isError()) {
147193
System.out.println("errors: " + String.join(",", parseResult.unwrapError()));
148194
System.out.println("args is " + String.join(",", args));
149195
args.add(reader.splice(reader.index()));
150-
return new CurrentNode(Result.error(parseResult.unwrapError()), nodeList, args, false);
196+
return new BuilderCommand.CurrentNode(Result.error(parseResult.unwrapError()), nodeList, args, false);
151197
}
152-
ApplicableNode applicableNode = parseResult.unwrap();
198+
BuilderCommand.ApplicableNode applicableNode = parseResult.unwrap();
153199
node = applicableNode.node;
154200
nodeSet = node.getChildren();
155201
nodeList.add(node);
@@ -158,7 +204,7 @@ class BuilderExecutor implements CommandExecutor {
158204
if (reader.atEnd() && reader.get() == ' ') args.add("");
159205
}
160206
System.out.println("args is " + String.join(",", args));
161-
return new CurrentNode(Result.ok(node), nodeList, args, false);
207+
return new BuilderCommand.CurrentNode(Result.ok(node), nodeList, args, false);
162208
}
163209

164210
private static class ApplicableNode {
Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.datasiqn.commandcore.command.builder;
22

33
import com.datasiqn.commandcore.command.Command;
4-
import com.datasiqn.commandcore.command.CommandExecutor;
54
import org.jetbrains.annotations.NotNull;
65
import org.jetbrains.annotations.Nullable;
76

@@ -12,14 +11,18 @@
1211
* Represents a builder that creates commands
1312
*/
1413
public class CommandBuilder extends CommandLink<CommandBuilder> {
15-
private String permission;
16-
private String description;
17-
private String[] aliases;
14+
protected final String name;
15+
protected String permission;
16+
protected String description;
17+
protected String[] aliases = new String[0];
1818

1919
/**
2020
* Creates a new {@code CommandBuilder}
21+
* @param name The name of the command
2122
*/
22-
public CommandBuilder() {}
23+
public CommandBuilder(@NotNull String name) {
24+
this.name = name;
25+
}
2326

2427
/**
2528
* Sets the permission of the command
@@ -46,7 +49,7 @@ public CommandBuilder description(@Nullable String description) {
4649
* @param aliases The aliases
4750
* @return The builder, for chaining
4851
*/
49-
public CommandBuilder alias(@NotNull String @Nullable ... aliases) {
52+
public CommandBuilder alias(@NotNull String @NotNull ... aliases) {
5053
this.aliases = aliases;
5154
return this;
5255
}
@@ -67,61 +70,11 @@ public CommandBuilder alias(@NotNull String @Nullable ... aliases) {
6770
}
6871
if (executor != null && hasOptional && canBeOptional) usages.remove(0);
6972

70-
return new BuilderCommand(usages);
73+
return new BuilderCommand(this, usages);
7174
}
7275

7376
@Override
7477
protected @NotNull CommandBuilder getThis() {
7578
return this;
7679
}
77-
78-
private class BuilderCommand implements Command {
79-
private final String description;
80-
private final String permission;
81-
private final List<String> usages;
82-
private final CommandExecutor commandExecutor;
83-
84-
public BuilderCommand(List<String> usages) {
85-
this.description = CommandBuilder.this.description;
86-
this.permission = CommandBuilder.this.permission;
87-
this.usages = usages;
88-
89-
this.commandExecutor = new BuilderExecutor(executor, children, requires);
90-
}
91-
92-
@Override
93-
public @NotNull CommandExecutor getExecutor() {
94-
return commandExecutor;
95-
}
96-
97-
@Override
98-
public @Nullable String getPermissionString() {
99-
return permission;
100-
}
101-
102-
@Override
103-
public boolean hasPermission() {
104-
return permission != null;
105-
}
106-
107-
@Override
108-
public @Nullable String getDescription() {
109-
return description;
110-
}
111-
112-
@Override
113-
public boolean hasDescription() {
114-
return description != null;
115-
}
116-
117-
@Override
118-
public @NotNull List<String> getUsages() {
119-
return usages;
120-
}
121-
122-
@Override
123-
public @NotNull String[] getAliases() {
124-
return aliases == null ? new String[0] : aliases;
125-
}
126-
}
12780
}

0 commit comments

Comments
 (0)