Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable nickname format #296

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.common.base.Suppliers;
import com.google.inject.Inject;
import java.util.function.Supplier;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.ArgumentFactory;
import net.draycia.carbon.common.command.CarbonCommand;
Expand Down Expand Up @@ -126,29 +124,27 @@ private void resetNickname(final Commander sender, final CarbonPlayer target) {
}

private void applyNickname(final Commander sender, final CarbonPlayer target, final String nick) {

// Lazy since the sender might not have permission to set the nickname
final Supplier<Component> parsedNick = Suppliers.memoize(() -> parseNickname(sender, nick));
final Component parsedNick = parseNickname(sender, nick);

// If the nickname is caught in the character limit, return without setting a nickname.
final int nickNameLength = PlainTextComponentSerializer.plainText().serialize(parsedNick.get()).length();
final int nickNameLength = PlainTextComponentSerializer.plainText().serialize(parsedNick).length();
final int minLength = this.config.primaryConfig().nickname().minLength();
final int maxLength = this.config.primaryConfig().nickname().maxLength();
if (nickNameLength < minLength || maxLength < nickNameLength) {
this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick.get(), minLength, maxLength);
this.carbonMessages.nicknameErrorCharacterLimit(sender, parsedNick, minLength, maxLength);
return;
}

target.nickname(parsedNick.get());
target.nickname(parsedNick);

if (sender instanceof PlayerCommander playerCommander
&& playerCommander.carbonPlayer().uuid().equals(target.uuid())) {
// Setting own nickname
this.carbonMessages.nicknameSet(sender, parsedNick.get());
this.carbonMessages.nicknameSet(sender, parsedNick);
} else {
// Setting other player's nickname
this.carbonMessages.nicknameSet(target, parsedNick.get());
this.carbonMessages.nicknameSetOthers(sender, target.username(), parsedNick.get());
this.carbonMessages.nicknameSet(target, parsedNick);
this.carbonMessages.nicknameSetOthers(sender, target.username(), parsedNick);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public static final class NicknameSettings {
@Comment("Whether Carbon's nickname management should be used. Disable this if you wish to have another plugin manage nicknames.")
private boolean useCarbonNicknames = true;

@Comment("Format used when displaying nicknames.")
public String format = "<hover:show_text:'<gray>@</gray><username>'><gray>~</gray><nickname></hover>";

@Comment("Whether to skip applying 'format' when a nickname matches a players username, only differing in decoration.")
public boolean skipFormatWhenNameMatches = true;

public boolean useCarbonNicknames() {
return this.useCarbonNicknames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.config.ConfigManager;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
Expand All @@ -54,6 +55,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private transient @MonotonicNonNull @Inject ProfileResolver profileResolver;
private transient @MonotonicNonNull @Inject PlatformScheduler scheduler;
private transient @MonotonicNonNull @Inject ConfigManager config;
private transient @MonotonicNonNull @Inject CarbonMessageRenderer messageRenderer;
private volatile transient long transientLoadedSince = -1;

protected final PersistentUserProperty<Boolean> muted;
Expand Down Expand Up @@ -443,6 +445,14 @@ public boolean hasNickname() {
return this.displayName.hasValue();
}

public ConfigManager configManager() {
return this.config;
}

public CarbonMessageRenderer messageRenderer() {
return this.messageRenderer;
}

@Override
public UUID uuid() {
return this.uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.config.PrimaryConfig;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
Expand Down Expand Up @@ -229,7 +232,26 @@ public String username() {
public Component displayName() {
final @Nullable Component nick = this.nickname();
if (nick != null) {
return nick;
final PrimaryConfig.NicknameSettings nicknames = this.carbonPlayerCommon.configManager().primaryConfig().nickname();

if (nicknames.skipFormatWhenNameMatches) {
final String plainNick = PlainTextComponentSerializer.plainText().serialize(nick);
if (plainNick.equals(this.username())) {
return nick;
}
}

try {
return this.carbonPlayerCommon.messageRenderer().render(
SourcedAudience.of(this, this),
nicknames.format,
Map.of("username", Tag.preProcessParsed(this.username()), "nickname", Tag.selfClosingInserting(nick)),
null,
null
);
} catch (final StackOverflowError overflow) {
throw new RuntimeException("Invalid nickname format '%s'. Makes circular reference to CarbonPlayer#displayName().".formatted(nicknames.format), overflow);
}
}
return this.platformDisplayName().orElseGet(() -> Component.text(this.username()));
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ command.ignore.description=Hides all incoming messages from ignored players.
command.ignorelist.description=Displays a paginated list of who you are ignoring.
command.ignorelist.none_ignored=<green>You are not ignoring any players.
command.ignorelist.pagination_header=<bold>Ignored players
command.ignorelist.pagination_element= - <hover:show_text:'Username: <username>'><display_name></hover> <click:run_command:'/unignore <username>'><hover:show_text:'Click to unignore <username>'><gray>[<white>unignore</white>]</gray>
command.ignorelist.pagination_element= - <display_name> <click:run_command:'/unignore <username>'><hover:show_text:'Click to unignore <username>'><gray>[<white>unignore</white>]</gray>
command.join.description=Join a channel you have previously left.
command.leave.description=Leave a channel that you currently have access to.
command.mute.argument.player=The name of the player to mute.
Expand Down