Re-add ability to only search in loaded chunks

This commit is contained in:
Phoenix616 2019-07-27 15:38:55 +01:00
parent 6ee71ec50e
commit aa9d995e4d
3 changed files with 36 additions and 5 deletions

View File

@ -195,9 +195,11 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("g", "generated", "l", "loaded"), (searcher, args) -> {
// loaded is removed as we load chunks async which should no longer lead to performance issues
// now it just works like the new "generated" option where it only checks generated chunks
addOptionParser(new SimpleOptionParser(array("l", "loaded"), (searcher, args) -> {
searcher.searchInLoadedOnly(true);
return true;
}));
addOptionParser(new SimpleOptionParser(array("g", "generated"), (searcher, args) -> {
searcher.searchInGeneratedOnly(true);
return true;
}));

View File

@ -26,6 +26,7 @@ import de.themoep.randomteleport.searcher.options.NotFoundException;
import de.themoep.randomteleport.searcher.validators.LocationValidator;
import io.papermc.lib.PaperLib;
import org.apache.commons.lang.Validate;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
@ -67,6 +68,7 @@ public class RandomSearcher {
private Location center;
private int minRadius = 0;
private int maxRadius = Integer.MAX_VALUE;
private boolean loadedOnly = false;
private boolean generatedOnly = false;
private int maxTries = 100;
private int cooldown;
@ -214,6 +216,16 @@ public class RandomSearcher {
this.maxRadius = maxRadius;
}
/**
* By default it will search for coordinates in any chunk, even unloaded ones prompting the server to load new
* chunks which might result in some performance impact if the server doesn't support async loading. This disables
* that and only searches in already loaded chunks. (But might fail more often)
* @param loadedOnly Whether or not to search in loaded chunks only
*/
public void searchInLoadedOnly(boolean loadedOnly) {
this.loadedOnly = loadedOnly;
}
/**
* By default it will search for coordinates in any chunk, even ungenerated ones prompting the world to get
* generated at the point which might result in some performance impact. This disables that and only searches
@ -292,9 +304,24 @@ public class RandomSearcher {
int maxChunk = maxRadius >> 4;
int randChunkX;
int randChunkZ;
Chunk[] loadedChunks = new Chunk[0];
if (loadedOnly) {
loadedChunks = randomLoc.getWorld().getLoadedChunks();
if (loadedChunks.length == 0) {
future.completeExceptionally(new NotFoundException("loaded chunk"));
return;
}
}
do {
randChunkX = (random.nextBoolean() ? 1 : -1) * random.nextInt(maxChunk + 1);
randChunkZ = (random.nextBoolean() ? 1 : -1) * random.nextInt(maxChunk + 1);
if (loadedOnly) {
Chunk chunk = loadedChunks[random.nextInt(loadedChunks.length)];
randChunkX = chunk.getX();
randChunkZ = chunk.getZ();
} else {
randChunkX = (random.nextBoolean() ? 1 : -1) * random.nextInt(maxChunk + 1);
randChunkZ = (random.nextBoolean() ? 1 : -1) * random.nextInt(maxChunk + 1);
}
checks++;
if (checks >= maxTries) {
future.completeExceptionally(new NotFoundException("location"));
@ -378,6 +405,7 @@ public class RandomSearcher {
", center=" + center +
", minRadius=" + minRadius +
", maxRadius=" + maxRadius +
", loadedOnly=" + loadedOnly +
", generatedOnly=" + generatedOnly +
", maxTries=" + maxTries +
", cooldown=" + cooldown +

View File

@ -22,6 +22,7 @@ commands:
> -b,-biome <biomename> [<biome 2> ...] - only teleport to this biome (multiple allowed, Bukkit biome names!)
> -x,-xPos <x value> - x axis of the center point, if not set the player's x axis is used
> -z,-zPos <z value> - z axis of the center point, if not set the player's z axis is used
> -l,-loaded - only search loaded chunks for possible locations (might fail more often)
> -g,-generated - only search generated chunks for possible locations
> -c, -cooldown <seconds> - cooldown in seconds after which the player can use this teleporter again
> -id <id> - The ID to use for the cooldown, uses automatically generated one if not provided