use of io.github.nucleuspowered.nucleus.modules.message.events.InternalNucleusMessageEvent in project Nucleus by NucleusPowered.
the class MessageHandler method sendMessage.
@Override
public boolean sendMessage(CommandSource sender, CommandSource receiver, String message) {
// Message is about to be sent. Send the event out. If canceled, then that's that.
boolean isBlocked = false;
boolean isCancelled = Sponge.getEventManager().post(new InternalNucleusMessageEvent(sender, receiver, message));
if (isCancelled) {
sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("message.cancel"));
// Only continue to show Social Spy messages if the subject is muted.
if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
return false;
}
}
// What about msgtoggle?
if (receiver instanceof Player && !sender.hasPermission(this.msgToggleBypass) && ucl.get((Player) receiver).map(x -> !x.get(MessageUserDataModule.class).isMsgToggle()).orElse(false)) {
isCancelled = true;
isBlocked = true;
sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithTextFormat("message.blocked", Nucleus.getNucleus().getNameUtil().getName((Player) receiver)));
if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
return false;
}
}
// Social Spies.
final UUID uuidSender = getUUID(sender);
final UUID uuidReceiver = getUUID(receiver);
final Map<String, Object> variables = Maps.newHashMap();
variables.put("from", sender);
variables.put("to", receiver);
// Create the tokens.
Map<String, Function<CommandSource, Optional<Text>>> tokens = Maps.newHashMap();
tokens.put("from", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToName));
tokens.put("to", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToName));
tokens.put("fromdisplay", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToDisplayName));
tokens.put("todisplay", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToDisplayName));
Text tm = useMessage(sender, message);
if (!isCancelled) {
sender.sendMessage(constructMessage(sender, tm, messageConfig.getMessageSenderPrefix(), tokens, variables));
receiver.sendMessage(constructMessage(sender, tm, messageConfig.getMessageReceiverPrefix(), tokens, variables));
}
NucleusTextTemplateImpl prefix = messageConfig.getMessageSocialSpyPrefix();
if (isBlocked) {
prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getBlockedTag() + prefix.getRepresentation());
}
if (isCancelled) {
prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getMutedTag() + prefix.getRepresentation());
}
MessageConfig.Targets targets = messageConfig.spyOn();
if (sender instanceof Player && targets.isPlayer() || sender instanceof ConsoleSource && targets.isCustom() || targets.isCustom()) {
Set<CommandSource> lm = onlinePlayersCanSpyOn(!uuidSender.equals(Util.consoleFakeUUID) && !uuidReceiver.equals(Util.consoleFakeUUID), sender, receiver);
MessageChannel mc = MessageChannel.fixed(lm);
if (!mc.getMembers().isEmpty()) {
mc.send(constructMessage(sender, tm, prefix, tokens, variables));
}
}
// Add the UUIDs to the reply list - the receiver will now reply to the sender.
if (!isCancelled) {
messagesReceived.put(uuidReceiver, uuidSender);
}
return !isCancelled;
}
Aggregations