From 59bcf6e79a404b1cbaf8e598f3f6d5e2cb378a4f Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Wed, 20 Feb 2019 00:44:10 +0100 Subject: [PATCH] Implement validators and command parsing. Also add language support. Theoretically this should already work... --- pom.xml | 9 + .../randomteleport/RandomTeleport.java | 190 +++++++++++++- .../randomteleport/RandomTeleportCommand.java | 54 ++++ .../randomteleport/ValidatorRegistry.java | 78 ++++++ .../searcher/RandomSearcher.java | 247 ++++++++++++++++++ .../searcher/options/NotFoundException.java | 32 +++ .../searcher/options/OptionParser.java | 27 ++ .../options/PlayerNotFoundException.java | 28 ++ .../searcher/options/SimpleOptionParser.java | 67 +++++ .../options/WorldNotFoundException.java | 28 ++ .../searcher/validators/BiomeValidator.java | 50 ++++ .../searcher/validators/BlockValidator.java | 50 ++++ .../validators/LocationValidator.java | 43 +++ .../validators/ProtectionValidator.java | 43 +++ .../validators/WorldborderValidator.java | 34 +++ .../src/main/resources/config.yml | 72 +++++ .../src/main/resources/languages/lang.en.yml | 11 + 17 files changed, 1062 insertions(+), 1 deletion(-) create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/ValidatorRegistry.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/NotFoundException.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/OptionParser.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/PlayerNotFoundException.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/SimpleOptionParser.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/WorldNotFoundException.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BiomeValidator.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BlockValidator.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/LocationValidator.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/ProtectionValidator.java create mode 100644 randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/WorldborderValidator.java create mode 100644 randomteleport-plugin/src/main/resources/languages/lang.en.yml diff --git a/pom.xml b/pom.xml index e9c91d0..c61c11d 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,10 @@ paper-repo https://papermc.io/repo/content/groups/public + + minebench-repo + https://repo.minebench.de/ + @@ -57,6 +61,11 @@ 1.0.1 compile + + de.themoep.utils + lang-bukkit + 1.1-SNAPSHOT + 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 480ab48..7d18474 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -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 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 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 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[] 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 getOptionParsers() { + return optionParsers; + } + + /** + * Add an option parser to this plugin + * @param parser The parser to add + */ + public void addOptionParser(OptionParser parser) { + optionParsers.add(parser); + } + } diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleportCommand.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleportCommand.java index a2a1916..71b0543 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleportCommand.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleportCommand.java @@ -18,9 +18,17 @@ package de.themoep.randomteleport; * along with this program. If not, see . */ +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); + } } diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/ValidatorRegistry.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/ValidatorRegistry.java new file mode 100644 index 0000000..4157cb5 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/ValidatorRegistry.java @@ -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 . + */ + +import de.themoep.randomteleport.searcher.validators.LocationValidator; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +public class ValidatorRegistry { + + private Map validators = new LinkedHashMap<>(); + + /** + * Get the map of currently set location validators + * @return The map of validators + */ + public Map getRaw() { + return validators; + } + + public Collection 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()); + } +} 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 new file mode 100644 index 0000000..8722499 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/RandomSearcher.java @@ -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 . + */ + +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 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 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 search() { + CompletableFuture future = new CompletableFuture<>(); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> checkRandom(future)); + return future; + } + + private void checkRandom(CompletableFuture 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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/NotFoundException.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/NotFoundException.java new file mode 100644 index 0000000..3f39a71 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/NotFoundException.java @@ -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 . + */ + +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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/OptionParser.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/OptionParser.java new file mode 100644 index 0000000..e1b2823 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/OptionParser.java @@ -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 . + */ + +import de.themoep.randomteleport.searcher.RandomSearcher; + +public interface OptionParser { + + boolean parse(RandomSearcher searcher, String[] args); + +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/PlayerNotFoundException.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/PlayerNotFoundException.java new file mode 100644 index 0000000..deb77dd --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/PlayerNotFoundException.java @@ -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 . + */ + +public class PlayerNotFoundException extends NotFoundException { + + public PlayerNotFoundException(String name) { + super(name); + } +} + + diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/SimpleOptionParser.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/SimpleOptionParser.java new file mode 100644 index 0000000..f806e16 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/SimpleOptionParser.java @@ -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 . + */ + +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 aliases; + private final BiFunction parser; + + public SimpleOptionParser(String option, BiFunction parser) { + this(new String[]{option}, parser); + } + + public SimpleOptionParser(String[] optionAliases, BiFunction 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 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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/WorldNotFoundException.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/WorldNotFoundException.java new file mode 100644 index 0000000..d1222be --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/options/WorldNotFoundException.java @@ -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 . + */ + +public class WorldNotFoundException extends NotFoundException { + + public WorldNotFoundException(String name) { + super(name); + } +} + + diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BiomeValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BiomeValidator.java new file mode 100644 index 0000000..22e0158 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BiomeValidator.java @@ -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 . + */ + +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 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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BlockValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BlockValidator.java new file mode 100644 index 0000000..ea5f3d5 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/BlockValidator.java @@ -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 . + */ + +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 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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/LocationValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/LocationValidator.java new file mode 100644 index 0000000..7c526ca --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/LocationValidator.java @@ -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 . + */ + +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); + +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/ProtectionValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/ProtectionValidator.java new file mode 100644 index 0000000..4be92b9 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/ProtectionValidator.java @@ -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 . + */ + +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; + } +} diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/WorldborderValidator.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/WorldborderValidator.java new file mode 100644 index 0000000..a9616c4 --- /dev/null +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/searcher/validators/WorldborderValidator.java @@ -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 . + */ + +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); + } +} diff --git a/randomteleport-plugin/src/main/resources/config.yml b/randomteleport-plugin/src/main/resources/config.yml index e69de29..7b8ed30 100644 --- a/randomteleport-plugin/src/main/resources/config.yml +++ b/randomteleport-plugin/src/main/resources/config.yml @@ -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 , player needs "randomteleport.presets." + # : "/rtp 1 2" + test: "rtp 10 200 -f" \ No newline at end of file diff --git a/randomteleport-plugin/src/main/resources/languages/lang.en.yml b/randomteleport-plugin/src/main/resources/languages/lang.en.yml new file mode 100644 index 0000000..86aee0c --- /dev/null +++ b/randomteleport-plugin/src/main/resources/languages/lang.en.yml @@ -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!" \ No newline at end of file