Skip to content

Commit

Permalink
Added name option
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 24, 2023
1 parent 697050e commit 5a294cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.entity.EntityDispatcher;
import net.minecraft.core.entity.EntityLiving;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.lang.I18n;
import net.minecraft.core.lang.text.Text;
Expand Down Expand Up @@ -43,6 +44,26 @@ public static void register(String key, Modifier modifier, Predicate<EntitySelec
}

static {
EntitySelectorOptions.register("name", entitySelectorParser -> {
int i = entitySelectorParser.getReader().getCursor();
boolean bl = entitySelectorParser.shouldInvertValue();
String string = entitySelectorParser.getReader().readString();
if (entitySelectorParser.hasNameNotEquals() && !bl) {
entitySelectorParser.getReader().setCursor(i);
throw INAPPLICABLE_OPTION.createWithContext(entitySelectorParser.getReader(), "name");
}
if (bl) {
entitySelectorParser.setHasNameNotEquals(true);
} else {
entitySelectorParser.setHasNameEquals(true);
}
entitySelectorParser.addPredicate(entity -> {
if (!(entity instanceof EntityLiving)) return bl;
else if (entity instanceof EntityPlayer) return ((EntityPlayer)entity).username.equals(string) != bl;
else if (!((EntityLiving)entity).getDisplayName().startsWith("§") || ((EntityLiving)entity).getDisplayName().length() < 2) return ((EntityLiving)entity).getDisplayName().equals(string) != bl;
return ((EntityLiving)entity).getDisplayName().substring(2).equals(string) != bl;
});
}, entitySelectorParser -> !entitySelectorParser.hasNameEquals(), new TextTranslatable("argument_types.commander.entity.selector.options.name.description"));
register("distance", (parser) -> {
int cursor = parser.getReader().getCursor();
MinMaxBounds.Doubles bounds = MinMaxBounds.Doubles.fromReader(parser.getReader());
Expand Down Expand Up @@ -74,6 +95,10 @@ public static void register(String key, Modifier modifier, Predicate<EntitySelec
return builder.buildFuture();
});

if (parser.isTypeInverse() && !invert) {
parser.getReader().setCursor(cursor);
throw INAPPLICABLE_OPTION.createWithContext(parser.getReader(), "type");
}
if (invert) {
parser.setTypeInverse(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ public BiFunction<SuggestionsBuilder, Consumer<SuggestionsBuilder>, CompletableF
private boolean isSorted = false;
private boolean hasLimit = false;
private boolean hasType = false;
private boolean hasNameEquals = false;
private boolean hasNameNotEquals = false;

public boolean hasGamemodeEquals() {
return this.hasGamemodeEquals;
Expand Down Expand Up @@ -313,6 +315,22 @@ public void setDistance(MinMaxBounds.Doubles distance) {
this.distance = distance;
}

public boolean hasNameEquals() {
return this.hasNameEquals;
}

public void setHasNameEquals(boolean bl) {
this.hasNameEquals = bl;
}

public boolean hasNameNotEquals() {
return this.hasNameNotEquals;
}

public void setHasNameNotEquals(boolean bl) {
this.hasNameNotEquals = bl;
}

public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/commander/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ argument_types.commander.entity.selector.options.type.description=Entities of ty
argument_types.commander.entity.selector.options.type.invalid=Invalid or unknown entity type '%s'
argument_types.commander.entity.selector.options.distance.description=Distance to entity
argument_types.commander.entity.selector.options.distance.invalid=Distance cannot be negative
argument_types.commander.entity.selector.options.name.description=Entity name
argument_types.commander.range.empty=Expected value or range of values
argument_types.commander.range.swapped=Min cannot be bigger than max
commands.commander.achievement.grant.exception_already_has_achievement=Player already has achievement
Expand Down

0 comments on commit 5a294cc

Please sign in to comment.