Skip to content
Lilac edited this page Nov 3, 2023 · 2 revisions

Message Events

PreParseMessageEvent

    @EventHandler
    public void onPreParseMessageEvent(PreParseMessageEvent event) {
        // This event is called when a message is about to be parsed.
        String rawMessage = event.getMessage().getPlayerInput(); // The raw message that will be parsed.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        RosePlayer viewer = event.getViewer(); // The RosePlayer who will receive the message.

        // If the message contains "badword":
        // Cancel the event to stop the message from sending.
        // Tell the sender that the message could not be sent.
        if (rawMessage.contains("badword")) {
            sender.send("Your message contained a badword!");
            event.setCancelled(true);
        }
    }

PostParseMessageEvent

    @EventHandler
    public void onPostParseMessageEvent(PostParseMessageEvent event) {
        // This event is called after a message has been parsed.
        String rawMessage = event.getMessage().getPlayerInput(); // The raw message before it was parsed.
        BaseComponent[] message = event.getMessageComponents().content(); // The BaseComponent[] consisting of the parsed message.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        RosePlayer viewer = event.getViewer(); // The RosePlayer who will receive the message.

        // Create a new component to append to the message
        ComponentBuilder componentBuilder = new ComponentBuilder();
        
        // Only adds the X if the player has permission.
        if (sender.hasPermission("rosechat.deletemessages")) {
            componentBuilder.append("X"); // Add an X before the message.
        }
        
        componentBuilder.append(message); // Append the message.

        // Sets the components of the message to the new components.
        event.setMessageComponents(new MessageTokenizerResults<>(componentBuilder.create(), event.getMessageComponents().outputs()));
    }

This is used within RoseChat to add the delete message button here.

PreParseDiscordMessageEvent

    @EventHandler
    public void onPreParseDiscordMessageEvent(PreParseDiscordMessageEvent event) {
        // This event is called when a message is about to be parsed and sent to Discord.
        TextChannel textChannel = event.getTextChannel(); // The TextChannel from DiscordSRV.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        
        // Cancels sending the message if the player doesn't have permission, or the TextChannel can't be spoken in.
        if (!sender.hasPermission("rosechat.sendtodiscord") 
                || !textChannel.canTalk()) {
            event.setCancelled(true);
        }
    }

PostParseDiscordMessageEvent

    @EventHandler
    public void onPostParseDiscordMessageEvent(PostParseDiscordMessageEvent event) {
        // This event is called after a message has been parsed and about to be sent to Discord.
        TextChannel textChannel = event.getTextChannel(); // The TextChannel from DiscordSRV.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        String message = event.getOutput(); // The message, after it has been parsed and converted into a String.
        
        // Cancels sending the message if the channel can't be spoken in.
        if (!textChannel.canTalk()) {
            event.setCancelled(true);
        }
    }

MessageFilteredEvent

    @EventHandler
    public void onMessageFiltered(MessageFilteredEvent event) {
        // This event is called after a message has been filtered.
        // To filter your own messages, PreParseMessageEvent should be used.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        String rawMessage = event.getOriginalMessage(); // The message before it was filtered.
        String message = event.getOutputs().getFilteredMessage(); // The message after it was filtered.

        // Your code here...
    }

MessageBlockedEvent

    @EventHandler
    public void onMessageBlocked(MessageBlockedEvent event) {
        // This event is called after a message has been blocked.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        String rawMessage = event.getOriginalMessage(); // The message that was blocked.
        
        // Logs that the message was blocked.
        Bukkit.getConsoleSender().sendMessage("Blocked Message from " + sender.getName()  + ": " + rawMessage);
        // Gets the reason why the message was blocked; CAPS, SPAM, URL, or SWEAR.
        Bukkit.getConsoleSender().sendMessage("This message was blocked for: " + event.getOutputs().getWarning());
    }

Player Events

PlayerSendMessageEvent

    @EventHandler
    public void onPlayerSendMessage(PlayerSendMessageEvent event) {
        // This event is called when a player sends a private message to another player.
        // This is very similar to PreParseMessageEvent, but guarantees it was sent as a private message.
        String rawMessage = event.getMessage().getPlayerInput(); // The raw message that will be parsed.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        RosePlayer receiver = event.getReceiver(); // The RosePlayer who will receive the message.
        
        // Your code here...
    }

PlayerReceiveMessageEvent

    @EventHandler
    public void onPlayerReceiveMessage(PlayerReceiveMessageEvent event) {
        // This event is called when a player receives a private message from another player.
        // This is very similar to PostParseMessageEvent, but guarantees it was sent as a private message.
        String rawMessage = event.getMessage().getPlayerInput(); // The raw message before it was parsed.
        BaseComponent[] message = event.getMessageComponents().content(); // The BaseComponent[] consisting of the parsed message.
        RosePlayer sender = event.getMessage().getSender(); // The sender of the message.
        RosePlayer receiver = event.getReceiver(); // The RosePlayer who will receive the message.
        
        // Your code here...
    }

PlayerNicknameEvent

    @EventHandler
    public void onPlayerNickname(PlayerNicknameEvent event) {
        // This event is called before a player is nicknamed.
        Player player = event.getPlayer(); // The player who is being nicknamed.
        String nickname = event.getNickname(); // The new nickname.

        // Cancels the event if the nickname contains "badword".
        if (nickname.contains("badword")) {
            event.setCancelled(true);
        }
    }

Channel Events

ChannelChangeEvent

    @EventHandler
    public void onChannelChange(ChannelChangeEvent event) {
        // This event is called when a player changes channel.
        Player player = event.getPlayer(); // The player who is changing channel.
        Channel oldChannel = event.getOldChannel(); // The channel that the player will move from.
        Channel newChannel = event.getChannel(); // The channel that the player will move into.
        
        // If the old channel was a FactionsUUID supported channel:
        if (oldChannel.getProvider().getSupportedPlugin().equalsIgnoreCase("FactionsUUID")) {
            // Cancels the event if the player is moving from a FactionsUUID channel to the default channel.
            if (newChannel.isDefaultChannel()) {
                event.setCancelled(true);
            }
        }
    }

Group Events

GroupPreCreateEvent

    @EventHandler
    public void onGroupPreCreate(GroupPreCreateEvent event) {
        // Called before a group is created.
        Player player = event.getPlayer(); // The player creating the group.
        String name = event.getName(); // The name of the group.
        
        // Cancels the event if the group name contains badword.
        if (name.contains("badword")) {
            event.setCancelled(true);
        }
    }

GroupCreateEvent

    @EventHandler
    public void onGroupCreate(GroupCreateEvent event) {
        // Called after a group has been created.
        GroupChannel group = event.getGroup(); // The group that was created.
        
        // Adds all online players to the group.
        for (Player player : Bukkit.getOnlinePlayers()) {
            group.addMember(player.getUniqueId());
        }
    }

GroupDisbandEvent

    @EventHandler
    public void onGroupDisband(GroupDisbandEvent event) {
        // Called when a group is about to be disbanded.
        GroupChannel group = event.getGroup(); // The group that is about to be disbanded.

        // Grabs the owner of the group.
        Player owner = Bukkit.getPlayer(group.getOwner());
        if (owner != null) {
            if (/* Your code here...*/) {
                event.setCancelled(true);
            }
        }
    }

GroupJoinEvent

    @EventHandler
    public void onGroupJoin(GroupJoinEvent event) {
        // Called before a player joins a group.
        Player player = event.getPlayer(); // The player who is joining the group.
        GroupChannel group = event.getGroup(); // The group that the player will join.

        // Grabs the owner of the group.
        Player owner = Bukkit.getPlayer(group.getOwner());
        if (owner != null) {
            // Cancels the event if the condition passes.
            if (!YourPlugin.areFriends(player, owner)) {
                event.setCancelled(true);
            }
        }
    }

GroupLeaveEvent

    @EventHandler
    public void onGroupJoin(GroupLeaveEvent event) {
        // Called before a player leaves, or gets kicked from, a group.
        OfflinePlayer player = event.getPlayer(); // The player who is leaving, or being kicked from, a group.
        GroupChannel group = event.getGroup(); // The group that the player will leave.

        // Cancels the event if the player is op.
        if (player.isOp()) {
            event.setCancelled(true);
        }
    }

GroupNameEvent

    @EventHandler
    public void onGroupName(GroupNameEvent event) {
        // Called when a group is being renamed.
        String name = event.getName();
        GroupChannel group = event.getGroup();
        
        if (name.contains("badword")) {
            event.setCancelled(true);
        } else if (name.contains("bad")) {
            event.setName(name.replace("bad", "***"));
        }
    }
Clone this wiki locally