Skip to content

Commit

Permalink
displayName -> nickname; renderName -> displayName
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Aug 10, 2023
1 parent 1267b3b commit 10b9862
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 100 deletions.
68 changes: 33 additions & 35 deletions api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -42,21 +40,6 @@
@DefaultQualifier(NonNull.class)
public interface CarbonPlayer extends Audience, Identified {

/**
* Returns the appropriate {@link Component} to represent the player's name.
*
* @param player the player
* @return the player's name
* @since 2.0.0
*/
static Component renderName(final CarbonPlayer player) {
if (player.hasCustomDisplayName()) {
return Objects.requireNonNull(player.displayName());
} else {
return player.get(Identity.DISPLAY_NAME).orElseGet(() -> Component.text(player.username()));
}
}

/**
* Returns the distance from the other {@link CarbonPlayer}, or -1 if the players are not in the same world.
*
Expand Down Expand Up @@ -84,41 +67,56 @@ static Component renderName(final CarbonPlayer player) {
String username();

/**
* Checks if the player has a display name set.
* Returns the player's display name.
*
* <p>The display name is the effective or displayed name of a player.
* When the player has a nickname set, either through Carbon or the platform,
* it will be reflected here. Else, a plain text component representing
* the player's name may be returned.</p>
*
* @return the player's display name
* @since 2.1.0
*/
Component displayName();

/**
* Checks if the player has a nickname set.
*
* <p>Will always return {@code false} when Carbon's nickname management is disabled.</p>
*
* @return if the player has a custom display name set
* @see #displayName()
* @see #displayName(Component)
* @since 2.0.0
* @return if the player has a nickname set
* @see #nickname()
* @see #nickname(Component)
* @since 2.1.0
*/
boolean hasCustomDisplayName();
boolean hasNickname();

/**
* Gets the player's display name, shown in places like chat and tab menu.
* Gets the player's nickname, shown in places like chat and tab menu.
*
* <p>Will always return {@code null} when Carbon's nickname management is disabled.</p>
*
* @return the player's display name
* @see #hasCustomDisplayName()
* @see #displayName(Component)
* @since 2.0.0
* @return the player's nickname
* @see #hasNickname()
* @see #nickname(Component)
* @see #displayName()
* @since 2.1.0
*/
@Nullable Component displayName();
@Nullable Component nickname();

/**
* Sets the player's display name.
* Sets the player's nickname.
*
* <p>Setting {@code null} will remove the current display name.</p>
* <p>Setting {@code null} will remove any current nickname.</p>
* <p>Won't have any visible effect when Carbon's nickname management is disabled.</p>
*
* @param displayName the new display name
* @param nickname the new nickname
* @see #hasNickname()
* @see #nickname()
* @see #displayName()
* @see #hasCustomDisplayName()
* @since 2.0.0
* @since 2.1.0
*/
void displayName(@Nullable Component displayName);
void nickname(@Nullable Component nickname);

/**
* The player's UUID, often used for identification purposes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private BasicChatChannel(
return this.service.basicChatFormat(
recipient,
sender.uuid(),
CarbonPlayer.renderName(sender),
sender.displayName(),
sender.username(),
message
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public List<String> commandAliases() {
SourcedAudience.of(sender, recipient),
sender.uuid(),
this.key(),
Objects.requireNonNull(CarbonPlayer.renderName(sender)),
Objects.requireNonNull(sender.displayName()),
sender.username(),
message
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import com.google.inject.Inject;
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;
Expand Down Expand Up @@ -87,7 +86,7 @@ public void init() {
final String username;

if (handler.getSender() instanceof PlayerCommander player) {
senderName = CarbonPlayer.renderName(player.carbonPlayer());
senderName = player.carbonPlayer().displayName();
username = player.carbonPlayer().username();
} else {
senderName = Component.text("Console");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void init() {
final UUID whisperTarget = sender.lastWhisperTarget();

if (whisperTarget == null) {
this.carbonMessages.whisperTargetNotSet(sender, CarbonPlayer.renderName(sender));
this.carbonMessages.whisperTargetNotSet(sender, sender.displayName());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ public void init() {
}

if (target.hasPermission("carbon.ignore.exempt")) {
this.carbonMessages.ignoreExempt(sender, CarbonPlayer.renderName(target));
this.carbonMessages.ignoreExempt(sender, target.displayName());
return;
}

if (target.ignoring(target)) {
this.carbonMessages.alreadyIgnored(sender, CarbonPlayer.renderName(target));
this.carbonMessages.alreadyIgnored(sender, target.displayName());
return;
}

sender.ignoring(target, true);
this.carbonMessages.nowIgnoring(sender, CarbonPlayer.renderName(target));
this.carbonMessages.nowIgnoring(sender, target.displayName());
})
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public void init() {
this.carbonMessages.muteAlertRecipient(target);

if (!sender.equals(target)) {
this.carbonMessages.muteAlertPlayers(sender, CarbonPlayer.renderName(target));
this.carbonMessages.muteAlertPlayers(sender, target.displayName());
}

for (final var player : this.carbonChat.server().players()) {
if (player.equals(target) || player.equals(sender)) {
continue;
}

this.carbonMessages.muteAlertPlayers(player, CarbonPlayer.renderName(target));
this.carbonMessages.muteAlertPlayers(player, target.displayName());
}

target.muted(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public void init() {
if (sender.equals(target)) {
this.carbonMessages.muteInfoSelfNotMuted(sender);
} else {
this.carbonMessages.muteInfoNotMuted(sender, CarbonPlayer.renderName(target));
this.carbonMessages.muteInfoNotMuted(sender, target.displayName());
}
} else {
if (sender.equals(target)) {
this.carbonMessages.muteInfoSelfMuted(sender);
} else {
this.carbonMessages.muteInfoMuted(sender, CarbonPlayer.renderName(target), target.muted());
this.carbonMessages.muteInfoMuted(sender, target.displayName(), target.muted());
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void init() {
}

private void resetNickname(final Commander sender, final CarbonPlayer target) {
target.displayName(null);
target.nickname(null);

if (sender instanceof PlayerCommander playerCommander
&& playerCommander.carbonPlayer().uuid().equals(target.uuid())) {
Expand All @@ -127,7 +127,7 @@ 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));
target.displayName(parsedNick.get());
target.nickname(parsedNick.get());

if (sender instanceof PlayerCommander playerCommander
&& playerCommander.carbonPlayer().uuid().equals(target.uuid())) {
Expand All @@ -141,16 +141,16 @@ private void applyNickname(final Commander sender, final CarbonPlayer target, fi
}

private void checkOwnNickname(final CarbonPlayer sender) {
if (sender.displayName() != null) {
this.carbonMessages.nicknameShow(sender, sender.username(), sender.displayName());
if (sender.nickname() != null) {
this.carbonMessages.nicknameShow(sender, sender.username(), sender.nickname());
} else {
this.carbonMessages.nicknameShowUnset(sender, sender.username());
}
}

private void checkOthersNickname(final Audience sender, final CarbonPlayer target) {
if (target.displayName() != null) {
this.carbonMessages.nicknameShowOthers(sender, target.username(), target.displayName());
if (target.nickname() != null) {
this.carbonMessages.nicknameShowOthers(sender, target.username(), target.nickname());
} else {
this.carbonMessages.nicknameShowOthersUnset(sender, target.username());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void init() {
final @Nullable UUID replyTarget = sender.whisperReplyTarget();

if (replyTarget == null) {
this.carbonMessages.replyTargetNotSet(sender, CarbonPlayer.renderName(sender));
this.carbonMessages.replyTargetNotSet(sender, sender.displayName());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void init() {
}

sender.ignoring(target, false);
this.carbonMessages.noLongerIgnoring(sender, CarbonPlayer.renderName(target));
this.carbonMessages.noLongerIgnoring(sender, target.displayName());
})
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void init() {
this.carbonMessages.unmuteAlertRecipient(target);

if (!sender.equals(target)) {
this.carbonMessages.unmuteAlertPlayers(sender, CarbonPlayer.renderName(target));
this.carbonMessages.unmuteAlertPlayers(sender, target.displayName());
}

for (final var player : this.carbonChat.server().players()) {
Expand All @@ -109,7 +109,7 @@ public void init() {
continue;
}

this.carbonMessages.unmuteAlertPlayers(player, CarbonPlayer.renderName(target));
this.carbonMessages.unmuteAlertPlayers(player, target.displayName());
}

target.muted(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void whisper(
final @Nullable String recipientInputString
) {
if (sender.equals(recipient)) {
this.messages.whisperSelfError(sender, CarbonPlayer.renderName(sender));
this.messages.whisperSelfError(sender, sender.displayName());
return;
}

Expand All @@ -178,23 +178,23 @@ public void whisper(
final boolean localRecipient = recipient.online();

if (sender.ignoring(recipient)) {
this.messages.whisperIgnoringTarget(sender, CarbonPlayer.renderName(recipient));
this.messages.whisperIgnoringTarget(sender, recipient.displayName());
return;
}

if (recipient.ignoring(sender)) {
this.messages.whisperTargetIgnoring(sender, CarbonPlayer.renderName(recipient));
this.messages.whisperTargetIgnoring(sender, recipient.displayName());
return;
}

final Component senderName = CarbonPlayer.renderName(sender);
final Component recipientName = CarbonPlayer.renderName(recipient);
final Component senderName = sender.displayName();
final Component recipientName = recipient.displayName();

final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message));
this.events.emit(privateChatEvent);

if (privateChatEvent.cancelled()) {
this.messages.whisperError(sender, CarbonPlayer.renderName(sender), CarbonPlayer.renderName(recipient));
this.messages.whisperError(sender, sender.displayName(), recipient.displayName());
return;
}

Expand Down Expand Up @@ -229,8 +229,8 @@ public void handlePacket(final WhisperPacket packet) {
return;
}
this.userManager.user(packet.from()).thenAccept(sender -> {
final Component senderName = CarbonPlayer.renderName(sender);
final Component recipientName = CarbonPlayer.renderName(recipient);
final Component senderName = sender.displayName();
final Component recipientName = recipient.displayName();

recipient.whisperReplyTarget(sender.uuid());
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderName, recipientName, packet.message());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,18 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Optional;
import java.util.regex.Pattern;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.config.ConfigFactory;
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.serializer.plain.PlainTextComponentSerializer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;

import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.kyori.adventure.key.Key.key;
Expand Down Expand Up @@ -67,18 +63,7 @@ public PingHandler(final CarbonEventHandler events, final ConfigFactory configFa

public Component convertPings(final CarbonPlayer recipient, final Component message) {
final String prefix = this.configFactory.primaryConfig().pings().prefix();
final @Nullable Component displayComponent = recipient.displayName();
final String displayName;

if (displayComponent != null) {
displayName = PlainTextComponentSerializer.plainText().serialize(displayComponent);
} else {
final @NotNull Optional<Component> audienceNickname = recipient.get(Identity.DISPLAY_NAME);

displayName = audienceNickname
.map(component -> PlainTextComponentSerializer.plainText().serialize(component))
.orElse(recipient.username()); // Hacky workaround
}
final String plainDisplayName = PlainTextComponentSerializer.plainText().serialize(recipient.displayName());

return message.replaceText(TextReplacementConfig.builder()
// \B(@Username|@Displayname)\b
Expand All @@ -87,7 +72,7 @@ public Component convertPings(final CarbonPlayer recipient, final Component mess
"\\B%1$s(%2$s|%3$s)\\b",
Pattern.quote(prefix),
Pattern.quote(recipient.username()),
Pattern.quote(displayName)),
Pattern.quote(plainDisplayName)),
Pattern.CASE_INSENSITIVE))
.replacement(matchedText -> {
if (this.configFactory.primaryConfig().pings().playSound()) {
Expand Down
Loading

0 comments on commit 10b9862

Please sign in to comment.