org.spongepowered.api.event.message.MessageEvent Java Examples

The following examples show how to use org.spongepowered.api.event.message.MessageEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UCCommands.java    From UltimateChat with GNU General Public License v3.0 6 votes vote down vote up
private void sendPreTell(CommandSource sender, CommandSource receiver, Text msg) {
    CommandSource src = sender;
    if (sender instanceof ConsoleSource) {
        src = receiver;
    }

    UChat.get().getLogger().timings(UCLogger.timingType.START, "UCListener#sendPreTell()|Fire AsyncPlayerChatEvent");

    MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(
            UChat.get().getVHelper().getCause(src),
            src.getMessageChannel(),
            Optional.of(src.getMessageChannel()),
            new MessageEvent.MessageFormatter(Text.builder("<" + src.getName() + "> ")
                    .onShiftClick(TextActions.insertText(src.getName()))
                    .onClick(TextActions.suggestCommand("/msg " + src.getName()))
                    .build(), msg),
            msg,
            false);
    Sponge.getEventManager().post(event);
}
 
Example #2
Source File: UCChannel.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Send a message from a channel as player.
 *
 * @param src     {@code Player}
 * @param message {@code Text} - Message to send.
 * @param direct  {@code boolean} - Send message direct to players on channel.
 */
public void sendMessage(Player src, Text message, boolean direct) {
    if (direct) {
        for (Player p : Sponge.getServer().getOnlinePlayers()) {
            UCChannel chp = UChat.get().getPlayerChannel(p);
            if (UChat.get().getPerms().channelReadPerm(p, this) && !this.isIgnoring(p.getName()) && (!this.neeFocus() || chp.equals(this))) {
                UChat.get().getLogger().timings(timingType.START, "UCChannel#sendMessage()|Direct Message");
                p.sendMessage(message);
            }
        }
        src.sendMessage(message);
    } else {
        MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(
                UChat.get().getVHelper().getCause(src),
                src.getMessageChannel(),
                Optional.of(src.getMessageChannel()),
                new MessageEvent.MessageFormatter(Text.builder("<" + src.getName() + "> ")
                        .onShiftClick(TextActions.insertText(src.getName()))
                        .onClick(TextActions.suggestCommand("/msg " + src.getName()))
                        .build(), message),
                message,
                false);
        UChat.get().getLogger().timings(timingType.START, "UCChannel#sendMessage()|Fire MessageChannelEvent");
        if (!Sponge.getEventManager().post(event)) {
            UChat.get().tempChannels.put(src.getName(), this.getAlias());
        }
    }
}
 
Example #3
Source File: PersonalmessageCommand.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkPermission(sender, PersonalmessagePermissions.UC_PERSONALMESSAGE_PERSONALMESSAGE_BASE);
    CommandSource t = args.<CommandSource>getOne("player").get();

    String message = args.<String>getOne("message").get();
    Text fmessage = Messages.getFormatted("personalmessage.command.personalmessage.format.receive", "%player%", sender, "%message%", message);

    //Event
    Cause cause = Cause.builder().append(UltimateCore.getContainer()).append(sender).append(t).build(EventContext.builder().build());
    MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(fmessage);
    final CommandSource tf = t;
    MessageChannel channel = () -> Arrays.asList(tf);
    PersonalmessageEvent event = new PersonalmessageEvent(cause, sender, t, formatter, channel, message, fmessage);
    Sponge.getEventManager().post(event);
    if (!event.isMessageCancelled()) {
        Text received = event.getFormatter().toText();
        t.sendMessage(received);
        //Reply
        UUID uuid_s = sender instanceof Player ? ((Player) sender).getUniqueId() : UUID.fromString("00000000-0000-0000-0000-000000000000");
        UUID uuid_t = t instanceof Player ? ((Player) t).getUniqueId() : UUID.fromString("00000000-0000-0000-0000-000000000000");
        if (t instanceof Player) {
            UltimateUser user = UltimateCore.get().getUserService().getUser((Player) t);
            user.offer(PersonalmessageKeys.REPLY, uuid_s);
        }
        if (sender instanceof Player) {
            UltimateUser user2 = UltimateCore.get().getUserService().getUser((Player) sender);
            user2.offer(PersonalmessageKeys.REPLY, uuid_t);
        }
        //TODO better system for this message?
        Text send = Messages.getFormatted("personalmessage.command.personalmessage.format.send", "%player%", t, "%message%", message);
        sender.sendMessage(send);
        return CommandResult.success();
    } else {
        throw new ErrorMessageException(Messages.getFormatted(t, "personalmessage.command.personalmessage.cancelled"));
    }
}
 
Example #4
Source File: ReplyCommand.java    From UltimateCore with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, PersonalmessagePermissions.UC_PERSONALMESSAGE_REPLY_BASE);

    Player p = (Player) sender;
    UltimateUser pu = UltimateCore.get().getUserService().getUser(p);
    Optional<UUID> tu = pu.get(PersonalmessageKeys.REPLY);
    if (!tu.isPresent()) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "personalmessage.command.reply.notarget"));
    }
    CommandSource t = Sponge.getServer().getPlayer(tu.get()).orElse(null);
    if (t == null) {
        if (tu.get() == UUID.fromString("00000000-0000-0000-0000-000000000000")) {
            t = Sponge.getServer().getConsole();
        } else {
            throw new ErrorMessageException(Messages.getFormatted(sender, "personalmessage.command.reply.notarget"));
        }
    }

    String message = args.<String>getOne("message").get();
    Text fmessage = Messages.getFormatted("personalmessage.command.personalmessage.format.receive", "%player%", sender, "%message%", message);

    //Event
    Cause cause = Cause.builder().append(UltimateCore.getContainer()).append(sender).append(t).build(EventContext.builder().build());
    MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(fmessage);
    final CommandSource tf = t;
    MessageChannel channel = () -> Arrays.asList(tf);
    PersonalmessageEvent event = new PersonalmessageEvent(cause, sender, t, formatter, channel, message, fmessage);
    Sponge.getEventManager().post(event);
    if (!event.isMessageCancelled()) {
        Text received = event.getFormatter().toText();
        t.sendMessage(received);
        //Reply
        UUID uuid_s = ((Player) sender).getUniqueId();
        UUID uuid_t = t instanceof Player ? ((Player) t).getUniqueId() : UUID.fromString("00000000-0000-0000-0000-000000000000");
        if (t instanceof Player) {
            UltimateUser user = UltimateCore.get().getUserService().getUser((Player) t);
            user.offer(PersonalmessageKeys.REPLY, uuid_s);
        }
        UltimateUser user2 = UltimateCore.get().getUserService().getUser((Player) sender);
        user2.offer(PersonalmessageKeys.REPLY, uuid_t);
        //TODO better system for this message?
        Text send = Messages.getFormatted("personalmessage.command.personalmessage.format.send", "%player%", t, "%message%", message);
        sender.sendMessage(send);
        return CommandResult.success();
    } else {
        throw new ErrorMessageException(Messages.getFormatted(t, "personalmessage.command.personalmessage.cancelled"));
    }
}