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 2761221..fe3ec20 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -149,6 +149,20 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { } return false; })); + addOptionParser(new SimpleOptionParser(array("miny"), (searcher, args) -> { + if (args.length > 0) { + searcher.setMinY(Integer.parseInt(args[0])); + return true; + } + return false; + })); + addOptionParser(new SimpleOptionParser(array("maxy"), (searcher, args) -> { + if (args.length > 0) { + searcher.setMaxY(Integer.parseInt(args[0])); + return true; + } + return false; + })); addOptionParser(new SimpleOptionParser(array("w", "world"), (searcher, args) -> { if (args.length > 0) { World world = getServer().getWorld(args[0]); 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 6cc9866..fe9aa0b 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 @@ -28,6 +28,7 @@ import io.papermc.lib.PaperLib; import org.apache.commons.lang.Validate; import org.bukkit.Chunk; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; @@ -68,6 +69,8 @@ public class RandomSearcher { private Location center; private int minRadius = 0; private int maxRadius = Integer.MAX_VALUE; + private int minY = 0; + private int maxY; private boolean loadedOnly = false; private boolean generatedOnly = false; private int maxTries = 100; @@ -85,6 +88,11 @@ public class RandomSearcher { setCenter(center); setMinRadius(minRadius); setMaxRadius(maxRadius); + if (center.getWorld().getEnvironment() == World.Environment.NETHER) { + maxY = 127; + } else { + maxY = center.getWorld().getMaxHeight(); + } this.validators.getRaw().putAll(plugin.getLocationValidators().getRaw()); Arrays.asList(validators).forEach(this.validators::add); } @@ -216,6 +224,40 @@ public class RandomSearcher { this.maxRadius = maxRadius; } + /** + * Get the minimum Y + * @return The minimum Y, always positive and less than the max Y! + */ + public int getMinY() { + return minY; + } + + /** + * Set the minimum search Y + * @param minY The min Y; has to be positive and less than the max Y! + */ + public void setMinY(int minY) { + Validate.isTrue(minY >= 0 && minY < maxY, "Min Y has to be positive and less than the max Y!"); + this.minY = minY; + } + + /** + * Get the maximum Y + * @return The maximum Y, always greater than the minimum Y + */ + public int getMaxY() { + return maxY; + } + + /** + * Set the maximum search Y + * @param maxY The max Y; has to be greater than the min Y! + */ + public void setMaxY(int maxY) { + Validate.isTrue(maxY > minY, "Max Y has to be greater than the min Y!"); + this.maxY = maxY; + } + /** * 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 diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/HeightValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/HeightValidator.java index e3f2f07..bb29892 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/HeightValidator.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/HeightValidator.java @@ -32,9 +32,16 @@ public class HeightValidator extends LocationValidator { @Override public boolean validate(RandomSearcher searcher, Location location) { Block block = location.getWorld().getHighestBlockAt(location); + if (block.getY() > searcher.getMaxY()) { + block = location.getWorld().getBlockAt( + block.getX(), + searcher.getMinY() + searcher.getRandom().nextInt(searcher.getMaxY() - searcher.getMinY()), + block.getZ() + ); + } while (block.isEmpty()) { block = block.getRelative(BlockFace.DOWN); - if (block == null || block.getY() == 0) { + if (block == null || block.getY() < searcher.getMinY()) { return false; } } diff --git a/randomteleport-plugin/src/main/resources/plugin.yml b/randomteleport-plugin/src/main/resources/plugin.yml index b74da44..6bd6abf 100644 --- a/randomteleport-plugin/src/main/resources/plugin.yml +++ b/randomteleport-plugin/src/main/resources/plugin.yml @@ -22,6 +22,8 @@ commands: > -b,-biome [ ...] - only teleport to this biome (multiple allowed, Bukkit biome names!) > -x,-xPos - x axis of the center point, if not set the player's x axis is used > -z,-zPos - z axis of the center point, if not set the player's z axis is used + > -minY - minimum y value that the random location should have (default: 0) + > -maxY - maximum y value that the random location should have (default: world height, half in nether) > -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