9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 11:29:11 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0195-Slightly-optimise-getNearestPlayer.patch
Dreeam f5b95a6716 Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@b0da38c2 Repository details in RuntimeException for MavenLibraryResolver#addRepository (#12939)
PaperMC/Paper@1922be90 Update custom tags (#12183)
PaperMC/Paper@79cf1353 Ignore HopperInventorySearchEvent when it has no listeners (#13009)
PaperMC/Paper@ea014f7a feat: add stuckEntityPoiRetryDelay config (#12949)
PaperMC/Paper@a9e76749 Support for showNotification in PlayerRecipeDiscoverEvent (#12992)
PaperMC/Paper@5622c9dd Expose attribute sentiment (#12974)
PaperMC/Paper@42b653b1 Expose more argument types (#12665)
PaperMC/Paper@52d9a221 [ci/skip] Fix typo in Display javadoc (#13010)
PaperMC/Paper@614e9acf Improve APIs around riptide tridents (#12996)
PaperMC/Paper@51706e5a Fixed DyeItem sheep dye hunk
2025-08-25 15:52:00 -04:00

75 lines
2.9 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Sun, 16 Feb 2025 09:21:50 +0100
Subject: [PATCH] Slightly optimise getNearestPlayer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Original:
Condition checks per iteration: ~3-4 (predicate check, distance check, and d comparison)
Calculations per iteration: Distance calculation + potentially a multiplication
Total operations: ~(5-6) × n
Patch:
Condition checks per iteration: 1 (just the distance comparison)
Calculations per iteration: Distance calculation
One-time operation: Convert distance to squared distance
Total operations: ~3 × n (in the no-predicate case) or ~4 × n (with predicate)
diff --git a/net/minecraft/world/level/EntityGetter.java b/net/minecraft/world/level/EntityGetter.java
index 6a7d99c172c2f6fc30a4ffcb3cd7bfad92e27adc..aa1a7fe56388bc103958bce0af43dd41c3d43b04 100644
--- a/net/minecraft/world/level/EntityGetter.java
+++ b/net/minecraft/world/level/EntityGetter.java
@@ -201,23 +201,43 @@ public interface EntityGetter extends ca.spottedleaf.moonrise.patches.chunk_syst
}
// Paper end - Affects Spawning API
+ // Leaf start - Slightly optimise getNearestPlayer
@Nullable
default Player getNearestPlayer(double x, double y, double z, double distance, @Nullable Predicate<Entity> predicate) {
- double d = -1.0;
+ if (distance < 0.0) {
+ distance = Double.MAX_VALUE;
+ } else {
+ distance = distance * distance;
+ }
+
Player player = null;
- for (Player player1 : this.players()) {
- if (predicate == null || predicate.test(player1)) {
+ List<? extends Player> players = this.players();
+ if (predicate == null) {
+ for (int i = 0, playersSize = players.size(); i < playersSize; i++) {
+ Player player1 = players.get(i);
double d1 = player1.distanceToSqr(x, y, z);
- if ((distance < 0.0 || d1 < distance * distance) && (d == -1.0 || d1 < d)) {
- d = d1;
+ if (d1 < distance) {
+ distance = d1;
player = player1;
}
}
+ } else {
+ for (int i = 0, playersSize = players.size(); i < playersSize; i++) {
+ Player player1 = players.get(i);
+ if (predicate.test(player1)) {
+ double d1 = player1.distanceToSqr(x, y, z);
+ if (d1 < distance) {
+ distance = d1;
+ player = player1;
+ }
+ }
+ }
}
return player;
}
+ // Leaf end - Slightly optimise getNearestPlayer
// Paper start
default List<org.bukkit.entity.HumanEntity> findNearbyBukkitPlayers(double x, double y, double z, double radius, boolean notSpectator) {