mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Auto entity viewing cleanup
This commit is contained in:
parent
a6cb126d76
commit
18eab183a0
@ -51,6 +51,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.entity.EntityUtils;
|
||||
import net.minestom.server.utils.instance.InstanceUtils;
|
||||
import net.minestom.server.utils.time.CooldownUtils;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
@ -1595,24 +1596,15 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
});
|
||||
|
||||
// Manage entities in unchecked chunks
|
||||
final long[] chunksInRange = ChunkUtils.getChunksInRange(newChunk.toPosition(), entityViewDistance);
|
||||
EntityUtils.forEachRange(instance, newChunk.toPosition(), entityViewDistance, entity -> {
|
||||
if (isAutoViewable() && !entity.viewers.contains(this)) {
|
||||
entity.addViewer(this);
|
||||
}
|
||||
|
||||
for (long chunkIndex : chunksInRange) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
|
||||
if (chunk == null)
|
||||
continue;
|
||||
instance.getChunkEntities(chunk).forEach(entity -> {
|
||||
if (isAutoViewable() && !entity.viewers.contains(this)) {
|
||||
entity.addViewer(this);
|
||||
}
|
||||
|
||||
if (entity instanceof Player && entity.isAutoViewable() && !viewers.contains(entity)) {
|
||||
addViewer((Player) entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (entity instanceof Player && entity.isAutoViewable() && !viewers.contains(entity)) {
|
||||
addViewer((Player) entity);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.entity.EntityUtils;
|
||||
import net.minestom.server.utils.time.CooldownUtils;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
@ -849,7 +850,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
AddEntityToInstanceEvent event = new AddEntityToInstanceEvent(this, entity);
|
||||
callCancellableEvent(AddEntityToInstanceEvent.class, event, () -> {
|
||||
final Position entityPosition = entity.getPosition();
|
||||
final long[] visibleChunksEntity = ChunkUtils.getChunksInRange(entityPosition, MinecraftServer.getEntityViewDistance());
|
||||
final boolean isPlayer = entity instanceof Player;
|
||||
|
||||
if (isPlayer) {
|
||||
@ -858,19 +858,17 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
}
|
||||
|
||||
// Send all visible entities
|
||||
for (long chunkIndex : visibleChunksEntity) {
|
||||
getEntitiesInChunk(chunkIndex).forEach(ent -> {
|
||||
if (isPlayer) {
|
||||
if (ent.isAutoViewable())
|
||||
ent.addViewer((Player) entity);
|
||||
}
|
||||
EntityUtils.forEachRange(this, entityPosition, MinecraftServer.getEntityViewDistance(), ent -> {
|
||||
if (isPlayer) {
|
||||
if (ent.isAutoViewable())
|
||||
ent.addViewer((Player) entity);
|
||||
}
|
||||
|
||||
if (ent instanceof Player) {
|
||||
if (entity.isAutoViewable())
|
||||
entity.addViewer((Player) ent);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (ent instanceof Player) {
|
||||
if (entity.isAutoViewable())
|
||||
entity.addViewer((Player) ent);
|
||||
}
|
||||
});
|
||||
|
||||
final Chunk chunk = getChunkAt(entityPosition);
|
||||
Check.notNull(chunk, "You tried to spawn an entity in an unloaded chunk, " + entityPosition);
|
||||
|
@ -8,6 +8,9 @@ import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class EntityUtils {
|
||||
|
||||
@ -15,7 +18,22 @@ public final class EntityUtils {
|
||||
|
||||
}
|
||||
|
||||
public static boolean areVisible(Entity ent1, Entity ent2) {
|
||||
public static void forEachRange(@NotNull Instance instance, @NotNull Position position,
|
||||
int viewDistance,
|
||||
@NotNull Consumer<Entity> consumer) {
|
||||
final long[] chunksInRange = ChunkUtils.getChunksInRange(position, viewDistance);
|
||||
|
||||
for (long chunkIndex : chunksInRange) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
|
||||
if (chunk == null)
|
||||
continue;
|
||||
instance.getChunkEntities(chunk).forEach(consumer::accept);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean areVisible(@NotNull Entity ent1, @NotNull Entity ent2) {
|
||||
if (ent1.getInstance() == null || ent2.getInstance() == null)
|
||||
return false;
|
||||
if (!ent1.getInstance().equals(ent2.getInstance()))
|
||||
@ -27,7 +45,6 @@ public final class EntityUtils {
|
||||
for (long visibleChunk : visibleChunksEntity) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(visibleChunk);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(visibleChunk);
|
||||
|
||||
if (chunk.getChunkX() == chunkX && chunk.getChunkZ() == chunkZ)
|
||||
return true;
|
||||
}
|
||||
@ -35,7 +52,7 @@ public final class EntityUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isOnGround(Entity entity) {
|
||||
public static boolean isOnGround(@NotNull Entity entity) {
|
||||
final Instance instance = entity.getInstance();
|
||||
if (instance == null)
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user