Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Client freezes when performing an action at onTradeMessage()


    dreamwiver

    Recommended Posts

    Hello,

    I'm trying to make a script which accepts incoming trades. As suggested by the javadocs, i'm implementing ChatListener.

    When a trade requests is received, the client freezes and it's impossible to interact with it. Only option I found is closing it, and opening it again. I noticied the client breaks with any kind of interaction, it also broke by making it follow the other player instead of trading with him.

    Here is my MainClass, notice i'm extending TaskScript

    package Trader;
    
    import org.dreambot.api.methods.emotes.Emotes;
    import org.dreambot.api.methods.interactive.Players;
    import org.dreambot.api.methods.trade.Trade;
    import org.dreambot.api.methods.trade.TradeUser;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.script.impl.TaskScript;
    import org.dreambot.api.script.listener.ChatListener;
    import org.dreambot.api.wrappers.interactive.Player;
    import org.dreambot.api.wrappers.widgets.message.Message;
    import org.dreambot.core.Instance;
    
    import java.awt.*;
    
    @ScriptManifest(version = 1.0, author = "dreamwiver", category = Category.MISC, name = "Auto Trader")
    public class MainClass extends TaskScript implements ChatListener {
    
        @Override
        public void onStart() {
            addNodes(new AccepterNode(), new TradingNode());
        }
    
        @Override
        public void onTradeMessage(Message message){
            log(message.getMessage());
            Player trader = Players.closest(message.getUsername());
            log("player found "+trader.getName());
            Trade.tradeWithPlayer(trader.getName());
            sleepUntil(()-> Players.getLocal().isAnimating(), 1000, 700);
        }
    
    }

     Any suggestion on how approach this issue is appreciated.

    Thank you so much

     

     

    Link to comment
    Share on other sites

    Hello, to expand on what Pandemic said, one way of doing it would be to assign a boolean a true value on trade message received. E.g. receivedTrade = true; and then anything else you need, maybe a string with the username.

    Then in your onLoop, call the rest of that stuff if that boolean is true, and make sure to set it to false at the end of your actions so that method isn't called again inadvertently.

    Link to comment
    Share on other sites

    Hello @Genius, @Pandemic

    Thank you for the fast response. However I don't understand how can I use the onLoop method since i'm using TaskScript and all my logic is done in the Nodes. I have a Node which is activated when the trade screen is open, and another one when the second trade screen is open.

    Is there any way I can activate a node "manually" from the onTradeMessage method? I would prefer to avoid overrating the onLoop method since I'd like to have all the logic on the nodes.

    Thank you so much

    Link to comment
    Share on other sites

    Hello @alexios1

    I'm afraid the client was frozen due to a bug of mine.

    If anyone is interested here is the working code of the issue:

    package Trader;
    
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.trade.Trade;
    import org.dreambot.api.script.ScriptManager;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.script.listener.ChatListener;
    import org.dreambot.api.wrappers.widgets.message.Message;
    
    public class AccepterNode extends TaskNode  {
    
        private boolean incomingTrade = false;
        private String trader;
    
        @Override
        public int priority(){
            return 2;
        }
    
        AccepterNode(){
            ScriptManager.getScriptManager().addListener(new ChatListener() {
                @Override
                public void onTradeMessage(Message message) {
                    incomingTrade = true;
                    trader = message.getUsername();
                }
            });
        }
    
        @Override
        public boolean accept() {
            return incomingTrade;
        }
    
        @Override
        public int execute() {
            Trade.tradeWithPlayer(trader);
            sleepUntil(()-> Trade.isOpen(), 5000, 500);
            incomingTrade = false;
            return Calculations.random(200, 1000);
        }
    }

     

    This Node will listen for trade requests, and accept them once one is received.

    I have another Node which is activated when the Trade screen is open, using this code:

    
        @Override
        public boolean accept() {
            return Trade.isOpen(1);
        }

     

    Link to comment
    Share on other sites

    11 hours ago, dreamwiver said:

    Hello @alexios1

    I'm afraid the client was frozen due to a bug of mine.

    If anyone is interested here is the working code of the issue:

    package Trader;
    
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.trade.Trade;
    import org.dreambot.api.script.ScriptManager;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.script.listener.ChatListener;
    import org.dreambot.api.wrappers.widgets.message.Message;
    
    public class AccepterNode extends TaskNode  {
    
        private boolean incomingTrade = false;
        private String trader;
    
        @Override
        public int priority(){
            return 2;
        }
    
        AccepterNode(){
            ScriptManager.getScriptManager().addListener(new ChatListener() {
                @Override
                public void onTradeMessage(Message message) {
                    incomingTrade = true;
                    trader = message.getUsername();
                }
            });
        }
    
        @Override
        public boolean accept() {
            return incomingTrade;
        }
    
        @Override
        public int execute() {
            Trade.tradeWithPlayer(trader);
            sleepUntil(()-> Trade.isOpen(), 5000, 500);
            incomingTrade = false;
            return Calculations.random(200, 1000);
        }
    }

     

    This Node will listen for trade requests, and accept them once one is received.

    I have another Node which is activated when the Trade screen is open, using this code:

    
        @Override
        public boolean accept() {
            return Trade.isOpen(1);
        }

     

    Good stuff! This is what Pandemic was referring to - the node execute is still technically in the onLoop since that's where all of your nodes are executed. Just as long as you don't perform game actions within the chat listener. This looks good to me now. :)

     

    Edited by Genius
    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.