Search in sources :

Example 1 with InternalNucleusMessageEvent

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;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) Function(java.util.function.Function) MessageConfig(io.github.nucleuspowered.nucleus.modules.message.config.MessageConfig) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageUserDataModule(io.github.nucleuspowered.nucleus.modules.message.datamodules.MessageUserDataModule) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) UUID(java.util.UUID) InternalNucleusMessageEvent(io.github.nucleuspowered.nucleus.modules.message.events.InternalNucleusMessageEvent)

Aggregations

NucleusTextTemplateImpl (io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl)1 MessageConfig (io.github.nucleuspowered.nucleus.modules.message.config.MessageConfig)1 MessageUserDataModule (io.github.nucleuspowered.nucleus.modules.message.datamodules.MessageUserDataModule)1 InternalNucleusMessageEvent (io.github.nucleuspowered.nucleus.modules.message.events.InternalNucleusMessageEvent)1 UUID (java.util.UUID)1 Function (java.util.function.Function)1 CommandSource (org.spongepowered.api.command.CommandSource)1 ConsoleSource (org.spongepowered.api.command.source.ConsoleSource)1 Player (org.spongepowered.api.entity.living.player.Player)1 Text (org.spongepowered.api.text.Text)1 MessageChannel (org.spongepowered.api.text.channel.MessageChannel)1