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 3 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(final boolean ignoring);
Draycia marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sends the message as the player.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void init() {
return;
}

if (sender.ignoringDirectMessages()) {
this.carbonMessages.whisperIgnoringAll(sender);
return;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void init() {
return;
}

if (sender.ignoringDirectMessages()) {
this.carbonMessages.whisperIgnoringAll(sender);
return;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.ArgumentFactory;
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;
private final ArgumentFactory argumentFactory;
Draycia marked this conversation as resolved.
Show resolved Hide resolved

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

@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 @@ -102,6 +102,11 @@ public void init() {
return;
}

if (sender.ignoringDirectMessages()) {
this.carbonMessages.whisperIgnoringAll(sender);
return;
}

final String message = ctx.get("message");
final CarbonPlayer recipient = ctx.get("player");

Expand Down Expand Up @@ -187,6 +192,10 @@ public void whisper(
return;
}

if (recipient.ignoringDirectMessages() && !sender.hasPermission("carbon.togglemsg.exempt")) {
Draycia marked this conversation as resolved.
Show resolved Hide resolved
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,21 @@ 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.toggled.on")
void whispersToggledOn(final Audience audience);

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

/*
* =============================================================
* ========================= Nicknames =========================
Expand Down Expand Up @@ -343,6 +352,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 boolean ignoringdirectMessages = false;
Draycia marked this conversation as resolved.
Show resolved Hide resolved

// Administrative
protected final PersistentUserProperty<Boolean> spying;
Expand Down Expand Up @@ -262,6 +263,16 @@ public void spying(final boolean spying) {
this.spying.set(spying);
}

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

@Override
public void ignoringDirectMessages(final boolean ignoring) {
this.ignoringdirectMessages = 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
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private static Update bindPlayerArguments(final Update update, final CarbonPlaye
.bind("displayname", player.displayNameRaw())
.bind("lastwhispertarget", player.lastWhisperTarget())
.bind("whisperreplytarget", player.whisperReplyTarget())
.bind("spying", player.spying());
.bind("spying", player.spying())
.bind("ignoringdirectmessages", player.ignoringDirectMessages());
}

public static final class Factory {
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ command.nickname.others.reset.description=Removes any set nickname from the targ
command.reload.description=Reloads Carbon's config, channel settings, and translations. Will not load or unload any channels.
command.reply.argument.message=The message to reply with.
command.reply.description=Sends a message to the last player that messaged you.
command.togglemsg.description=Allows and disallows other players from mesaging you.
command.unignore.argument.player=The name of the player to unignore.
command.unignore.argument.uuid=The UUID of the player to unignore.
command.unignore.description=Stops hiding messages from the specified player.
Expand Down Expand Up @@ -99,7 +100,10 @@ whisper.error=<red>Failed to send private message
whisper.from=<gold>[<green><sender_display_name></green>] -> [<green>You</green>] <message>
whisper.ignored_by_target=<red><target> <red>is ignoring you
whisper.ignoring_target=<red>You are ignoring <target>
whisper.ignoring_all=<red>You cannot send messages while they are ignored!
whisper.to=<gold>[<green>You</green>] -> [<green><recipient_display_name></green>] <message>
whisper.toggled.on=Now receiving private messages.
whisper.toggled.off=No longer receiving private messages.
channel.radius.empty_recipients=<red>You're not close enough to anyone to send a message
channel.joined=<green>You have rejoined the channel</green>
channel.left=<red>You have left the channel</red>
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/resources/queries/insert-player.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ INSERT IGNORE INTO carbon_users SET
displayname = :displayname,
lastwhispertarget = :lastwhispertarget,
whisperreplytarget = :whisperreplytarget,
spying = :spying;
spying = :spying,
ignoringdirectmessages = :ignoringdirectmessages;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ CREATE TABLE carbon_users (
`displayname` VARCHAR(1024),
`lastwhispertarget` UUID,
`whisperreplytarget` UUID,
`spying` BOOLEAN
`spying` BOOLEAN,
`ignoringdirectmessages` BOOLEAN
);

CREATE TABLE carbon_ignores (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE carbon_users ADD COLUMN ignoringdirectmessages BOOLEAN;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE carbon_users ADD COLUMN ignoringdirectmessages BOOLEAN;
3 changes: 2 additions & 1 deletion common/src/main/resources/queries/select-player.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ SELECT
displayname,
lastwhispertarget,
whisperreplytarget,
spying
spying,
ignoringdirectmessages
FROM carbon_users WHERE (id = :id);
3 changes: 2 additions & 1 deletion common/src/main/resources/queries/update-player.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ UPDATE carbon_users SET
displayname = :displayname,
lastwhispertarget = :lastwhispertarget,
whisperreplytarget = :whisperreplytarget,
spying = :spying;
spying = :spying,
ignoringdirectmessages = :ignoringdirectmessages;
Draycia marked this conversation as resolved.
Show resolved Hide resolved