下面列出了org.bukkit.World#isChunkLoaded ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int getWorldBlockData(UserConnection user, int bx, int by, int bz) {
UUID uuid = user.getProtocolInfo().getUuid();
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
World world = player.getWorld();
int x = bx >> 4;
int z = bz >> 4;
if (world.isChunkLoaded(x, z)) {
Chunk c = getChunk(world, x, z);
Block b = c.getBlock(bx, by, bz);
return b.getTypeId() << 4 | b.getData();
}
}
return 0;
}
@Override
public ChunkSnapshot getCachedChunk(World world, int cx, int cz) {
long pair = MathMan.pairInt(cx, cz);
ChunkSnapshot cached = chunkCache.get(pair);
if (cached != null) return cached;
if (world.isChunkLoaded(cx, cz)) {
Long originalKeep = keepLoaded.get(pair);
keepLoaded.put(pair, Long.MAX_VALUE);
if (world.isChunkLoaded(cx, cz)) {
Chunk chunk = world.getChunkAt(cx, cz);
ChunkSnapshot snapshot = getAndCacheChunk(chunk);
if (originalKeep != null) {
keepLoaded.put(pair, originalKeep);
} else {
keepLoaded.remove(pair);
}
return snapshot;
} else {
keepLoaded.remove(pair);
return null;
}
} else {
return null;
}
}
public static Sign getSign(World world, Vector3i pos) {
if (pos == null) {
return null;
}
// Don't load chunks to update signs
if (!world.isChunkLoaded(pos.getX() >> 4, pos.getZ() >> 4)) {
return null;
}
return getSign(VecHelper.toLocation(world, pos));
}
public boolean isChunkLoaded() {
World world = Bukkit.getServer().getWorld(worldName);
if (world != null) {
return world.isChunkLoaded(chunkX, chunkZ);
}
return false;
}
public Set<Claim> getNearbyClaims(Location location, int blockDistance, boolean includeChildren) {
Set<Claim> claims = new HashSet<>();
GDClaimManager claimWorldManager = GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(location.getWorld().getUID());
if (claimWorldManager == null) {
return claims;
}
final World world = location.getWorld();
org.bukkit.Chunk lesserChunk = location.getWorld().getChunkAt((location.getBlockX() - blockDistance) >> 4, (location.getBlockZ() - blockDistance) >> 4);
org.bukkit.Chunk greaterChunk = location.getWorld().getChunkAt((location.getBlockX() + blockDistance) >> 4, (location.getBlockZ() + blockDistance) >> 4);
if (lesserChunk != null && greaterChunk != null) {
for (int chunkX = lesserChunk.getX(); chunkX <= greaterChunk.getX(); chunkX++) {
for (int chunkZ = lesserChunk.getZ(); chunkZ <= greaterChunk.getZ(); chunkZ++) {
if (!world.isChunkLoaded(chunkX, chunkZ)) {
continue;
}
org.bukkit.Chunk chunk = location.getWorld().getChunkAt(chunkX, chunkZ);
if (chunk != null) {
Set<Claim> claimsInChunk = claimWorldManager.getInternalChunksToClaimsMap().get(NMSUtil.getInstance().getChunkCoordIntPair(chunkX, chunkZ));
if (claimsInChunk != null) {
for (Claim claim : claimsInChunk) {
final GDClaim gdClaim = (GDClaim) claim;
if (!includeChildren) {
if (gdClaim.parent == null && !claims.contains(claim)) {
claims.add(claim);
}
} else if (!claims.contains(claim)){
claims.add(claim);
for (Claim child : gdClaim.getChildren(true)) {
claims.add(child);
}
}
}
}
}
}
}
}
return claims;
}
public boolean isChunkLoaded(World world, int cx, int cz) {
return world.isChunkLoaded(cx, cz);
}
public boolean isChunkLoadedAtBlock(World world, int x, int z) {
return world.isChunkLoaded(x >> 4, z >> 4);
}
private boolean isChunkLoaded(World world, int x, int z) {
return world.isChunkLoaded(x, z);
}
public static List<Entity> getNearbyEntities(Location location, double radius, EntityType... types) {
List<Entity> entities = new ArrayList<Entity>();
if (location == null) return entities;
if (radius <= 0.0D) return entities;
double radius2 = radius * radius;
int chunkRadius = ((int) (radius / 16)) + 1;
Chunk center = location.getChunk();
int startX = center.getX() - chunkRadius;
int endX = center.getX() + chunkRadius;
int startZ = center.getZ() - chunkRadius;
int endZ = center.getZ() + chunkRadius;
World world = location.getWorld();
for (int chunkX = startX; chunkX <= endX; chunkX++) {
for (int chunkZ = startZ; chunkZ <= endZ; chunkZ++) {
if (!world.isChunkLoaded(chunkX, chunkZ)) continue;
Chunk chunk = world.getChunkAt(chunkX, chunkZ);
for (Entity entity : chunk.getEntities()) {
Location entityLoc = entity.getLocation();
// TODO: this is a workaround: for some yet unknown reason entities sometimes report to be in a
// different world..
if (!entityLoc.getWorld().equals(world)) {
Log.debug("Found an entity which reports to be in a different world than the chunk we got it from:");
Log.debug("Location=" + location + ", Chunk=" + chunk + ", ChunkWorld=" + chunk.getWorld()
+ ", entityType=" + entity.getType() + ", entityLocation=" + entityLoc);
continue; // skip this entity
}
if (entityLoc.distanceSquared(location) <= radius2) {
if (types == null) {
entities.add(entity);
} else {
EntityType type = entity.getType();
for (EntityType t : types) {
if (type.equals(t)) {
entities.add(entity);
break;
}
}
}
}
}
}
}
return entities;
}
/**
* Checks if the chunk of certain coordinates are loaded.
*
* @param world the {@link World} to check for the chunk
* @param blockX the x - coordinate of a {@link org.bukkit.block.Block} in that {@link World}
* @param blockZ the z - coordinate of a {@link org.bukkit.block.Block} in that {@link World}
*
* @return True if the chunk is loaded, else false.
*/
public static boolean isChunkLoaded(final World world, final int blockX, final int blockZ)
{
return world.isChunkLoaded(blockX >> 4, blockZ >> 4);
}