Skip to content

Commit

Permalink
Fix entity selector crash
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jun 26, 2024
1 parent 0c7075d commit 1f60ccd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.2
loader_version=0.15.11

# Mod Properties
mod_version=0.5.0
mod_version=0.5.1
maven_group=net.modfest
archives_base_name=fireblanket

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.google.common.collect.Lists;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.command.EntitySelector;
import net.minecraft.entity.Entity;
import net.minecraft.predicate.NumberRange;
import net.minecraft.resource.featuretoggle.FeatureSet;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Util;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;
Expand All @@ -15,6 +18,7 @@
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand All @@ -31,7 +35,7 @@ public abstract class MixinEntitySelector {

@Shadow @Final private Function<Vec3d, Vec3d> positionOffset;

@Shadow protected abstract Predicate<Entity> getPositionPredicate(Vec3d pos);
@Shadow protected abstract Predicate<Entity> getPositionPredicate(Vec3d pos, @Nullable Box box, @Nullable FeatureSet enabledFeatures);

@Shadow @Final private boolean senderOnly;

Expand All @@ -41,12 +45,14 @@ public abstract class MixinEntitySelector {

@Shadow protected abstract <T extends Entity> List<T> getEntities(Vec3d pos, List<T> entities);

@Shadow @Final private Predicate<Entity> basePredicate;

@Shadow @Final private NumberRange.DoubleRange distance;

@Shadow @Final private @Nullable Box box;

@Shadow @Nullable protected abstract Box getOffsetBox(Vec3d offset);

@Shadow @Final private List<Predicate<Entity>> predicates;

/**
* @author Jasmine
* @reason Always predicate on distance and d(x|y|z) *before* checking the NBT, or any other predicate.
Expand All @@ -62,7 +68,8 @@ public List<ServerPlayerEntity> getPlayers(ServerCommandSource source) throws Co
return serverPlayerEntity == null ? Collections.emptyList() : Lists.<ServerPlayerEntity>newArrayList(serverPlayerEntity);
} else {
Vec3d pos = this.positionOffset.apply(source.getPosition());
Predicate<Entity> predicate = this.getPositionPredicate(pos);
Box box = this.getOffsetBox(pos);
Predicate<Entity> predicate = this.getPositionPredicate(pos, box, null);
if (this.senderOnly) {
if (source.getEntity() instanceof ServerPlayerEntity serverPlayerEntity2 && predicate.test(serverPlayerEntity2)) {
return Lists.newArrayList(serverPlayerEntity2);
Expand All @@ -74,10 +81,12 @@ public List<ServerPlayerEntity> getPlayers(ServerCommandSource source) throws Co
List<ServerPlayerEntity> list;
if (this.isLocalWorldOnly()) {
// The change is here: Get players with distance predicate first, move onto base predicate later.
predicate = getPositionOnlyPredicate(pos);
predicate = getPositionOnlyPredicate(pos, box, null);

Predicate<Entity> basePredicate = Util.allOf(this.predicates);

list = source.getWorld().getPlayers(predicate, i);
list.removeIf(this.basePredicate.negate());
list.removeIf(basePredicate.negate());
} else {
list = Lists.newArrayList();

Expand Down Expand Up @@ -109,4 +118,32 @@ private Predicate<Entity> getPositionOnlyPredicate(Vec3d pos) {

return predicate;
}

private Predicate<Entity> getPositionOnlyPredicate(Vec3d pos, @Nullable Box box, @Nullable FeatureSet enabledFeatures) {
boolean bl = enabledFeatures != null;
boolean bl2 = box != null;
boolean bl3 = !this.distance.isDummy();
int i = (bl ? 1 : 0) + (bl2 ? 1 : 0) + (bl3 ? 1 : 0);
List<Predicate<Entity>> list;
if (i == 0) {
list = new ArrayList<>();
} else {
List<Predicate<Entity>> list2 = new ObjectArrayList<>(3);
if (bl) {
list2.add(entity -> entity.getType().isEnabled(enabledFeatures));
}

if (bl2) {
list2.add(entity -> box.intersects(entity.getBoundingBox()));
}

if (bl3) {
list2.add(entity -> this.distance.testSqrt(entity.squaredDistanceTo(pos)));
}

list = list2;
}

return Util.allOf(list);
}
}

0 comments on commit 1f60ccd

Please sign in to comment.