mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
temporary display names part 1
This commit is contained in:
1 parent
9b876e2e00
commit
73c1f53e1b
7 files changed
+100
-84
No files matched your search
+64
-13
@@ -2,8 +2,16 @@ package net.draycia.carbon.common.command.commands;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.arguments.compound.FlagArgument;
|
||||
import cloud.commandframework.arguments.standard.StringArgument;
|
||||
import cloud.commandframework.permission.Permission;
|
||||
import com.google.inject.Inject;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.Period;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
@@ -11,6 +19,7 @@ import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument;
|
||||
import net.draycia.carbon.common.command.arguments.OptionValueParser;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
|
||||
public class NicknameCommand {
|
||||
|
||||
@@ -26,11 +35,11 @@ public class NicknameCommand {
|
||||
.build();
|
||||
|
||||
var command = commandManager.commandBuilder("nickname", "nick")
|
||||
//.flag(commandManager.flagBuilder("duration")
|
||||
// .withAliases("d")
|
||||
// .withArgument()
|
||||
// .withPermission(Permission.of("carbon.nickname.duration"))
|
||||
//)
|
||||
.flag(commandManager.flagBuilder("duration")
|
||||
.withAliases("d")
|
||||
.withArgument(StringArgument.optional("duration"))
|
||||
.withPermission(Permission.of("carbon.nickname.duration"))
|
||||
)
|
||||
.flag(commandManager.flagBuilder("player")
|
||||
.withAliases("p")
|
||||
.withArgument(carbonPlayerArgument.newInstance(true, "recipient"))
|
||||
@@ -46,28 +55,43 @@ public class NicknameCommand {
|
||||
.handler(handler -> {
|
||||
CarbonPlayer sender = ((PlayerCommander)handler.getSender()).carbonPlayer();
|
||||
long expirationTime = -1; // TODO: implement timed nicknames
|
||||
@MonotonicNonNull String durationFormat = null;
|
||||
|
||||
if (handler.flags().contains("duration")) {
|
||||
expirationTime = handler.flags().get("duration");
|
||||
durationFormat = handler.flags().get("duration");
|
||||
expirationTime = this.parsePeriod(durationFormat);
|
||||
}
|
||||
|
||||
// Setting nickname
|
||||
if (handler.flags().contains("nickname")) {
|
||||
final var nickname = MiniMessage.get().parse(handler.flags().get("nickname"));
|
||||
|
||||
final @MonotonicNonNull CarbonPlayer target = handler.flags().get("player");
|
||||
|
||||
// Setting other player's nickname
|
||||
if (handler.flags().contains("player")) {
|
||||
final CarbonPlayer target = handler.flags().get("player");
|
||||
target.displayName(nickname);
|
||||
messageService.nicknameSet(target, nickname);
|
||||
messageService.nicknameSetOthers(sender, target.username(), nickname);
|
||||
if (target != null && !target.equals(sender)) {
|
||||
if (expirationTime > -1) {
|
||||
target.temporaryDisplayName(nickname, expirationTime);
|
||||
messageService.temporaryNicknameSet(target, nickname, durationFormat);
|
||||
messageService.temporaryNicknameSetOthers(sender, target.username(), nickname, durationFormat);
|
||||
} else {
|
||||
target.displayName(nickname);
|
||||
messageService.nicknameSet(target, nickname);
|
||||
messageService.nicknameSetOthers(sender, target.username(), nickname);
|
||||
}
|
||||
} else {
|
||||
// Setting own nickname
|
||||
sender.displayName(nickname);
|
||||
messageService.nicknameSet(sender, nickname);
|
||||
if (expirationTime > -1) {
|
||||
sender.temporaryDisplayName(nickname, expirationTime);
|
||||
messageService.temporaryNicknameSet(sender, nickname, durationFormat);
|
||||
} else {
|
||||
sender.displayName(nickname);
|
||||
messageService.nicknameSet(sender, nickname);
|
||||
}
|
||||
}
|
||||
} else if (handler.flags().contains("player")) {
|
||||
// Checking other player's nickname
|
||||
// TODO: show temporary display name durations, create inverse parsePeriod method
|
||||
final CarbonPlayer target = handler.flags().get("player");
|
||||
|
||||
if (target.displayName() != null) {
|
||||
@@ -89,4 +113,31 @@ public class NicknameCommand {
|
||||
commandManager.command(command);
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/56395975
|
||||
private final Pattern periodPattern = Pattern.compile("([0-9]+)([smhdWMY])");
|
||||
|
||||
private Long parsePeriod(@MonotonicNonNull String period) {
|
||||
period = Objects.requireNonNull(period).toLowerCase(Locale.ENGLISH);
|
||||
|
||||
final Matcher matcher = periodPattern.matcher(period);
|
||||
Instant instant = Instant.now();
|
||||
|
||||
while (matcher.find()) {
|
||||
final int num = Integer.parseInt(matcher.group(1));
|
||||
final String typ = matcher.group(2);
|
||||
|
||||
switch (typ) {
|
||||
case "s" -> instant = instant.plus(Duration.ofSeconds(num));
|
||||
case "m" -> instant = instant.plus(Duration.ofMinutes(num));
|
||||
case "h" -> instant = instant.plus(Duration.ofHours(num));
|
||||
case "d" -> instant = instant.plus(Duration.ofDays(num));
|
||||
case "W" -> instant = instant.plus(Period.ofWeeks(num));
|
||||
case "M" -> instant = instant.plus(Period.ofMonths(num));
|
||||
case "Y" -> instant = instant.plus(Period.ofYears(num));
|
||||
}
|
||||
}
|
||||
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -94,6 +94,13 @@ public interface CarbonMessageService {
|
||||
@Placeholder Component nickname
|
||||
);
|
||||
|
||||
@Message("nickname.set.temporary")
|
||||
void temporaryNicknameSet(
|
||||
final Audience audience,
|
||||
@Placeholder Component nickname,
|
||||
@Placeholder String duration
|
||||
);
|
||||
|
||||
@Message("nickname.set.others")
|
||||
void nicknameSetOthers(
|
||||
final Audience audience,
|
||||
@@ -101,6 +108,14 @@ public interface CarbonMessageService {
|
||||
@Placeholder Component nickname
|
||||
);
|
||||
|
||||
@Message("nickname.set.others.temporary")
|
||||
void temporaryNicknameSetOthers(
|
||||
final Audience audience,
|
||||
@Placeholder String target,
|
||||
@Placeholder Component nickname,
|
||||
@Placeholder String duration
|
||||
);
|
||||
|
||||
@Message("nickname.show.others")
|
||||
void nicknameShowOthers(
|
||||
final Audience audience,
|
||||
|
||||
@@ -20,14 +20,15 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
|
||||
protected boolean deafened = false;
|
||||
protected @Nullable Component displayName;
|
||||
protected transient @Nullable Component temporaryDisplayName; // Don't persist temp names
|
||||
protected @Nullable UUID lastWhisperTarget = null;
|
||||
protected boolean muted = false;
|
||||
protected @Nullable ChatChannel selectedChannel;
|
||||
protected boolean spying = false;
|
||||
protected @Nullable Component temporaryDisplayName = null;
|
||||
protected long temporaryDisplayNameExpiration = -1;
|
||||
protected @MonotonicNonNull String username;
|
||||
protected @MonotonicNonNull UUID uuid;
|
||||
protected @Nullable UUID whisperReplyTarget = null;
|
||||
protected @Nullable UUID lastWhisperTarget = null;
|
||||
|
||||
public CarbonPlayerCommon(
|
||||
final @Nullable Component displayName,
|
||||
@@ -61,13 +62,14 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
|
||||
@Override
|
||||
public @Nullable Component displayName() {
|
||||
// TODO: redesign this "temporary display name" feature. idk which value should override which.
|
||||
if (this.displayName != null) {
|
||||
return this.displayName;
|
||||
} else if (this.temporaryDisplayName != null) {
|
||||
if (this.temporaryDisplayName != null && System.currentTimeMillis() < this.temporaryDisplayNameExpiration) {
|
||||
return this.temporaryDisplayName;
|
||||
}
|
||||
|
||||
if (this.displayName != null) {
|
||||
return this.displayName;
|
||||
} else
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -77,9 +79,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void temporaryDisplayName(final @Nullable Component temporaryDisplayName) {
|
||||
// TODO: support durations (expires after X time)
|
||||
public void temporaryDisplayName(final @Nullable Component temporaryDisplayName, final long expirationEpoch) {
|
||||
// TODO: see why these aren't persisting
|
||||
this.temporaryDisplayName = temporaryDisplayName;
|
||||
this.temporaryDisplayNameExpiration = expirationEpoch;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,3 +17,5 @@ nickname.show.others=<target><green>'s nickname is </green><nickname><green>.</g
|
||||
nickname.show.others.unset=<target><red> does not have a nickname set.</red>
|
||||
nickname.show=<green>Your nickname is </green><nickname><green>.</green>
|
||||
nickname.show.unset=<red>You do not have a nickname set.
|
||||
nickname.set.temporary=<green>Your nickname has been set to </green><nickname><green> for </green><gold><duration></gold><green>.</green>
|
||||
nickname.set.others.temporary=<green>You set </green><target><green>'s nickname to </green><nickname><green> for </green><gold><duration></gold><green>.</green>
|
||||
Reference in new issue
Block a user