diff --git a/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java b/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java index ccd4365b8..aa6836d6a 100644 --- a/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java @@ -17,7 +17,7 @@ public class PerChunkThreadProvider extends ThreadProvider { } @Override - public long findThread(@NotNull Chunk chunk) { + public int findThread(@NotNull Chunk chunk) { return chunk.hashCode(); } diff --git a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java index baba20527..e1f5dbcf2 100644 --- a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java @@ -18,7 +18,7 @@ public class PerInstanceThreadProvider extends ThreadProvider { } @Override - public long findThread(@NotNull Chunk chunk) { + public int findThread(@NotNull Chunk chunk) { return chunk.getInstance().hashCode(); } diff --git a/src/main/java/net/minestom/server/thread/SingleThreadProvider.java b/src/main/java/net/minestom/server/thread/SingleThreadProvider.java index 34fa99b98..40b584a07 100644 --- a/src/main/java/net/minestom/server/thread/SingleThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/SingleThreadProvider.java @@ -13,7 +13,7 @@ public class SingleThreadProvider extends ThreadProvider { } @Override - public long findThread(@NotNull Chunk chunk) { + public int findThread(@NotNull Chunk chunk) { return 0; } diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 666d4b04c..1b9a84bf7 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -69,7 +69,7 @@ public abstract class ThreadProvider { * * @param chunk the chunk */ - public abstract long findThread(@NotNull Chunk chunk); + public abstract int findThread(@NotNull Chunk chunk); /** * Defines how often chunks thread should be updated. @@ -165,7 +165,6 @@ public abstract class ThreadProvider { processRemovedEntities(); // Update entities chunks processUpdatedEntities(); - if (getChunkRefreshType() == RefreshType.NEVER) return; @@ -175,21 +174,18 @@ public abstract class ThreadProvider { final int size = chunks.size(); int counter = 0; while (true) { - Chunk chunk = chunks.pollFirst(); + final Chunk chunk = chunks.pollFirst(); if (!ChunkUtils.isLoaded(chunk)) { removeChunk(chunk); - return; + continue; } - // Update chunk threads switchChunk(chunk); - // Add back to the deque chunks.addLast(chunk); if (++counter > size) break; - if (System.currentTimeMillis() >= endTime) break; } @@ -226,22 +222,13 @@ public abstract class ThreadProvider { this.threads.forEach(TickThread::shutdown); } - /** - * Gets all the {@link TickThread tick threads}. - * - * @return the tick threads - */ - public @NotNull List<@NotNull TickThread> getThreads() { - return threads; - } - - protected void addChunk(@NotNull Chunk chunk) { + private void addChunk(@NotNull Chunk chunk) { ChunkEntry chunkEntry = setChunkThread(chunk, (thread) -> new ChunkEntry(thread, chunk)); this.chunkEntryMap.put(chunk, chunkEntry); this.chunks.add(chunk); } - protected void switchChunk(@NotNull Chunk chunk) { + private void switchChunk(@NotNull Chunk chunk) { ChunkEntry chunkEntry = chunkEntryMap.get(chunk); if (chunkEntry == null) return; @@ -256,9 +243,9 @@ public abstract class ThreadProvider { }); } - protected @NotNull ChunkEntry setChunkThread(@NotNull Chunk chunk, - @NotNull Function chunkEntrySupplier) { - final int threadId = getThreadId(chunk); + private @NotNull ChunkEntry setChunkThread(@NotNull Chunk chunk, + @NotNull Function chunkEntrySupplier) { + final int threadId = Math.abs(findThread(chunk)) % threads.size(); TickThread thread = threads.get(threadId); var chunks = threadChunkMap.computeIfAbsent(thread, tickThread -> ConcurrentHashMap.newKeySet()); @@ -267,7 +254,7 @@ public abstract class ThreadProvider { return chunkEntry; } - protected void removeChunk(Chunk chunk) { + private void removeChunk(Chunk chunk) { ChunkEntry chunkEntry = chunkEntryMap.get(chunk); if (chunkEntry != null) { TickThread thread = chunkEntry.thread; @@ -280,16 +267,6 @@ public abstract class ThreadProvider { this.chunks.remove(chunk); } - /** - * Finds the thread id associated to a {@link Chunk}. - * - * @param chunk the chunk to find the thread id from - * @return the chunk thread id - */ - protected int getThreadId(@NotNull Chunk chunk) { - return (int) (Math.abs(findThread(chunk)) % threads.size()); - } - private void processRemovedEntities() { if (removedEntities.isEmpty()) return; @@ -310,25 +287,15 @@ public abstract class ThreadProvider { for (Entity entity : updatableEntities) { var acquirableEntity = entity.getAcquirable(); ChunkEntry handlerChunkEntry = acquirableEntity.getHandler().getChunkEntry(); - - Chunk entityChunk = entity.getChunk(); - - // Entity is possibly not in the correct thread - // Remove from previous list - { - if (handlerChunkEntry != null) { - handlerChunkEntry.entities.remove(entity); - } + if (handlerChunkEntry != null) { + handlerChunkEntry.entities.remove(entity); } - // Add to new list - { - ChunkEntry chunkEntry = chunkEntryMap.get(entityChunk); - if (chunkEntry != null) { - chunkEntry.entities.add(entity); - acquirableEntity.getHandler().refreshChunkEntry(chunkEntry); - } + ChunkEntry chunkEntry = chunkEntryMap.get(entity.getChunk()); + if (chunkEntry != null) { + chunkEntry.entities.add(entity); + acquirableEntity.getHandler().refreshChunkEntry(chunkEntry); } } this.updatableEntities.clear(); @@ -387,5 +354,4 @@ public abstract class ThreadProvider { return Objects.hash(chunk); } } - }