Re-add debug option and fix min/max y when using world specifier

This commit is contained in:
Phoenix616 2022-11-23 00:20:20 +01:00
parent ebeb3451af
commit 8b282d5e76
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
4 changed files with 101 additions and 8 deletions

View File

@ -39,6 +39,7 @@ Option | Description
`-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
`-debug` | print some more debugging information in the log
## Permissions

View File

@ -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<Player> 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;

View File

@ -64,13 +64,16 @@ public class RandomSearcher {
private Set<Entity> 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;

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
import de.themoep.randomteleport.searcher.RandomSearcher;
import java.util.function.Consumer;
public class BooleanOptionParser extends SimpleOptionParser {
public BooleanOptionParser(String optionAlias, Consumer<RandomSearcher> consumer) {
this(new String[]{optionAlias}, consumer);
}
public BooleanOptionParser(String[] optionAliases, Consumer<RandomSearcher> consumer) {
super(optionAliases, 0, ((searcher, args) -> {
consumer.accept(searcher);
return true;
}));
}
}