Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require non-null requirements when building command nodes #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;

public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
@SuppressWarnings("unchecked")
public static <S> Predicate<S> requiresNothing() {
return (Predicate<S>) RequiresNothing.INSTANCE;
}

private final RootCommandNode<S> arguments = new RootCommandNode<>();
private Command<S> command;
private Predicate<S> requirement = s -> true;
private Predicate<S> requirement = requiresNothing();
private CommandNode<S> target;
private RedirectModifier<S> modifier = null;
private boolean forks;
Expand Down Expand Up @@ -53,7 +59,7 @@ public Command<S> getCommand() {
}

public T requires(final Predicate<S> requirement) {
this.requirement = requirement;
this.requirement = Objects.requireNonNull(requirement, "requirement");
return getThis();
}

Expand Down Expand Up @@ -96,4 +102,13 @@ public boolean isFork() {
}

public abstract CommandNode<S> build();

private static final class RequiresNothing implements Predicate<Object> {
private static final Predicate<Object> INSTANCE = new RequiresNothing();

@Override
public boolean test(final Object o) {
return true;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
Expand All @@ -35,7 +36,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {

protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
this.command = command;
this.requirement = requirement;
this.requirement = Objects.requireNonNull(requirement, "requirement");
this.redirect = redirect;
this.modifier = modifier;
this.forks = forks;
Expand Down