The client is offline due to RuneScape update. You may see instance errors but this is due to the update and not actual instance errors. This should be resolved soon. Join our Discord for more information.

Handle randoms like a pro



  • I wanted to handle the random events that require only dialog. The Frog one needs testing and I wanted the camo outfit so I chose to make the script AFK until I get there. The cool thing is the popups so you never miss a random if you wanted to baby sit your scripts.

    import org.rspeer.runetek.adapter.component.InterfaceComponent;
    import org.rspeer.runetek.adapter.scene.Npc;
    import org.rspeer.runetek.api.commons.Time;
    import org.rspeer.runetek.api.component.Dialog;
    import org.rspeer.runetek.api.component.Interfaces;
    import org.rspeer.runetek.api.component.tab.Inventory;
    import org.rspeer.runetek.api.scene.Npcs;
    import org.rspeer.runetek.api.scene.Players;
    import org.rspeer.script.Script;
    import org.rspeer.ui.Log;
    
    import javax.swing.*;
    import java.io.*;
    import java.security.SecureRandom;
    import java.time.LocalDateTime;
    import java.util.Arrays;
    
    class RandomHandler {
        double SCRIPT_VERSION = 1.0;
    
        private boolean showPopup = false, showSecondPopup = false;
    
        void checkLamp() {
            if (Inventory.contains("Lamp")) {
                if (!Interfaces.isOpen(134)) {
                    Inventory.getFirst("Lamp").interact("Rub");
                    Time.sleepUntil(() -> Interfaces.isOpen(134), 500, 10000);
                }
                if (Interfaces.isOpen(134)) {
                    InterfaceComponent prayer = Interfaces.getComponent(134, 9);//Get prayer XP from the lamp
                    InterfaceComponent confirm = Interfaces.getComponent(134, 26);
                    prayer.interact("Advance Prayer");
                    Time.sleep(random(1000, 1500));
                    confirm.interact("Ok");
                    Time.sleepUntil(() -> !Interfaces.isOpen(134), 500, 10000);
                }
                if (Dialog.canContinue()) {
                    Dialog.processContinue();
                    Time.sleep(random(200, 500));
                }
            }
        }
    
        boolean findNearbyRandoms() {
            if (Npcs.getNearest(npc -> npc.containsAction("Dismiss")) != null) {
                if (!showPopup) {
                    java.awt.EventQueue.invokeLater(() -> {
                        JOptionPane optionPane = new JOptionPane("RANDOM EVENT DETECTED!", JOptionPane.WARNING_MESSAGE);
                        JDialog dialog = optionPane.createDialog("Warning!");
                        dialog.setAlwaysOnTop(true);
                        dialog.setVisible(true);
                    });
                    showPopup = true;
                }
                Log.info("Detected random event!");
                Npc event = Npcs.getNearest(npc -> npc.containsAction("Dismiss"));
    
                logChat("We have detected a random event [" + event.getName() + "] Actions: [" + Arrays.toString(event.getActions()) + "]"
                        + " NPC Target: " + (event.getTarget() != null ? event.getTarget().getName() : "N/A")
                        + " Message: " + (event.getOverheadText() != null ? event.getOverheadText() : "N/A"));
                return true;
            }
            showPopup = false;
            return false;
        }
    
        boolean doWeHaveRandom() {
            if (Npcs.getNearest(npc -> npc.containsAction("Dismiss") && npc.getTarget() == Players.getLocal() && npc.isPositionWalkable()) != null) {
                if (!showSecondPopup) {
                    java.awt.EventQueue.invokeLater(() -> {
                        JOptionPane optionPane = new JOptionPane("WE HAVE A RANDOM EVENT!", JOptionPane.WARNING_MESSAGE);
                        JDialog dialog = optionPane.createDialog("Warning!!");
                        dialog.setAlwaysOnTop(true);
                        dialog.setVisible(true);
                    });
                    showSecondPopup = true;
                }
                return true;
            }
            showSecondPopup = false;
            return false;
        }
    
        void handleRandom() {
            Npc event = Npcs.getNearest(npc -> npc.containsAction("Dismiss") && npc.getTarget() == Players.getLocal());
    
            if ((event.getName().equals("Genie") || event.getName().equals("Drunken Dwarf") || event.getName().equals("Dr Jekyll") || event.getName().equals("Rick Turpentine")) && event.isPositionWalkable()) {
                if (!Dialog.isOpen()) {
                    event.interact(x -> true);
                    Time.sleepUntil(Dialog::isOpen, 1000, 10000);
                }
                if (Dialog.canContinue()) {
                    Dialog.processContinue();
                    Time.sleep(random(200, 500));
                }
    
                //NEEDS TESTING
            } else if (event.getName().equals("Frog")) {
                Npc frogPrince = Npcs.getNearest(npc -> !npc.containsAction("Dismiss") && npc.getName().equals("Frog") && npc.containsAction("Talk-to"));
                if (!Dialog.isOpen()) {
                    frogPrince.interact(x -> true);
                    Time.sleepUntil(Dialog::isOpen, 1000, 10000);
                }
                if (Dialog.canContinue()) {
                    Dialog.processContinue();
                    Time.sleep(random(200, 500));
                }
                if (Dialog.isViewingChatOptions()) {
                    Dialog.process("I suppose so, sure.");
                    //  Dialog.process(opt->opt.contains("sure"));
                    //Dialog.process(0);//Yes?
                    Time.sleep(random(200, 500));
                }
            } else if (event.getName().equals("Sergeant Damien")) {
                Log.info("We got sergeant lets do it!");
                Time.sleep(random(500, 1000));
            } else {
                Time.sleep(random(200, 500));
                event.interact("Dismiss");
                Time.sleep(random(300, 500));
            }
    
        }
    
        private void logChat(String text) {
            LocalDateTime timestamp = LocalDateTime.now();
            if (!new File(Script.getDataDirectory() + "\\Koko\\Logs\\Screenshots").exists()) {
                new File(Script.getDataDirectory() + "\\Koko\\Logs\\Screenshots").mkdirs();
            }
    
            try (FileWriter fw = new FileWriter(Script.getDataDirectory() + "\\Koko\\Logs\\Randoms.txt", true);
                 BufferedWriter bw = new BufferedWriter(fw);
                 PrintWriter out = new PrintWriter(bw)) {
                out.println(timestamp + "> " + text);
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }
        }
    
        private int random(int min, int max) {
            SecureRandom random = new SecureRandom();
            return (random.nextInt(max - min + 1) + min);
        }
    }
    

    Example usage:

    private RandomHandler randomEvent = new RandomHandler();//up at the top
    
    
    @Override
    public int loop() {
        randomEvent.checkLamp();
        if (randomEvent.findNearbyRandoms()) {
            Time.sleep(1000);
        }
        if (randomEvent.doWeHaveRandom()) {
            takeScreenie = true;
            randomEvent.handleRandom();
        } else if (doYourScript) {
            Log.info("Get mone, get paid");
        }
        return random(200, 300);
    }
    
    

    To take screenshots:

    private void TakeScreenshot(Image img) {
        Log.info("Attempting to take a screenshot");
        BufferedImage buffered = (BufferedImage) img;
    
        //Get the current date to save in the screenshot folder
        long now = Instant.now().getEpochSecond();
    
        try {
            //Create a folder with my forum name to tell the user which script created this folder
            if (!new File(getDataDirectory() + "\\Koko\\ArdyKnights\\Screenshots").exists()) {
                new File(getDataDirectory() + "\\Koko\\ArdyKnights\\Screenshots").mkdirs();
            }
            //save the image to the folder and rename all player spaces with underscores
            try {
                if (ImageIO.write(
                        buffered,
                        "png",
                        new File(getDataDirectory() + "\\Koko\\ArdyKnights\\Screenshots\\" + now + "-" + Players.getLocal().getName().replaceAll("\\u00a0", "_") + ".png"))) {
                    Log.info("Saved " + getDataDirectory() + "\\Koko\\ArdyKnights\\Screenshots\\" + now + "-" + Players.getLocal().getName().replaceAll("\\u00a0", "_") + ".png");
                }
            } catch (IOException e) {
                Log.info("Error! " + e.getMessage());
            }
        } catch (Exception e) {
            Log.info("Error! " + e.getMessage());
        }
    }
    

    Use the function inside paint, add to the bottom for capturing everything inside your paint, or at the top to grab no paint:

    @Override
        public void notify(RenderEvent renderEvent) {
    
           /*
            * Put this at the bottom
            */
            if (takeScreenie) {
                TakeScreenshot(renderEvent.getProvider().getImage());
                takeScreenie = false;
            }
    }
    


  • How do you take screenshots?



  • @Bobbey O I forgot to add that in there, I'll put it in there



  • @Koko You're an MVP!



  • I simplified this to a single call, and no popup/seargent stuff. Thanks @Koko

    public static boolean handleRandom() {
        checkLamp();
        Npc event = Npcs.getNearest(npc -> npc.containsAction("Dismiss") && npc.getTarget() == Players.getLocal());
        if(event == null)
            return false;
    
        String eventName = event.getName();
        if ((eventName.equals("Genie") || eventName.equals("Drunken Dwarf") || eventName.equals("Dr Jekyll") || eventName.equals("Rick Turpentine")) && event.isPositionWalkable()) {
            if (!Dialog.isOpen()) {
                event.interact(x -> true);
                Time.sleepUntil(Dialog::isOpen, 1000, 10000);
            }
            while (Dialog.canContinue()) {
                Dialog.processContinue();
                Time.sleepWhile(()->Dialog.isOpen() || Dialog.isProcessing(), 1000);
            }
            //NEEDS TESTING
        } else if (eventName.equals("Frog")) {
            Npc frogPrince = Npcs.getNearest(npc -> !npc.containsAction("Dismiss") && npc.getName().equals("Frog") && npc.containsAction("Talk-to"));
            if (!Dialog.isOpen()) {
                frogPrince.interact(x -> true);
                Time.sleepUntil(Dialog::isOpen, 1000, 10000);
            }
            if (Dialog.canContinue()) {
                Dialog.processContinue();
                Time.sleep(200, 500);
            }
            if (Dialog.isViewingChatOptions()) {
                Dialog.process("I suppose so, sure.");
                //  Dialog.process(opt->opt.contains("sure"));
                //Dialog.process(0);//Yes?
                Time.sleep(200, 500);
            }
        } else {
            Time.sleep(200, 500);
            event.interact("Dismiss");
            Time.sleep(300, 500);
        }
        return true;
    }
    
    static void checkLamp() {
        if (Inventory.contains("Lamp")) {
            if (!Interfaces.isOpen(134)) {
                Inventory.getFirst("Lamp").interact("Rub");
                Time.sleepUntil(() -> Interfaces.isOpen(134), 500, 10000);
            }
            if (Interfaces.isOpen(134)) {
                InterfaceComponent prayer = Interfaces.getComponent(134, 9);//Get prayer XP from the lamp
                InterfaceComponent confirm = Interfaces.getComponent(134, 26);
                prayer.interact("Advance Prayer");
                Time.sleep(1000, 1500);
                confirm.interact("Ok");
                Time.sleepUntil(() -> !Interfaces.isOpen(134), 500, 10000);
            }
            if (Dialog.canContinue()) {
                Dialog.processContinue();
                Time.sleep(200, 500);
            }
        }
    }


  • Looks nice!



  • Does anyone know how to find out the numbers of other skill than prayer for the checkLamp() method?
    I cannot test with a in-game Lamp so is there a way to know the other interfaces component without testing?


  • Contributor Script Writer VIP

    @1234567Alex said in Handle randoms like a pro:

    Does anyone know how to find out the numbers of other skill than prayer for the checkLamp() method?
    I cannot test with a in-game Lamp so is there a way to know the other interfaces component without testing?

    There's almost no reason to use this now since the client can handle this



  • @Man16
    thanks for replying 16!
    Actually, then can you please give me an example of how it works ?

    • Does your script have to handle the interactions with the Random Event's npcs ?
    • If there is a lamp in my inventory while a script is running that have not been coded to interact with a Lamp at all, will the client do it itself ??

  • Contributor Script Writer VIP

    @1234567Alex said in Handle randoms like a pro:

    @Man16
    thanks for replying 16!
    Actually, then can you please give me an example of how it works ?

    • Does your script have to handle the interactions with the Random Event's npcs ?
    • If there is a lamp in my inventory while a script is running that have not been coded to interact with a Lamp at all, will the client do it itself ??

    Your script doesn't have to do anything. The user just selects their account in the account manager before running a script and it will apply all the breaks and randoms specified for that account.



  • @Man16 I'm sorry but I have never assisted to a successful handling of a random event before using the shown method of this OP.

    But I trust your word and experience I'll try to catch one time that it handles it by itself , thank you 🙂


  • Contributor Script Writer VIP

    @1234567Alex said in Handle randoms like a pro:

    @Man16 I'm sorry but I have never assisted to a successful handling of a random event before using the shown method of this OP.

    But I trust your word and experience I'll try to catch one time that it handles it by itself , thank you 🙂

    You have to configure an account in the account manager and select it before you run a script.



  • @Man16 said in Handle randoms like a pro:

    efore you

    Yes, I already do. And I have set dismiss rate at around 30% and choose Ranged experience.



  • Yeah i would have to agree, ive had multiple different accounts configured and have never once interacted with a random.



  • Update on this: Now I have seen many times solving the randoms by dismissing when I put the dismiss rate at 90%.
    What I have noticed and that could have been the cause of it sometimes not doing anything was probably in my script that was not allowing a time for the random handler to trigger or something like that.

    I am still waiting for the first damn genie to show up so I can see what it does with it (I am hoping it will not dismiss it even if I had set 100% dismiss rate - in the sense that genie should be the only exception to not dismiss since you get xp and you only need to click it once).


 

49
Online

16.5k
Users

1.4k
Topics

19.2k
Posts