Skip to content

Commit 40b1c82

Browse files
committed
Added function to send usage when no command is matched (Resolves #7)
1 parent 5b85d9e commit 40b1c82

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/main/java/me/despical/commandframework/CommandFramework.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.lang.reflect.InvocationTargetException;
3232
import java.lang.reflect.Method;
3333
import java.util.*;
34-
import java.util.function.Consumer;
34+
import java.util.function.Function;
3535
import java.util.logging.Level;
3636
import java.util.stream.Collectors;
3737
import java.util.stream.Stream;
@@ -77,10 +77,15 @@ public class CommandFramework implements CommandExecutor, TabCompleter {
7777
@NotNull
7878
private final Map<CommandSender, Map<Command, Long>> cooldowns = new HashMap<>();
7979
/**
80-
* Consumer to accept if there is no matched commands related framework.
80+
* Function to apply if there is no matched commands related framework.
81+
*
82+
* <pre>
83+
* // To disable sending usage to command sender return true
84+
* CommandFramework#setMatchFunction(arguments -> true);
85+
* </pre>
8186
*/
8287
@Nullable
83-
private Consumer<CommandArguments> anyMatchConsumer;
88+
private Function<CommandArguments, Boolean> matchFunction = (arguments) -> false;
8489
/**
8590
* Default command map of Bukkit.
8691
*/
@@ -112,12 +117,12 @@ public CommandFramework(@NotNull Plugin plugin) {
112117
}
113118

114119
/**
115-
* Consumer to accept if there is no matched commands related framework.
120+
* Function to apply if there is no matched commands related framework.
116121
*
117-
* @param anyMatchConsumer to be accepted if there is no matched commands
122+
* @param matchFunction to be applied if there is no matched commands
118123
*/
119-
public void setAnyMatch(@NotNull Consumer<CommandArguments> anyMatchConsumer) {
120-
this.anyMatchConsumer = anyMatchConsumer;
124+
public void setMatchFunction(@NotNull Function<CommandArguments, Boolean> matchFunction) {
125+
this.matchFunction = matchFunction;
121126
}
122127

123128
/**
@@ -320,7 +325,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
320325
final Map.Entry<Command, Map.Entry<Method, Object>> entry = this.getAssociatedCommand(cmd.getName(), args);
321326

322327
if (entry == null) {
323-
if (anyMatchConsumer != null) anyMatchConsumer.accept(new CommandArguments(sender, cmd, label, args));
328+
if (matchFunction != null) return matchFunction.apply(new CommandArguments(sender, cmd, label, args));
324329

325330
return true;
326331
}

0 commit comments

Comments
 (0)