From e0f1ed950bbdbe89f0f39a543c0375696c00f3f6 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 22 Jun 2020 21:49:34 +0100 Subject: [PATCH] Add delay between random location checks and command parameter to set it --- README.md | 3 ++- .../randomteleport/RandomTeleport.java | 7 +++++ .../searcher/RandomSearcher.java | 27 ++++++++++++++++++- .../src/main/resources/config.yml | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 003405e..ac88d02 100644 --- a/README.md +++ b/README.md @@ -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 ` | cooldown in seconds after which the player can use this teleporter again -`-id ` | The ID to use for the cooldown, uses automatically generated one if not provided +`-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 []` | only ignore blocks or regions `-t,-tries ` | 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 ` | the amount of ticks to wait between each chunk check ## Permissions diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java index 760c782..4327120 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -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")); } diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java index a3a64f3..9e183ad 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java @@ -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 options = new LinkedHashMap<>(); + private long lastCheck; private int checks = 0; private Multimap 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); } diff --git a/randomteleport-plugin/src/main/resources/config.yml b/randomteleport-plugin/src/main/resources/config.yml index a36535f..6c2f451 100644 --- a/randomteleport-plugin/src/main/resources/config.yml +++ b/randomteleport-plugin/src/main/resources/config.yml @@ -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: