Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to hide incoming private messages #281

Merged
merged 8 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ record ChannelMessage(Component message, ChatChannel channel) {}
*/
void spying(boolean spying);

/**
* Controls if the player should receive direct messages or if they should be hidden.
*
* @return if the player is ignoring direct messages
* @since 2.1.0
*/
boolean ignoringDirectMessages();

/**
* Sets whether the player should receive direct messages or if they should be hidden.
*
* @param ignoring if the player is ignoring direct messages
* @since 2.1.0
*/
void ignoringDirectMessages(boolean ignoring);

/**
* Sends the message as the player.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public final class ContinueCommand extends CarbonCommand {

private final UserManager<?> users;
private final CommandManager<Commander> commandManager;
private final CarbonMessages carbonMessages;
private final CarbonMessages messages;
private final WhisperCommand.WhisperHandler whisper;

@Inject
public ContinueCommand(
final UserManager<?> userManager,
final CommandManager<Commander> commandManager,
final CarbonMessages carbonMessages,
final CarbonMessages messages,
final WhisperCommand.WhisperHandler whisper
) {
this.users = userManager;
this.commandManager = commandManager;
this.carbonMessages = carbonMessages;
this.messages = messages;
this.whisper = whisper;
}

Expand All @@ -73,23 +73,23 @@ public Key key() {
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.argument(StringArgument.greedy("message"),
RichDescription.of(this.carbonMessages.commandContinueArgumentMessage()))
RichDescription.of(this.messages.commandContinueArgumentMessage()))
.permission("carbon.whisper.continue")
.senderType(PlayerCommander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandContinueDescription())
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.messages.commandContinueDescription())
.handler(ctx -> {
final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer();

if (sender.muted()) {
this.carbonMessages.muteCannotSpeak(sender);
this.messages.muteCannotSpeak(sender);
return;
}

final String message = ctx.get("message");
final @Nullable UUID whisperTarget = sender.lastWhisperTarget();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public final class ReplyCommand extends CarbonCommand {

private final UserManager<?> users;
private final CommandManager<Commander> commandManager;
private final CarbonMessages carbonMessages;
private final CarbonMessages messages;
private final WhisperCommand.WhisperHandler whisper;

@Inject
public ReplyCommand(
final UserManager<?> userManager,
final CommandManager<Commander> commandManager,
final CarbonMessages carbonMessages,
final CarbonMessages messages,
final WhisperCommand.WhisperHandler whisper
) {
this.users = userManager;
this.commandManager = commandManager;
this.carbonMessages = carbonMessages;
this.messages = messages;
this.whisper = whisper;
}

Expand All @@ -73,23 +73,23 @@ public Key key() {
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.argument(StringArgument.greedy("message"),
RichDescription.of(this.carbonMessages.commandReplyArgumentMessage()))
RichDescription.of(this.messages.commandReplyArgumentMessage()))
.permission("carbon.whisper.reply")
.senderType(PlayerCommander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandReplyDescription())
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.messages.commandReplyDescription())
.handler(ctx -> {
final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer();

if (sender.muted()) {
this.carbonMessages.muteCannotSpeak(sender);
this.messages.muteCannotSpeak(sender);
return;
}

final String message = ctx.get("message");
final @Nullable UUID replyTarget = sender.whisperReplyTarget();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.command.commands;

import cloud.commandframework.CommandManager;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import com.google.inject.Inject;
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;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.key.Key;

public class ToggleMessagesCommand extends CarbonCommand {

private final CommandManager<Commander> commandManager;
private final CarbonMessages carbonMessages;

@Inject
public ToggleMessagesCommand(
final CommandManager<Commander> commandManager,
final CarbonMessages carbonMessages
) {
this.commandManager = commandManager;
this.carbonMessages = carbonMessages;
}

@Override
protected CommandSettings _commandSettings() {
return new CommandSettings("togglemsg", "togglepm");
}

@Override
public Key key() {
return Key.key("carbon", "togglemsg");
}

@Override
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.permission("carbon.togglemsg")
.senderType(PlayerCommander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandToggleMsgDescription())
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
final boolean nowIgnoring = !sender.ignoringDirectMessages();
sender.ignoringDirectMessages(nowIgnoring);

if (nowIgnoring) {
this.carbonMessages.whispersToggledOff(sender);
} else {
this.carbonMessages.whispersToggledOn(sender);
}
})
.build();

this.commandManager.command(command);

final var toggleOn = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.permission("carbon.togglemsg")
.literal("on", "allow")
.senderType(PlayerCommander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandToggleMsgDescription())
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
sender.ignoringDirectMessages(false);
this.carbonMessages.whispersToggledOn(sender);
})
.build();

this.commandManager.command(toggleOn);

final var toggleOff = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.permission("carbon.togglemsg")
.literal("off", "ignore")
.senderType(PlayerCommander.class)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandToggleMsgDescription())
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
sender.ignoringDirectMessages(true);
this.carbonMessages.whispersToggledOff(sender);
})
.build();

this.commandManager.command(toggleOff);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public void whisper(
return;
}

if (sender.ignoringDirectMessages() && !sender.hasPermission("carbon.togglemsg.exempt")) {
this.messages.whisperIgnoringAll(sender);
return;
}

if (!this.network.online(recipient) || !sender.awareOf(recipient) && !sender.hasPermission("carbon.whisper.vanished")) {
final var exception = new CarbonPlayerArgument.ParseException(
recipientInputString == null ? recipient.username() : recipientInputString,
Expand All @@ -187,6 +192,11 @@ public void whisper(
return;
}

if (recipient.ignoringDirectMessages() && !sender.hasPermission("carbon.togglemsg.exempt")) {
Draycia marked this conversation as resolved.
Show resolved Hide resolved
this.messages.whisperTargetIgnoringDMs(sender, recipient.displayName());
return;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,24 @@ void whisperTargetNotSet(
@Placeholder("sender_display_name") final Component senderDisplayName
);

@Message("whisper.ignoring_all")
void whisperIgnoringAll(final Audience audience);

@Message("whisper.ignoring_target")
void whisperIgnoringTarget(final Audience audience, final Component target);

@Message("whisper.ignored_by_target")
void whisperTargetIgnoring(final Audience audience, final Component target);

@Message("whisper.ignored_dms")
void whisperTargetIgnoringDMs(final Audience audience, final Component target);

@Message("whisper.toggled.on")
void whispersToggledOn(final Audience audience);

@Message("whisper.toggled.off")
void whispersToggledOff(final Audience audience);

/*
* =============================================================
* ========================= Nicknames =========================
Expand Down Expand Up @@ -343,6 +355,9 @@ void errorCommandCommandExecution(
@Message("command.reply.description")
Component commandReplyDescription();

@Message("command.togglemsg.description")
Component commandToggleMsgDescription();

@Message("command.unignore.argument.player")
Component commandUnignoreArgumentPlayer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
// Whispers
protected transient @Nullable UUID lastWhisperTarget = null;
protected transient @Nullable UUID whisperReplyTarget = null;
protected final PersistentUserProperty<Boolean> ignoringDirectMessages;

// Administrative
protected final PersistentUserProperty<Boolean> spying;
Expand All @@ -88,7 +89,8 @@ public CarbonPlayerCommon(
final @Nullable Component displayName,
final @Nullable UUID lastWhisperTarget,
final @Nullable UUID whisperReplyTarget,
final boolean spying
final boolean spying,
final boolean ignoreDirectMessages
) {
this.muted = PersistentUserProperty.of(muted);
this.deafened = PersistentUserProperty.of(deafened);
Expand All @@ -101,6 +103,7 @@ public CarbonPlayerCommon(
this.spying = PersistentUserProperty.of(spying);
this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet());
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.ignoringDirectMessages = PersistentUserProperty.of(ignoreDirectMessages);
}

public CarbonPlayerCommon(
Expand All @@ -116,6 +119,7 @@ public CarbonPlayerCommon(
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.username = username;
this.uuid = uuid;
this.ignoringDirectMessages = PersistentUserProperty.of(false);
}

public CarbonPlayerCommon() {
Expand All @@ -126,6 +130,7 @@ public CarbonPlayerCommon() {
this.spying = PersistentUserProperty.of(false);
this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet());
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.ignoringDirectMessages = PersistentUserProperty.of(false);
}

public boolean needsSave() {
Expand All @@ -140,7 +145,8 @@ private Stream<PersistentUserProperty<?>> properties() {
this.displayName,
this.spying,
this.ignoredPlayers,
this.leftChannels
this.leftChannels,
this.ignoringDirectMessages
);
}

Expand Down Expand Up @@ -262,6 +268,16 @@ public void spying(final boolean spying) {
this.spying.set(spying);
}

@Override
public boolean ignoringDirectMessages() {
return this.ignoringDirectMessages.get();
}

@Override
public void ignoringDirectMessages(final boolean ignoring) {
this.ignoringDirectMessages.set(ignoring);
}

@Override
public void sendMessageAsPlayer(final String message) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ public void spying(final boolean spying) {

}

@Override
public boolean ignoringDirectMessages() {
return false;
}

@Override
public void ignoringDirectMessages(final boolean ignoring) {

}

@Override
public void sendMessageAsPlayer(final String message) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public void ignoring(final CarbonPlayer player, final boolean nowIgnoring) {
this.carbonPlayerCommon.ignoring(player, nowIgnoring);
}

@Override
public boolean ignoringDirectMessages() {
return this.carbonPlayerCommon.ignoringDirectMessages();
}

@Override
public void ignoringDirectMessages(final boolean ignoring) {
this.carbonPlayerCommon.ignoringDirectMessages(ignoring);
}

@Override
public boolean hasPermission(final String permission) {
final @Nullable User user = this.user();
Expand Down