diff --git a/src/main/java/net/minestom/server/entity/acquirable/AcquirableEntity.java b/src/main/java/net/minestom/server/entity/acquirable/AcquirableEntity.java index 6ed00d659..776d677f8 100644 --- a/src/main/java/net/minestom/server/entity/acquirable/AcquirableEntity.java +++ b/src/main/java/net/minestom/server/entity/acquirable/AcquirableEntity.java @@ -8,10 +8,9 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collection; -import java.util.Collections; import java.util.Objects; import java.util.function.Consumer; +import java.util.stream.Stream; /** * Represents an {@link Entity entity} which can be acquired. @@ -19,7 +18,7 @@ import java.util.function.Consumer; */ public class AcquirableEntity { - private static final ThreadLocal> CURRENT_ENTITIES = ThreadLocal.withInitial(Collections::emptyList); + private static final ThreadLocal> CURRENT_ENTITIES = ThreadLocal.withInitial(Stream::empty); /** * Gets all the {@link Entity entities} being ticked in the current thread. @@ -28,7 +27,7 @@ public class AcquirableEntity { * * @return the entities ticked in the current thread */ - public static @NotNull Collection<@NotNull Entity> current() { + public static @NotNull Stream<@NotNull Entity> current() { return CURRENT_ENTITIES.get(); } @@ -41,7 +40,7 @@ public class AcquirableEntity { * @param entities the new entity collection */ @ApiStatus.Internal - public static void refresh(@NotNull Collection<@NotNull Entity> entities) { + public static void refresh(@NotNull Stream<@NotNull Entity> entities) { CURRENT_ENTITIES.set(entities); } diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 919d98e37..a7a48948f 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -14,7 +14,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Used to link chunks into multiple groups. @@ -121,13 +121,12 @@ public abstract class ThreadProvider { final var chunkEntries = threadChunkMap.get(thread); if (chunkEntries == null || chunkEntries.isEmpty()) { // Nothing to tick - AcquirableEntity.refresh(Collections.emptyList()); + AcquirableEntity.refresh(Stream.empty()); return; } final var entities = chunkEntries.stream() - .flatMap(chunkEntry -> chunkEntry.entities.stream()) - .collect(Collectors.toUnmodifiableList()); + .flatMap(chunkEntry -> chunkEntry.entities.stream()); AcquirableEntity.refresh(entities); final ReentrantLock lock = thread.getLock(); @@ -147,7 +146,7 @@ public abstract class ThreadProvider { entity.tick(time); }); }); - AcquirableEntity.refresh(Collections.emptyList()); + AcquirableEntity.refresh(Stream.empty()); lock.unlock(); }); } @@ -292,6 +291,8 @@ public abstract class ThreadProvider { } private void processRemovedEntities() { + if (removedEntities.isEmpty()) + return; for (Entity entity : removedEntities) { AcquirableEntity acquirableEntity = entity.getAcquirable(); ChunkEntry chunkEntry = acquirableEntity.getHandler().getChunkEntry(); @@ -304,6 +305,8 @@ public abstract class ThreadProvider { } private void processUpdatedEntities() { + if (updatableEntities.isEmpty()) + return; for (Entity entity : updatableEntities) { AcquirableEntity acquirableEntity = entity.getAcquirable(); ChunkEntry handlerChunkEntry = acquirableEntity.getHandler().getChunkEntry();