diff --git a/common/src/main/java/com/discordsrv/common/linking/impl/CachedLinkProvider.java b/common/src/main/java/com/discordsrv/common/linking/impl/CachedLinkProvider.java index b8a126ec..3116f388 100644 --- a/common/src/main/java/com/discordsrv/common/linking/impl/CachedLinkProvider.java +++ b/common/src/main/java/com/discordsrv/common/linking/impl/CachedLinkProvider.java @@ -22,6 +22,7 @@ import com.discordsrv.api.event.bus.Subscribe; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.event.events.player.PlayerConnectedEvent; import com.discordsrv.common.linking.LinkProvider; +import com.discordsrv.common.linking.LinkStore; import com.github.benmanes.caffeine.cache.AsyncCacheLoader; import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.github.benmanes.caffeine.cache.Cache; @@ -155,4 +156,38 @@ public abstract class CachedLinkProvider implements LinkProvider { playerToUser.get(uuid); linkingAllowed.remove(uuid); } + + protected void addToCache(UUID playerUUID, long userId) { + playerToUser.put(playerUUID, CompletableFuture.completedFuture(userId)); + } + + protected void evictFromCache(UUID playerUUID) { + playerToUser.synchronous().invalidate(playerUUID); + } + + public static abstract class Store extends CachedLinkProvider implements LinkStore { + + public Store(DiscordSRV discordSRV) { + super(discordSRV); + } + + public abstract CompletableFuture link(@NotNull UUID playerUUID, long userId); + public abstract CompletableFuture unlink(@NotNull UUID playerUUID, long userId); + + @Override + public final CompletableFuture createLink(@NotNull UUID playerUUID, long userId) { + return link(playerUUID, userId).thenApply(v -> { + addToCache(playerUUID, userId); + return null; + }); + } + + @Override + public CompletableFuture removeLink(@NotNull UUID playerUUID, long userId) { + return unlink(playerUUID, userId).thenApply(v -> { + evictFromCache(playerUUID); + return null; + }); + } + } } diff --git a/common/src/main/java/com/discordsrv/common/linking/impl/MinecraftAuthenticationLinker.java b/common/src/main/java/com/discordsrv/common/linking/impl/MinecraftAuthenticationLinker.java index d1c5a9d9..3417dcf2 100644 --- a/common/src/main/java/com/discordsrv/common/linking/impl/MinecraftAuthenticationLinker.java +++ b/common/src/main/java/com/discordsrv/common/linking/impl/MinecraftAuthenticationLinker.java @@ -22,7 +22,6 @@ import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.function.CheckedSupplier; import com.discordsrv.common.future.util.CompletableFutureUtil; -import com.discordsrv.common.linking.LinkProvider; import com.discordsrv.common.linking.LinkStore; import com.discordsrv.common.linking.LinkingModule; import com.discordsrv.common.linking.requirelinking.RequiredLinkingModule; @@ -44,7 +43,7 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Supplier; -public class MinecraftAuthenticationLinker extends CachedLinkProvider implements LinkProvider { +public class MinecraftAuthenticationLinker extends CachedLinkProvider { public static final String BASE_LINK_URL = DiscordSRV.WEBSITE + "/link"; diff --git a/common/src/main/java/com/discordsrv/common/linking/impl/StorageLinker.java b/common/src/main/java/com/discordsrv/common/linking/impl/StorageLinker.java index fcf1620c..67019474 100644 --- a/common/src/main/java/com/discordsrv/common/linking/impl/StorageLinker.java +++ b/common/src/main/java/com/discordsrv/common/linking/impl/StorageLinker.java @@ -21,8 +21,6 @@ package com.discordsrv.common.linking.impl; import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.component.util.ComponentUtil; -import com.discordsrv.common.linking.LinkProvider; -import com.discordsrv.common.linking.LinkStore; import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,7 +31,7 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; -public class StorageLinker extends CachedLinkProvider implements LinkProvider, LinkStore { +public class StorageLinker extends CachedLinkProvider.Store { public StorageLinker(DiscordSRV discordSRV) { super(discordSRV); @@ -56,12 +54,12 @@ public class StorageLinker extends CachedLinkProvider implements LinkProvider, L } @Override - public CompletableFuture createLink(@NotNull UUID playerUUID, long userId) { + public CompletableFuture link(@NotNull UUID playerUUID, long userId) { return discordSRV.scheduler().execute(() -> discordSRV.storage().createLink(playerUUID, userId)); } @Override - public CompletableFuture removeLink(@NotNull UUID playerUUID, long userId) { + public CompletableFuture unlink(@NotNull UUID playerUUID, long userId) { return discordSRV.scheduler().execute(() -> discordSRV.storage().removeLink(playerUUID, userId)); }