Implement validators and command parsing. Also add language support.

Theoretically this should already work...
This commit is contained in:
Phoenix616 2019-02-20 00:44:10 +01:00
parent 17f303fb01
commit 59bcf6e79a
17 changed files with 1062 additions and 1 deletions

View File

@ -42,6 +42,10 @@
<id>paper-repo</id>
<url>https://papermc.io/repo/content/groups/public</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
</repositories>
<dependencies>
@ -57,6 +61,11 @@
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.themoep.utils</groupId>
<artifactId>lang-bukkit</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
</dependencies>
<profiles>

View File

@ -19,20 +19,208 @@ package de.themoep.randomteleport;
*/
import de.themoep.randomteleport.hook.HookManager;
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.validators.BiomeValidator;
import de.themoep.randomteleport.searcher.validators.BlockValidator;
import de.themoep.randomteleport.searcher.validators.ProtectionValidator;
import de.themoep.randomteleport.searcher.validators.WorldborderValidator;
import de.themoep.utils.lang.bukkit.LanguageManager;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Level;
public class RandomTeleport extends JavaPlugin {
public static final Random RANDOM = new Random();
private HookManager hookManager;
private LanguageManager lang;
private ValidatorRegistry locationValidators = new ValidatorRegistry();
private List<OptionParser> optionParsers = new ArrayList<>();
private Material[] saveBlocks;
private Material[] unsaveBlocks;
public void onEnable() {
hookManager = new HookManager(this);
loadConfig();
initOptionParsers();
initValidators();
getCommand("randomteleport").setExecutor(new RandomTeleportCommand(this));
}
private void loadConfig() {
public void loadConfig() {
saveDefaultConfig();
reloadConfig();
saveBlocks = getConfig().getStringList("save-blocks").stream()
.map(s -> {
Material mat = Material.matchMaterial(s);
if (mat == null) {
getLogger().log(Level.WARNING, "Error in save-blocks config! No material found with name " + s);
}
return mat;
})
.filter(Objects::nonNull)
.toArray(Material[]::new);
unsaveBlocks = getConfig().getStringList("unsave-blocks").stream()
.map(s -> {
Material mat = Material.matchMaterial(s);
if (mat == null) {
getLogger().log(Level.WARNING, "Error in unsave-blocks config! No material found with name " + s);
}
return mat;
})
.filter(Objects::nonNull)
.toArray(Material[]::new);
lang = new LanguageManager(this, getConfig().getString("lang"));
lang.setPlaceholderPrefix("{");
lang.setPlaceholderSuffix("}");
}
private void initOptionParsers() {
addOptionParser(new SimpleOptionParser(array("p", "player"), (searcher, args) -> {
if (args.length > 0) {
List<Player> players = new ArrayList<>();
for (String arg : args) {
Player player = getServer().getPlayer(arg);
if (player == null) {
throw new PlayerNotFoundException(arg);
}
players.add(player);
}
searcher.getTargets().addAll(players);
return true;
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("x", "xpos"), (searcher, args) -> {
if (args.length > 0) {
searcher.getCenter().setX(Integer.parseInt(args[0]));
return true;
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("y", "ypos"), (searcher, args) -> {
if (args.length > 0) {
searcher.getCenter().setX(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]);
if (world == null) {
throw new WorldNotFoundException(args[0]);
}
searcher.getCenter().setWorld(world);
return true;
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("c", "cooldown"), (searcher, args) -> {
if (args.length > 0) {
searcher.setCooldown(Integer.parseInt(args[0]));
return true;
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("f", "force"), (searcher, args) -> {
if (args.length > 0) {
if ("regions".equalsIgnoreCase(args[1])) {
searcher.getValidators().remove("protection");
} else if ("blocks".equalsIgnoreCase(args[1])) {
searcher.getValidators().add(new BlockValidator(false, unsaveBlocks));
} else {
throw new NotFoundException(args[1]);
}
} else {
searcher.getValidators().remove("protection");
searcher.getValidators().add(new BlockValidator(false, unsaveBlocks));
}
return true;
}));
addOptionParser(new SimpleOptionParser(array("b", "biome"), (searcher, args) -> {
if (args.length > 0) {
List<Biome> biomes = new ArrayList<>();
for (String arg : args) {
biomes.add(Biome.valueOf(arg.toUpperCase()));
}
if (!biomes.isEmpty()) {
searcher.getValidators().add(new BiomeValidator(biomes.toArray(new Biome[0])));
return true;
}
return false;
}
return false;
}));
addOptionParser(new SimpleOptionParser(array("g", "generated", "l", "loaded"), (searcher, args) -> {
// loaded is removed as we load chunks async which should no longer lead to performance issues
// now it just works like the new "generated" option where it only checks generated chunks
searcher.searchInGeneratedOnly(true);
return true;
}));
}
private void initValidators() {
locationValidators.add(new WorldborderValidator());
locationValidators.add(new ProtectionValidator());
locationValidators.add(new BlockValidator(saveBlocks));
}
/**
* Utility method to create arrays with a nicer syntax. Seriously, why does Java not just accept {"string"}?!?
* @param array The array values
* @return The same array
*/
private static <T> T[] array(T... array) {
return array;
}
public boolean sendMessage(CommandSender sender, String key, String... replacements) {
String message = lang.getConfig(sender).get(key, replacements);
if (message != null && !message.isEmpty()) {
sender.sendMessage(message);
return true;
}
return false;
}
public HookManager getHookManager() {
return hookManager;
}
public ValidatorRegistry getLocationValidators() {
return locationValidators;
}
/**
*
* @return
*/
public List<OptionParser> getOptionParsers() {
return optionParsers;
}
/**
* Add an option parser to this plugin
* @param parser The parser to add
*/
public void addOptionParser(OptionParser parser) {
optionParsers.add(parser);
}
}

View File

@ -18,9 +18,17 @@ package de.themoep.randomteleport;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import de.themoep.randomteleport.searcher.RandomSearcher;
import de.themoep.randomteleport.searcher.options.OptionParser;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import java.util.Arrays;
public class RandomTeleportCommand implements CommandExecutor {
private final RandomTeleport plugin;
@ -31,6 +39,52 @@ public class RandomTeleportCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1) {
if ("reload".equalsIgnoreCase(args[0])) {
plugin.loadConfig();
plugin.sendMessage(sender, "reloaded");
return true;
}
} else if (args.length > 1) {
try {
RandomSearcher searcher = new RandomSearcher(plugin, sender, getLocation(sender), Integer.parseInt(args[0]), Integer.parseInt(args[1]));
String[] optionArgs = Arrays.copyOfRange(args, 2, args.length);
for (OptionParser parser : plugin.getOptionParsers()) {
parser.parse(searcher, optionArgs);
}
searcher.getTargets().forEach(p -> plugin.sendMessage(p, "search", "world", searcher.getCenter().getWorld().getName()));
searcher.search().thenApply(location -> {
searcher.getTargets().forEach(p -> {
p.teleport(location);
plugin.sendMessage(p, "teleport",
"world", location.getWorld().getName(),
"x", String.valueOf(location.getBlockX()),
"y", String.valueOf(location.getBlockY()),
"z", String.valueOf(location.getBlockZ())
);
});
return true;
}).exceptionally(ex -> {
plugin.sendMessage(sender, "error.location");
sender.sendMessage(ex.getMessage());
return true;
});
return true;
} catch (IllegalArgumentException e) {
sender.sendMessage(e.getMessage());
}
}
return false;
}
private static Location getLocation(CommandSender sender) {
if (sender instanceof Entity) {
return ((Entity) sender).getLocation();
} else if (sender instanceof BlockCommandSender) {
return ((BlockCommandSender) sender).getBlock().getLocation();
}
return new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
}
}

View File

@ -0,0 +1,78 @@
package de.themoep.randomteleport;
/*
* RandomTeleport
* 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.validators.LocationValidator;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class ValidatorRegistry {
private Map<String, LocationValidator> validators = new LinkedHashMap<>();
/**
* Get the map of currently set location validators
* @return The map of validators
*/
public Map<String, LocationValidator> getRaw() {
return validators;
}
public Collection<LocationValidator> getAll() {
return validators.values();
}
/**
* Add a location validator that is provided in this plugin
* @param validator The validator to add
* @return The previously registered validator of the same type and name or null if none was registered
*/
public LocationValidator add(LocationValidator validator) {
return validators.put(validator.getType().toLowerCase(), validator);
}
/**
* Remove a location validator that is provided in this plugin
* @param validator The validator to remove
* @return The removed registered validator with the same type or null if it wasn't registered
*/
public LocationValidator remove(LocationValidator validator) {
return remove(validator.getType());
}
/**
* Remove a location validator that is provided in this plugin
* @param type The type of the validator to remove
* @return The removed registered validator with the same type or null if it wasn't registered
*/
public LocationValidator remove(String type) {
return validators.remove(type.toLowerCase());
}
/**
* Get a location validator that is provided in this plugin
* @param type The type of the validator to get
* @return The registered validator with the provided type or null if none was registered
*/
public LocationValidator get(String type) {
return validators.get(type.toLowerCase());
}
}

View File

@ -0,0 +1,247 @@
package de.themoep.randomteleport.searcher;
/*
* 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.RandomTeleport;
import de.themoep.randomteleport.ValidatorRegistry;
import de.themoep.randomteleport.searcher.options.NotFoundException;
import de.themoep.randomteleport.searcher.validators.LocationValidator;
import io.papermc.lib.PaperLib;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
public class RandomSearcher {
private final RandomTeleport plugin;
private final CommandSender initiator;
private ValidatorRegistry validators = new ValidatorRegistry();
private Random random = RandomTeleport.RANDOM;
private List<Player> targets = new ArrayList<>();
private long seed = -1;
private Location center;
private int minRadius = 0;
private int maxRadius = Integer.MAX_VALUE;
private boolean generatedOnly = false;
private int maxChecks = 100;
private int cooldown;
private int checks = 0;
public RandomSearcher(RandomTeleport plugin, CommandSender initiator, Location center, int minRadius, int maxRadius) {
this.plugin = plugin;
this.initiator = initiator;
setCenter(center);
setMinRadius(minRadius);
setMaxRadius(maxRadius);
validators.getRaw().putAll(plugin.getLocationValidators().getRaw());
}
/**
* Get all players targeted by this searcher
* @return The players to target
*/
public List<Player> getTargets() {
return targets;
}
public ValidatorRegistry getValidators() {
return validators;
}
/**
* Set the seed that should be used when selecting locations. See {@link Random#setSeed(long)}.
* @param seed The seed.
*/
public void setSeed(long seed) {
this.seed = seed;
if (random == RandomTeleport.RANDOM ) {
random = new Random(seed);
} else {
random.setSeed(seed);
}
}
/**
* Get the seed of this random searcher. Returns -1 if none was set.
* @return The seed or -1
*/
public long getSeed() {
return seed;
}
/**
* Directly set the Random instance used for selecting coordinates
* @param random The random instance
*/
public void setRandom(Random random) {
this.random = random;
}
/**
* Get the random instance that is used for finding locations
* @return The random instance; {@link RandomTeleport#RANDOM} by default
*/
public Random getRandom() {
return random;
}
/**
* Get the center for this searcher
* @return The center location
*/
public Location getCenter() {
return center;
}
/**
* Set the center of this searcher
* @param center The center location; never null
*/
public void setCenter(Location center) {
Validate.notNull(center, "Center cannot be null!");
this.center = center;
}
/**
* Get the minimum radius
* @return The minimum radius, always positive and less than the max radius!
*/
public int getMinRadius() {
return minRadius;
}
/**
* Set the minimum search radius
* @param minRadius The min radius; has to be positive and less than the max radius!
*/
public void setMinRadius(int minRadius) {
Validate.isTrue(minRadius >= 0 && minRadius < maxRadius, "Min radius has to be positive and less than the max radius!");
this.minRadius = minRadius;
}
/**
* Get the maximum radius
* @return The maximum radius, always greater than the minimum radius
*/
public int getMaxRadius() {
return maxRadius;
}
/**
* Set the maximum search radius
* @param maxRadius The max radius; has to be greater than the min radius!
*/
public void setMaxRadius(int maxRadius) {
Validate.isTrue(maxRadius > minRadius, "Max radius has to be greater than the min radius!");
this.maxRadius = maxRadius;
}
/**
* By default it will search for coordinates in any chunk, even ungenerated ones prompting the world to get
* generated at the point which might result in some performance impact. This disables that and only searches
* in already generated chunks.
* @param generatedOnly Whether or not to search in generated chunks only
*/
public void searchInGeneratedOnly(boolean generatedOnly) {
this.generatedOnly = generatedOnly;
}
public int getMaxChecks() {
return maxChecks;
}
public void setMaxChecks(int maxChecks) {
this.maxChecks = maxChecks;
}
/**
* Set the cooldown that a player has to wait before using a searcher with similar settings again
* @param cooldown The cooldown in seconds
*/
public void setCooldown(int cooldown) {
Validate.isTrue(cooldown >= 0, "Cooldown can't be negative!");
this.cooldown = cooldown;
}
/**
* Get the cooldown that a player has to wait before using a searcher with similar settings again
* @return The cooldown in seconds
*/
public int getCooldown() {
return cooldown;
}
/**
* Search for a valid location
* @return A CompletableFuture for when the search task is complete
*/
public CompletableFuture<Location> search() {
CompletableFuture<Location> future = new CompletableFuture<>();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> checkRandom(future));
return future;
}
private void checkRandom(CompletableFuture<Location> future) {
if (checks >= maxChecks) {
future.completeExceptionally(new NotFoundException("location"));
return;
}
if (future.isCancelled() || future.isDone() || future.isCompletedExceptionally()) {
return;
}
Location randomLoc = new Location(
center.getWorld(),
center.getBlockX() + random.nextInt(maxRadius - minRadius),
0,
center.getBlockX() + random.nextInt(maxRadius - minRadius)
);
PaperLib.getChunkAtAsync(randomLoc, generatedOnly).thenApply(c -> {
checks++;
for (LocationValidator validator : getValidators().getAll()) {
if (!validator.validate(this, randomLoc)) {
checkRandom(future);
return false;
}
}
future.complete(randomLoc);
return true;
});
}
public RandomTeleport getPlugin() {
return plugin;
}
/**
* The sender who initiated this search
* @return The initiator
*/
public CommandSender getInitiator() {
return initiator;
}
}

View File

@ -0,0 +1,32 @@
package de.themoep.randomteleport.searcher.options;
/*
* 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/>.
*/
public class NotFoundException extends IllegalArgumentException {
private final String what;
public NotFoundException(String what) {
super(what + " was not found!");
this.what = what;
}
public String getWhat() {
return what;
}
}

View File

@ -0,0 +1,27 @@
package de.themoep.randomteleport.searcher.options;
/*
* RandomTeleport
* 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;
public interface OptionParser {
boolean parse(RandomSearcher searcher, String[] args);
}

View File

@ -0,0 +1,28 @@
package de.themoep.randomteleport.searcher.options;
/*
* RandomTeleport
* 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/>.
*/
public class PlayerNotFoundException extends NotFoundException {
public PlayerNotFoundException(String name) {
super(name);
}
}

View File

@ -0,0 +1,67 @@
package de.themoep.randomteleport.searcher.options;
/*
* 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class SimpleOptionParser implements OptionParser {
private Set<String> aliases;
private final BiFunction<RandomSearcher, String[], Boolean> parser;
public SimpleOptionParser(String option, BiFunction<RandomSearcher, String[], Boolean> parser) {
this(new String[]{option}, parser);
}
public SimpleOptionParser(String[] optionAliases, BiFunction<RandomSearcher, String[], Boolean> parser) {
this.aliases = Arrays.stream(optionAliases).map(String::toLowerCase).collect(Collectors.toSet());
this.parser = parser;
}
@Override
public boolean parse(RandomSearcher searcher, String[] args) {
boolean ret = false;
String option = null;
List<String> values = new ArrayList<>();
for (String arg : args) {
if (arg.startsWith("-")) {
if (option != null) {
if (aliases.contains(option.toLowerCase())) {
ret |= parser.apply(searcher, values.toArray(new String[0]));
}
values.clear();
}
option = arg.substring(1);
if (option.startsWith("-") && option.length() > 1) {
option = option.substring(1);
}
} else {
values.add(arg);
}
}
return ret;
}
}

View File

@ -0,0 +1,28 @@
package de.themoep.randomteleport.searcher.options;
/*
* RandomTeleport
* 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/>.
*/
public class WorldNotFoundException extends NotFoundException {
public WorldNotFoundException(String name) {
super(name);
}
}

View File

@ -0,0 +1,50 @@
package de.themoep.randomteleport.searcher.validators;
/*
* 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 org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
public class BiomeValidator extends LocationValidator {
private final boolean whitelist;
private final Set<Biome> biomes = EnumSet.noneOf(Biome.class);
public BiomeValidator(Biome... biomes) {
this(true, biomes);
}
public BiomeValidator(boolean whitelist, Biome... biomes) {
super("biome");
this.whitelist = whitelist;
Collections.addAll(this.biomes, biomes);
}
@Override
public boolean validate(RandomSearcher searcher, Location location) {
Block block = location.getBlock();
return block != null && biomes.contains(block.getBiome()) == whitelist;
}
}

View File

@ -0,0 +1,50 @@
package de.themoep.randomteleport.searcher.validators;
/*
* 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 org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
public class BlockValidator extends LocationValidator {
private final boolean whitelist;
private final Set<Material> materials = EnumSet.noneOf(Material.class);
public BlockValidator(Material... materials) {
this(true, materials);
}
public BlockValidator(boolean whitelist, Material... materials) {
super("block");
this.whitelist = whitelist;
Collections.addAll(this.materials, materials);
}
@Override
public boolean validate(RandomSearcher searcher, Location location) {
Block block = location.getBlock();
return block != null && materials.contains(block.getType()) == whitelist;
}
}

View File

@ -0,0 +1,43 @@
package de.themoep.randomteleport.searcher.validators;
/*
* 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 org.bukkit.Location;
public abstract class LocationValidator {
private String type;
public LocationValidator(String type) {
this.type = type;
}
public String getType() {
return type;
}
/**
* Validate a location
* @param searcher The searcher attempting to use this validator
* @param location The location to validate
* @return True if it's valid; false if not
*/
public abstract boolean validate(RandomSearcher searcher, Location location);
}

View File

@ -0,0 +1,43 @@
package de.themoep.randomteleport.searcher.validators;
/*
* RandomTeleport
* 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 org.bukkit.Location;
import org.bukkit.entity.Player;
public class ProtectionValidator extends LocationValidator {
public ProtectionValidator() {
super("protection");
}
@Override
public boolean validate(RandomSearcher searcher, Location location) {
if (searcher.getTargets().isEmpty()) {
return true;
}
for (Player player : searcher.getTargets()) {
if (!searcher.getPlugin().getHookManager().canBuild(player, location)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,34 @@
package de.themoep.randomteleport.searcher.validators;
/*
* 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 org.bukkit.Location;
public class WorldborderValidator extends LocationValidator {
public WorldborderValidator() {
super("worldborder");
}
@Override
public boolean validate(RandomSearcher searcher, Location location) {
return searcher.getPlugin().getHookManager().isInsideBorder(location);
}
}

View File

@ -0,0 +1,72 @@
# Default language to use when client's language isn't available
lang: en
# Some debug information
debug: true
# Blocks to teleport on in normal mode
save-blocks:
- sand
- gravel
- dirt
- stone
- cobblestone
- ender_stone
- netherrack
# Blocks unsave when in "--force blocks" mode
unsave-blocks:
- air
- water
- stationary_water
- lava
- stationary_lava
- sapling
- bed_block
- powered_rail
- detector_rail
- web
- piston_extension
- tnt
- torch
- fire
- sign_post
- wooden_door
- ladder
- rails
- wall_sign
- lever
- stone_plate
- iron_door_block
- redstone_torch_off
- redstone_torch_on
- stone_button
- cactus
- magma
- portal
- vine
- ender_portal
- tripwire_hook
- tripwire
- wood_button
- flower_pot
- leaves
- leaves_2
- barrier
- iron_trapdoor
- wall_banner
- spruce_door
- birch_door
- jungle_door
- acacia_door
- dark_oak_door
- end_rod
- end_gateway
# Just write your command as you would use it ingame here
# Don't use the -p parameter, this will get added automaticly with the senders name/the specified playername
presets:
# Triggered when you use /rtp without any additional paramters
default: "/rtp 100 1000 -f"
# add more to use /rtp <rtpname>, player needs "randomteleport.presets.<rtpname>"
# <rtpname>: "/rtp 1 2"
test: "rtp 10 200 -f"

View File

@ -0,0 +1,11 @@
reloaded: "&eReloaded the config. Some settings might require a server restart!"
search: "&7RandomTeleport searches for a safe place in world {worldname}. . ."
teleport: "&7RandomTeleport teleported you to X: {x} Y: {y} Z: {z}!"
setspawnpoint: "&7Your Respawnpoint has been set to your current location!"
error:
location: "&4Error: &cRandomTeleport could not find a save location!"
cooldown: "&cYou have to wait {cooldown_text}before using this RandomTeleport again!"
parse-error: "&cError while parsing option &f{option}&c with value &f{value}&c: {error}"
not-found: "&cCould not find &f{what}&c!"
player-not-found: "&cCould not find a player with the name &f{what}&c!"
world-not-found: "&cCould not find a world with the name &f{what}&c!"