Skip to content

Commit

Permalink
track players in view distance
Browse files Browse the repository at this point in the history
  • Loading branch information
sathonay committed Dec 19, 2024
1 parent c8fac5f commit 249a894
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ public void updatePlayers() {
while (iterator.hasNext()) {
EntityTrackerEntry entitytrackerentry = iterator.next();

entitytrackerentry.track(this.world.players);
if (entitytrackerentry.tracker instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) entitytrackerentry.tracker;
entitytrackerentry.track(this.world.getPlayersAround(player.getChunkCoordinates(), player.viewDistance));
} else {
entitytrackerentry.track(this.world.players);
}

if (entitytrackerentry.n && entitytrackerentry.tracker instanceof EntityPlayer) {
playerTrackers.add((EntityPlayer) entitytrackerentry.tracker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public void track(List list) {
this.v = 0;
// CraftBukkit start - Refresh list of who can see a player before sending teleport packet
if (this.tracker instanceof EntityPlayer) {
this.scanPlayers(this.tracker.world.players); // scan all players of the world (that's fix invisibility after teleportation)
EntityPlayer player = (EntityPlayer) this.tracker;
this.scanPlayers(this.tracker.world.getPlayersAround(player.getChunkCoordinates(), player.viewDistance)); // get players around in view distance // scan all players of the world (that's fix invisibility after teleportation)
}
// CraftBukkit end
packet = new PacketPlayOutEntityTeleport(this.tracker.getId(), i, j, k, (byte) l, (byte) i1, tracker.onGround, tracker instanceof EntityFallingBlock || tracker instanceof EntityTNTPrimed); // Spigot - protocol patch
Expand Down
39 changes: 28 additions & 11 deletions nPaper-Server/src/main/java/net/minecraft/server/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ private void initializeHoppers() {
triggerHoppersList.clear();
}

// could use getEntities but urf
public List<EntityPlayer> getPlayersAround(ChunkCoordinates coords, int distance)
{
int cx = coords.x;
int cz = coords.z;
List<EntityPlayer> list = new ArrayList<>();

for (int x = cx - distance; x <= cx + distance; ++x) {
for (int z = cz - distance; z <= cz + distance; ++z) {
Chunk chunk = this.getChunkIfLoaded(x, z);
if (chunk != null)
list.addAll(chunk.playersInChunk);
}
}
return list;
}

// Helper method for altHopperTicking. Updates chests at the specified location,
// accounting for double chests. Updating the chest will update adjacent hoppers.
public void updateChestAndHoppers(int a, int b, int c) {
Expand Down Expand Up @@ -610,7 +627,7 @@ public void update(int i, int j, int k, Block block) {
// CraftBukkit end
this.applyPhysics(i, j, k, block);
}

protected boolean isChunkLoaded(int i, int j, boolean flag) {
return (this.chunkProvider.isChunkLoaded(i, j) && (flag || !this.chunkProvider.getOrCreateChunk(i, j).isEmpty()));
}
Expand Down Expand Up @@ -1730,7 +1747,7 @@ public void entityJoinedWorld(Entity entity, boolean flag) {
if (entity.ag && this.isChunkLoaded(entity.ah, entity.aj)) {
this.getChunkAt(entity.ah, entity.aj).a(entity, entity.ai);
}

final boolean loaded = entity.ag = this.isChunkLoaded(k, i1);
if (loaded) {
this.getChunkAt(k, i1).a(entity);
Expand Down Expand Up @@ -2273,15 +2290,15 @@ protected void o() {
protected void C() {
// this.chunkTickList.clear(); // CraftBukkit - removed
this.methodProfiler.a("buildList");

// Rinny - Moved down
/*
int j;
int k;
int l;
*/
// Rinny

// Spigot start
final int optimalChunks = spigotConfig.chunksPerTick;
// Quick conditions to allow us to exist early
Expand Down Expand Up @@ -2928,7 +2945,7 @@ public EntityHuman findNearbyPlayer(double d0, double d1, double d2, double d3)
}
// CraftBukkit end
double d5 = entityPlayer.e(d0, d1, d2);

if ((d3 < 0.0D || d5 < d3 * d3) && (d4 == -1.0D || d5 < d4)) {
d4 = d5;
entityhuman = entityPlayer;
Expand Down Expand Up @@ -2957,25 +2974,25 @@ public EntityHuman findNearbyVulnerablePlayer(double d0, double d1, double d2, d
continue;
}
// CraftBukkit end

if (!entityPlayer.abilities.isInvulnerable && entityPlayer.isAlive()) {
double d5 = entityPlayer.e(d0, d1, d2);
double d6 = d3;

if (entityPlayer.isSneaking()) {
d6 = d3 * 0.800000011920929D;
}

if (entityPlayer.isInvisible()) {
float f = entityPlayer.bE();

if (f < 0.1F) {
f = 0.1F;
}

d6 *= (double) (0.7F * f);
}

if ((d3 < 0.0D || d5 < d6 * d6) && (d4 == -1.0D || d5 < d4)) {
d4 = d5;
entityhuman = entityPlayer;
Expand Down

0 comments on commit 249a894

Please sign in to comment.