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 db66318..06f06a6 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -20,6 +20,7 @@ package de.themoep.randomteleport; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; +import de.themoep.randomteleport.api.RandomTeleportAPI; import de.themoep.randomteleport.hook.HookManager; import de.themoep.randomteleport.listeners.SignListener; import de.themoep.randomteleport.searcher.RandomSearcher; @@ -32,6 +33,7 @@ import de.themoep.randomteleport.searcher.options.WorldNotFoundException; import de.themoep.randomteleport.searcher.validators.BiomeValidator; import de.themoep.randomteleport.searcher.validators.BlockValidator; import de.themoep.randomteleport.searcher.validators.HeightValidator; +import de.themoep.randomteleport.searcher.validators.LocationValidator; import de.themoep.randomteleport.searcher.validators.ProtectionValidator; import de.themoep.randomteleport.searcher.validators.WorldborderValidator; import de.themoep.utils.lang.bukkit.LanguageManager; @@ -55,10 +57,11 @@ import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.stream.Collectors; -public class RandomTeleport extends JavaPlugin { +public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { public static final Random RANDOM = new Random(); private HookManager hookManager; @@ -364,4 +367,19 @@ public class RandomTeleport extends JavaPlugin { } return parseAndRun(sender, center, cmd.split(" ")); } + + @Override + public CompletableFuture getRandomLocation(Player player, Location origin, int minRange, int maxRange, LocationValidator... validators) { + return getRandomSearcher(player, origin, minRange, maxRange, validators).search(); + } + + @Override + public CompletableFuture teleportToRandomLocation(Player player, Location origin, int minRange, int maxRange, LocationValidator... validators) { + return getRandomLocation(player, origin, minRange, maxRange, validators).thenApply(player::teleport); + } + + @Override + public RandomSearcher getRandomSearcher(Player player, Location origin, int minRange, int maxRange, LocationValidator... validators) { + return new RandomSearcher(this, player, origin, minRange, maxRange, validators); + } } diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/api/RandomTeleportAPI.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/api/RandomTeleportAPI.java new file mode 100644 index 0000000..93a5728 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/api/RandomTeleportAPI.java @@ -0,0 +1,67 @@ +package de.themoep.randomteleport.api; + +/* + * RandomTeleport - randomteleport-plugin - $project.description + * Copyright (c) 2019 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 de.themoep.randomteleport.searcher.validators.LocationValidator; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public interface RandomTeleportAPI { + + /** + * Returns a random Location + * + * @param player the Player initiating the search + * @param center the location where the search should begin + * @param minRange the minimum distance a found location has to the center location + * @param maxRange the maximum distance a found location has to the center location + * @param validators additional LocationValidators to customize validity check of a location + * @return a random CompletableFuture + */ + CompletableFuture getRandomLocation(Player player, Location center, int minRange, int maxRange, LocationValidator... validators); + + /** + * Teleports the passed Player to a random Location + * + * @param player the Player initiating the search + * @param center the location where the search should begin + * @param minRange the minimum distance a found location has to the center location + * @param maxRange the maximum distance a found location has to the center location + * @param validators additional LocationValidators to customize validity check of a location + * @return a CompletableFuture true if teleport was successful else false + */ + CompletableFuture teleportToRandomLocation(Player player, Location center, int minRange, int maxRange, LocationValidator... validators) throws ExecutionException, InterruptedException; + + /** + * Creates a RandomSearcher instance with the passed parameters + * + * @param player the Player initiating the search + * @param center the location where the search should begin + * @param minRange the minimum distance a found location has to the center location + * @param maxRange the maximum distance a found location has to the center location + * @param validators additional LocationValidators to customize validity check of a location + * @return a randomSearcher instance + * @throws ExecutionException see {@link CompletableFuture#get()} + * @throws InterruptedException see {@link CompletableFuture#get()} + */ + RandomSearcher getRandomSearcher(Player player, Location center, int minRange, int maxRange, LocationValidator... validators) throws ExecutionException, InterruptedException; +} 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 1f7ab75..53c7cc1 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 @@ -29,6 +29,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -72,13 +73,14 @@ public class RandomSearcher { private CompletableFuture future = null; - public RandomSearcher(RandomTeleport plugin, CommandSender initiator, Location center, int minRadius, int maxRadius) { + public RandomSearcher(RandomTeleport plugin, CommandSender initiator, Location center, int minRadius, int maxRadius, LocationValidator... validators) { this.plugin = plugin; this.initiator = initiator; setCenter(center); setMinRadius(minRadius); setMaxRadius(maxRadius); - validators.getRaw().putAll(plugin.getLocationValidators().getRaw()); + this.validators.getRaw().putAll(plugin.getLocationValidators().getRaw()); + Arrays.asList(validators).forEach(this.validators::add); } /**