Add an API (#27)

This commit is contained in:
Yannick Lamprecht 2019-04-27 01:57:32 +02:00 committed by Max Lee
parent 9a1004723c
commit 483a0bb2b3
3 changed files with 90 additions and 3 deletions

View File

@ -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<Location> getRandomLocation(Player player, Location origin, int minRange, int maxRange, LocationValidator... validators) {
return getRandomSearcher(player, origin, minRange, maxRange, validators).search();
}
@Override
public CompletableFuture<Boolean> 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);
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<Location>
*/
CompletableFuture<Location> 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<Boolean> true if teleport was successful else false
*/
CompletableFuture<Boolean> 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;
}

View File

@ -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<Location> 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);
}
/**