Skip to content

Commit

Permalink
LightAPI: Do not interact with chunks if they are not loaded
Browse files Browse the repository at this point in the history
Change-Id: Iea331ebd346607ec0f34ed27c6028cd66d9e3d72
  • Loading branch information
BeYkeRYkt committed Oct 31, 2021
1 parent 866c000 commit 8148f40
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public boolean isValidChunkSection(int sectionY) {
return getHandler().isValidChunkSection(sectionY);
}

@Override
public boolean isChunkLoaded(String worldName, int chunkX, int chunkZ) {
if (!getPlatformImpl().isWorldAvailable(worldName)) {
return false;
}
World world = Bukkit.getWorld(worldName);
return world.isChunkLoaded(chunkX, chunkZ);
}

@Override
public List<IChunkData> collectChunkSections(String worldName, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private long chunkCoordToLong(int chunkX, int chunkZ) {

public abstract boolean isValidChunkSection(int sectionY);

public abstract boolean isChunkLoaded(String worldName, int chunkX, int chunkZ);

/* @hide */
private int notifyUpdateChunksLocked(String worldName, int blockX, int blockY, int blockZ, int lightLevel,
int lightType) {
Expand All @@ -100,14 +102,16 @@ private int notifyUpdateChunksLocked(String worldName, int blockX, int blockY, i
for (int dZ = -CHUNK_RADIUS; dZ <= CHUNK_RADIUS; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!isChunkLoaded(worldName, chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
long chunkCoord = chunkCoordToLong(chunkX, chunkZ);

IChunkData data;
if (observedChunks.containsKey(chunkCoord)) {
data = observedChunks.get(chunkCoord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -389,13 +390,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -432,6 +435,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, int sectionMaskSky,
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().playerChunkMap.a(chunkCoordIntPair, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -389,13 +390,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -432,6 +435,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, int sectionMaskSky,
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().playerChunkMap.a(chunkCoordIntPair, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -389,13 +390,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -432,6 +435,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, int sectionMaskSky,
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().playerChunkMap.a(chunkCoordIntPair, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -389,13 +390,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -432,6 +435,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, int sectionMaskSky,
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().playerChunkMap.a(chunkCoordIntPair, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -399,13 +400,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -442,6 +445,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, int sectionMaskSky,
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().playerChunkMap.a(chunkCoordIntPair, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ private IChunkData searchChunkDataFromList(List<IChunkData> list, World world, i
@Override
public List<IChunkData> collectChunkSections(World world, int blockX, int blockY, int blockZ, int lightLevel,
int lightFlags) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
List<IChunkData> list = Lists.newArrayList();
int finalLightLevel = lightLevel < 0 ? 0 : lightLevel > 15 ? 15 : lightLevel;

Expand All @@ -408,13 +409,15 @@ public List<IChunkData> collectChunkSections(World world, int blockX, int blockY
for (int dZ = -1; dZ <= 1; dZ++) {
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dZ);
if (lightLevelZ > 0) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
continue;
}
for (int dY = -1; dY <= 1; dY++) {
if (lightLevelZ > getDeltaLight(blockY & 15, dY)) {
int sectionY = (blockY >> 4) + dY;
if (isValidChunkSection(sectionY)) {
int chunkX = (blockX >> 4) + dX;
int chunkZ = (blockZ >> 4) + dZ;

IChunkData data = searchChunkDataFromList(list, world, chunkX, chunkZ);
if (!list.contains(data)) {
list.add(data);
Expand Down Expand Up @@ -451,6 +454,9 @@ protected int sendChunk(World world, int chunkX, int chunkZ, BitSet sectionMaskS
return ResultCode.WORLD_NOT_AVAILABLE;
}
WorldServer worldServer = ((CraftWorld) world).getHandle();
if (!worldServer.getChunkProvider().isChunkLoaded(chunkX, chunkZ)) {
return ResultCode.CHUNK_NOT_LOADED;
}
Chunk chunk = worldServer.getChunkAt(chunkX, chunkZ);
ChunkCoordIntPair chunkCoordIntPair = chunk.getPos();
Stream<EntityPlayer> stream = worldServer.getChunkProvider().a.a(chunkCoordIntPair, false);
Expand Down

0 comments on commit 8148f40

Please sign in to comment.