Jump to content

Easy Trade Method


LoudPacks

Recommended Posts

	protected abstract static class Widgets {

		public static boolean containsText(int parent, int child, String text) {
			RS2Widget widget = s.getWidgets().get(parent, child);
			if (exists(widget) && widget.isVisible()) {
				return widget.getMessage().contains(text);
			}
			return false;
		}

		public static boolean interactTil(String action, int i, int j, int k, ConditionalSleep con) {
			RS2Widget widget = s.getWidgets().get(i, j, k);
			if (exists(widget)) {
				widget.interact(action);
				con.sleep();
				return true;
			}
			return false;
		}

		public static String getText(int i, int j) {
			RS2Widget widget = s.getWidgets().get(i, j);
			if (exists(widget) && widget.isVisible()) {
				return widget.getMessage();
			}
			return "";
		}

		private static boolean exists(RS2Widget widget) {
			return widget != null;
		}


		public static boolean interactTil(String action, int parent, int child, ConditionalSleep con) {
			RS2Widget widget = s.getWidgets().get(parent, child);
			if (exists(widget)) {
				widget.interact(action);
				con.sleep();
				return true;
			}
			return false;
		}

		public static boolean interactTilNull(String action, int parent, int child) {
			RS2Widget widget = s.getWidgets().get(parent, child);
			if (exists(widget)) {
				widget.interact(action);
				new ConditionalSleep(2500, 3000) {
					@Override
					public boolean condition() {
						return !exists(widget);
					}
				}.sleep();
				return true;
			}
			return false;
		}

		public static boolean interactTilNull(String action, int parent, int child, int child2) {
			RS2Widget widget = s.getWidgets().get(parent, child, child2);
			if (exists(widget)) {
				widget.interact(action);
				new ConditionalSleep(2500, 3000) {
					@Override
					public boolean condition() {
						return !exists(widget);
					}
				}.sleep();
				return true;
			}
		return false;
		}

	}
protected abstract static class Trade {

		public static boolean isTrading() {
			return s.getTrade().isCurrentlyTrading() || s.getTrade().isFirstInterfaceOpen() || s.getTrade().isSecondInterfaceOpen();
		}

		public static void trade(String name, HashMap<String, Integer> itemSet, boolean acceptLast) {
			String cleanName = name.replaceAll(" ", "\\u00a0");
			Player player = s.getScript().getPlayers().closest(cleanName);
			if (player != null && !isTrading() && player.interact("trade with")) {
				new ConditionalSleep(3000, 4000) {
					@Override
					public boolean condition() {
						return isTrading();
					}
				}.sleep();
			}

			if (isTrading() && s.getTrade().isFirstInterfaceOpen()) {
				if (!tradeOfferMatches(itemSet)) {
					for (String item : itemSet.keySet()) {
						if (!s.getTrade().getOurOffers().contains(item)) {
							if (s.getTrade().offer(item, itemSet.get(item))) {
								new ConditionalSleep(2000, 3000) {

									@Override
									public boolean condition() {
										return s.getTrade().getOurOffers().contains(item);
									}
								}.sleep();
							}
						}
					}
				} else {
					if (acceptLast && s.getTrade().didOtherAcceptTrade()) {
						if (Widgets.interactTil("Accept", 335, 11, new ConditionalSleep(1500, 2000) {
							@Override
							public boolean condition() {
								return s.getTrade().isSecondInterfaceOpen();
							}
						})) {
							new ConditionalSleep(3000, 4000) {
								@Override
								public boolean condition() {
									return s.getTrade().isSecondInterfaceOpen();
								}
							}.sleep();
						}

					} else if (!acceptLast && !hasAccepted()) {
						if (Widgets.interactTil("Accept", 335, 11, new ConditionalSleep(1500, 2000) {
							@Override
							public boolean condition() {
								return hasAccepted();
							}
						})) {
							new ConditionalSleep(3000, 4000) {

								@Override
								public boolean condition() {
									return s.getTrade().isSecondInterfaceOpen();
								}
							}.sleep();
						}
					}

				}
			} else if (isTrading() && s.getTrade().isSecondInterfaceOpen()) {
				
					if (acceptLast && s.getTrade().didOtherAcceptTrade()) {
						if (Widgets.interactTil("Accept", 334, 25, new ConditionalSleep(1500, 2000) {
							@Override
							public boolean condition() {
								return !isTrading();
							}
						})) {
							new ConditionalSleep(3000, 4000) {

								@Override
								public boolean condition() {
									return s.getTrade().isSecondInterfaceOpen();
								}
							}.sleep();
						}

					} else if (!acceptLast && !hasAccepted()) {
						if (Widgets.interactTil("Accept", 334, 25, new ConditionalSleep(1500, 2000) {
							@Override
							public boolean condition() {
								return !isTrading();
							}
						})) {
							new ConditionalSleep(3000, 4000) {
								@Override
								public boolean condition() {
									return s.getTrade().isSecondInterfaceOpen();
								}
							}.sleep();
						}

					}
			}
		}

		private static boolean hasAccepted() {
			return Widgets.containsText(335, 30, "Waiting for other player...") || Widgets.containsText(334, 4, "Waiting for other player...");
		}

		private static boolean tradeOfferMatches(HashMap<String, Integer> itemSet) {
			for (String item : itemSet.keySet()) {
				if (isTrading() && s.getTrade().getOurOffers().getItem(item) == null) {
					s.log("Trade Offer Missing: " + item);
					return false;
				}
			}
			return true;
		}
	}

Note: you can make it non static / abstract just remove the s.* thats just how my api is.

 

Usage: 

HashMap<String, Integer> itemMap = new HashMap<String, Item>();
itemMap.put("Shark", 12);
itemMap.put("Coins", 42069);
private int onLoop(){
if(shouldTrade())
     trade("Zezima", itemMap, false); //trades zezima the itemMap and doesnt wait for them to accept.
return 250;
}
Edited by LoudPacks
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

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...