Add delay between random location checks and command parameter to set it

This commit is contained in:
Phoenix616 2020-06-22 21:49:34 +01:00
parent 4047c6529d
commit e0f1ed950b
4 changed files with 36 additions and 2 deletions

View File

@ -33,11 +33,12 @@ Option | Description
`-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
`-id <id>` | the ID to use for the cooldown, uses automatically generated one if not provided
`-f,-force` | teleport even if there is no dirt/grass/sand/gravel, only checks for lava/water/cactus, ignores WorldGuard/Faction regions
`-f,-force [<blocks/regions>]` | only ignore blocks or regions
`-t,-tries <amount>` | the amount of times the plugin should try to find a random location before giving up
`-sp,spawnpoint [force]` | set the respawn point of the player to the location he teleported to (force overrides existing spawnpoint)
`-checkdelay <ticks>` | the amount of ticks to wait between each chunk check
## Permissions

View File

@ -232,6 +232,13 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
}
return false;
}));
addOptionParser(new SimpleOptionParser("checkdelay", (searcher, args) -> {
if (args.length > 0) {
searcher.setCheckDelay(Integer.parseInt(args[0]));
return true;
}
return false;
}));
addOptionParser(new AdditionalOptionParser("spawnpoint", "sp"));
}

View File

@ -69,6 +69,7 @@ public class RandomSearcher {
private Location center;
private int minRadius = 0;
private int maxRadius = Integer.MAX_VALUE;
private int checkDelay = 1;
private int minY = 0;
private int maxY;
private boolean loadedOnly = false;
@ -77,6 +78,7 @@ public class RandomSearcher {
private int cooldown;
private Map<String, String> options = new LinkedHashMap<>();
private long lastCheck;
private int checks = 0;
private Multimap<Integer, Integer> checked = MultimapBuilder.hashKeys().hashSetValues().build();
@ -187,6 +189,7 @@ public class RandomSearcher {
*/
public void setCenter(Location center) {
Validate.notNull(center, "Center cannot be null!");
Validate.notNull(center.getWorld(), "Center world cannot be null!");
this.center = center;
}
@ -224,6 +227,22 @@ public class RandomSearcher {
this.maxRadius = maxRadius;
}
/**
* Get the delay in ticks between checking chunks when searching
* @return The delay in ticks
*/
public int getCheckDelay() {
return checkDelay;
}
/**
* Set the delay in ticks between checking chunks when searching
* @param checkDelay The delay in ticks
*/
public void setCheckDelay(int checkDelay) {
this.checkDelay = checkDelay;
}
/**
* Get the minimum Y
* @return The minimum Y, always positive and less than the max Y!
@ -340,6 +359,7 @@ public class RandomSearcher {
if (future.isCancelled() || future.isDone() || future.isCompletedExceptionally()) {
return;
}
lastCheck = center.getWorld().getTime();
Location randomLoc = center.clone();
randomLoc.setY(0);
int minChunk = minRadius >> 4;
@ -408,7 +428,12 @@ public class RandomSearcher {
future.complete(foundLoc.add(0, 1, 0));
return true;
}
checkRandom(future);
long diff = center.getWorld().getTime() - lastCheck;
if (diff < checkDelay) {
plugin.getServer().getScheduler().runTaskLater(plugin, () -> checkRandom(future), checkDelay - diff);
} else {
checkRandom(future);
}
return false;
}).exceptionally(future::completeExceptionally);
}

View File

@ -2,6 +2,7 @@
lang: en
# Some debug information
debug: true
# Delay in ticks between checking chunks when searching
# Blocks to teleport on in normal mode
save-blocks: