diff --git a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java index f126fed0..8d20d2aa 100644 --- a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java +++ b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java @@ -212,6 +212,14 @@ public interface CarbonPlayer extends Audience, Identified { */ boolean muted(); + /** + * The time the mute will expire, in epoch millis. + * + * @return the mute expiration time + * @since 3.0.0 + */ + long muteExpiration(); + /** * Mutes and unmutes the player. * @@ -220,6 +228,14 @@ public interface CarbonPlayer extends Audience, Identified { */ void muted(boolean muted); + /** + * Sets the epoch time the player's mute will expire. + * + * @param epochMillis the expiration time + * @since 3.0.0 + */ + void muteExpiration(long epochMillis); + /** * Gets the ids of the players this player is currently ignoring. * diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java index d0b79e43..f86b6096 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java @@ -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()); + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 265ee864..c056f085 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -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(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index 1fe89e9d..ba1dae68 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -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 muted; + protected final PersistentUserProperty muteExpiration; protected final PersistentUserProperty deafened; protected final PersistentUserProperty 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> 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 ignoring() { return this.ignoredPlayers.get(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java index c4841726..c30f71af 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java @@ -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 ignoring() { return Collections.emptySet(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java index d6ce20ee..ba358e9f 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java @@ -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(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java index b9a4f933..6e70e11b 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java @@ -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) diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/mapper/PlayerRowMapper.java b/common/src/main/java/net/draycia/carbon/common/users/db/mapper/PlayerRowMapper.java index d6ced458..3afd329c 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/mapper/PlayerRowMapper.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/mapper/PlayerRowMapper.java @@ -39,6 +39,7 @@ public final class PlayerRowMapper implements RowMapper { final ColumnMapper 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, diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 93004820..2a74c7fe 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -33,6 +33,7 @@ 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. command.mute.argument.uuid=The UUID of the player to mute. +command.mute.argument.duration=The duration the player will be muted for. command.mute.description=Mutes players, preventing them from using chat or whispering other players. command.muteinfo.argument.player=The name of the player. command.muteinfo.argument.uuid=The UUID of the player. @@ -92,6 +93,8 @@ command.party.disband.description=Disband your current party. command.spy.enabled=Spying is now enabled. command.spy.disabled=Spying is now disabled. command.spy.description=Allows a player to view all private and channel messages they otherwise wouldn't see. +duration.days=dhms +duration.hours=hms party.player_joined= joined your party. party.player_left= left your party. party.cannot_use_channel=You must join a party to use this channel. @@ -112,7 +115,9 @@ ignore.invalid_target=No target found ignore.now_ignoring=You are now ignoring ignore.no_longer_ignoring=You are no longer ignoring mute.alert.players= has been muted +mute.alert.players.temp= has been muted for mute.alert.target=You have been muted +mute.alert.target.temp=You have been muted for mute.cannot_speak=You cannot speak when muted mute.exempt=That player is exempt from being muted mute.info.muted= is muted diff --git a/common/src/main/resources/queries/insert-player.sql b/common/src/main/resources/queries/insert-player.sql index 64ef9761..50876c42 100644 --- a/common/src/main/resources/queries/insert-player.sql +++ b/common/src/main/resources/queries/insert-player.sql @@ -1,6 +1,7 @@ INSERT{!PSQL: IGNORE} INTO carbon_users( id, muted, + muteexpiration, deafened, selectedchannel, displayname, @@ -13,6 +14,7 @@ INSERT{!PSQL: IGNORE} INTO carbon_users( ) VALUES ( :id, :muted, + :muteexpiration, :deafened, :selectedchannel, :displayname, diff --git a/common/src/main/resources/queries/migrations/h2/V5__tempmute.sql b/common/src/main/resources/queries/migrations/h2/V5__tempmute.sql new file mode 100644 index 00000000..9daa9402 --- /dev/null +++ b/common/src/main/resources/queries/migrations/h2/V5__tempmute.sql @@ -0,0 +1 @@ +ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted; diff --git a/common/src/main/resources/queries/migrations/mysql/V9__tempmute.sql b/common/src/main/resources/queries/migrations/mysql/V9__tempmute.sql new file mode 100644 index 00000000..9daa9402 --- /dev/null +++ b/common/src/main/resources/queries/migrations/mysql/V9__tempmute.sql @@ -0,0 +1 @@ +ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted; diff --git a/common/src/main/resources/queries/migrations/postgresql/V9__tempmute.sql b/common/src/main/resources/queries/migrations/postgresql/V9__tempmute.sql new file mode 100644 index 00000000..9daa9402 --- /dev/null +++ b/common/src/main/resources/queries/migrations/postgresql/V9__tempmute.sql @@ -0,0 +1 @@ +ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted; diff --git a/common/src/main/resources/queries/select-player.sql b/common/src/main/resources/queries/select-player.sql index 786ea963..4e0e9058 100644 --- a/common/src/main/resources/queries/select-player.sql +++ b/common/src/main/resources/queries/select-player.sql @@ -1,6 +1,7 @@ SELECT id, muted, + muteexpiration, deafened, selectedchannel, displayname, diff --git a/common/src/main/resources/queries/update-player.sql b/common/src/main/resources/queries/update-player.sql index 53704cb7..48328a2a 100644 --- a/common/src/main/resources/queries/update-player.sql +++ b/common/src/main/resources/queries/update-player.sql @@ -1,5 +1,6 @@ UPDATE carbon_users SET muted = :muted, + muteexpiration = :muteexpiration, deafened = :deafened, selectedchannel = :selectedchannel, displayname = :displayname,