Skip to content

Commit

Permalink
Entity selector now accepts NBT as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jan 23, 2024
1 parent 6678fd1 commit c610db4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,8 @@ protected CompletableFuture<Suggestions> suggestOpenTag(SuggestionsBuilder sugge
return suggestionsBuilder.buildFuture();
}

protected CompletableFuture<Suggestions> suggestOpenMetadata(SuggestionsBuilder suggestionsBuilder, Consumer<SuggestionsBuilder> consumer) {
if (!this.reader.canRead()) suggestionsBuilder.suggest(String.valueOf('['));
return suggestionsBuilder.buildFuture();
}

private CompletableFuture<Suggestions> suggestCloseMetadata(SuggestionsBuilder suggestionsBuilder, Consumer<SuggestionsBuilder> consumer) {
if (!this.reader.canRead()) suggestionsBuilder.suggest(String.valueOf(']'));
return suggestionsBuilder.buildFuture();
}

private CompletableFuture<Suggestions> suggestCloseTag(SuggestionsBuilder suggestionsBuilder, Consumer<SuggestionsBuilder> consumer) {
if (!this.reader.canRead()) suggestionsBuilder.suggest(String.valueOf('}'));
return suggestionsBuilder.buildFuture();
}

private CompletableFuture<Suggestions> suggestTagSeparator(SuggestionsBuilder suggestionsBuilder, Consumer<SuggestionsBuilder> consumer) {
if (!this.reader.canRead()) suggestionsBuilder.suggest(String.valueOf(':'));
return suggestionsBuilder.buildFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.nbt.CompoundTag;
import com.mojang.nbt.Tag;
import net.minecraft.client.lang.text.ChainText;
import net.minecraft.client.lang.text.Text;
import net.minecraft.core.entity.Entity;
Expand All @@ -16,6 +18,7 @@
import net.minecraft.core.player.gamemode.Gamemode;
import net.minecraft.core.util.helper.LogPrintStream;
import net.pedroricardo.commander.CommanderHelper;
import net.pedroricardo.commander.NbtHelper;

import java.util.*;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -224,6 +227,26 @@ public static void register(String key, Modifier modifier, Predicate<EntitySelec
parser.setHasGamemodeEquals(true);
}
}, parser -> !parser.hasGamemodeEquals(), ChainText.text().trans("argument_types.commander.entity.selector.options.gamemode.description"));
register("nbt", (parser) -> {
boolean invert = parser.shouldInvertValue();
CompoundTag nbt = NbtHelper.parseNbt(parser.getReader());
parser.addPredicate((entity) -> {
if (nbt.getValue().isEmpty()) return !invert;
CompoundTag entityNbt = new CompoundTag();
entity.addAdditionalSaveData(entityNbt);
for (Map.Entry<String, Tag<?>> entry : nbt.getValue().entrySet()) {
if (!entityNbt.getValue().containsKey(entry.getKey()) || (entityNbt.getValue().get(entry.getKey()) != entry.getValue() && !entityNbt.getValue().get(entry.getKey()).equals(entry.getValue()) && !entityNbt.getValue().get(entry.getKey()).getValue().equals(entry.getValue().getValue()))) {
if (!invert) return false;
}
}
return true;
});
if (invert) {
parser.setHasNotNbt(true);
} else {
parser.setHasNbt(true);
}
}, parser -> !parser.hasNbt(), ChainText.text().trans("argument_types.commander.entity.selector.options.nbt.description"));
}

public static Modifier get(EntitySelectorParser entitySelectorParser, String string, int i) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ public BiFunction<SuggestionsBuilder, Consumer<SuggestionsBuilder>, CompletableF
private boolean hasType = false;
private boolean hasNameEquals = false;
private boolean hasNameNotEquals = false;
private boolean hasNbt = false;
private boolean hasNotNbt = false;

public boolean hasGamemodeEquals() {
return this.hasGamemodeEquals;
Expand Down Expand Up @@ -387,6 +389,22 @@ public void setCurrentEntity(boolean currentEntity) {
this.currentEntity = currentEntity;
}

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

public void setHasNbt(boolean bl) {
this.hasNbt = bl;
}

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

public void setHasNotNbt(boolean bl) {
this.hasNotNbt = bl;
}

public void setX(double x) {
this.x = x;
}
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 @@ -23,6 +23,7 @@ argument_types.commander.entity.selector.options.unterminated=Expected end of op
argument_types.commander.entity.selector.options.valueless=Expected value for option '%s'
argument_types.commander.entity.selector.options.gamemode.invalid=Invalid or unknown game mode '%s'
argument_types.commander.entity.selector.options.gamemode.description=Players with game mode
argument_types.commander.entity.selector.options.nbt.description=Entities with NBT
argument_types.commander.entity.selector.options.sort.description=Sort the entities
argument_types.commander.entity.selector.options.sort.invalid=Invalid or unknown sort type '%s'
argument_types.commander.entity.selector.options.limit.description=Maximum number of entities to return
Expand Down

0 comments on commit c610db4

Please sign in to comment.