Skip to content

Commit

Permalink
Merge pull request #6098 from TownyAdvanced/feature/worldcoord-getchunks
Browse files Browse the repository at this point in the history
Add WorldCoord#getChunks
  • Loading branch information
LlmDl authored Aug 11, 2022
2 parents f3d86b6 + f020a4b commit 4bd9a5d
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/com/palmergames/bukkit/towny/object/WorldCoord.java
Original file line number Diff line number Diff line change
@@ -14,8 +14,13 @@
import org.bukkit.entity.Entity;
import org.bukkit.util.BoundingBox;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class WorldCoord extends Coord {
@@ -157,7 +162,7 @@ public boolean isWilderness() {
/**
* Loads the chunks represented by a WorldCoord. Creates a PluginChunkTicket so
* that the WorldCoord will remain loaded, even when no players are present.
*
* <p>
* Uses PaperLib's getChunkAtAsync when Paper is present.
*/
public void loadChunks() {
@@ -169,25 +174,13 @@ public void loadChunks() {
}

private void loadChunks(Towny plugin) {
if (getCellSize() > 16) {
// Dealing with a townblocksize greater than 16, we will have multiple chunks per WorldCoord.
int side = Math.round(getCellSize() / 16);
for (int x = 0; x <= side; x++) {
for (int z = 0; z <= side; z++) {
CompletableFuture<Chunk> futureChunk = PaperLib.getChunkAtAsync(getSubCorner(x, z));
futureChunk.thenAccept(chunk-> chunk.addPluginChunkTicket(plugin));
}
}
} else {
CompletableFuture<Chunk> futureChunk = PaperLib.getChunkAtAsync(getCorner());
futureChunk.thenAccept(chunk-> chunk.addPluginChunkTicket(plugin));
}
getChunks().forEach(future -> future.thenAccept(chunk -> chunk.addPluginChunkTicket(plugin)));
}

/**
* Unloads the chunks presented by a WorldCoord. Removes a PluginChunkTicket so
* that the WorldCoord will no longer remain loaded.
*
* <p>
* Uses PaperLib's getChunkAtAsync when Paper is present.
*/
public void unloadChunks() {
@@ -199,18 +192,31 @@ public void unloadChunks() {
}

private void unloadChunks(Towny plugin) {
getChunks().forEach(future -> future.thenAccept(chunk -> chunk.addPluginChunkTicket(plugin)));
}

/**
* Loads & returns the chunk(s) inside this WorldCoord.
* <p>
* Chunks are loaded async on servers using paper.
* @return An unmodifiable collection of chunk futures.
*/
@Unmodifiable
public Collection<CompletableFuture<Chunk>> getChunks() {
if (getCellSize() > 16) {
// Dealing with a townblocksize greater than 16, we will have multiple chunks per WorldCoord.
int side = Math.round(getCellSize() / 16);
final Set<CompletableFuture<Chunk>> chunkFutures = new HashSet<>();

int side = Math.round(getCellSize() / 16f);
for (int x = 0; x <= side; x++) {
for (int z = 0; z <= side; z++) {
CompletableFuture<Chunk> futureChunk = PaperLib.getChunkAtAsync(getSubCorner(x, z));
futureChunk.thenAccept(chunk-> chunk.removePluginChunkTicket(plugin));
chunkFutures.add(PaperLib.getChunkAtAsync(getSubCorner(x, z)));
}
}

return Collections.unmodifiableSet(chunkFutures);
} else {
CompletableFuture<Chunk> futureChunk = PaperLib.getChunkAtAsync(getCorner());
futureChunk.thenAccept(chunk-> chunk.removePluginChunkTicket(plugin));
return Collections.singleton(PaperLib.getChunkAtAsync(getCorner()));
}
}

0 comments on commit 4bd9a5d

Please sign in to comment.