Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource Get recent Chatbox messages without using a listener

Joined
May 24, 2016
Messages
1,113
One of the things I do to reduce cpu usage in some bots, is to ditch using a ChatboxListener wherever possible.

Instead you can use:
Code:
List<Chatbox.Message> messages = Chatbox.getMessages()

However this returns every single message in your chatbox. To fix this I made a class which will return recent messages only. Feel free to use it!

Code:
/**
* Class for grabbing recent messages in the Chatbox
*
* @author  auxi
* @version 1.0
*/

public class Chat {

    public static List<Chatbox.Message> getRecentMessages(int messages, Chatbox.Message.Type type) {
        return Chatbox.getMessages(type).stream().sorted(Comparator.reverseOrder()).limit(messages).collect(Collectors.toList());
    }

    public static boolean isMessagePresent(String message, int messagesToCheck, Chatbox.Message.Type type) {
        return getRecentMessages(messagesToCheck, type).stream().map(Chatbox.Message::getMessage).anyMatch(a -> a.contains(message));
    }
}

Example usage:

Code:
if (Interaction.interact(pouch, "Summon") && Execution.delayUntil(() -> !pouch.isValid(), 1200, 1800)) {
    bot.setStatus("[Familiar] Successfully summoned familiar");
} else if (Chat.isMessagePresent("too big to summon here", 5, Chatbox.Message.Type.SERVER)) {
    getLogger().warn("[Familiar] Failed to summon familiar as area is too small!");
}
 
Last edited:
Mod Automation
Joined
Jul 26, 2013
Messages
3,053
Chatbox messages should be gotten using queries (and builders) like everything else and thus should be able to be limited to X messages. Please evaluate integrating this functionality into the client API @Cloud @Party.
 
Joined
May 24, 2016
Messages
1,113
That would be pretty sweet!
 
Just a heads up - the ChatboxQueryBuilder has just been implemented into the latest RuneMate version.
 
Top