Skip to content

Commit d3a8c5a

Browse files
committed
Generalize CommandExecutionHandler to both simplify and allow more control
1 parent 91fff7e commit d3a8c5a

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ repositories {
77
}
88

99
dependencies {
10-
implementation("de.bluecolored:bluecommands-core:1.1.4")
11-
implementation("de.bluecolored:bluecommands-brigadier:1.1.4") // Optional
10+
implementation("de.bluecolored:bluecommands-core:1.2.0")
11+
implementation("de.bluecolored:bluecommands-brigadier:1.2.0") // Optional
1212
}
1313
```
1414

bluecommands-brigadier/src/main/java/de/bluecolored/bluecommands/brigadier/CommandCommand.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,8 @@ public CommandCommand(Command<C,T> command, CommandExecutionHandler<C,T> executi
5050
public int run(CommandContext<D> context) throws CommandSyntaxException {
5151
InputReader inputReader = new InputReader(context.getInput());
5252
inputReader.setPosition(context.getRange().getStart());
53-
5453
ParseResult<C, T> result = command.parse(contextConverter.apply(context.getSource()), inputReader);
55-
if (result.getMatches().isEmpty())
56-
return executionHandler.handleParseFailure(result);
57-
58-
ParseMatch<C, T> executable = result.getMatches().stream()
59-
.max(Comparator.comparing(ParseMatch::getPriority))
60-
.orElseThrow(IllegalStateException::new);
61-
62-
T executionResult;
63-
try {
64-
executionResult = executable.execute();
65-
} catch (Exception exception) {
66-
return executionHandler.handleExecutionException(executable.getContext(), exception);
67-
}
68-
69-
return executionHandler.handleExecution(executable.getContext(), executionResult);
54+
return executionHandler.handle(result);
7055
}
7156

7257
}

bluecommands-brigadier/src/main/java/de/bluecolored/bluecommands/brigadier/CommandExecutionHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030

3131
public interface CommandExecutionHandler<C, T> {
3232

33-
int handleParseFailure(ParseResult<C, T> parseResult) throws CommandSyntaxException;
34-
35-
int handleExecution(C context, @Nullable T result) throws CommandSyntaxException;
36-
37-
int handleExecutionException(C context, Throwable throwable) throws CommandSyntaxException;
33+
int handle(ParseResult<C, T> parseResult) throws CommandSyntaxException;
3834

3935
}

bluecommands-brigadier/src/main/java/de/bluecolored/bluecommands/brigadier/DefaultExecutionHandler.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,32 @@
2727
import com.mojang.brigadier.exceptions.CommandSyntaxException;
2828
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
2929
import de.bluecolored.bluecommands.ParseFailure;
30+
import de.bluecolored.bluecommands.ParseMatch;
3031
import de.bluecolored.bluecommands.ParseResult;
3132

3233
import java.util.Comparator;
3334

3435
public class DefaultExecutionHandler<C, T> implements CommandExecutionHandler<C, T> {
3536

3637
@Override
38+
public int handle(ParseResult<C, T> parseResult) throws CommandSyntaxException {
39+
if (parseResult.getMatches().isEmpty())
40+
return handleParseFailure(parseResult);
41+
42+
ParseMatch<C, T> executable = parseResult.getMatches().stream()
43+
.max(Comparator.comparing(ParseMatch::getPriority))
44+
.orElseThrow(IllegalStateException::new);
45+
46+
T executionResult;
47+
try {
48+
executionResult = executable.execute();
49+
} catch (Exception exception) {
50+
return handleExecutionException(executable.getContext(), exception);
51+
}
52+
53+
return handleExecution(executable.getContext(), executionResult);
54+
}
55+
3756
public int handleParseFailure(ParseResult<C, T> result) throws CommandSyntaxException {
3857
ParseFailure<C, ?> failure = result.getFailures().stream()
3958
.max(Comparator.comparing(ParseFailure::getPosition))
@@ -46,15 +65,13 @@ public int handleParseFailure(ParseResult<C, T> result) throws CommandSyntaxExce
4665
);
4766
}
4867

49-
@Override
5068
public int handleExecution(C context, T result) {
5169
if (result instanceof Number)
5270
return ((Number) result).intValue();
5371

5472
return 1;
5573
}
5674

57-
@Override
5875
public int handleExecutionException(C context, Throwable throwable) {
5976
throw new RuntimeException(throwable);
6077
}

0 commit comments

Comments
 (0)