diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java index 64da919f4..0df18ea94 100644 --- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java +++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java @@ -26,6 +26,8 @@ package com.plotsquared.core.uuid; import com.google.common.collect.Lists; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.util.task.TaskManager; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -49,8 +51,14 @@ public class UUIDPipeline { private final Executor executor; private final List serviceList; - private final List> consumerList; + private final List>> consumerList; + /** + * Construct a new UUID pipeline + * + * @param executor Executor that is used to run asynchronous tasks inside + * of the pipeline + */ public UUIDPipeline(@NotNull final Executor executor) { this.executor = executor; this.serviceList = Lists.newLinkedList(); @@ -71,7 +79,7 @@ public class UUIDPipeline { * * @param mappingConsumer Consumer to register */ - public void registerConsumer(@NotNull final Consumer mappingConsumer) { + public void registerConsumer(@NotNull final Consumer> mappingConsumer) { this.consumerList.add(mappingConsumer); } @@ -84,19 +92,54 @@ public class UUIDPipeline { return Collections.unmodifiableList(this.serviceList); } - private void consume(@NotNull final UUIDMapping mapping) { - for (final Consumer consumer : this.consumerList) { - consumer.accept(mapping); + /** + * Let all consumers act on the given mapping. + * + * @param mappings Mappings + */ + public void consume(@NotNull final List mappings) { + final Runnable runnable = () -> { + for (final Consumer> consumer : this.consumerList) { + consumer.accept(mappings); + } + }; + if (PlotSquared.get().isMainThread(Thread.currentThread())) { + TaskManager.runTaskAsync(runnable); + } else { + runnable.run(); } } + /** + * Consume a single mapping + * + * @param mapping Mapping to consume + */ + public void consume(@NotNull final UUIDMapping mapping) { + this.consume(Collections.singletonList(mapping)); + } + + /** + * This will store the given username-UUID pair directly, and overwrite + * any existing caches. This can be used to update usernames automatically + * whenever a player joins the server, to make sure an up-to-date UUID + * mapping is stored + * + * @param username Player username + * @param uuid Player uuid + */ + public void storeImmediately(@NotNull final String username, @NotNull final UUID uuid) { + this.consume(new UUIDMapping(uuid, username)); + } + /** * Asynchronously attempt to fetch the mapping from a list of UUIDs * * @param requests UUIDs * @return Mappings */ - public CompletableFuture> getNames(@NotNull final Collection requests) { + public CompletableFuture> getNames( + @NotNull final Collection requests) { final List serviceList = this.getServiceListInstance(); return CompletableFuture.supplyAsync(() -> { final List mappings = new ArrayList<>(requests.size()); @@ -111,6 +154,7 @@ public class UUIDPipeline { } if (mappings.size() == requests.size()) { + this.consume(mappings); return mappings; } @@ -124,7 +168,8 @@ public class UUIDPipeline { * @param requests Names * @return Mappings */ - public CompletableFuture> getUUIDs(@NotNull final Collection requests) { + public CompletableFuture> getUUIDs( + @NotNull final Collection requests) { final List serviceList = this.getServiceListInstance(); return CompletableFuture.supplyAsync(() -> { final List mappings = new ArrayList<>(requests.size()); @@ -139,6 +184,7 @@ public class UUIDPipeline { } if (mappings.size() == requests.size()) { + this.consume(mappings); return mappings; }