1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Resource Get recent Chatbox messages without using a listener

Discussion in 'Tutorials & Resources' started by auxi, Mar 6, 2018.

  1. auxi

    Joined:
    May 24, 2016
    Messages:
    1,113
    Likes Received:
    990
    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 (Text):
    1. 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 (Text):
    1.  
    2.  
    3. /**
    4. * Class for grabbing recent messages in the Chatbox
    5. *
    6. * @author  auxi
    7. * @version 1.0
    8. */
    9.  
    10. public class Chat {
    11.  
    12.     public static List<Chatbox.Message> getRecentMessages(int messages, Chatbox.Message.Type type) {
    13.         return Chatbox.getMessages(type).stream().sorted(Comparator.reverseOrder()).limit(messages).collect(Collectors.toList());
    14.     }
    15.  
    16.     public static boolean isMessagePresent(String message, int messagesToCheck, Chatbox.Message.Type type) {
    17.         return getRecentMessages(messagesToCheck, type).stream().map(Chatbox.Message::getMessage).anyMatch(a -> a.contains(message));
    18.     }
    19. }
    20.  
    Example usage:

    Code (Text):
    1.  
    2. if (Interaction.interact(pouch, "Summon") && Execution.delayUntil(() -> !pouch.isValid(), 1200, 1800)) {
    3.     bot.setStatus("[Familiar] Successfully summoned familiar");
    4. } else if (Chat.isMessagePresent("too big to summon here", 5, Chatbox.Message.Type.SERVER)) {
    5.     getLogger().warn("[Familiar] Failed to summon familiar as area is too small!");
    6. }
    7.  
     
    #1 auxi, Mar 6, 2018
    Last edited: Mar 6, 2018
    cookie monster, Wet Rag and Jhinn like this.
  2. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    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.
     
    Wet Rag, Derk and auxi like this.
  3. auxi

    Joined:
    May 24, 2016
    Messages:
    1,113
    Likes Received:
    990
    That would be pretty sweet!
    --- Double Post Merged, Mar 10, 2018, Original Post Date: Mar 7, 2018 ---
    Just a heads up - the ChatboxQueryBuilder has just been implemented into the latest RuneMate version.
     
    Derk likes this.

Share This Page

Loading...