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

Add redirects to the equality checks for nodes. #89

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
11 changes: 8 additions & 3 deletions src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,18 @@ public boolean equals(final Object o) {

if (!children.equals(that.children)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false;

return true;
// Because the primary use of a redirect is to be able to create commands that effectively loop,
// we can't do a standard equality check on them as we could very well end up with a stack overflow
// if a branch loops back to this node. However, they are still important here as two nodes that
// only differ by where they redirect to should not be considered the same node...
//
// Hence, we do a reference equality check.
return redirect == that.redirect;
}

@Override
public int hashCode() {
return 31 * children.hashCode() + (command != null ? command.hashCode() : 0);
return 31 * children.hashCode() + (command != null ? command.hashCode() : 0) + System.identityHashCode(redirect);
}

public Predicate<S> getRequirement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.junit.Test;

import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -87,6 +89,13 @@ public void testEquals() throws Exception {
.testEquals();
}

@Test
public void testNodesWithDifferentRedirectsAreNotEqual() throws Exception {
final CommandNode<Object> redirectTarget = argument("baz", integer()).build();
assertThat(argument("foo", integer()).redirect(redirectTarget).build(), not(argument("foo", integer()).build()));
assertThat(literal("foo").redirect(redirectTarget).build(), not(literal("foo").build()));
}

@Test
public void testCreateBuilder() throws Exception {
final RequiredArgumentBuilder<Object, Integer> builder = node.createBuilder();
Expand Down