mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
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:
1 parent
108561b5b9
commit
3cf502d963
15 files changed
+138
-2
No files matched your search
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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=<green>Spying is now enabled.
|
||||
command.spy.disabled=<red>Spying is now disabled.
|
||||
command.spy.description=Allows a player to view all private and channel messages they otherwise wouldn't see.
|
||||
duration.days=<days>d<hours>h<minutes>m<seconds>s
|
||||
duration.hours=<hours>h<minutes>m<seconds>s
|
||||
party.player_joined=<display_name><green> joined your party.
|
||||
party.player_left=<display_name><green> left your party.
|
||||
party.cannot_use_channel=<red>You must join a party to use this channel.
|
||||
@@ -112,7 +115,9 @@ ignore.invalid_target=<red>No target found
|
||||
ignore.now_ignoring=<green>You are now ignoring <target>
|
||||
ignore.no_longer_ignoring=<green>You are no longer ignoring <target>
|
||||
mute.alert.players=<red><target> <red>has been muted
|
||||
mute.alert.players.temp=<red><target> <red>has been muted for <duration>
|
||||
mute.alert.target=<red>You have been muted
|
||||
mute.alert.target.temp=<red>You have been muted for <duration>
|
||||
mute.cannot_speak=<red>You cannot speak when muted
|
||||
mute.exempt=<red>That player is exempt from being muted
|
||||
mute.info.muted=<red><target> <red>is muted
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE carbon_users ADD COLUMN muteexpiration BOOLEAN AFTER muted;
|
||||
@@ -1,6 +1,7 @@
|
||||
SELECT
|
||||
id,
|
||||
muted,
|
||||
muteexpiration,
|
||||
deafened,
|
||||
selectedchannel,
|
||||
displayname,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
UPDATE carbon_users SET
|
||||
muted = :muted,
|
||||
muteexpiration = :muteexpiration,
|
||||
deafened = :deafened,
|
||||
selectedchannel = :selectedchannel,
|
||||
displayname = :displayname,
|
||||
|
||||
Reference in new issue
Block a user