Skip to content

Commit

Permalink
Found a better way to get entities around the player
Browse files Browse the repository at this point in the history
Fabric Discord guys told me that
  • Loading branch information
boholder committed Feb 8, 2024
1 parent ca7ab35 commit 49f6c6a
Showing 1 changed file with 18 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import lombok.extern.slf4j.Slf4j;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EyeOfEnderEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.mob.WaterCreatureEntity;
import net.minecraft.entity.passive.PassiveEntity;
Expand All @@ -19,9 +17,9 @@
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;

import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Predicate;

Expand All @@ -30,22 +28,6 @@
*/
@Slf4j
public class POIEntities {
/**
* Entity types to be scanned for.
*/
private static final Set<Predicate<Entity>> INTERESTED_ENTITY_TYPES = Set.of(
// LivingEntity = MobEntity + PlayerEntity + ArmorStandEntity
e -> e instanceof MobEntity,
e -> e instanceof PlayerEntity,
// For notifying dropped items
e -> e instanceof ItemEntity,
// For auto-locking eye of ender
e -> e instanceof EyeOfEnderEntity,
// boat + mine cart
e -> e instanceof VehicleEntity
);
private static final Predicate<Entity> IS_INTERESTED_ENTITY_TYPE = INTERESTED_ENTITY_TYPES.stream().reduce(Predicate::or).get();

private TreeMap<Double, Entity> passiveEntity = new TreeMap<>();
private TreeMap<Double, Entity> hostileEntity = new TreeMap<>();
private TreeMap<Double, Entity> markedEntities = new TreeMap<>();
Expand Down Expand Up @@ -94,25 +76,22 @@ public void update(boolean onMarking, Entity markedEntity) {
markedEntities = new TreeMap<>();
vehicleEntities = new TreeMap<>();

log.debug("POIEntities started...");
log.debug("POIEntities started.");

for (Entity i : minecraftClient.world.getEntities()) {
// Only scan interested entity types
if (!IS_INTERESTED_ENTITY_TYPE.test(i)) continue;
// Exclude player self
if (i == minecraftClient.player) continue;
// Copied from PlayerEntity.tickMovement()
Box scanBox = minecraftClient.player.getBoundingBox().expand(range, range, range);
List<Entity> entities = minecraftClient.world.getOtherEntities(minecraftClient.player, scanBox);

for (Entity i : entities) {
double distance = minecraftClient.player.distanceTo(i);
if (distance > range) continue;

BlockPos blockPos = i.getBlockPos();
BlockPos entityPos = i.getBlockPos();

if (this.markedEntity.test(i)) {
markedEntities.put(distance, i);
if (i instanceof HostileEntity) {
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 2f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 2f);
} else {
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
}
}

Expand All @@ -123,21 +102,21 @@ public void update(boolean onMarking, Entity markedEntity) {

if (i instanceof PassiveEntity) {
passiveEntity.put(distance, i);
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
} else if (i instanceof Monster) {
hostileEntity.put(distance, i);
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 2f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 2f);
} else if (i instanceof WaterCreatureEntity) {
passiveEntity.put(distance, i);
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
} else if (i instanceof ItemEntity itemEntity && itemEntity.isOnGround()) {
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_METAL_PRESSURE_PLATE_CLICK_ON, 2f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_METAL_PRESSURE_PLATE_CLICK_ON, 2f);
} else if (i instanceof PlayerEntity) {
passiveEntity.put(distance, i);
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
} else if (i instanceof VehicleEntity) {
vehicleEntities.put(distance, i);
this.playSoundAtBlockPos(blockPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
this.playSoundAt(entityPos, SoundEvents.BLOCK_NOTE_BLOCK_BELL.value(), 0f);
}
}
log.debug("POIEntities end.");
Expand All @@ -147,10 +126,10 @@ public void update(boolean onMarking, Entity markedEntity) {
}
}

private void playSoundAtBlockPos(BlockPos blockPos, SoundEvent soundEvent, float pitch) {
private void playSoundAt(BlockPos pos, SoundEvent soundEvent, float pitch) {
if (!playSound || volume == 0f) return;
log.debug("Play sound at [x:%d y:%d z%d]".formatted(blockPos.getX(), blockPos.getY(), blockPos.getZ()));
WorldUtils.playSoundAtPosition(soundEvent, volume, pitch, blockPos.toCenterPos());
log.debug("Play sound at [x:%d y:%d z%d]".formatted(pos.getX(), pos.getY(), pos.getZ()));
WorldUtils.playSoundAtPosition(soundEvent, volume, pitch, pos.toCenterPos());
}

/**
Expand Down

0 comments on commit 49f6c6a

Please sign in to comment.