mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-05 10:20:53 +01:00
31699ae9a8
* Updated Upstream (Bukkit/CraftBukkit) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: a6a9d2a4 Remove some old ApiStatus.Experimental annotations be72314c SPIGOT-7300, PR-829: Add new DamageSource API providing enhanced information about entity damage b252cf05 SPIGOT-7576, PR-970: Add methods in MushroomCow to change stew effects b1c689bd PR-902: Add Server#isLoggingIPs to get log-ips configuration 08f86d1c PR-971: Add Player methods for client-side potion effects 2e3024a9 PR-963: Add API for in-world structures a23292a7 SPIGOT-7530, PR-948: Improve Resource Pack API with new 1.20.3 functionality 1851857b SPIGOT-3071, PR-969: Add entity spawn method with spawn reason cde4c52a SPIGOT-5553, PR-964: Add EntityKnockbackEvent CraftBukkit Changes: 38fd4bd50 Fix accidentally renamed internal damage method 80f0ce4be SPIGOT-7300, PR-1180: Add new DamageSource API providing enhanced information about entity damage 7e43f3b16 SPIGOT-7581: Fix typo in BlockMushroom ea14b7d90 SPIGOT-7576, PR-1347: Add methods in MushroomCow to change stew effects 4c687f243 PR-1259: Add Server#isLoggingIPs to get log-ips configuration 22a541a29 Improve support for per-world game rules cb7dccce2 PR-1348: Add Player methods for client-side potion effects b8d6109f0 PR-1335: Add API for in-world structures 4398a1b5b SPIGOT-7577: Make CraftWindCharge#explode discard the entity e74107678 Fix Crafter maximum stack size 0bb0f4f6a SPIGOT-7530, PR-1314: Improve Resource Pack API with new 1.20.3 functionality 4949f556d SPIGOT-3071, PR-1345: Add entity spawn method with spawn reason 20ac73ca2 PR-1353: Fix Structure#place not working as documented with 0 palette 3c1b77871 SPIGOT-6911, PR-1349: Change max book length in CraftMetaBook 333701839 SPIGOT-7572: Bee nests generated without bees f48f4174c SPIGOT-5553, PR-1336: Add EntityKnockbackEvent
230 lines
12 KiB
Diff
230 lines
12 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
Date: Sat, 23 Sep 2023 23:15:52 -0700
|
|
Subject: [PATCH] Optimise nearby player retrieval
|
|
|
|
Instead of searching/testing every player online on the server,
|
|
we can instead use the nearby player tracking system to reduce
|
|
the number of tests per search.
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
index 5bbfb1af24e13a9e6a02ad8c36bb504a17f06398..6934e9dac0d69c043b73b7c46d59f2d39b37c67f 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
@@ -576,6 +576,115 @@ public class ServerLevel extends Level implements WorldGenLevel {
|
|
this.lagCompensationTick = (System.nanoTime() - net.minecraft.server.MinecraftServer.SERVER_INIT) / (java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(50L));
|
|
}
|
|
// Paper end - lag compensation
|
|
+ // Paper start - optimise nearby player retrieval
|
|
+ @Override
|
|
+ public java.util.List<net.minecraft.world.entity.player.Player> getNearbyPlayers(net.minecraft.world.entity.ai.targeting.TargetingConditions targetPredicate,
|
|
+ net.minecraft.world.entity.LivingEntity entity,
|
|
+ net.minecraft.world.phys.AABB box) {
|
|
+ return this.getNearbyEntities(Player.class, targetPredicate, entity, box);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Player getNearestPlayer(double x, double y, double z, double maxDistance, @Nullable Predicate<Entity> targetPredicate) {
|
|
+ if (maxDistance > 0.0D) {
|
|
+ io.papermc.paper.util.player.NearbyPlayers players = this.chunkSource.chunkMap.getNearbyPlayers();
|
|
+
|
|
+ com.destroystokyo.paper.util.maplist.ReferenceList<ServerPlayer> nearby = players.getPlayersByBlock(
|
|
+ io.papermc.paper.util.CoordinateUtils.getBlockCoordinate(x),
|
|
+ io.papermc.paper.util.CoordinateUtils.getBlockCoordinate(z),
|
|
+ io.papermc.paper.util.player.NearbyPlayers.NearbyMapType.GENERAL
|
|
+ );
|
|
+
|
|
+ if (nearby == null) {
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ ServerPlayer nearest = null;
|
|
+ double nearestDist = maxDistance * maxDistance;
|
|
+ Object[] rawData = nearby.getRawData();
|
|
+ for (int i = 0, len = nearby.size(); i < len; ++i) {
|
|
+ ServerPlayer player = (ServerPlayer)rawData[i];
|
|
+ double dist = player.distanceToSqr(x, y, z);
|
|
+ if (dist >= nearestDist) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (targetPredicate == null || targetPredicate.test(player)) {
|
|
+ nearest = player;
|
|
+ nearestDist = dist;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return nearest;
|
|
+ } else {
|
|
+ ServerPlayer nearest = null;
|
|
+ double nearestDist = Double.MAX_VALUE;
|
|
+
|
|
+ for (ServerPlayer player : this.players()) {
|
|
+ double dist = player.distanceToSqr(x, y, z);
|
|
+ if (dist >= nearestDist) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (targetPredicate == null || targetPredicate.test(player)) {
|
|
+ nearest = player;
|
|
+ nearestDist = dist;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return nearest;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Player getNearestPlayer(net.minecraft.world.entity.ai.targeting.TargetingConditions targetPredicate, LivingEntity entity) {
|
|
+ return this.getNearestPlayer(targetPredicate, entity, entity.getX(), entity.getY(), entity.getZ());
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Player getNearestPlayer(net.minecraft.world.entity.ai.targeting.TargetingConditions targetPredicate, LivingEntity entity,
|
|
+ double x, double y, double z) {
|
|
+ double range = targetPredicate.range;
|
|
+ if (range > 0.0D) {
|
|
+ io.papermc.paper.util.player.NearbyPlayers players = this.chunkSource.chunkMap.getNearbyPlayers();
|
|
+
|
|
+ com.destroystokyo.paper.util.maplist.ReferenceList<ServerPlayer> nearby = players.getPlayersByBlock(
|
|
+ io.papermc.paper.util.CoordinateUtils.getBlockCoordinate(x),
|
|
+ io.papermc.paper.util.CoordinateUtils.getBlockCoordinate(z),
|
|
+ io.papermc.paper.util.player.NearbyPlayers.NearbyMapType.GENERAL
|
|
+ );
|
|
+
|
|
+ if (nearby == null) {
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ ServerPlayer nearest = null;
|
|
+ double nearestDist = Double.MAX_VALUE;
|
|
+ Object[] rawData = nearby.getRawData();
|
|
+ for (int i = 0, len = nearby.size(); i < len; ++i) {
|
|
+ ServerPlayer player = (ServerPlayer)rawData[i];
|
|
+ double dist = player.distanceToSqr(x, y, z);
|
|
+ if (dist >= nearestDist) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (targetPredicate.test(entity, player)) {
|
|
+ nearest = player;
|
|
+ nearestDist = dist;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return nearest;
|
|
+ } else {
|
|
+ return this.getNearestEntity(this.players(), targetPredicate, entity, x, y, z);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ public Player getNearestPlayer(net.minecraft.world.entity.ai.targeting.TargetingConditions targetPredicate, double x, double y, double z) {
|
|
+ return this.getNearestPlayer(targetPredicate, null, x, y, z);
|
|
+ }
|
|
+ // Paper end - optimise nearby player retrieval
|
|
|
|
// Add env and gen to constructor, IWorldDataServer -> WorldDataServer
|
|
public ServerLevel(MinecraftServer minecraftserver, Executor executor, LevelStorageSource.LevelStorageAccess convertable_conversionsession, PrimaryLevelData iworlddataserver, ResourceKey<Level> resourcekey, LevelStem worlddimension, ChunkProgressListener worldloadlistener, boolean flag, long i, List<CustomSpawner> list, boolean flag1, @Nullable RandomSequences randomsequences, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider) {
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
|
|
index ed1b95ec694b0fe8b647964b18b8c33707fc0b47..65cd42ce9f553e0aa5bf248bdbf902f9d1f55460 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
|
|
@@ -21,18 +21,50 @@ public class PlayerSensor extends Sensor<LivingEntity> {
|
|
|
|
@Override
|
|
protected void doTick(ServerLevel world, LivingEntity entity) {
|
|
- List<Player> list = world.players().stream().filter(EntitySelector.NO_SPECTATORS).filter((player) -> {
|
|
- return entity.closerThan(player, 16.0D);
|
|
- }).sorted(Comparator.comparingDouble(entity::distanceToSqr)).collect(Collectors.toList());
|
|
+ // Paper start - Perf: optimise nearby player retrieval & remove streams from hot code
|
|
+ io.papermc.paper.util.player.NearbyPlayers nearbyPlayers = world.chunkSource.chunkMap.getNearbyPlayers();
|
|
+ net.minecraft.world.phys.Vec3 entityPos = entity.position();
|
|
+ com.destroystokyo.paper.util.maplist.ReferenceList<net.minecraft.server.level.ServerPlayer> nearby = nearbyPlayers.getPlayersByChunk(
|
|
+ entity.chunkPosition().x,
|
|
+ entity.chunkPosition().z,
|
|
+ io.papermc.paper.util.player.NearbyPlayers.NearbyMapType.GENERAL_REALLY_SMALL
|
|
+ );
|
|
+
|
|
+ List<Player> players = new java.util.ArrayList<>(nearby == null ? 0 : nearby.size());
|
|
+ if (nearby != null) {
|
|
+ Object[] rawData = nearby.getRawData();
|
|
+ for (int index = 0, len = nearby.size(); index < len; ++index) {
|
|
+ net.minecraft.server.level.ServerPlayer player = (net.minecraft.server.level.ServerPlayer) rawData[index];
|
|
+ if (player.isSpectator()) {
|
|
+ continue;
|
|
+ }
|
|
+ if (player.distanceToSqr(entityPos.x, entityPos.y, entityPos.z) >= (16.0 * 16.0)) {
|
|
+ continue;
|
|
+ }
|
|
+ players.add(player);
|
|
+ }
|
|
+ }
|
|
+ players.sort(Comparator.comparingDouble(entity::distanceToSqr));
|
|
Brain<?> brain = entity.getBrain();
|
|
- brain.setMemory(MemoryModuleType.NEAREST_PLAYERS, list);
|
|
- List<Player> list2 = list.stream().filter((player) -> {
|
|
- return isEntityTargetable(entity, player);
|
|
- }).collect(Collectors.toList());
|
|
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, list2.isEmpty() ? null : list2.get(0));
|
|
- Optional<Player> optional = list2.stream().filter((player) -> {
|
|
- return isEntityAttackable(entity, player);
|
|
- }).findFirst();
|
|
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER, optional);
|
|
+
|
|
+ brain.setMemory(MemoryModuleType.NEAREST_PLAYERS, players);
|
|
+
|
|
+ Player firstTargetable = null;
|
|
+ Player firstAttackable = null;
|
|
+ for (Player player : players) {
|
|
+ if (firstTargetable == null && Sensor.isEntityTargetable(entity, player)) {
|
|
+ firstTargetable = player;
|
|
+ }
|
|
+ if (firstAttackable == null && Sensor.isEntityAttackable(entity, player)) {
|
|
+ firstAttackable = player;
|
|
+ }
|
|
+
|
|
+ if (firstAttackable != null && firstTargetable != null) {
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, firstTargetable);
|
|
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER, Optional.ofNullable(firstAttackable));
|
|
+ // Paper end - Perf: optimise nearby player retrieval & remove streams from hot code
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java b/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
|
|
index fae0dbfb6ac09a0c152c0f74a72583f44316def7..c8a80c1b2fedff22e8a877d466062375ffb2f0d7 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/targeting/TargetingConditions.java
|
|
@@ -10,7 +10,7 @@ public class TargetingConditions {
|
|
public static final TargetingConditions DEFAULT = forCombat();
|
|
private static final double MIN_VISIBILITY_DISTANCE_FOR_INVISIBLE_TARGET = 2.0D;
|
|
private final boolean isCombat;
|
|
- private double range = -1.0D;
|
|
+ public double range = -1.0D; // Paper - public
|
|
private boolean checkLineOfSight = true;
|
|
private boolean testInvisible = true;
|
|
@Nullable
|
|
diff --git a/src/main/java/net/minecraft/world/level/EntityGetter.java b/src/main/java/net/minecraft/world/level/EntityGetter.java
|
|
index f42dd9602805e9d538506ee4e3eac7e2811a3da6..ed84c87a3f76bc0254c1abb189e6b8b808823465 100644
|
|
--- a/src/main/java/net/minecraft/world/level/EntityGetter.java
|
|
+++ b/src/main/java/net/minecraft/world/level/EntityGetter.java
|
|
@@ -230,9 +230,13 @@ public interface EntityGetter {
|
|
T livingEntity = null;
|
|
|
|
for(T livingEntity2 : entityList) {
|
|
+ // Paper start - optimise nearby player retrieval; move up
|
|
+ // don't check entities outside closest range
|
|
+ double e = livingEntity2.distanceToSqr(x, y, z);
|
|
+ if (d == -1.0D || e < d) {
|
|
+ // Paper end - move up
|
|
if (targetPredicate.test(entity, livingEntity2)) {
|
|
- double e = livingEntity2.distanceToSqr(x, y, z);
|
|
- if (d == -1.0D || e < d) {
|
|
+ // Paper - optimise nearby player retrieval; move up
|
|
d = e;
|
|
livingEntity = livingEntity2;
|
|
}
|