Shove login processing into an async task.

ez
This commit is contained in:
Draycia committed 2020-11-17 03:46:18 -08:00
1 parent c1ef5ff8ae
commit cbd1d66f60
4 files changed
+27 -14

No files matched your search

@@ -4,20 +4,19 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface UserService<T extends PlayerUser> {
@NonNull
T wrap(UUID uuid);
@Nullable <C extends ConsoleUser> C consoleUser();
@Nullable // Maybe NonNull?
<C extends ConsoleUser> C consoleUser();
@NonNull T wrap(@NonNull UUID uuid);
@Nullable
T wrapIfLoaded(@NonNull UUID uuid);
@NonNull CompletableFuture<T> wrapLater(@NonNull UUID uuid);
@Nullable
T refreshUser(@NonNull UUID uuid);
@Nullable T wrapIfLoaded(@NonNull UUID uuid);
@Nullable T refreshUser(@NonNull UUID uuid);
void onDisable();
@@ -104,18 +104,20 @@ public class BukkitChatListener implements Listener {
@EventHandler
public void onPlayerJoin(final PlayerJoinEvent event) {
final PlayerUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId());
final UserEvent.Join joinEvent = new UserEvent.Join(user);
this.carbonChat.userService().wrapLater(event.getPlayer().getUniqueId()).thenAcceptAsync(user -> {
final UserEvent.Join joinEvent = new UserEvent.Join(user);
CarbonEvents.post(joinEvent);
CarbonEvents.post(joinEvent);
});
}
@EventHandler
public void onPlayerLeave(final PlayerQuitEvent event) {
final PlayerUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId());
final UserEvent.Leave leaveEvent = new UserEvent.Leave(user);
this.carbonChat.userService().wrapLater(event.getPlayer().getUniqueId()).thenAcceptAsync(user -> {
final UserEvent.Leave leaveEvent = new UserEvent.Leave(user);
CarbonEvents.post(leaveEvent);
CarbonEvents.post(leaveEvent);
});
}
}
@@ -24,6 +24,7 @@ import java.lang.reflect.Type;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -78,6 +79,11 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
}
}
@Override
public @NonNull CompletableFuture<T> wrapLater(final @NonNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> this.wrap(uuid));
}
@Override
public @Nullable C consoleUser() {
return this.consoleFactory.get();
@@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -124,6 +125,11 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
}
}
@Override
public @NonNull CompletableFuture<T> wrapLater(final @NonNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> this.wrap(uuid));
}
@Override
public @Nullable C consoleUser() {
return this.consoleFactory.get();