dreamwiver 12 Share Posted January 7, 2023 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 More sharing options...
Pandemic 2711 Share Posted January 7, 2023 Hey there, you shouldn't interact or perform any other game action in the listener callbacks. You should usually always do that in your script's onLoop. Link to comment Share on other sites More sharing options...
Genius 49 Share Posted January 8, 2023 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 More sharing options...
dreamwiver 12 Author Share Posted January 8, 2023 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 More sharing options...
alexios1 2 Share Posted January 8, 2023 this weekened all scripts client get frozen annoying af and i bought voucher yerterday Link to comment Share on other sites More sharing options...
dreamwiver 12 Author Share Posted January 8, 2023 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); } Pandemic 1 Link to comment Share on other sites More sharing options...
Genius 49 Share Posted January 9, 2023 (edited) 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 January 9, 2023 by Genius dreamwiver 1 Link to comment Share on other sites More sharing options...
dreamwiver 12 Author Share Posted January 9, 2023 Right! I may be writing a small guide later on how to use listeners on a scrip made on TaskScript. Genius 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now