Add CarbonPrivateChatEvent

This commit is contained in:
Draycia committed 2023-06-08 15:15:52 -07:00
1 parent f9fcd9b72d
commit 8ced22ffb9
6 files changed
+158 -12

No files matched your search

@@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}
@@ -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) {
@@ -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) {
@@ -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());
@@ -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")
@@ -96,6 +96,7 @@ reply.target.missing=<red>You have no-one to reply to
reply.target.self=<red>You cannot whisper to yourself
whisper.console=<gold>[<green><sender_display_name></green>] -> [<green><recipient_display_name></green>] <message>
whisper.continue.target_missing=<red>You have no one to whisper
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>