Jump to content

Welcome to DreamBot

VIP Enhancement

Want to upgrade your DreamBot experience? Consider signing up for VIP!
VIP allows you to run as many accounts as you want, view the forums ad-free, receive 10% off all script purchases, and so much more!
Visit the store to learn about all of these great features!

Upgrade to VIP Now
Frequently Asked Questions
  • Are you not able to open the client? Make sure you have Java 8 installed
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
Download the DreamBot client today!
Sign in to follow this  
Andrew74

W308 Anvil Assistant

Recommended Posts

This plugin removes the tedious job of manually calculating the total cost of alchables when buying from other players in smithing clans.

Displays a HUD of how many Rune 2h/Addy PL8/Mith PL8 can be purchased and the cost of the quantity.

In addition, it will automatically offer the correct amount of coins and complete a trade when purchasing alchables from other players. (It does not accept trades automatically; the player must initiate a trade for this feature to take over control)

Prices are based off the "W308 Anvil" clan chat. Prices can be adjusted in the plugin variables. (_r2hPrice, _addyPl8Price, ...)

This is my first DB plugin/script; if there are any obvious mistakes, please let me know. :)

Regards,
Andrew74

Untitled.png.c9d5bd328832957571781c2fe3e08589.png

 

package mainplugin;

import java.awt.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.dreambot.api.methods.Calculations;
import org.dreambot.api.methods.widget.Widget;
import org.dreambot.api.plugin.Plugin;
import org.dreambot.api.plugin.PluginManifest;
import org.dreambot.api.script.listener.AdvancedMessageListener;
import org.dreambot.api.wrappers.items.Item;
import org.dreambot.api.wrappers.widgets.WidgetChild;
import org.dreambot.api.wrappers.widgets.message.Message;

@PluginManifest(author = "Andrew74", name = "W308 Anvil", version = 3.9)
public class MainPlugIn extends Plugin  {

    private String _Version = "3.9";

    private boolean
        _exited = false,
        _scriptPausec = false;

    private Font font;
    private Thread thread;
    private DecimalFormat formatter;

    private Item
                _invCoins = null,
                _myOfferCoins = null;
    private int
            _r2hQty = 0,
            _addyPl8Qty = 0,
            _mithPl8Qty = 0,

            _r2hBalanceDue = 0,
            _addyPl8BalanceDue = 0,
            _mithPl8BalanceDue = 0;

    private final int
            _r2hPrice = 37950,
            _addyPl8Price = 9600,
            _mithPl8Price = 2800;

    private String
            _plugInStatus = null,
            _tradingWithUsername = null;

    private long
            _tradeWindowOpen = 0,
            _tradeWindowZeroBalance = 0;

    public HashMap<String, Integer>
            _avoidPlayers;

    public void onStart() {
        font = new Font("Arial", 0, 11);
        formatter = new DecimalFormat("#,###");
        _avoidPlayers = new HashMap<>();

        log("Starting Plugin");

        thread = new Thread(){
            public void run(){
                log("Thread Running");
                while(!_exited && getClient().getInstance().getPluginManager().isPluginRunning(MainPlugIn.class)) {
                    try {
                        sleep(50);
                        // _plugInStatus = "Idle";

                        _invCoins = getInventory().get(i -> i != null && i.getName().equals("Coins"));

                        _r2hQty = 0;
                        _addyPl8Qty = 0;
                        _mithPl8Qty = 0;

                        _r2hBalanceDue = 0;
                        _addyPl8BalanceDue = 0;
                        _mithPl8BalanceDue = 0;

                        if(getTrade().isOpen()) {

                            WidgetChild _username = GetWidget_HELPER(335, 31, -1);
                            if(_username != null && _username.isVisible())
                                _tradingWithUsername = _username.getText().replace("Trading With: ", "");

                            if(_tradingWithUsername == null || _tradingWithUsername.length() == 0) {
                                _plugInStatus = "Username not found";
                                continue;
                            }

                            // Avoid any annoying players. Normal players shouldn't hit this threshold
                            if(GetUserPoints(_tradingWithUsername) > Calculations.random(7, 12))
                            {
                                _plugInStatus = "Declined trade - User avoided";
                                getTrade().declineTrade();
                                sleepUntil(()->!getTrade().isOpen(), 10000);
                                continue;
                            }

//                            if(!_scriptPausec) {
//                                if(getClient().getInstance().getScriptManager().isRunning()) {
//                                    getClient().getInstance().getScriptManager().pause();
//                                    sleepUntil(() -> getClient().getInstance().getScriptManager().isPaused(), 5000);
//                                    _scriptPausec = true;
//                                }
//                            }

                            // Record when the trade window opened
                            if(_tradeWindowOpen == 0)
                                _tradeWindowOpen = System.currentTimeMillis();

                            // Record when the trade window opened
                            if(_tradeWindowZeroBalance == 0)
                                _tradeWindowZeroBalance = System.currentTimeMillis();

                            _plugInStatus = "Trade Init with " + _tradingWithUsername;

                            _myOfferCoins = null;

                            Item[] _thierItems = getTrade().getTheirItems();
                            if (_thierItems != null) {
                                for (Item _i : _thierItems) {
                                    if (_i != null) {
                                        if (_i.getName().equalsIgnoreCase("Rune 2h sword")) {
                                            _r2hQty = _i.getAmount();
                                            _r2hBalanceDue = _i.getAmount() * _r2hPrice;
                                        } else if (_i.getName().equalsIgnoreCase("Adamant platebody")) {
                                            _addyPl8Qty = _i.getAmount();
                                            _addyPl8BalanceDue = _i.getAmount() * _addyPl8Price;
                                        } else if (_i.getName().equalsIgnoreCase("Mithril platebody")) {
                                            _mithPl8Qty = _i.getAmount();
                                            _mithPl8BalanceDue = _i.getAmount() * _mithPl8Price;
                                        }
                                    }
                                }
                            }

                            Item[] _myItems = getTrade().getMyItems();

                            if (_myItems != null) {
                                for (Item _m : _myItems) {
                                    if (_m != null && _m.getName().equalsIgnoreCase("Coins")) {
                                        _myOfferCoins = _m;
                                        break;
                                    }
                                }
                            }

                            int _totalBalance = _r2hBalanceDue + _addyPl8BalanceDue + _mithPl8BalanceDue;

                            _plugInStatus = "Waiting for player offer";
                            if(_totalBalance > 0) {

                                _tradeWindowZeroBalance = System.currentTimeMillis();

                                if (getTrade().isOpen(1)) {
                                    _plugInStatus = "Trade Window 1 Opened";

                                    if (_myOfferCoins != null) {
                                        if (_myOfferCoins.getAmount() != _totalBalance) {
                                            _plugInStatus = "Resetting Offer";
                                            final Item _tradeCoinsLamba = _myOfferCoins;
                                            getTrade().removeItem(_tradeCoinsLamba, _tradeCoinsLamba.getAmount());
                                            _myOfferCoins = null;
                                            sleep(Calculations.random(100, 750));
                                        }
                                    }

                                    if (_myOfferCoins == null && _invCoins != null) {
                                        sleep(Calculations.random(3000, 7000));

                                        if (_totalBalance <= _invCoins.getAmount()) {
                                            _plugInStatus = "Offering Balance Due";
                                            final Item _tradeCoinsLamba = _myOfferCoins;
                                            getTrade().addItem(_invCoins, _totalBalance);
                                            sleep(Calculations.random(100, 750));
                                        } else {
                                            _plugInStatus = "Insuffient funds";
                                            getTrade().declineTrade(1);

                                            // Player putting too much in
                                            AddPointToUser(_tradingWithUsername, Calculations.random(1, 2));
                                        }
                                    }
                                    else if (_myOfferCoins != null) {
                                        if (_myOfferCoins.getAmount() <= _totalBalance) {
                                            _plugInStatus = "Accepting Trade Window 1";

                                            getTrade().acceptTrade(1);
                                            sleep(Calculations.random(100, 250));

                                            // If the other user changed the offer, we need to update our offer
                                            sleepUntil(() -> {
                                                WidgetChild w2 = GetWidget_HELPER(335, 30, -1);
                                                return getTrade().isOpen(2) || (w2 != null && w2.isVisible() && !w2.getText().equalsIgnoreCase("Waiting for other player..."));
                                            }, Calculations.random(10000, 15000));

                                            WidgetChild w = GetWidget_HELPER(335, 30, -1);
                                            // Other player never accepted or changed the offer
                                            if (w != null && w.isVisible() && w.getText().equalsIgnoreCase("Waiting for other player...")) {
                                                if (!getTrade().isOpen(2)) {
                                                    _plugInStatus = "Trade Declined (Trade window 1 timeout)";
                                                    getTrade().declineTrade(1);

                                                    // Player didn't accept the last window and let timeout
                                                    AddPointToUser(_tradingWithUsername, Calculations.random(1, 3));
                                                }
                                            } else {
                                                _plugInStatus = "Offer updated?";
                                                sleep(Calculations.random(500, 1500));
                                            }
                                        }
                                    }
                                } else if (getTrade().isOpen(2)) {
                                    _plugInStatus = "Trade Window 2 Opened";
                                    if (_myOfferCoins != null && _myOfferCoins.getAmount() <= _totalBalance) {
                                        _plugInStatus = "Accepting Trade Window 2";
                                        sleep(Calculations.random(100, 2000));
                                        getTrade().acceptTrade(2);

                                        sleepUntil(() -> !getTrade().isOpen(), Calculations.random(10000, 15000));

                                        if (getTrade().isOpen(2)) {
                                            _plugInStatus = "Trade Declined (Trade window 2 timeout)";
                                            getTrade().declineTrade(2);

                                            // Player didn't accept the last window and let timeout
                                            AddPointToUser(_tradingWithUsername, Calculations.random(1, 2));
                                        } else {
                                            _plugInStatus = "Trade Accepted...?";
                                        }
                                    } else {
                                        _plugInStatus = "Declining Trade Window 2";
                                        sleep(Calculations.random(250, 1000));
                                        getTrade().declineTrade(2);

                                        // User modified the trade to be unfair
                                        AddPointToUser(_tradingWithUsername, Calculations.random(1, 3));
                                    }
                                }
                            } else {
                                if((System.currentTimeMillis() - _tradeWindowZeroBalance) > Calculations.random(15000, 20000)) {
                                    _plugInStatus = "Declined trade - zero balance max timeout reached";
                                    getTrade().declineTrade();
                                    sleepUntil(()->!getTrade().isOpen(), 10000);
                                    _tradeWindowZeroBalance = 0;

                                    // User left the trade window time out
                                    AddPointToUser(_tradingWithUsername, Calculations.random(2, 4));
                                }
                            }

                            if((System.currentTimeMillis() - _tradeWindowOpen) > Calculations.random(35000, 60000)) {
                                _plugInStatus = "Declined trade - max timeout reached";
                                getTrade().declineTrade();
                                sleepUntil(()->!getTrade().isOpen(), 10000);
                                _tradeWindowOpen = 0;

                                // User let the trade window time out
                                AddPointToUser(_tradingWithUsername, Calculations.random(2, 4));
                            }
                        }
                        else {

                            // User canceled the trade add a point
                            AddPointToUser(_tradingWithUsername, Calculations.random(1, 2));

                            // Reset when the trade window is closed
                            _tradeWindowZeroBalance = 0;
                            _tradeWindowOpen = 0;
                            _tradingWithUsername = null;

                            // Reinstate the bot
//                            if(_scriptPausec) {
//                                if(getClient().getInstance().getScriptManager().isPaused()) {
//                                    getClient().getInstance().getScriptManager().resume();
//                                    sleepUntil(() -> getClient().getInstance().getScriptManager().isRunning(), 5000);
//                                    _scriptPausec = false;
//                                }
//                            }

                        }
                    } catch (Exception ee) {
                        log(ee.toString());
                    }
                }
                log("Thread Exited");
            }
        };

        log("Starting Plugin Thread");
        thread.start();
        log("Started Plugin Thread");
    }

    public void onExit() {
        _exited = true;
    }

    public void onPaint(Graphics g) {

        g.setFont(font);
        g.setColor(Color.green);

        if(_invCoins != null) {
            Rectangle _rec2 = getInventory().slotBounds(_invCoins.getSlot());

            double _price = _r2hPrice,
                    _qty = Math.floor(_invCoins.getAmount() / _price);

            g.drawString("R2H: " + (int) _qty + " (" + formatter.format((int) (_qty * _price)) + ")", _rec2.x - 10, _rec2.y + _rec2.height + 15);

            _price = _addyPl8Price;
            _qty = Math.floor(_invCoins.getAmount() / _addyPl8Price);

            g.drawString("Addy PL8: " + (int) _qty + " (" + formatter.format((int) (_qty * _price)) + ")", _rec2.x - 10, _rec2.y + _rec2.height + 30);

            _price = _mithPl8Price;
            _qty = Math.floor(_invCoins.getAmount() / _mithPl8Price);

            g.drawString("Mith PL8: " + (int) _qty + " (" + formatter.format((int) (_qty * _price)) + ")", _rec2.x - 10, _rec2.y + _rec2.height + 45);
        }

        if(getTrade().isOpen(1)) {
            int _totalBalance = _r2hBalanceDue + _addyPl8BalanceDue + _mithPl8BalanceDue;
            WidgetChild w = GetWidget_HELPER(335, 28, 0);
            if (w != null && w.isVisible()) {
                g.drawString("Total: " + formatter.format(_totalBalance) + " (R:" + _r2hQty + ", A:" + _addyPl8Qty + ", M:" + _mithPl8Qty + ")", w.getRectangle().x, w.getRectangle().y + w.getRectangle().height + 15);
            }

            if(_invCoins == null || ((_myOfferCoins != null ? _myOfferCoins.getAmount() : 0) + _invCoins.getAmount()) < _totalBalance) {
                WidgetChild _myInv_slot_1 = GetWidget_HELPER(335, 25, 0);

                if (_myInv_slot_1 != null && _myInv_slot_1.isVisible()) {
                    g.setColor(Color.red);
                    g.drawString("Insufficient funds: (" + formatter.format(_totalBalance - (_invCoins == null ? 0 : _invCoins.getAmount())) + ")", _myInv_slot_1.getX() + 15, _myInv_slot_1.getY() + 15);
                }
            }
        }

        if(_plugInStatus != null) {
            g.drawString(_plugInStatus, 24, 36);
        }

        g.setColor(Color.yellow);
        g.drawString(_Version, 745, 15);
    }

    private WidgetChild GetWidget_HELPER(int PC, int C, int GC) {
        Widget pw = null;
        WidgetChild w = null;

        pw = getWidgets().getWidget(PC);

        if(pw != null) {
            w = pw.getChild(C);

            if(GC > -1) {
                if (w != null) {
                    w = w.getChild(GC);
                } else w = null;
            }
        }

        return w;
    }

    private void AddPointToUser(String _username, int _value) {
        if(_avoidPlayers.containsKey(_username))
            _avoidPlayers.put(_username, _avoidPlayers.get(_username) + _value);
        else
            _avoidPlayers.put(_username, _value);
    }

    private void ClearUserPoints(String _username) {
        if(_avoidPlayers.containsKey(_username))
            _avoidPlayers.remove(_username);
    }

    private int GetUserPoints(String _username) {
        if(_avoidPlayers.containsKey(_username))
            return _avoidPlayers.get(_username).intValue();
        return 0;
    }
}

 

Share this post


Link to post
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.

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.

Sign in to follow this  

×
×
  • Create New...