Skip to content

Commit

Permalink
#1409: Add methods to get players seeing specific chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
MetallicGoat authored and md-5 committed Jun 7, 2024
1 parent 16c9767 commit 58c41ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/bukkit/craftbukkit/CraftChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.generator.structure.GeneratedStructure;
import org.bukkit.generator.structure.Structure;
import org.bukkit.persistence.PersistentDataContainer;
Expand Down Expand Up @@ -376,6 +377,11 @@ public Collection<GeneratedStructure> getStructures(Structure structure) {
return getCraftWorld().getStructures(getX(), getZ(), structure);
}

@Override
public Collection<Player> getPlayersSeeingChunk() {
return getWorld().getPlayersSeeingChunk(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/bukkit/craftbukkit/CraftWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,31 @@ public boolean refreshChunk(int x, int z) {
return true;
}

@Override
public Collection<Player> getPlayersSeeingChunk(Chunk chunk) {
Preconditions.checkArgument(chunk != null, "chunk cannot be null");

return getPlayersSeeingChunk(chunk.getX(), chunk.getZ());
}

@Override
public Collection<Player> getPlayersSeeingChunk(int x, int z) {
if (!isChunkLoaded(x, z)) {
return Collections.emptySet();
}

List<EntityPlayer> players = world.getChunkSource().chunkMap.getPlayers(new ChunkCoordIntPair(x, z), false);

if (players.isEmpty()) {
return Collections.emptySet();
}

return players.stream()
.filter(Objects::nonNull)
.map(EntityPlayer::getBukkitEntity)
.collect(Collectors.toUnmodifiableSet());
}

@Override
public boolean isChunkInUse(int x, int z) {
return isChunkLoaded(x, z);
Expand Down

0 comments on commit 58c41ce

Please sign in to comment.