mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Use stream instead of a collection for current thread entities
This commit is contained in:
parent
7983362f25
commit
99920a77ba
@ -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<Collection<Entity>> CURRENT_ENTITIES = ThreadLocal.withInitial(Collections::emptyList);
|
||||
private static final ThreadLocal<Stream<Entity>> 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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user