Add simple cooldown functionality

Currently doesn't support storing of that information beyond server restarts.
Also improve formatting in the plugin.yml
This commit is contained in:
Phoenix616 2019-03-27 19:02:21 +01:00
parent 2ae3593687
commit d16f95b3b1
4 changed files with 96 additions and 21 deletions

View File

@ -18,6 +18,8 @@ package de.themoep.randomteleport;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import de.themoep.randomteleport.hook.HookManager; import de.themoep.randomteleport.hook.HookManager;
import de.themoep.randomteleport.listeners.SignListener; import de.themoep.randomteleport.listeners.SignListener;
import de.themoep.randomteleport.searcher.RandomSearcher; import de.themoep.randomteleport.searcher.RandomSearcher;
@ -37,15 +39,19 @@ import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.AbstractMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -54,6 +60,7 @@ public class RandomTeleport extends JavaPlugin {
public static final Random RANDOM = new Random(); public static final Random RANDOM = new Random();
private HookManager hookManager; private HookManager hookManager;
private LanguageManager lang; private LanguageManager lang;
private Table<String, UUID, Map.Entry<Long, Integer>> cooldowns = HashBasedTable.create();
private ValidatorRegistry locationValidators = new ValidatorRegistry(); private ValidatorRegistry locationValidators = new ValidatorRegistry();
private List<OptionParser> optionParsers = new ArrayList<>(); private List<OptionParser> optionParsers = new ArrayList<>();
@ -185,6 +192,13 @@ public class RandomTeleport extends JavaPlugin {
searcher.searchInGeneratedOnly(true); searcher.searchInGeneratedOnly(true);
return true; return true;
})); }));
addOptionParser(new SimpleOptionParser(array("id"), (searcher, args) -> {
if (args.length > 0) {
searcher.setId(args[0]);
return true;
}
return false;
}));
} }
private void initValidators() { private void initValidators() {
@ -195,7 +209,7 @@ public class RandomTeleport extends JavaPlugin {
} }
/** /**
* Utility method to create arrays with a nicer syntax. Seriously, why does Java not just accept {"string"}?!? * Utility method to create arrays with a nicer syntax. Seriously, why does Java not just accept {"string"} as parameters?!?
* @param array The array values * @param array The array values
* @return The same array * @return The same array
*/ */
@ -203,6 +217,14 @@ public class RandomTeleport extends JavaPlugin {
return array; return array;
} }
public boolean sendMessage(List<? extends CommandSender> senders, String key, String... replacements) {
boolean r = false;
for (CommandSender sender : senders) {
r |= sendMessage(sender, key, replacements);
}
return r;
}
public boolean sendMessage(CommandSender sender, String key, String... replacements) { public boolean sendMessage(CommandSender sender, String key, String... replacements) {
String message = lang.getConfig(sender).get(key, replacements); String message = lang.getConfig(sender).get(key, replacements);
if (message != null && !message.isEmpty()) { if (message != null && !message.isEmpty()) {
@ -250,7 +272,7 @@ public class RandomTeleport extends JavaPlugin {
* @param sender The sender of the command * @param sender The sender of the command
* @param center The center location for the searcher * @param center The center location for the searcher
* @param args The arguments to parse * @param args The arguments to parse
* @return Returns the searcher that is running * @return Returns the searcher that is running or null if it was stopped due to a cooldown
* @throws IllegalArgumentException Thrown when arguments couldn't be handled properly * @throws IllegalArgumentException Thrown when arguments couldn't be handled properly
*/ */
public RandomSearcher parseAndRun(CommandSender sender, Location center, String[] args) { public RandomSearcher parseAndRun(CommandSender sender, Location center, String[] args) {
@ -261,9 +283,26 @@ public class RandomTeleport extends JavaPlugin {
parser.parse(searcher, optionArgs); parser.parse(searcher, optionArgs);
} }
searcher.getTargets().forEach(e -> sendMessage(e, "search", "worldname", searcher.getCenter().getWorld().getName())); int cooldown = 0;
for (Entity target : searcher.getTargets()) {
Map.Entry<Long, Integer> lastUse = cooldowns.get(searcher.getId(), target.getUniqueId());
if (lastUse != null) {
int targetCooldown = (int) ((System.currentTimeMillis() - lastUse.getKey()) / 1000);
if (targetCooldown > cooldown) {
cooldown = targetCooldown;
}
}
}
if (cooldown > 0) {
sendMessage(searcher.getTargets(), "cooldown", "cooldown_text", cooldown + "s");
return null;
}
sendMessage(searcher.getTargets(), "search", "worldname", searcher.getCenter().getWorld().getName());
searcher.search().thenApply(targetLoc -> { searcher.search().thenApply(targetLoc -> {
searcher.getTargets().forEach(e -> { searcher.getTargets().forEach(e -> {
cooldowns.put(searcher.getId(), e.getUniqueId(), new AbstractMap.SimpleImmutableEntry<>(System.currentTimeMillis(), searcher.getCooldown()));
e.teleport(targetLoc); e.teleport(targetLoc);
sendMessage(e, "teleport", sendMessage(e, "teleport",
"worldname", center.getWorld().getName(), "worldname", center.getWorld().getName(),

View File

@ -43,6 +43,7 @@ public class RandomSearcher {
private List<Entity> targets = new ArrayList<>(); private List<Entity> targets = new ArrayList<>();
private String id = null;
private long seed = -1; private long seed = -1;
private Location center; private Location center;
private int minRadius = 0; private int minRadius = 0;
@ -50,6 +51,7 @@ public class RandomSearcher {
private boolean generatedOnly = false; private boolean generatedOnly = false;
private int maxChecks = 100; private int maxChecks = 100;
private int cooldown; private int cooldown;
private int checks = 0; private int checks = 0;
private CompletableFuture<Location> future = null; private CompletableFuture<Location> future = null;
@ -75,6 +77,25 @@ public class RandomSearcher {
return validators; return validators;
} }
/**
* 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
*/
public void setId(String id) {
this.id = id;
}
/**
* Get the ID of the searcher used for cooldowns. If no specific one is set then one generated by the settings will be returned
* @return The ID of the searcher
*/
public String getId() {
if (id == null) {
return toString();
}
return id;
}
/** /**
* Set the seed that should be used when selecting locations. See {@link Random#setSeed(long)}. * Set the seed that should be used when selecting locations. See {@link Random#setSeed(long)}.
* @param seed The seed. * @param seed The seed.
@ -254,4 +275,18 @@ public class RandomSearcher {
public CompletableFuture<Location> getFuture() { public CompletableFuture<Location> getFuture() {
return future; return future;
} }
@Override
public String toString() {
return "RandomSearcher{" +
"id='" + id + '\'' +
", seed=" + seed +
", center=" + center +
", minRadius=" + minRadius +
", maxRadius=" + maxRadius +
", generatedOnly=" + generatedOnly +
", maxChecks=" + maxChecks +
", cooldown=" + cooldown +
'}';
}
} }

View File

@ -21,8 +21,9 @@ commands:
> -b,-biome <biomename> [<biome 2> ...] - only teleport to this biome (multiple allowed, Bukkit biome names!) > -b,-biome <biomename> [<biome 2> ...] - only teleport to this biome (multiple allowed, Bukkit biome names!)
> -x,-xPos <x value> - x axis of the center point, if not set the player's x axis is used > -x,-xPos <x value> - x axis of the center point, if not set the player's x axis is used
> -z,-zPos <z value> - z axis of the center point, if not set the player's z axis is used > -z,-zPos <z value> - z axis of the center point, if not set the player's z axis is used
> -l,-loaded - only search loaded chunks for possible locations > -g,-generated - only search generated chunks for possible locations
> -c, -cooldown <seconds> - cooldown in seconds after which the player can use this teleporter again > -c, -cooldown <seconds> - cooldown in seconds after which the player can use this teleporter again
> -id <id> - The ID to use for the cooldown, uses automatically generated one if not provided
> -f,-force - teleport even if there is no dirt/grass/sand/gravel, only checks for lava/water/cactus, ignores WorldGuard/Faction regions > -f,-force - teleport even if there is no dirt/grass/sand/gravel, only checks for lava/water/cactus, ignores WorldGuard/Faction regions
> -f,-force [<blocks|regions>] - only ignore blocks or regions > -f,-force [<blocks|regions>] - only ignore blocks or regions
/<command> --stat - shows a statistic of the teleports since the last restart /<command> --stat - shows a statistic of the teleports since the last restart