Search in sources :

Example 1 with TypeMessageSeparator

use of cc.hyperium.mods.togglechat.toggles.defaults.TypeMessageSeparator in project Hyperium by HyperiumClient.

the class ToggleChatEvents method onChatReceive.

// We use the high priority to grab things first
@InvokeEvent(priority = Priority.HIGH)
public void onChatReceive(ServerChatEvent event) {
    // Strip the message of any colors for improved detectability
    String unformattedText = ChatColor.stripColor(event.getChat().getUnformattedText());
    // The formatted message for a few of the custom toggles
    String formattedText = event.getChat().getFormattedText();
    try {
        // Loop through all the toggles
        for (ToggleBase type : mod.getToggleHandler().getToggles().values()) {
            // The chat its looking for shouldn't be toggled, move to next one!
            if (type.isEnabled())
                continue;
            // the whole toggle system crashing down in flames.
            try {
                // The text we want to input into the shouldToggle method.
                String input = type.useFormattedMessage() ? formattedText : unformattedText;
                // don't send the message to the player & stop looping
                if (type.shouldToggle(input)) {
                    if (type instanceof TypeMessageSeparator) {
                        // Attempt to keep the formatting
                        ChatStyle style = event.getChat().getChatStyle();
                        String edited = ((TypeMessageSeparator) type).editMessage(formattedText);
                        // Don't bother sending the message if its empty
                        if (!input.equals(edited) && edited.isEmpty()) {
                            event.setCancelled(true);
                        } else {
                            event.setChat(new ChatComponentText(edited).setChatStyle(style));
                        }
                    } else {
                        event.setCancelled(true);
                    }
                    break;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}
Also used : ChatStyle(net.minecraft.util.ChatStyle) TypeMessageSeparator(cc.hyperium.mods.togglechat.toggles.defaults.TypeMessageSeparator) ChatComponentText(net.minecraft.util.ChatComponentText) ToggleBase(cc.hyperium.mods.togglechat.toggles.ToggleBase) InvokeEvent(cc.hyperium.event.InvokeEvent)

Aggregations

InvokeEvent (cc.hyperium.event.InvokeEvent)1 ToggleBase (cc.hyperium.mods.togglechat.toggles.ToggleBase)1 TypeMessageSeparator (cc.hyperium.mods.togglechat.toggles.defaults.TypeMessageSeparator)1 ChatComponentText (net.minecraft.util.ChatComponentText)1 ChatStyle (net.minecraft.util.ChatStyle)1