Merge pull request #281 from Hexaoxide/feat/whispertoggle

This commit is contained in:
Josua Parks authored and GitHub committed 2023-08-25 20:33:48 -07:00
commit 517131726c
19 files changed
+221 -24

No files matched your search

@@ -284,6 +284,22 @@ public interface CarbonPlayer extends Audience, Identified {
*/
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.
*
@@ -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;
}
@@ -73,15 +73,15 @@ public final class ContinueCommand extends CarbonCommand {
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;
}
@@ -89,7 +89,7 @@ public final class ContinueCommand extends CarbonCommand {
final @Nullable UUID whisperTarget = sender.lastWhisperTarget();
if (whisperTarget == null) {
this.carbonMessages.whisperTargetNotSet(sender, sender.displayName());
this.messages.whisperTargetNotSet(sender, sender.displayName());
return;
}
@@ -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;
}
@@ -73,15 +73,15 @@ public final class ReplyCommand extends CarbonCommand {
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;
}
@@ -89,7 +89,7 @@ public final class ReplyCommand extends CarbonCommand {
final @Nullable UUID replyTarget = sender.whisperReplyTarget();
if (replyTarget == null) {
this.carbonMessages.replyTargetNotSet(sender, sender.displayName());
this.messages.replyTargetNotSet(sender, sender.displayName());
return;
}
@@ -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);
}
}
@@ -166,6 +166,11 @@ public final class WhisperCommand extends CarbonCommand {
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,
@@ -187,6 +192,11 @@ public final class WhisperCommand extends CarbonCommand {
return;
}
if (recipient.ignoringDirectMessages() && !sender.hasPermission("carbon.togglemsg.exempt")) {
this.messages.whisperTargetIgnoringDMs(sender, recipient.displayName());
return;
}
final Component senderName = sender.displayName();
final Component recipientName = recipient.displayName();
@@ -150,12 +150,24 @@ public interface CarbonMessages {
@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 =========================
@@ -343,6 +355,9 @@ public interface CarbonMessages {
@Message("command.reply.description")
Component commandReplyDescription();
@Message("command.togglemsg.description")
Component commandToggleMsgDescription();
@Message("command.unignore.argument.player")
Component commandUnignoreArgumentPlayer();
@@ -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;
@@ -88,7 +89,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
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);
@@ -101,6 +103,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.spying = PersistentUserProperty.of(spying);
this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet());
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.ignoringDirectMessages = PersistentUserProperty.of(ignoreDirectMessages);
}
public CarbonPlayerCommon(
@@ -116,6 +119,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.username = username;
this.uuid = uuid;
this.ignoringDirectMessages = PersistentUserProperty.of(false);
}
public CarbonPlayerCommon() {
@@ -126,6 +130,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
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() {
@@ -140,7 +145,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.displayName,
this.spying,
this.ignoredPlayers,
this.leftChannels
this.leftChannels,
this.ignoringDirectMessages
);
}
@@ -262,6 +268,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
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) {
@@ -174,6 +174,16 @@ public class ConsoleCarbonPlayer implements CarbonPlayer, ForwardingAudience.Sin
}
@Override
public boolean ignoringDirectMessages() {
return false;
}
@Override
public void ignoringDirectMessages(final boolean ignoring) {
}
@Override
public void sendMessageAsPlayer(final String message) {
@@ -161,6 +161,16 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
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();
@@ -170,7 +170,8 @@ public final class DatabaseUserManager extends CachingUserManager {
.bind("displayname", player.displayNameRaw())
.bind("lastwhispertarget", player.lastWhisperTarget())
.bind("whisperreplytarget", player.whisperReplyTarget())
.bind("spying", player.spying());
.bind("spying", player.spying())
.bind("ignoringdms", player.ignoringDirectMessages());
}
public static final class Factory {
@@ -43,7 +43,8 @@ public final class PlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
ctx.findColumnMapperFor(Component.class).orElseThrow().map(rs, "displayname", ctx),
uuid.map(rs, "lastwhispertarget", ctx),
uuid.map(rs, "whisperreplytarget", ctx),
rs.getBoolean("spying")
rs.getBoolean("spying"),
rs.getBoolean("ignoringdms")
);
}
@@ -52,6 +52,7 @@ import net.draycia.carbon.common.command.commands.MuteInfoCommand;
import net.draycia.carbon.common.command.commands.NicknameCommand;
import net.draycia.carbon.common.command.commands.ReloadCommand;
import net.draycia.carbon.common.command.commands.ReplyCommand;
import net.draycia.carbon.common.command.commands.ToggleMessagesCommand;
import net.draycia.carbon.common.command.commands.UnignoreCommand;
import net.draycia.carbon.common.command.commands.UnmuteCommand;
import net.draycia.carbon.common.command.commands.UpdateUsernameCommand;
@@ -74,8 +75,8 @@ public final class CloudUtils {
public static final List<Class<? extends CarbonCommand>> COMMAND_CLASSES = List.of(ClearChatCommand.class,
ContinueCommand.class, DebugCommand.class, HelpCommand.class, IgnoreCommand.class, MuteCommand.class,
MuteInfoCommand.class, NicknameCommand.class, ReloadCommand.class, ReplyCommand.class, UnignoreCommand.class,
UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class, JoinCommand.class, LeaveCommand.class);
MuteInfoCommand.class, NicknameCommand.class, ReloadCommand.class, ReplyCommand.class, ToggleMessagesCommand.class,
UnignoreCommand.class, UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class, JoinCommand.class, LeaveCommand.class);
private static final List<CarbonCommand> CONSTRUCTED_COMMANDS = new ArrayList<>();
@@ -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.
@@ -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>
@@ -6,4 +6,5 @@ INSERT IGNORE INTO carbon_users SET
displayname = :displayname,
lastwhispertarget = :lastwhispertarget,
whisperreplytarget = :whisperreplytarget,
spying = :spying;
spying = :spying,
ignoringdms = :ignoringdms;
@@ -6,7 +6,8 @@ CREATE TABLE carbon_users (
`displayname` VARCHAR(1024),
`lastwhispertarget` UUID,
`whisperreplytarget` UUID,
`spying` BOOLEAN
`spying` BOOLEAN,
`ignoringdms` BOOLEAN
);
CREATE TABLE carbon_ignores (
@@ -0,0 +1 @@
ALTER TABLE carbon_users ADD COLUMN ignoringdms BOOLEAN;
@@ -0,0 +1 @@
ALTER TABLE carbon_users ADD COLUMN ignoringdms BOOLEAN;
@@ -6,5 +6,6 @@ SELECT
displayname,
lastwhispertarget,
whisperreplytarget,
spying
spying,
ignoringdms
FROM carbon_users WHERE (id = :id);
@@ -6,4 +6,5 @@ UPDATE carbon_users SET
displayname = :displayname,
lastwhispertarget = :lastwhispertarget,
whisperreplytarget = :whisperreplytarget,
spying = :spying;
spying = :spying,
ignoringdms = :ignoringdms;