mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Simplify user manager hierarchy
This commit is contained in:
19 files changed
+72
-207
No files matched your search
@@ -37,6 +37,7 @@ import net.draycia.carbon.common.listeners.Listener;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.PlatformUserManager;
|
||||
import net.draycia.carbon.common.users.ProfileCache;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
@@ -72,7 +73,7 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
|
||||
final ScheduledExecutorService periodicTasks,
|
||||
final ProfileCache profileCache,
|
||||
final ProfileResolver profileResolver,
|
||||
final UserManagerInternal<C> userManager,
|
||||
final PlatformUserManager userManager,
|
||||
final ExecutionCoordinatorHolder commandExecutor,
|
||||
final CarbonServer carbonServer,
|
||||
final CarbonMessages carbonMessages,
|
||||
@@ -86,7 +87,7 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
|
||||
this.periodicTasks = periodicTasks;
|
||||
this.profileCache = profileCache;
|
||||
this.profileResolver = profileResolver;
|
||||
this.userManager = userManager;
|
||||
this.userManager = (UserManagerInternal<C>) userManager;
|
||||
this.commandExecutor = commandExecutor;
|
||||
this.carbonServer = carbonServer;
|
||||
this.carbonMessages = carbonMessages;
|
||||
|
||||
@@ -36,6 +36,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.command.ArgumentFactory;
|
||||
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
|
||||
@@ -66,6 +67,7 @@ import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.Backing;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.NetworkUsers;
|
||||
import net.draycia.carbon.common.users.PlatformUserManager;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.common.users.db.mysql.MySQLUserManager;
|
||||
import net.draycia.carbon.common.users.db.postgresql.PostgreSQLUserManager;
|
||||
@@ -142,6 +144,8 @@ public final class CarbonCommonModule extends AbstractModule {
|
||||
this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class);
|
||||
this.bind(CarbonEventHandler.class).to(CarbonEventHandlerImpl.class);
|
||||
this.bind(PlayerSuggestions.class).to(NetworkUsers.class);
|
||||
this.bind(new TypeLiteral<UserManager<?>>() {}).to(PlatformUserManager.class);
|
||||
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(PlatformUserManager.class);
|
||||
|
||||
final Multibinder<Listener> listeners = Multibinder.newSetBinder(this.binder(), Listener.class);
|
||||
listeners.addBinding().to(DeafenHandler.class);
|
||||
|
||||
@@ -19,31 +19,40 @@
|
||||
*/
|
||||
package net.draycia.carbon.common.users;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.assistedinject.FactoryModuleBuilder;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class PlatformUserManager<C extends WrappedCarbonPlayer> implements UserManagerInternal<C> {
|
||||
public final class PlatformUserManager implements UserManagerInternal<WrappedCarbonPlayer> {
|
||||
|
||||
private final UserManagerInternal<CarbonPlayerCommon> backingManager;
|
||||
private final PlayerFactory playerFactory;
|
||||
|
||||
public PlatformUserManager(final UserManagerInternal<CarbonPlayerCommon> backingManager) {
|
||||
@Inject
|
||||
private PlatformUserManager(
|
||||
final UserManagerInternal<CarbonPlayerCommon> backingManager,
|
||||
final PlayerFactory playerFactory
|
||||
) {
|
||||
this.backingManager = backingManager;
|
||||
this.playerFactory = playerFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<C> user(final UUID uuid) {
|
||||
public CompletableFuture<WrappedCarbonPlayer> user(final UUID uuid) {
|
||||
return this.backingManager.user(uuid).thenApply(common -> {
|
||||
final C wrapped = this.wrap(common);
|
||||
final WrappedCarbonPlayer wrapped = this.playerFactory.wrap(common);
|
||||
common.markTransientLoaded(!wrapped.online());
|
||||
return wrapped;
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract C wrap(final CarbonPlayerCommon common);
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
this.backingManager.shutdown();
|
||||
@@ -55,7 +64,7 @@ public abstract class PlatformUserManager<C extends WrappedCarbonPlayer> impleme
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> saveIfNeeded(final C player) {
|
||||
public CompletableFuture<Void> saveIfNeeded(final WrappedCarbonPlayer player) {
|
||||
return this.backingManager.saveIfNeeded(player.carbonPlayerCommon());
|
||||
}
|
||||
|
||||
@@ -69,4 +78,16 @@ public abstract class PlatformUserManager<C extends WrappedCarbonPlayer> impleme
|
||||
this.backingManager.cleanup();
|
||||
}
|
||||
|
||||
public interface PlayerFactory {
|
||||
|
||||
WrappedCarbonPlayer wrap(CarbonPlayerCommon common);
|
||||
|
||||
static Module moduleFor(final Class<? extends WrappedCarbonPlayer> carbonPlayerImpl) {
|
||||
return new FactoryModuleBuilder()
|
||||
.implement(WrappedCarbonPlayer.class, carbonPlayerImpl)
|
||||
.build(PlatformUserManager.PlayerFactory.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user