diff --git a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java
new file mode 100644
index 00000000..09242b4a
--- /dev/null
+++ b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java
@@ -0,0 +1,110 @@
+/*
+ * 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 .
+ */
+package net.draycia.carbon.api.event.events;
+
+import com.seiama.event.Cancellable;
+import java.util.Objects;
+import net.draycia.carbon.api.event.CarbonEvent;
+import net.draycia.carbon.api.users.CarbonPlayer;
+import net.kyori.adventure.text.Component;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
+
+/**
+ * Called whenever a player privately messages another player.
+ *
+ * @since 2.1.0
+ */
+@DefaultQualifier(NonNull.class)
+public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable {
+
+ private final CarbonPlayer sender;
+ private final CarbonPlayer recipient;
+
+ private Component message;
+
+ private boolean cancelled = false;
+
+ /**
+ * Called whenever a player privately messages another player.
+ *
+ * @param sender the message sender
+ * @param recipient the message recipient
+ * @param message the message
+ * @since 2.1.0
+ */
+ public CarbonPrivateChatEvent(final CarbonPlayer sender, final CarbonPlayer recipient, final Component message) {
+ this.sender = sender;
+ this.recipient = recipient;
+ this.message = message;
+ }
+
+ /**
+ * Sets the message that will be sent.
+ *
+ * @param message the new message
+ * @throws NullPointerException if message is null
+ * @since 2.1.0
+ */
+ public void message(final Component message) {
+ this.message = Objects.requireNonNull(message);
+ }
+
+ /**
+ * The message that will be sent.
+ *
+ * @return the message
+ * @since 2.1.0
+ */
+ public Component message() {
+ return this.message;
+ }
+
+ /**
+ * The message sender.
+ *
+ * @return the sender of the message
+ * @since 2.1.0
+ */
+ public CarbonPlayer sender() {
+ return this.sender;
+ }
+
+ /**
+ * The message recipient.
+ *
+ * @return the recipient of the message
+ * @since 2.1.0
+ */
+ public CarbonPlayer recipient() {
+ return this.recipient;
+ }
+
+ @Override
+ public boolean cancelled() {
+ return this.cancelled;
+ }
+
+ @Override
+ public void cancelled(final boolean cancelled) {
+ this.cancelled = cancelled;
+ }
+
+}
diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java
index 1caf7aeb..1b845020 100644
--- a/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java
+++ b/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java
@@ -26,6 +26,7 @@ import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
+import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -128,9 +129,17 @@ public class ContinueCommand extends CarbonCommand {
final Component senderName = CarbonPlayer.renderName(sender);
final Component recipientName = CarbonPlayer.renderName(recipient);
- this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, message);
- this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, message);
- this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, message);
+ final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message));
+ this.carbonChat.eventHandler().emit(privateChatEvent);
+
+ if (privateChatEvent.cancelled()) {
+ this.carbonMessages.whisperError(sender, CarbonPlayer.renderName(sender), CarbonPlayer.renderName(recipient));
+ return;
+ }
+
+ this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, privateChatEvent.message());
final @Nullable Sound messageSound = this.configFactory.primaryConfig().messageSound();
if (messageSound != null) {
diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java
index e8a8c978..fb092409 100644
--- a/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java
+++ b/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java
@@ -26,6 +26,7 @@ import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
+import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -127,9 +128,17 @@ public class ReplyCommand extends CarbonCommand {
final Component senderName = CarbonPlayer.renderName(sender);
final Component recipientName = CarbonPlayer.renderName(recipient);
- this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, message);
- this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, message);
- this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, message);
+ final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message));
+ this.carbonChat.eventHandler().emit(privateChatEvent);
+
+ if (privateChatEvent.cancelled()) {
+ this.carbonMessages.whisperError(sender, CarbonPlayer.renderName(sender), CarbonPlayer.renderName(recipient));
+ return;
+ }
+
+ this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, privateChatEvent.message());
final @Nullable Sound messageSound = this.configFactory.primaryConfig().messageSound();
if (messageSound != null) {
diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java
index 02bc55d5..29160914 100644
--- a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java
+++ b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java
@@ -25,6 +25,7 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import net.draycia.carbon.api.CarbonChat;
+import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.command.ArgumentFactory;
@@ -124,14 +125,23 @@ public class WhisperCommand extends CarbonCommand {
final Component senderName = CarbonPlayer.renderName(sender);
final Component recipientName = CarbonPlayer.renderName(recipient);
- this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, message);
- this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, message);
- this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, message);
+ final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message));
+ this.carbonChat.eventHandler().emit(privateChatEvent);
+
+ if (privateChatEvent.cancelled()) {
+ this.carbonMessages.whisperError(sender, CarbonPlayer.renderName(sender), CarbonPlayer.renderName(recipient));
+ return;
+ }
+
+ this.carbonMessages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, privateChatEvent.message());
+ this.carbonMessages.whisperConsoleLog(this.carbonChat.server().console(), senderName, recipientName, privateChatEvent.message());
final @Nullable Sound messageSound = this.configFactory.primaryConfig().messageSound();
if (messageSound != null) {
recipient.playSound(messageSound);
}
+
sender.lastWhisperTarget(recipient.uuid());
sender.whisperReplyTarget(recipient.uuid());
recipient.whisperReplyTarget(sender.uuid());
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 5ef694c9..fa4c0c7b 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
@@ -123,7 +123,7 @@ public interface CarbonMessages {
final SourcedAudience audience,
@Placeholder("sender_display_name") final Component senderDisplayName,
@Placeholder("recipient_display_name") final Component recipientDisplayName,
- final String message
+ final Component message
);
@Message("whisper.from")
@@ -131,7 +131,7 @@ public interface CarbonMessages {
final SourcedAudience audience,
@Placeholder("sender_display_name") final Component senderDisplayName,
@Placeholder("recipient_display_name") final Component recipientDisplayName,
- final String message
+ final Component message
);
@Message("whisper.console")
@@ -139,7 +139,14 @@ public interface CarbonMessages {
final Audience audience,
@Placeholder("sender_display_name") final Component senderDisplayName,
@Placeholder("recipient_display_name") final Component recipientDisplayName,
- final String message
+ final Component message
+ );
+
+ @Message("whisper.error")
+ void whisperError(
+ final Audience audience,
+ @Placeholder("sender_display_name") final Component senderDisplayName,
+ @Placeholder("recipient_display_name") final Component recipientDisplayName
);
@Message("reply.target.missing")
diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties
index a0b4ab11..ad99db40 100644
--- a/common/src/main/resources/locale/messages-en_US.properties
+++ b/common/src/main/resources/locale/messages-en_US.properties
@@ -96,6 +96,7 @@ reply.target.missing=You have no-one to reply to
reply.target.self=You cannot whisper to yourself
whisper.console=[] -> []
whisper.continue.target_missing=You have no one to whisper
+whisper.error=Failed to send private message
whisper.from=[] -> [You]
whisper.ignored_by_target= is ignoring you
whisper.ignoring_target=You are ignoring