Don't start multiple searches at once

This only applies to sign interactions and when a player uses a preset himself
This commit is contained in:
Phoenix616 2019-04-12 16:07:06 +01:00
parent 80eeab8fc0
commit c4d9fd74af
5 changed files with 47 additions and 5 deletions

View File

@ -47,6 +47,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -62,6 +64,7 @@ public class RandomTeleport extends JavaPlugin {
private HookManager hookManager;
private LanguageManager lang;
private Table<String, UUID, Map.Entry<Long, Integer>> cooldowns = HashBasedTable.create();
private Map<UUID, RandomSearcher> runningSearchers = new HashMap<>();
private ValidatorRegistry locationValidators = new ValidatorRegistry();
private List<OptionParser> optionParsers = new ArrayList<>();
@ -210,6 +213,14 @@ public class RandomTeleport extends JavaPlugin {
locationValidators.add(new BlockValidator(saveBlocks));
}
/**
* Get the map of all running searchers
* @return The map of running searchers
*/
public Map<UUID, RandomSearcher> getRunningSearchers() {
return runningSearchers;
}
/**
* Utility method to create arrays with a nicer syntax. Seriously, why does Java not just accept {"string"} as parameters?!?
* @param array The array values
@ -219,7 +230,7 @@ public class RandomTeleport extends JavaPlugin {
return array;
}
public boolean sendMessage(List<? extends CommandSender> senders, String key, String... replacements) {
public boolean sendMessage(Collection<? extends CommandSender> senders, String key, String... replacements) {
boolean r = false;
for (CommandSender sender : senders) {
r |= sendMessage(sender, key, replacements);

View File

@ -18,6 +18,7 @@ package de.themoep.randomteleport;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import de.themoep.randomteleport.searcher.RandomSearcher;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
@ -85,6 +86,15 @@ public class RandomTeleportCommand implements CommandExecutor {
} else if (plugin.getConfig().getString("presets." + preset) == null) {
plugin.sendMessage(sender, "error.preset-doesnt-exist", "preset", preset);
} else {
if (sender == target) {
for (RandomSearcher searcher : plugin.getRunningSearchers().values()) {
if (searcher.getTargets().contains(target)) {
plugin.sendMessage(sender, "error.already-searching", "preset", preset);
return;
}
}
}
try {
plugin.runPreset(plugin.getServer().getConsoleSender(), preset, target, center);
} catch (IllegalArgumentException e) {

View File

@ -19,6 +19,7 @@ package de.themoep.randomteleport.listeners;
*/
import de.themoep.randomteleport.RandomTeleport;
import de.themoep.randomteleport.searcher.RandomSearcher;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -79,6 +80,13 @@ public class SignListener implements Listener {
if (plugin.getConfig().getString("presets." + preset) == null) {
plugin.sendMessage(event.getPlayer(), "error.preset-doesnt-exist", "preset", preset);
} else {
for (RandomSearcher searcher : plugin.getRunningSearchers().values()) {
if (searcher.getTargets().contains(event.getPlayer())) {
plugin.sendMessage(event.getPlayer(), "error.already-searching", "preset", preset);
return;
}
}
try {
plugin.runPreset(plugin.getServer().getConsoleSender(), preset, event.getPlayer(), event.getClickedBlock().getLocation());
} catch (IllegalArgumentException e) {

View File

@ -28,22 +28,24 @@ import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class RandomSearcher {
private final RandomTeleport plugin;
private final CommandSender initiator;
private final UUID uniqueId = UUID.randomUUID();
private ValidatorRegistry validators = new ValidatorRegistry();
private Random random = RandomTeleport.RANDOM;
private List<Entity> targets = new ArrayList<>();
private Set<Entity> targets = Collections.newSetFromMap(new LinkedHashMap<>());
private String id = null;
private long seed = -1;
@ -72,7 +74,7 @@ public class RandomSearcher {
* Get all entities targeted by this searcher
* @return The entitiy to target
*/
public List<Entity> getTargets() {
public Set<Entity> getTargets() {
return targets;
}
@ -80,6 +82,14 @@ public class RandomSearcher {
return validators;
}
/**
* Get a ID unique to each searcher
* @return The searcher's version 4 UUID
*/
public UUID getUniqueId() {
return uniqueId;
}
/**
* 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
@ -235,8 +245,10 @@ public class RandomSearcher {
* @return A CompletableFuture for when the search task is complete
*/
public CompletableFuture<Location> search() {
plugin.getRunningSearchers().put(uniqueId, this);
future = new CompletableFuture<>();
plugin.getServer().getScheduler().runTask(plugin, () -> checkRandom(future));
future.whenComplete((l, e) -> plugin.getRunningSearchers().remove(uniqueId));
return future;
}

View File

@ -18,6 +18,7 @@ error:
world-not-found: "&cCould not find a world with the name &f{what}&c!"
preset-doesnt-exist: "&cThe RandomTeleport preset &6{preset}&c does not exist!"
preset-invalid: "&cThe preset &6{preset}&c is not setup correctly! Please contact an admin."
already-searching: "&cA search is already in progress&c!"
no-permission:
general: "&cYou don't have permission to do that! &o({perm})"
preset: "&cYou don't have permission to use the preset &6{preset}&c! &o({perm})"