Add duration argument to mute cmd (#607)

* Add mute expiration and duration argument

* feat: require permission to receive mute alerts

* fix: Missing comma in update-player query
This commit is contained in:
Josua Parks authored and GitHub committed 2025-01-24 22:00:45 -08:00
1 parent 108561b5b9
commit 3cf502d963
15 files changed
+138 -2

No files matched your search

@@ -20,6 +20,8 @@
package net.draycia.carbon.common.command.commands;
import com.google.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
@@ -30,9 +32,12 @@ import net.draycia.carbon.common.command.ParserFactory;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.parser.standard.DurationParser;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.UUIDParser.uuidParser;
@@ -81,6 +86,11 @@ public final class MuteCommand extends CarbonCommand {
.withDescription(richDescription(this.carbonMessages.commandMuteArgumentUUID()))
.withComponent(uuidParser())
)
.flag(this.commandManager.flagBuilder("duration")
.withAliases("d")
.withDescription(richDescription(this.carbonMessages.commandMuteArgumentDuration()))
.withComponent(DurationParser.durationParser())
)
.permission("carbon.mute")
.senderType(Commander.class)
.commandDescription(richDescription(this.carbonMessages.commandMuteDescription()))
@@ -105,6 +115,12 @@ public final class MuteCommand extends CarbonCommand {
if (sender instanceof PlayerCommander playerCommander && playerCommander.carbonPlayer().equals(target)) {
this.carbonMessages.muteExempt(playerCommander);
return;
}
if (handler.flags().contains("duration")) {
this.handleTempMute(handler.flags().get("duration"), target);
return;
}
this.carbonMessages.muteAlertRecipient(target);
@@ -115,7 +131,9 @@ public final class MuteCommand extends CarbonCommand {
continue;
}
this.carbonMessages.muteAlertPlayers(player, target.displayName());
if (player.hasPermission("carbon.mute.alert")) {
this.carbonMessages.muteAlertPlayers(player, target.displayName());
}
}
target.muted(true);
@@ -125,4 +143,32 @@ public final class MuteCommand extends CarbonCommand {
this.commandManager.command(command);
}
private void handleTempMute(final Duration duration, final CarbonPlayer target) {
final @Nullable Component formattedDuration;
if (duration.toDaysPart() > 0) {
formattedDuration = this.carbonMessages.durationDays(duration.toDaysPart(), duration.toHoursPart(),
duration.toMinutesPart(), duration.toSecondsPart());
} else {
formattedDuration = this.carbonMessages.durationHours(duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart());
}
this.carbonMessages.tempMuteAlertRecipient(target, formattedDuration);
this.carbonMessages.tempMuteAlertRecipient(this.server.console(), formattedDuration);
for (final var player : this.server.players()) {
if (player.equals(target)) {
continue;
}
if (player.hasPermission("carbon.mute.alert")) {
this.carbonMessages.tempMuteAlertPlayers(player, target.displayName(), formattedDuration);
}
}
target.muted(true);
target.muteExpiration(Instant.now().plus(duration).toEpochMilli());
}
}
@@ -116,6 +116,18 @@ public interface CarbonMessages {
@Message("mute.spy.prefix")
Component muteSpyPrefix(final Audience audience);
@Message("mute.alert.target.temp")
void tempMuteAlertRecipient(final Audience audience, final Component duration);
@Message("mute.alert.players.temp")
void tempMuteAlertPlayers(final Audience audience, final Component target, final Component duration);
@Message("duration.hours")
Component durationHours(final int hours, final int minutes, final int seconds);
@Message("duration.days")
Component durationDays(final long days, final int hours, final int minutes, final int seconds);
/*
* =============================================================
* ====================== Direct Messages ======================
@@ -403,6 +415,9 @@ public interface CarbonMessages {
@Message("command.mute.argument.player")
Component commandMuteArgumentPlayer();
@Message("command.mute.argument.duration")
Component commandMuteArgumentDuration();
@Message("command.mute.argument.uuid")
Component commandMuteArgumentUUID();
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.users;
import com.google.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@@ -62,6 +63,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private volatile transient long transientLoadedSince = -1;
protected final PersistentUserProperty<Boolean> muted;
protected final PersistentUserProperty<Long> muteExpiration;
protected final PersistentUserProperty<Boolean> deafened;
protected final PersistentUserProperty<Key> selectedChannel;
@@ -90,6 +92,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
public CarbonPlayerCommon(
final boolean muted,
final long muteExpiration,
final boolean deafened,
final @Nullable Key selectedChannel,
final @Nullable String username, // will be resolved when requested
@@ -103,6 +106,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
final boolean applyOptionalChatFilters
) {
this.muted = PersistentUserProperty.of(muted);
this.muteExpiration = PersistentUserProperty.of(muteExpiration);
this.deafened = PersistentUserProperty.of(deafened);
this.selectedChannel = PersistentUserProperty.of(selectedChannel);
this.username = username;
@@ -123,6 +127,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
final UUID uuid
) {
this.muted = PersistentUserProperty.of(false);
this.muteExpiration = PersistentUserProperty.of(0L);
this.deafened = PersistentUserProperty.of(false);
this.selectedChannel = PersistentUserProperty.empty();
this.displayName = PersistentUserProperty.empty();
@@ -138,6 +143,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
public CarbonPlayerCommon() {
this.muted = PersistentUserProperty.of(false);
this.muteExpiration = PersistentUserProperty.of(0L);
this.deafened = PersistentUserProperty.of(false);
this.selectedChannel = PersistentUserProperty.empty();
this.displayName = PersistentUserProperty.empty();
@@ -156,6 +162,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private Stream<PersistentUserProperty<?>> properties() {
return Stream.of(
this.muted,
this.muteExpiration,
this.deafened,
this.selectedChannel,
this.displayName,
@@ -220,7 +227,15 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public boolean muted() {
return this.muted.get();
if (this.muted.get()) {
if (this.muteExpiration() > 0) {
return Instant.now().toEpochMilli() < this.muteExpiration();
}
return true;
}
return false;
}
@Override
@@ -228,6 +243,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.muted.set(muted);
}
@Override
public long muteExpiration() {
return this.muteExpiration.get();
}
@Override
public void muteExpiration(final long epochMillis) {
this.muteExpiration.set(epochMillis);
}
@Override
public Set<UUID> ignoring() {
return this.ignoredPlayers.get();
@@ -138,6 +138,16 @@ public class ConsoleCarbonPlayer implements CarbonPlayer, ForwardingAudience.Sin
}
@Override
public long muteExpiration() {
return 0;
}
@Override
public void muteExpiration(final long epochMillis) {
}
@Override
public Set<UUID> ignoring() {
return Collections.emptySet();
@@ -282,6 +282,16 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
this.carbonPlayerCommon.muted(muted);
}
@Override
public long muteExpiration() {
return this.carbonPlayerCommon.muteExpiration();
}
@Override
public void muteExpiration(final long epochMillis) {
this.carbonPlayerCommon.muteExpiration(epochMillis);
}
@Override
public boolean deafened() {
return this.carbonPlayerCommon.deafened();
@@ -258,6 +258,7 @@ public final class DatabaseUserManager extends CachingUserManager {
}
return update.bind("id", player.uuid())
.bind("muted", player.muted())
.bind("muteexpiration", player.muteExpiration())
.bind("deafened", player.deafened())
.bind("selectedchannel", player.selectedChannelKey())
.bind("displayname", nicknameJson)
@@ -39,6 +39,7 @@ public final class PlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
final ColumnMapper<UUID> uuid = ctx.findColumnMapperFor(UUID.class).orElseThrow();
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getLong("muteexpiration"),
rs.getBoolean("deafened"),
ctx.findColumnMapperFor(Key.class).orElseThrow().map(rs, "selectedchannel", ctx),
null,