-
Notifications
You must be signed in to change notification settings - Fork 395
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
Fix for issue 109 #126
base: master
Are you sure you want to change the base?
Fix for issue 109 #126
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> { | ||
private final String literal; | ||
|
||
private boolean isCaseInsensitive = false; | ||
|
||
protected LiteralArgumentBuilder(final String literal) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the additional Then create the method Then modify |
||
this.literal = literal; | ||
} | ||
|
@@ -26,9 +28,16 @@ public String getLiteral() { | |
return literal; | ||
} | ||
|
||
public boolean isCaseInsensitive() { return isCaseInsensitive; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
|
||
public LiteralArgumentBuilder<S> caseInsensitive(boolean isCaseInsensitive) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be removed as |
||
this.isCaseInsensitive = isCaseInsensitive; | ||
return getThis(); | ||
} | ||
|
||
@Override | ||
public LiteralCommandNode<S> build() { | ||
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork()); | ||
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), isCaseInsensitive()); | ||
|
||
for (final CommandNode<S> argument : getArguments()) { | ||
result.addChild(argument); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,14 @@ | |
public class LiteralCommandNode<S> extends CommandNode<S> { | ||
private final String literal; | ||
private final String literalLowerCase; | ||
private boolean isCaseInsensitive = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be final, also rename to |
||
|
||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) { | ||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks, | ||
final boolean isCaseInsensitive) { | ||
super(command, requirement, redirect, modifier, forks); | ||
this.literal = literal; | ||
this.literalLowerCase = literal.toLowerCase(Locale.ROOT); | ||
this.isCaseInsensitive = isCaseInsensitive; | ||
} | ||
|
||
public String getLiteral() { | ||
|
@@ -39,6 +42,12 @@ public String getName() { | |
return literal; | ||
} | ||
|
||
public boolean isCaseInsensitive() { return isCaseInsensitive; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to ignoresCase() |
||
|
||
public void caseInsensitive(boolean isCaseInsensitive) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this method, nodes should never be able to be modified after they have been created, otherwise it is possible to violate the guarantees stipulated in |
||
this.isCaseInsensitive = isCaseInsensitive; | ||
} | ||
|
||
@Override | ||
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException { | ||
final int start = reader.getCursor(); | ||
|
@@ -55,7 +64,17 @@ private int parse(final StringReader reader) { | |
final int start = reader.getCursor(); | ||
if (reader.canRead(literal.length())) { | ||
final int end = start + literal.length(); | ||
if (reader.getString().substring(start, end).equals(literal)) { | ||
|
||
boolean foundMatch = false; | ||
String subString = reader.getString().substring(start, end); | ||
|
||
if (isCaseInsensitive) { | ||
foundMatch = subString.equalsIgnoreCase(literal); | ||
} else { | ||
foundMatch = subString.equals(literal); | ||
} | ||
|
||
if (foundMatch) { | ||
reader.setCursor(end); | ||
if (!reader.canRead() || reader.peek() == ' ') { | ||
return end; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
package com.mojang.brigadier; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.context.CommandContextBuilder; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
|
@@ -61,6 +63,15 @@ public void testCreateAndExecuteCommand() throws Exception { | |
verify(command).run(any(CommandContext.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void testCreateAndExecuteCaseInsensitiveCommand() throws Exception { | ||
subject.register(literal("Foo").then(argument("bar", integer()).executes(command)).caseInsensitive(true)); | ||
|
||
assertThat(subject.execute("foo 123", source), is(42)); | ||
verify(command).run(any(CommandContext.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void testCreateAndExecuteOffsetCommand() throws Exception { | ||
|
@@ -70,6 +81,14 @@ public void testCreateAndExecuteOffsetCommand() throws Exception { | |
verify(command).run(any(CommandContext.class)); | ||
} | ||
|
||
@Test | ||
public void testCreateAndExecuteCaseInsensitiveOffsetCommand() throws Exception { | ||
subject.register(literal("Foo").executes(command).caseInsensitive(true)); | ||
|
||
assertThat(subject.execute(inputWithOffset("/foo", 1), source), is(42)); | ||
verify(command).run(any(CommandContext.class)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional tests for using ignoreCase as a command argument should be added. Currently this only these tests only test the base node. |
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void testCreateAndMergeCommands() throws Exception { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be final. Also rename the variable to
ignoreCase
as that is the convention.