Username caching changes

This commit is contained in:
Draycia committed 2022-03-26 14:13:14 -07:00
1 parent 5aed0a813a
commit 6f157ce07d
6 files changed
+160 -3

No files matched your search

@@ -0,0 +1,122 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.common.command.commands;
import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.standard.UUIDArgument;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
import net.draycia.carbon.common.command.CommandSettings;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.command.argument.CarbonPlayerArgument;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class UpdateUsernameCommand extends CarbonCommand {
final CarbonChat carbonChat;
final CommandManager<Commander> commandManager;
final CarbonMessageService messageService;
final PlayerSuggestions playerSuggestions;
@Inject
public UpdateUsernameCommand(
final CarbonChat carbonChat,
final CommandManager<Commander> commandManager,
final CarbonMessageService messageService,
final PlayerSuggestions playerSuggestions
) {
this.carbonChat = carbonChat;
this.commandManager = commandManager;
this.messageService = messageService;
this.playerSuggestions = playerSuggestions;
}
@Override
protected CommandSettings _commandSettings() {
return new CommandSettings("updateusername", "updatename");
}
@Override
public Key key() {
return Key.key("carbon", "updateusername");
}
@Override
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.argument(CarbonPlayerArgument.newBuilder("player").withMessageService(this.messageService).withSuggestionsProvider(this.playerSuggestions).asOptional(),
RichDescription.of(this.messageService.commandUpdateUsernameArgumentPlayer().component()))
.flag(this.commandManager.flagBuilder("uuid")
.withAliases("u")
.withDescription(RichDescription.of(this.messageService.commandUpdateUsernameArgumentUUID().component()))
.withArgument(UUIDArgument.optional("uuid"))
)
.permission("carbon.updateusername")
.senderType(Commander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.messageService.commandUpdateUsernameDescription().component())
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
CarbonPlayer target;
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
target = sender;
}
if (target instanceof WrappedCarbonPlayer wrappedPlayer) {
target = wrappedPlayer.carbonPlayerCommon();
} else if (!(target instanceof CarbonPlayerCommon)) {
this.messageService.usernameNotUpdated(sender);
return;
}
this.messageService.usernameFetching(sender);
final CarbonPlayer finalTarget = target;
this.carbonChat.server().resolveName(target.uuid()).thenAccept(name -> {
Objects.requireNonNull(name, "Unable to fetch username for player.");
((CarbonPlayerCommon) finalTarget).username(name);
this.messageService.usernameUpdated(sender, name);
});
})
.build();
this.commandManager.command(command);
}
}
@@ -341,4 +341,22 @@ public interface CarbonMessageService {
@Message("command.whisper.description")
RenderedMessage commandWhisperDescription();
@Message("command.updateusername.description")
RenderedMessage commandUpdateUsernameDescription();
@Message("command.updateusername.argument.player")
RenderedMessage commandUpdateUsernameArgumentPlayer();
@Message("command.updateusername.argument.uuid")
RenderedMessage commandUpdateUsernameArgumentUUID();
@Message("command.updateusername.notupdated")
void usernameNotUpdated(final Audience recipient);
@Message("command.updateusername.fetching")
void usernameFetching(final Audience audience);
@Message("command.updateusername.updated")
void usernameUpdated(final Audience audience, @Placeholder("newname") final String newName);
}
@@ -22,6 +22,7 @@ package net.draycia.carbon.common.users;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
@@ -47,7 +48,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
protected @Nullable Key selectedChannel = null;
// All players have these
protected @MonotonicNonNull String username;
protected transient @MonotonicNonNull String username = null;
protected @MonotonicNonNull UUID uuid;
// Display information
@@ -231,9 +232,18 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public String username() {
if (this.username == null) {
this.username = Objects.requireNonNull(CarbonChatProvider.carbonChat().server()
.resolveName(this.uuid).join());
}
return this.username;
}
public void username(final String username) {
this.username = username;
}
@Override
public boolean hasCustomDisplayName() {
return this.displayName != null;
@@ -50,6 +50,7 @@ import net.draycia.carbon.common.command.commands.ReloadCommand;
import net.draycia.carbon.common.command.commands.ReplyCommand;
import net.draycia.carbon.common.command.commands.UnignoreCommand;
import net.draycia.carbon.common.command.commands.UnmuteCommand;
import net.draycia.carbon.common.command.commands.UpdateUsernameCommand;
import net.draycia.carbon.common.command.commands.WhisperCommand;
import net.draycia.carbon.common.config.CommandConfig;
import net.draycia.carbon.common.config.ConfigFactory;
@@ -71,7 +72,7 @@ public final class CloudUtils {
public static final List<Class<? extends CarbonCommand>> COMMAND_CLASSES = List.of(ClearChatCommand.class,
ContinueCommand.class, DebugCommand.class, HelpCommand.class, IgnoreCommand.class, MuteCommand.class,
MuteInfoCommand.class, NicknameCommand.class, ReloadCommand.class, ReplyCommand.class, UnignoreCommand.class,
UnmuteCommand.class, WhisperCommand.class);
UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class);
private static final List<CarbonCommand> CONSTRUCTED_COMMANDS = new ArrayList<>();
@@ -42,6 +42,12 @@ command.unignore.description=Stops hiding messages from the specified player.
command.unmute.argument.player=The name of the player to unmute.
command.unmute.argument.uuid=The UUID of the player to unmute.
command.unmute.description=Unmutes players, allowing them to use chat and whisper other players.
command.updateusername.argument.player=The name of the player to update.
command.updateusername.argument.uuid=The uuid of the player to update.
command.updateusername.description=Updates the player's username to match their mojang name.
command.updateusername.fetching=Fetching username...
command.updateusername.notupdated=Unable to fetch username.
command.updateusername.updated=Updated <newname>'s username!
command.whisper.argument.message=The message to send.
command.whisper.argument.player=The name of the player to message.
command.whisper.description=Sends a private message to the specified player.
+1 -1
View File
@@ -1,5 +1,5 @@
# Project Properties
projectVersion=2.0.9
projectVersion=2.0.10-SNAPSHOT
# Gradle Properties
org.gradle.configureondemand=true