mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
119 lines
6.1 KiB
Diff
119 lines
6.1 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
Date: Wed, 4 Sep 2024 11:25:19 -0700
|
|
Subject: [PATCH] Moonrise: Add direct lookup by chunk for NearbyPlayers
|
|
|
|
Removed since Leaf 1.21.3, Paper 1.21.3 included it
|
|
|
|
Original license: GPLv3
|
|
Original project: https://github.com/Tuinity/Moonrise
|
|
|
|
https://github.com/Tuinity/Moonrise/commit/d9988c86f42f543571fd1ebe8ec1079a37935e62
|
|
|
|
This simplifies the lookup code by reducing the number of "random"
|
|
accesses to 1 (get() call) assuming that the directByChunk array
|
|
is cached.
|
|
|
|
diff --git a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java
|
|
index ab093b0e8ac6f762921eb1d15f5217345c4eba05..994456ea99d78aebe41398f72019d9f6842172ff 100644
|
|
--- a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java
|
|
+++ b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java
|
|
@@ -37,6 +37,14 @@ public final class NearbyPlayers {
|
|
private final ServerLevel world;
|
|
private final Reference2ReferenceOpenHashMap<ServerPlayer, TrackedPlayer[]> players = new Reference2ReferenceOpenHashMap<>();
|
|
private final Long2ReferenceOpenHashMap<TrackedChunk> byChunk = new Long2ReferenceOpenHashMap<>();
|
|
+ // Moonrise start - Add direct lookup by chunk for NearbyPlayers
|
|
+ private final Long2ReferenceOpenHashMap<ReferenceList<ServerPlayer>>[] directByChunk = new Long2ReferenceOpenHashMap[TOTAL_MAP_TYPES];
|
|
+ {
|
|
+ for (int i = 0; i < this.directByChunk.length; ++i) {
|
|
+ this.directByChunk[i] = new Long2ReferenceOpenHashMap<>();
|
|
+ }
|
|
+ }
|
|
+ // Moonrise end - Add direct lookup by chunk for NearbyPlayers
|
|
|
|
public NearbyPlayers(final ServerLevel world) {
|
|
this.world = world;
|
|
@@ -95,37 +103,40 @@ public final class NearbyPlayers {
|
|
}
|
|
|
|
public ReferenceList<ServerPlayer> getPlayers(final BlockPos pos, final NearbyMapType type) {
|
|
- final TrackedChunk chunk = this.byChunk.get(CoordinateUtils.getChunkKey(pos));
|
|
-
|
|
- return chunk == null ? null : chunk.players[type.ordinal()];
|
|
+ return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(pos)); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
}
|
|
|
|
public ReferenceList<ServerPlayer> getPlayers(final ChunkPos pos, final NearbyMapType type) {
|
|
- final TrackedChunk chunk = this.byChunk.get(CoordinateUtils.getChunkKey(pos));
|
|
-
|
|
- return chunk == null ? null : chunk.players[type.ordinal()];
|
|
+ return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(pos)); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
}
|
|
|
|
public ReferenceList<ServerPlayer> getPlayersByChunk(final int chunkX, final int chunkZ, final NearbyMapType type) {
|
|
- final TrackedChunk chunk = this.byChunk.get(CoordinateUtils.getChunkKey(chunkX, chunkZ));
|
|
-
|
|
- return chunk == null ? null : chunk.players[type.ordinal()];
|
|
+ return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(chunkX, chunkZ)); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
}
|
|
|
|
public ReferenceList<ServerPlayer> getPlayersByBlock(final int blockX, final int blockZ, final NearbyMapType type) {
|
|
- final TrackedChunk chunk = this.byChunk.get(CoordinateUtils.getChunkKey(blockX >> 4, blockZ >> 4));
|
|
-
|
|
- return chunk == null ? null : chunk.players[type.ordinal()];
|
|
+ return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(blockX >> 4, blockZ >> 4)); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
}
|
|
|
|
public static final class TrackedChunk {
|
|
|
|
private static final ServerPlayer[] EMPTY_PLAYERS_ARRAY = new ServerPlayer[0];
|
|
|
|
+ // Moonrise start - Add direct lookup by chunk for NearbyPlayers
|
|
+ private final long chunkKey;
|
|
+ private final NearbyPlayers nearbyPlayers;
|
|
+ // Moonrise end - Add direct lookup by chunk for NearbyPlayers
|
|
private final ReferenceList<ServerPlayer>[] players = new ReferenceList[TOTAL_MAP_TYPES];
|
|
private int nonEmptyLists;
|
|
private long updateCount;
|
|
|
|
+ // Moonrise start - Add direct lookup by chunk for NearbyPlayers
|
|
+ public TrackedChunk(final long chunkKey, final NearbyPlayers nearbyPlayers) {
|
|
+ this.chunkKey = chunkKey;
|
|
+ this.nearbyPlayers = nearbyPlayers;
|
|
+ }
|
|
+ // Moonrise end - Add direct lookup by chunk for NearbyPlayers
|
|
+
|
|
public boolean isEmpty() {
|
|
return this.nonEmptyLists == 0;
|
|
}
|
|
@@ -145,7 +156,11 @@ public final class NearbyPlayers {
|
|
final ReferenceList<ServerPlayer> list = this.players[idx];
|
|
if (list == null) {
|
|
++this.nonEmptyLists;
|
|
- (this.players[idx] = new ReferenceList<>(EMPTY_PLAYERS_ARRAY)).add(player);
|
|
+ // Moonrise start - Add direct lookup by chunk for NearbyPlayers
|
|
+ final ReferenceList<ServerPlayer> players = (this.players[idx] = new ReferenceList<>(EMPTY_PLAYERS_ARRAY));
|
|
+ this.nearbyPlayers.directByChunk[idx].put(this.chunkKey, players);
|
|
+ players.add(player);
|
|
+ // Moonrise end - Add direct lookup by chunk for NearbyPlayers
|
|
return;
|
|
}
|
|
|
|
@@ -169,6 +184,7 @@ public final class NearbyPlayers {
|
|
|
|
if (list.size() == 0) {
|
|
this.players[idx] = null;
|
|
+ this.nearbyPlayers.directByChunk[idx].remove(this.chunkKey); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
--this.nonEmptyLists;
|
|
}
|
|
}
|
|
@@ -188,7 +204,7 @@ public final class NearbyPlayers {
|
|
final long chunkKey = CoordinateUtils.getChunkKey(chunkX, chunkZ);
|
|
|
|
NearbyPlayers.this.byChunk.computeIfAbsent(chunkKey, (final long keyInMap) -> {
|
|
- return new TrackedChunk();
|
|
+ return new TrackedChunk(keyInMap, NearbyPlayers.this); // Moonrise - Add direct lookup by chunk for NearbyPlayers
|
|
}).addPlayer(parameter, this.type);
|
|
}
|
|
|