diff --git a/README.md b/README.md index ac88d02..64fc476 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Option | Description `-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 +`-debug` | print some more debugging information in the log ## 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 25aaa92..7e4240b 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -25,12 +25,7 @@ import de.themoep.randomteleport.api.RandomTeleportAPI; import de.themoep.randomteleport.hook.HookManager; import de.themoep.randomteleport.listeners.SignListener; import de.themoep.randomteleport.searcher.RandomSearcher; -import de.themoep.randomteleport.searcher.options.AdditionalOptionParser; -import de.themoep.randomteleport.searcher.options.NotFoundException; -import de.themoep.randomteleport.searcher.options.OptionParser; -import de.themoep.randomteleport.searcher.options.PlayerNotFoundException; -import de.themoep.randomteleport.searcher.options.SimpleOptionParser; -import de.themoep.randomteleport.searcher.options.WorldNotFoundException; +import de.themoep.randomteleport.searcher.options.*; import de.themoep.randomteleport.searcher.validators.BiomeValidator; import de.themoep.randomteleport.searcher.validators.BlockValidator; import de.themoep.randomteleport.searcher.validators.HeightValidator; @@ -134,6 +129,7 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { } private void initOptionParsers() { + addOptionParser(new BooleanOptionParser("debug", (searcher) -> searcher.setDebug(true))); addOptionParser(new SimpleOptionParser(array("p", "player"), (searcher, args) -> { if (args.length > 0 && searcher.getInitiator().hasPermission("randomteleport.tpothers")) { List players = new ArrayList<>(); @@ -185,7 +181,7 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { if (world == null) { throw new WorldNotFoundException(args[0]); } - searcher.getCenter().setWorld(world); + searcher.setWorld(world); return true; } return false; @@ -401,7 +397,11 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { targetLoc.setX(targetLoc.getBlockX() + 0.5); targetLoc.setY(targetLoc.getY() + 0.1); targetLoc.setZ(targetLoc.getBlockZ() + 0.5); - PaperLib.teleportAsync(e, targetLoc).thenAccept(success -> { + if (searcher.isDebug()) { + getLogger().info("[DEBUG] Search " + searcher.getId() + " triggered by " + searcher.getInitiator().getName() + + " will try to teleport " + e.getType() + " " + e.getName() + "/" + e.getUniqueId() + " to " + targetLoc); + } + PaperLib.teleportAsync(e, targetLoc).whenComplete((success, ex) -> { if (success) { cooldowns.put(searcher.getId(), e.getUniqueId(), new AbstractMap.SimpleImmutableEntry<>(System.currentTimeMillis(), searcher.getCooldown())); sendMessage(e, "teleport", @@ -429,6 +429,9 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { "z", String.valueOf(targetLoc.getBlockZ()) ); } + if (ex != null && searcher.isDebug()) { + getLogger().log(Level.SEVERE, "Error while trying to teleport to location!", ex); + } }); }); return true; 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 b076a5f..96ee0ae 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 @@ -64,13 +64,16 @@ public class RandomSearcher { private Set targets = Collections.newSetFromMap(new LinkedHashMap<>()); + private boolean debug = false; private String id = null; private long seed = -1; private Location center; private int minRadius = 0; private int maxRadius = Integer.MAX_VALUE; private int checkDelay = 1; + private boolean minYWasProvided = false; private int minY; + private boolean maxYWasProvided = false; private int maxY; private boolean loadedOnly = false; private boolean generatedOnly = false; @@ -120,6 +123,22 @@ public class RandomSearcher { return uniqueId; } + /** + * Enable debugging messages for this searcher + * @param debug Whether to print debug messages + */ + public void setDebug(boolean debug) { + this.debug = debug; + } + + /** + * Get if debugging is enabled for this searcher + * @return Whether to print debug messages + */ + public boolean isDebug() { + return debug; + } + /** * Set the ID of this searcher used for cooldowns. Set to null to use an automatically generated one! * @param id The ID of the searcher @@ -244,6 +263,33 @@ public class RandomSearcher { this.checkDelay = checkDelay; } + /** + * Get the world this searcher will search in + * @return The world + */ + public World getWorld() { + return center.getWorld(); + } + + /** + * Set the world this searcher will search in + * @param world The world + */ + public void setWorld(World world) { + center.setWorld(world); + if (!minYWasProvided) { + minY = plugin.getMinHeight(world); + } + + if (!maxYWasProvided) { + if (world.getEnvironment() == World.Environment.NETHER) { + maxY = 126; + } else { + maxY = world.getMaxHeight(); + } + } + } + /** * Get the minimum Y * @return The minimum Y, always positive and less than the max Y! @@ -260,6 +306,7 @@ public class RandomSearcher { Validate.isTrue(minY >= plugin.getMinHeight(center.getWorld()), "Min Y has to be at least the world's minimum height!"); Validate.isTrue(minY < maxY, "Min Y has to be less than the max Y!"); this.minY = minY; + minYWasProvided = true; } /** @@ -277,6 +324,7 @@ public class RandomSearcher { public void setMaxY(int maxY) { Validate.isTrue(maxY <= center.getWorld().getMaxHeight() && maxY > minY, "Max Y has to be greater than the min Y and at most the world's max height!"); this.maxY = maxY; + maxYWasProvided = true; } /** @@ -348,6 +396,9 @@ public class RandomSearcher { future = new CompletableFuture<>(); checks = 0; checked.clear(); + if (debug) { + plugin.getLogger().info("[DEBUG] " + uniqueId + " " + this + " started searching..."); + } plugin.getServer().getScheduler().runTask(plugin, () -> checkRandom(future)); future.whenComplete((l, e) -> plugin.getRunningSearchers().remove(uniqueId)); return future; diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/BooleanOptionParser.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/BooleanOptionParser.java new file mode 100644 index 0000000..ff50743 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/BooleanOptionParser.java @@ -0,0 +1,38 @@ +package de.themoep.randomteleport.searcher.options; + +/* + * RandomTeleport - randomteleport-plugin - $project.description + * Copyright (c) 2022 Max Lee aka Phoenix616 (mail@moep.tv) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import de.themoep.randomteleport.searcher.RandomSearcher; + +import java.util.function.Consumer; + +public class BooleanOptionParser extends SimpleOptionParser { + + public BooleanOptionParser(String optionAlias, Consumer consumer) { + this(new String[]{optionAlias}, consumer); + } + + public BooleanOptionParser(String[] optionAliases, Consumer consumer) { + super(optionAliases, 0, ((searcher, args) -> { + consumer.accept(searcher); + return true; + })); + } + +}