Fix a bunch of minor issues

This commit is contained in:
Alexander Söderberg 2020-05-20 00:31:59 +02:00
parent 8efc78e1c9
commit ea41c842bc
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
10 changed files with 160 additions and 35 deletions

View File

@ -66,6 +66,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.github.pavog</groupId>
<artifactId>SquirrelID</artifactId>
<version>0.6.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.sk89q.worldedit</groupId> <groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId> <artifactId>worldedit-core</artifactId>

View File

@ -318,7 +318,6 @@ public class PaperListener implements Listener {
} }
@EventHandler public void onAsyncTabCompletion(final AsyncTabCompleteEvent event) { @EventHandler public void onAsyncTabCompletion(final AsyncTabCompleteEvent event) {
PlotSquared.debug("ASYNC COMPLETION");
String buffer = event.getBuffer(); String buffer = event.getBuffer();
if (!(event.getSender() instanceof Player)) { if (!(event.getSender() instanceof Player)) {
return; return;
@ -332,7 +331,7 @@ public class PaperListener implements Listener {
final String[] unprocessedArgs = buffer.split(Pattern.quote(" ")); final String[] unprocessedArgs = buffer.split(Pattern.quote(" "));
if (unprocessedArgs.length == 1) { if (unprocessedArgs.length == 1) {
return; // We don't do anything in this case return; // We don't do anything in this case
} else if (!Arrays.asList("plots", "p", "plotsquared", "plot2", "p2", "ps", "2", "plotme", "plotz", "ap") } else if (!Arrays.asList("plot", "plots", "p", "plotsquared", "plot2", "p2", "ps", "2", "plotme", "plotz", "ap")
.contains(unprocessedArgs[0].toLowerCase(Locale.ENGLISH))) { .contains(unprocessedArgs[0].toLowerCase(Locale.ENGLISH))) {
return; return;
} }
@ -350,7 +349,6 @@ public class PaperListener implements Listener {
} }
event.setCompletions(result); event.setCompletions(result);
event.setHandled(true); event.setHandled(true);
PlotSquared.debug("ASYNC COMPLETION HANDLED");
} catch (final Exception ignored) {} } catch (final Exception ignored) {}
} }

View File

@ -32,9 +32,12 @@ import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -127,4 +130,8 @@ public class Add extends Command {
return future; return future;
} }
@Override public Collection<Command> tab(final PlotPlayer player, final String[] args, final boolean space) {
return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList());
}
} }

View File

@ -34,8 +34,10 @@ import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.entity.EntityCategory; import com.plotsquared.core.util.entity.EntityCategory;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.uuid.UUIDMapping;
import com.sk89q.worldedit.world.entity.EntityType; import com.sk89q.worldedit.world.entity.EntityType;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.Map; import java.util.Map;
@ -65,6 +67,11 @@ public class Debug extends SubCommand {
.currentThread().getName())); .currentThread().getName()));
return true; return true;
} }
if (args.length > 0 && "uuids".equalsIgnoreCase(args[0])) {
final Collection<UUIDMapping> mappings = PlotSquared.get().getImpromptuUUIDPipeline().getAllImmediately();
MainUtil.sendMessage(player, String.format("There are %d cached UUIDs", mappings.size()));
return true;
}
if (args.length > 0 && "entitytypes".equalsIgnoreCase(args[0])) { if (args.length > 0 && "entitytypes".equalsIgnoreCase(args[0])) {
EntityCategories.init(); EntityCategories.init();
player.sendMessage(Captions.PREFIX.getTranslated() + "§cEntity Categories: "); player.sendMessage(Captions.PREFIX.getTranslated() + "§cEntity Categories: ");

View File

@ -150,7 +150,7 @@ public class Owner extends SetCommand {
if (throwable instanceof TimeoutException) { if (throwable instanceof TimeoutException) {
MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT); MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT);
} else if (throwable != null) { } else if (throwable != null) {
MainUtil.sendMessage(player, Captions.INVALID_PLAYER); MainUtil.sendMessage(player, Captions.INVALID_PLAYER, value);
} else { } else {
uuidConsumer.accept(uuid); uuidConsumer.accept(uuid);
} }

View File

@ -32,9 +32,12 @@ import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -126,4 +129,8 @@ public class Trust extends Command {
return CompletableFuture.completedFuture(true); return CompletableFuture.completedFuture(true);
} }
@Override public Collection<Command> tab(final PlotPlayer player, final String[] args, final boolean space) {
return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList());
}
} }

View File

@ -82,6 +82,7 @@ import java.util.Optional;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
@ -902,24 +903,61 @@ public class MainUtil {
return (directory.delete()); return (directory.delete());
} }
/** /*
* Get a list of names given a list of uuids.<br> @NotNull public static String getName(UUID owner) {
* - Uses the format {@link Captions#PLOT_USER_LIST} for the returned string if (owner == null) {
*
* @param uuids
* @return
*/
public static String getPlayerList(Collection<UUID> uuids) {
ArrayList<UUID> l = new ArrayList<>(uuids);
if (l.size() < 1) {
return Captions.NONE.getTranslated(); return Captions.NONE.getTranslated();
} }
List<String> users = if (owner.equals(DBFunc.EVERYONE)) {
l.stream().map(MainUtil::getName).sorted().collect(Collectors.toList()); return Captions.EVERYONE.getTranslated();
}
if (owner.equals(DBFunc.SERVER)) {
return Captions.SERVER.getTranslated();
}
String name = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(owner, Settings.UUID.BLOCKING_TIMEOUT);
if (name == null) {
return Captions.UNKNOWN.getTranslated();
}
return name;
}
*/
/**
* Get a list of names given a list of UUIDs.
* - Uses the format {@link Captions#PLOT_USER_LIST} for the returned string
*/
public static String getPlayerList(final Collection<UUID> uuids) {
if (uuids.size() < 1) {
return Captions.NONE.getTranslated();
}
final List<UUID> players = new LinkedList<>();
final List<String> users = new LinkedList<>();
for (final UUID uuid : uuids) {
if (uuid == null) {
users.add(Captions.NONE.getTranslated());
} else if (DBFunc.EVERYONE.equals(uuid)) {
users.add(Captions.EVERYONE.getTranslated());
} else if (DBFunc.SERVER.equals(uuid)) {
users.add(Captions.SERVER.getTranslated());
} else {
players.add(uuid);
}
}
try {
for (final UUIDMapping mapping : PlotSquared.get().getImpromptuUUIDPipeline().getNames(players).get(Settings.UUID.BLOCKING_TIMEOUT,
TimeUnit.MILLISECONDS)) {
users.add(mapping.getUsername());
}
} catch (final Exception e) {
e.printStackTrace();
}
String c = Captions.PLOT_USER_LIST.getTranslated(); String c = Captions.PLOT_USER_LIST.getTranslated();
StringBuilder list = new StringBuilder(); StringBuilder list = new StringBuilder();
for (int x = 0; x < users.size(); x++) { for (int x = 0; x < users.size(); x++) {
if (x + 1 == l.size()) { if (x + 1 == uuids.size()) {
list.append(c.replace("%user%", users.get(x)).replace(",", "")); list.append(c.replace("%user%", users.get(x)).replace(",", ""));
} else { } else {
list.append(c.replace("%user%", users.get(x))); list.append(c.replace("%user%", users.get(x)));

View File

@ -78,4 +78,7 @@ public class CacheUUIDService implements UUIDService, Consumer<List<UUIDMapping>
return this.usernameCache.asMap().values(); return this.usernameCache.asMap().values();
} }
@Override public boolean canBeSynchronous() {
return true;
}
} }

View File

@ -27,6 +27,7 @@ package com.plotsquared.core.uuid;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.util.ThreadUtils; import com.plotsquared.core.util.ThreadUtils;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
@ -156,10 +157,14 @@ public class UUIDPipeline {
@Nullable public UUID getSingle(@NotNull final String username, final long timeout) { @Nullable public UUID getSingle(@NotNull final String username, final long timeout) {
ThreadUtils.catchSync("Blocking UUID retrieval from the main thread"); ThreadUtils.catchSync("Blocking UUID retrieval from the main thread");
try { try {
this.getUUIDs(Collections.singletonList(username)).get(timeout, TimeUnit.MILLISECONDS); final List<UUIDMapping> mappings = this.getUUIDs(Collections.singletonList(username)).get(timeout, TimeUnit.MILLISECONDS);
if (mappings.size() == 1) {
return mappings.get(0).getUuid();
}
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} catch (TimeoutException ignored) { } catch (TimeoutException ignored) {
PlotSquared.log(Captions.PREFIX + " (UUID) Request for " + username + " timed out");
// This is completely valid, we just don't care anymore // This is completely valid, we just don't care anymore
} }
return null; return null;
@ -175,10 +180,14 @@ public class UUIDPipeline {
@Nullable public String getSingle(@NotNull final UUID uuid, final long timeout) { @Nullable public String getSingle(@NotNull final UUID uuid, final long timeout) {
ThreadUtils.catchSync("Blocking username retrieval from the main thread"); ThreadUtils.catchSync("Blocking username retrieval from the main thread");
try { try {
this.getNames(Collections.singletonList(uuid)).get(timeout, TimeUnit.MILLISECONDS); final List<UUIDMapping> mappings = this.getNames(Collections.singletonList(uuid)).get(timeout, TimeUnit.MILLISECONDS);
if (mappings.size() == 1) {
return mappings.get(0).getUsername();
}
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} catch (TimeoutException ignored) { } catch (TimeoutException ignored) {
PlotSquared.log(Captions.PREFIX + " (UUID) Request for " + uuid + " timed out");
// This is completely valid, we just don't care anymore // This is completely valid, we just don't care anymore
} }
return null; return null;
@ -274,25 +283,45 @@ public class UUIDPipeline {
if (requests.isEmpty()) { if (requests.isEmpty()) {
return CompletableFuture.completedFuture(Collections.emptyList()); return CompletableFuture.completedFuture(Collections.emptyList());
} }
final List<UUIDService> serviceList = this.getServiceListInstance();
return CompletableFuture.supplyAsync(() -> {
final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
final List<UUID> remainingRequests = new ArrayList<>(requests);
for (final UUIDService service : serviceList) { final List<UUIDService> serviceList = this.getServiceListInstance();
if (remainingRequests.isEmpty()) { final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
break; final List<UUID> remainingRequests = new ArrayList<>(requests);
}
for (final UUIDService service : serviceList) {
// We can chain multiple synchronous
// ones in a row
if (service.canBeSynchronous()) {
final List<UUIDMapping> completedRequests = service.getNames(remainingRequests); final List<UUIDMapping> completedRequests = service.getNames(remainingRequests);
for (final UUIDMapping mapping : completedRequests) { for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUuid()); remainingRequests.remove(mapping.getUuid());
} }
mappings.addAll(completedRequests); mappings.addAll(completedRequests);
} else {
break;
}
if (remainingRequests.isEmpty()) {
return CompletableFuture.completedFuture(mappings);
}
}
return CompletableFuture.supplyAsync(() -> {
for (final UUIDService service : serviceList) {
final List<UUIDMapping> completedRequests = service.getNames(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUuid());
}
mappings.addAll(completedRequests);
if (remainingRequests.isEmpty()) {
break;
}
} }
if (mappings.size() == requests.size()) { if (mappings.size() == requests.size()) {
this.consume(mappings); this.consume(mappings);
return mappings; return mappings;
} else if (Settings.DEBUG) {
PlotSquared.log("Failed to find all usernames");
} }
throw new ServiceError("End of pipeline"); throw new ServiceError("End of pipeline");
@ -310,25 +339,45 @@ public class UUIDPipeline {
if (requests.isEmpty()) { if (requests.isEmpty()) {
return CompletableFuture.completedFuture(Collections.emptyList()); return CompletableFuture.completedFuture(Collections.emptyList());
} }
final List<UUIDService> serviceList = this.getServiceListInstance();
return CompletableFuture.supplyAsync(() -> {
final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
final List<String> remainingRequests = new ArrayList<>(requests);
for (final UUIDService service : serviceList) { final List<UUIDService> serviceList = this.getServiceListInstance();
if (remainingRequests.isEmpty()) { final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
break; final List<String> remainingRequests = new ArrayList<>(requests);
}
for (final UUIDService service : serviceList) {
// We can chain multiple synchronous
// ones in a row
if (service.canBeSynchronous()) {
final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests); final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests);
for (final UUIDMapping mapping : completedRequests) { for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUsername()); remainingRequests.remove(mapping.getUsername());
} }
mappings.addAll(completedRequests); mappings.addAll(completedRequests);
} else {
break;
}
if (remainingRequests.isEmpty()) {
return CompletableFuture.completedFuture(mappings);
}
}
return CompletableFuture.supplyAsync(() -> {
for (final UUIDService service : serviceList) {
final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUsername());
}
mappings.addAll(completedRequests);
if (remainingRequests.isEmpty()) {
break;
}
} }
if (mappings.size() == requests.size()) { if (mappings.size() == requests.size()) {
this.consume(mappings); this.consume(mappings);
return mappings; return mappings;
} else if (Settings.DEBUG) {
PlotSquared.log("Failed to find all UUIDs");
} }
throw new ServiceError("End of pipeline"); throw new ServiceError("End of pipeline");

View File

@ -65,4 +65,14 @@ public interface UUIDService {
return Collections.emptyList(); return Collections.emptyList();
} }
/**
* Check whether or not this service can be safely used synchronously
* without blocking the server for an extended amount of time.
*
* @return True if the service can be used synchronously
*/
default boolean canBeSynchronous() {
return false;
}
} }