Adjust UserManager creation and access

This commit is contained in:
Jason Penilla committed 2023-03-21 20:03:52 -07:00
1 parent b41d745458
commit e5524eefd2
37 files changed
+252 -109

No files matched your search

@@ -21,7 +21,6 @@ package net.draycia.carbon.fabric;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import java.nio.file.Path;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -32,9 +31,11 @@ import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.events.CarbonEventHandler;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.ProfileCache;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ListenerUtils;
import net.draycia.carbon.common.util.PlayerUtils;
@@ -76,7 +77,7 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
private @Nullable MinecraftServer minecraftServer;
private @MonotonicNonNull ModContainer modContainer;
private @MonotonicNonNull Injector injector;
private @MonotonicNonNull UserManager<CarbonPlayerCommon> userManager;
private @MonotonicNonNull FabricUserManager userManager;
private @MonotonicNonNull Logger logger;
private @MonotonicNonNull CarbonServerFabric carbonServerFabric;
private @MonotonicNonNull CarbonMessages carbonMessages;
@@ -99,7 +100,7 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
this.carbonMessages = this.injector.getInstance(CarbonMessages.class);
this.channelRegistry = this.injector.getInstance(ChannelRegistry.class);
this.carbonServerFabric = this.injector.getInstance(CarbonServerFabric.class);
this.userManager = this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<UserManager<CarbonPlayerCommon>>() {}));
this.userManager = this.injector.getInstance(FabricUserManager.class);
// Platform Listeners
this.registerChatListener();
@@ -129,7 +130,13 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
private void registerServerLifecycleListeners() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> this.minecraftServer = server);
ServerLifecycleEvents.SERVER_STOPPING.register($ -> PlayerUtils.saveLoggedInPlayers(this.carbonServerFabric, this.userManager, this.logger).forEach(CompletableFuture::join));
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.minecraftServer = null);
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
this.injector.getInstance(ProfileCache.class).save();
this.injector.getInstance(ProfileResolver.class).shutdown();
this.userManager.shutdown();
this.injector.getInstance(ExecutionCoordinatorHolder.class).shutdown();
this.minecraftServer = null;
});
}
private void registerPlayerStatusListeners() {
@@ -139,11 +146,15 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
private void registerTickListeners() {
final long saveDelay = 5 * 60 * 20; // 5 minutes
final long saveProfilesDelay = 15 * 60 * 20; // 15 minutes
ServerTickEvents.END_SERVER_TICK.register(server -> {
if (server.getTickCount() != 0 && server.getTickCount() % saveDelay == 0) {
PlayerUtils.saveLoggedInPlayers(this.carbonServerFabric, this.userManager, this.logger);
}
if (server.getTickCount() != 0 && server.getTickCount() % saveProfilesDelay == 0) {
this.injector.getInstance(ProfileCache.class).save();
}
});
}
@@ -181,6 +192,11 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
return this.carbonServerFabric;
}
@Override
public UserManager<?> userManager() {
return this.userManager;
}
@Override
public ChannelRegistry channelRegistry() {
return this.channelRegistry;
@@ -20,24 +20,28 @@
package net.draycia.carbon.fabric;
import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.fabric.FabricServerCommandManager;
import cloud.commandframework.fabric.argument.FabricArgumentParsers;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import java.nio.file.Path;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.fabric.command.FabricCommander;
import net.draycia.carbon.fabric.command.FabricPlayerCommander;
import net.draycia.carbon.fabric.users.FabricProfileResolver;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
@@ -65,9 +69,9 @@ public final class CarbonChatFabricModule extends AbstractModule {
@Provides
@Singleton
public CommandManager<Commander> commandManager() {
public CommandManager<Commander> commandManager(final ExecutionCoordinatorHolder executionCoordinatorHolder) {
final FabricServerCommandManager<Commander> commandManager = new FabricServerCommandManager<>(
AsynchronousCommandExecutionCoordinator.<Commander>builder().build(),
executionCoordinatorHolder.executionCoordinator(),
commandSourceStack -> {
if (commandSourceStack.getEntity() instanceof ServerPlayer) {
return new FabricPlayerCommander(this.carbonChat, commandSourceStack);
@@ -105,7 +109,12 @@ public final class CarbonChatFabricModule extends AbstractModule {
this.bind(Logger.class).toInstance(this.logger);
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.dataDirectory);
this.bind(CarbonServer.class).to(CarbonServerFabric.class);
this.bind(PlayerSuggestions.class).toInstance(FabricArgumentParsers.<Commander>singlePlayerSelector()::suggestions);
this.bind(PlayerSuggestions.class).toInstance((ctx, input) -> this.carbonChat.minecraftServer().getPlayerList().getPlayers().stream()
.map(player -> player.getGameProfile().getName())
.toList());
this.bind(ProfileResolver.class).to(FabricProfileResolver.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(FabricUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(FabricUserManager.class);
}
}
@@ -26,7 +26,6 @@ import java.util.Objects;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.fabric.users.CarbonPlayerFabric;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
@@ -40,12 +39,12 @@ import org.jetbrains.annotations.NotNull;
public final class CarbonServerFabric implements CarbonServer, ForwardingAudience.Single {
private final CarbonChatFabric carbonChatFabric;
private final UserManager<CarbonPlayerFabric> userManager;
private final FabricUserManager userManager;
@Inject
private CarbonServerFabric(final CarbonChatFabric carbonChatFabric, final UserManager<CarbonPlayerCommon> userManager) {
private CarbonServerFabric(final CarbonChatFabric carbonChatFabric, final FabricUserManager userManager) {
this.carbonChatFabric = carbonChatFabric;
this.userManager = new FabricUserManager(userManager, carbonChatFabric);
this.userManager = userManager;
}
@Override
@@ -55,7 +54,7 @@ public final class CarbonServerFabric implements CarbonServer, ForwardingAudienc
@Override
public Audience console() {
return this.carbonChatFabric.minecraftServer();
return this.carbonChatFabric.minecraftServer().createCommandSourceStack();
}
@Override
@@ -19,7 +19,9 @@
*/
package net.draycia.carbon.fabric;
import net.draycia.carbon.api.users.UserManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.common.users.Backing;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.PlatformUserManager;
import net.draycia.carbon.common.users.UserManagerInternal;
@@ -28,12 +30,14 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
@Singleton
public class FabricUserManager extends PlatformUserManager<CarbonPlayerFabric> {
private final CarbonChatFabric carbonChatFabric;
public FabricUserManager(final UserManager<CarbonPlayerCommon> proxiedUserManager, final CarbonChatFabric carbonChatFabric) {
super((UserManagerInternal<CarbonPlayerCommon>) proxiedUserManager);
@Inject
private FabricUserManager(final @Backing UserManagerInternal<CarbonPlayerCommon> proxiedUserManager, final CarbonChatFabric carbonChatFabric) {
super(proxiedUserManager);
this.carbonChatFabric = carbonChatFabric;
}
@@ -38,7 +38,7 @@ public class MessageRecipientFilter {
}
public boolean shouldFilterMessageTo(final ServerPlayer serverPlayer) {
final UserManager<? extends CarbonPlayer> userManager = CarbonChatProvider.carbonChat().server().userManager();
final UserManager<? extends CarbonPlayer> userManager = CarbonChatProvider.carbonChat().userManager();
final CarbonPlayer author = userManager.user(this.sender.getUUID()).join();
final CarbonPlayer recipient = userManager.user(serverPlayer.getUUID()).join();
@@ -48,7 +48,7 @@ public record FabricPlayerCommander(
@Override
public CarbonPlayer carbonPlayer() {
return requireNonNull(
this.carbon.server().userManager().user(this.player().getUUID()).join(),
this.carbon.userManager().user(this.player().getUUID()).join(),
"No CarbonPlayer for logged in Player!"
);
}
@@ -59,7 +59,7 @@ public class FabricChatListener implements Consumer<ChatCallback.Chat> {
@Override
public void accept(final ChatCallback.Chat chat) {
final @Nullable CarbonPlayer sender = this.carbonChatFabric.server().userManager().user(chat.sender().getUUID()).join();
final @Nullable CarbonPlayer sender = this.carbonChatFabric.userManager().user(chat.sender().getUUID()).join();
var channel = requireNonNullElse(sender.selectedChannel(), this.channelRegistry.defaultValue());
final var originalMessage = chat.message();
@@ -117,7 +117,7 @@ public class FabricChatListener implements Consumer<ChatCallback.Chat> {
try {
final Optional<UUID> uuid = viewer.get(Identity.UUID);
if (uuid.isPresent()) {
final CarbonPlayer targetPlayer = this.carbonChatFabric.server().userManager().user(uuid.get()).join();
final CarbonPlayer targetPlayer = this.carbonChatFabric.userManager().user(uuid.get()).join();
renderedMessage = renderer.render(sender, targetPlayer, renderedMessage, chatEvent.message());
} else {
@@ -70,7 +70,7 @@ public class FabricChatPreviewListener implements ChatDecorator {
return CompletableFuture.completedFuture(component);
}
final @Nullable CarbonPlayer sender = this.carbonChat.server().userManager().user(serverPlayer.getUUID()).join();
final @Nullable CarbonPlayer sender = this.carbonChat.userManager().user(serverPlayer.getUUID()).join();
var channel = requireNonNullElse(sender.selectedChannel(), this.channelRegistry.defaultValue());
@@ -69,7 +69,7 @@ abstract class ServerGamePacketListenerImplMixin implements ServerGamePacketList
@Redirect(method = "broadcastChatMessage", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/network/chat/ChatType$Bound;)V"))
private void broadcastChatMessage(final PlayerList instance, final PlayerChatMessage playerChatMessage, final ServerPlayer serverPlayer, final ChatType.Bound bound) {
// TODO: Get the channel the message was sent in, when the player uses /channel <message>
final CarbonPlayer result = CarbonChatProvider.carbonChat().server().userManager().user(serverPlayer.getUUID()).join();
final CarbonPlayer result = CarbonChatProvider.carbonChat().userManager().user(serverPlayer.getUUID()).join();
final MessageRecipientFilter filter = new MessageRecipientFilter(serverPlayer, result.selectedChannel());
for (final ServerPlayer player : this.server.getPlayerList().getPlayers()) {
@@ -0,0 +1,72 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric.users;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.common.users.MojangProfileResolver;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.fabric.CarbonChatFabric;
import net.minecraft.server.level.ServerPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@Singleton
@DefaultQualifier(NonNull.class)
public final class FabricProfileResolver implements ProfileResolver {
private final CarbonChatFabric carbonChatFabric;
private final ProfileResolver mojang;
@Inject
private FabricProfileResolver(final CarbonChatFabric carbonChatFabric, final MojangProfileResolver mojang) {
this.carbonChatFabric = carbonChatFabric;
this.mojang = mojang;
}
@Override
public CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) {
final @Nullable ServerPlayer online = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayerByName(username);
if (online != null) {
return CompletableFuture.completedFuture(online.getUUID());
}
return this.mojang.resolveUUID(username, cacheOnly);
}
@Override
public CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) {
final @Nullable ServerPlayer online = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayer(uuid);
if (online != null) {
return CompletableFuture.completedFuture(online.getGameProfile().getName());
}
return this.mojang.resolveName(uuid, cacheOnly);
}
@Override
public void shutdown() {
this.mojang.shutdown();
}
}