Jump to content

How to properly use widgets for accepting a trade?


TheGreatests

Recommended Posts

Anyone know any good ways to properly code a widget to look for the "someone wishes to trade with you" dialog?

 

RS2Widget widgey = widgets.getWidgetContainingText("wishes to trade with you"); //this is what you're looking for, BUT \/

 

That will get you any trade offer thats made though, regardless of where it is within the chatbox. You'll want to, instead, keep track of when the trade request appeared (by getting when the text occurred inside of the chatbox, probably using onMessage) and if it's within a certain timeframe, accept the trade offer

Edited by Imateamcape
Link to comment
Share on other sites

You can override the onMessage() function in your script. Then when you receive a message with the text "wishes to trade with you" grab their name from it via "text".split(" ").get(0) which would be the players name. do stuff with that


This code should work, (haven't tested as I'm at school).

Declare currentTradeUser as a string.
Check currentTradeUser in your onLoop and if it has a value, do stuff with it. Finally set it back to an empty string when your finished with it.

@ Override // Remove space
public void onMessage(Message msg)
{
	if(msg.getMessage.contains("wishes to trade")
		currentTradeUser = msg.getMessage().split(" ")[0];
}

Edit:
Also you would want to check if the message has color, I think that color is actually stored in the string as an HTML color or something. Otherwise people could just say "wishes to trade" in chat and it would trigger it and break shit.

Edited by venetox
  • Like 1
Link to comment
Share on other sites

Used -

Player player = players.closest("Mule name!");
        if(player.isInteracting(myPlayer())){
            log= "We detected that our mule is trading us";
            condition = true;

 

Thinking this will be fine, I have a Wait4Player Method that returns a state also. - State that starts the trade/


EDIT: The first code structure I had was nearly using 50 of the CPU power, I just reconstructed the code, and now I am at 6%. Glad I am getting back to the programming I use to be on.

Edited by TheGreatests
Link to comment
Share on other sites

Uhh, having endless struggles with my damn code. If anyone has any time, the issues ive been running with is

1. First code( Script accepted everything, and went through the loop fine. However once it accepted it would lag out to 56% CPU

2. Second Code(Script accepted everything, and went through the loop fine. However once a second trade was iniated, it would get stuck in the TradeAccept() method.

3. I got to this last editting, and it is now not starting. If anyone can give me a good code structuor to go with would help me. Got a headache, and going to bed but will be on tomorrow!.

 

Code -

import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;
import java.lang.String;
import java.text.Normalizer;

import org.osbot.rs07.api.Bank;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Tab;

import javax.swing.*;

@SuppressWarnings("unused")
@ScriptManifest(author = "Elvis", info = "Muler V2.7", name = "Mule Trader V2.7", version = 2.7, logo = "Trades your miner to collect the profit!")
public class ClayMule extends Script{
	private final Area TRADING_AREA = new Area(3185, 3437, 3181, 3434);
	private int claycount;
	private int clay = 435;
	private State state = State.STARTIT;
	private long elapsed;
	private String log;
	private boolean isRunning = true;
	private int world;
	private boolean condition;
	Player player = players.closest("Mule_name");

	private enum State {
		Wait, TradeAccepted, STARTIT,Trade
	}
	@[member=Override]
	public int onLoop() throws InterruptedException {

		switch(state) {
		
		
		case Wait:
			Wait4Player();
			break;
			
		case Trade:
			Trade();
			break;
			
		case TradeAccepted:
			TradeAccept();
			break;
			
		case STARTIT:
			STARTIT();
			break;
		}
		return random(120, 160);
	}
	@[member=Override]
	public void onStart() {
		isRunning = true;

		random(18, 32);
		world = worlds.getCurrentWorld();



		Thread runTimeThread = new Thread() {

			@[member=Override]
			public void run() {
				try {
					RunTime();
				}
				catch (Exception e) {
					log(e.getMessage());
				}
			}
		};

		runTimeThread.start();
	}

	private void Wait4Player() throws InterruptedException {
		Player player = players.closest("Mule_name");
		if(player.isInteracting(myPlayer())){
			log= "We detected that our mule is trading us";
			condition = true;
			log=("Our mule has activated one of our conditions to trade");
			state =State.Trade;
			log=("Switching to trade method");
			return;
		}
		state = State.TradeAccepted;
	}
	private void TradeAccept() throws InterruptedException {

		//if(isTrading()) {

			if(trade.isFirstInterfaceOpen()) {
				//log=("We are in the first trade interface");
				//if(!trade.offer(435, inventory.getItem(clay).getAmount())) return;
				log=("We are in the first trade interface");
				sleep(880, 1350);
				RandomizeMouse(); RandomSleep();

				trade.acceptTrade(); sleep(1200, 2300); RandomizeMouse();
				log=("Accepted first trade interface");
				while(isTrading() && !trade.didOtherAcceptTrade());
			}

			if(trade.isSecondInterfaceOpen()) {
				RandomSleep();
				trade.acceptTrade(); sleep(150, 300); RandomizeMouse();
				log=("Accepted second trade interface");
				while(isTrading() && !trade.didOtherAcceptTrade());
			Wait4Player();
			}
		//	STARTIT();
	//	}
	}

	private void Trade() throws InterruptedException {
		sleep(1600, 2500);
		//if(isTrading()) {
		//	log=("We are trading");
		//	TradeAccept();
	//	}
		//else {
			if(getWorlds().getCurrentWorld() != 383) {
				log=("Detected we are in not in the world of our mule, hopping to world");
				worlds.hop(383); sleep(8*1000, 14*1000);
				return;
			}


			if(!TRADING_AREA.contains(myPlayer())) {
				log=("Moving to trade area");
				MoveToZone(TRADING_AREA);
				sleep(880, 1350);
				return;
			}


			//Player player = players.closest("MuleName"); lets try this

			if(player != null && condition == true) {
				if(!player.isVisible()) 
					camera.toEntity(player);
				sleep(650, 2450);
				player.hover(); sleep(400, 850);
				if(player.interact("Trade with")) {
					log=("Trading with Mule");
					condition = false;
					log("Our condition is now false, until mule trades us again");
					sleep(1250, 1650); RandomizeMouse(); sleep(4500, 6500);
					return;
				}
			}
		}
	//}
	private void STARTIT() throws InterruptedException {
		log=("Idling until our mule activates us...");
		state = State.Wait;
		return;
	}
	@[member=Override]
	public void onExit() {
		isRunning = false;
		log="We are finished";
	}

	@[member=Override]
	public void onPaint(Graphics2D g) {
		claycount = (int) inventory.getAmount(434);
		int x = getMouse().getPosition().x;
		int y = getMouse().getPosition().y;

		g.drawLine(0, y, 765, y);
		g.drawLine(x, 0, x, 503);

		DrawUpdate(g);
	}


	private void DrawUpdate(Graphics2D g) {

		g.setColor(new Color(0, 0, 0, 80));
		g.fillRect(4, 280, 512, 58);

		g.setColor(Color.white);
		g.setFont(new Font("Consolas", Font.PLAIN, 12));
		g.drawString("RunTime: " + GetRunTime(), 14, 300);

		g.drawString("Clay traded over " + claycount, 170, 300);
		g.drawString("Status: " + log, 219, 325);
	}

	private void RunTime() throws InterruptedException {

		long startTime = System.currentTimeMillis();

		while(isRunning) {
			elapsed = (System.currentTimeMillis() - startTime);
			sleep(1000);
		}
	}

	/*private void LaunchGUI() {

	}

	private void CloseGUI() {

	} 
	 */



	private void sleep(int min, int max) throws InterruptedException {
		sleep(random(min, max));
	}

	private String GetRunTime() {

		long hours = (elapsed / (1000 * 60 * 60)) % 24;
		long minutes = (elapsed / (1000 * 60)) % 60;
		long seconds = (elapsed / 1000) % 60;

		return String.format("%02d:%02d:%02d", hours, minutes, seconds);
	}




	private void MoveToZone(Area zone) throws InterruptedException {

		walking.webWalk(zone.getRandomPosition());
	}

	private boolean isTrading() {
		return trade.isCurrentlyTrading();
	}

	private boolean isInRadius(NPC npc) {
		Player me = myPlayer();

		int x = me.getX(),
				y = me.getY();

		return new Area(x + 1, y + 1, x - 1, y - 1).contains(npc);
	}

	private void RandomizeMouse() {
		switch(random(0, 5)) {
		case 0:
			mouse.moveRandomly();
			break;
		case 1:
			mouse.moveOutsideScreen();
			break;
		case 2:
			mouse.moveSlightly();
			break;
		}
	}

	private void RandomSleep() throws InterruptedException {

		switch(random(0, 5)) {
		case 0:
			sleep(650, 1250);
			break;

		case 1:  
			sleep(1450, 2050);
			break;

		case 2: 
			sleep(2250, 2850);
			break;

		case 3:
			sleep(3050, 3850);
			break;
		}
	}
}


Link to comment
Share on other sites

If your lagging out to 55% cpu. Check your logger. That normally happens from your logger being spammed with errors. I'm a bit too burnt out to even look at code atm. Will have a look tomorrow and see if I can help.

 

Sounds good, I might be using a node structure for this soon. I just dont got time atm.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...