Jump to content

Trade Handler


BravoTaco

Recommended Posts

7 hours ago, BravoTaco said:

Hopefully this works, haven't tested. Let me know if it doesn't

  Hide contents


//Use this when the item to receive, is not already in the inventory.
public boolean itemReceived(String itemName, int amountToReceive) {
    Item temp;
    return (temp = mp.getInventory().getItem(itemName)) != null && temp.getAmount() == amountToReceive;
}

//Use this when the item to receive, is already in the inventory.
public boolean itemReceivedWithSameItemInInventory(String itemName, int startAmount, int amountToReceive) {
    Item temp;
    return (temp = mp.getInventory().getItem(itemName)) != null && temp.getAmount() - startAmount == amountToReceive;
}

//Use this when the item you are trading will have no left-over amounts.
public boolean itemTraded(String itemName) {
    return mp.getInventory().getItem(itemName) == null;
}

//Use this when the item you are trading will have left-over amounts.
public boolean itemTradedWithLeftOverItemAmount(String itemName, int startAmount, int amountToTrade) {
    Item temp;
    return (temp = mp.getInventory().getItem(itemName)) == null || temp.getAmount() + amountToTrade == startAmount;
}


 

Could you please show an example on how to use this?

Link to comment
Share on other sites

10 hours ago, danielmedvec said:

Could you please show an example on how to use this?

Sure can 🙂 

Trading Items:

Spoiler

//Something like this if you have more of an item in the inventory than you are attempting to trade
//IE You have 200 Iron arrows in the inventory but only want to trade 100 of them.
if (trader.trade("Player")) {
    if (trader.offerItem("Iron arrow", 100)) {
        if (trader.acceptTrade(true)) {
            // Item Name, The Amount that you started with in the inventory, And the amount you wanted to trade
            if (trader.itemTradedWithLeftOverItemAmount("Iron arrow", 200, 100)) {
                log("Item Successfully Traded!");
            } else {
                log("Trade Unsuccessful!");
            }
        }
    }
}

//This will be for if you will not have the item left-over in the inventory
//IE You have 200 Iron arrows in the inventory and are trading all of them.
if (trader.trade("Player")) {
    if (trader.offerItem("Iron arrow", 100)) {
        if (trader.acceptTrade(true)) {
            // Item Name
            if (trader.itemTraded("Iron arrow")) {
                log("Item Successfully Traded!");
            } else {
                log("Trade Unsuccessful!");
            }
        }
    }
}

Receiving Items:

Spoiler

//This will be for receiving an item when you already had the same item in the inventory
//IE You have 100 Iron arrows in the inventory and are expecting to receive 200 more
if (trader.trade("Player")) {
    if (trader.acceptTrade(true)) {
        // Item Name, The Amount that you started with in the inventory, And the amount you are expecting to receive
        if (trader.itemReceivedWithSameItemInInventory("Iron arrow", 100, 200)) {
            log("Item Successfully Received!");
        } else {
            log("Trade Unsuccessful!");
        }
    }
}

//This will be for receiving an item when you don't already have the item in the inventory
//IE You are expecting to receive 100 iron arrows
if (trader.trade("Player")) {
    if (trader.acceptTrade(true)) {
        // Item Name And the amount you are expecting to receive
        if (trader.itemReceived("Iron arrow", 100)) {
            log("Item Successfully Received!");
        } else {
            log("Trade Unsuccessful!");
        }
    }
}

 

Edited by BravoTaco
  • Heart 1
Link to comment
Share on other sites

30 minutes ago, BravoTaco said:

Sure can 🙂 

Trading Items:

  Hide contents


//Something like this if you have more of an item in the inventory than you are attempting to trade
//IE You have 200 Iron arrows in the inventory but only want to trade 100 of them.
if (trader.trade("Player")) {
    if (trader.offerItem("Iron arrow", 100)) {
        if (trader.acceptTrade(true)) {
            // Item Name, The Amount that you started with in the inventory, And the amount you wanted to trade
            if (trader.itemTradedWithLeftOverItemAmount("Iron arrow", 200, 100)) {
                log("Item Successfully Traded!");
            } else {
                log("Trade Unsuccessful!");
            }
        }
    }
}

//This will be for if you will not have the item left-over in the inventory
//IE You have 200 Iron arrows in the inventory and are trading all of them.
if (trader.trade("Player")) {
    if (trader.offerItem("Iron arrow", 100)) {
        if (trader.acceptTrade(true)) {
            // Item Name
            if (trader.itemTraded("Iron arrow")) {
                log("Item Successfully Traded!");
            } else {
                log("Trade Unsuccessful!");
            }
        }
    }
}

Receiving Items:

  Hide contents


//This will be for receiving an item when you already had the same item in the inventory
//IE You have 100 Iron arrows in the inventory and are expecting to receive 200 more
if (trader.trade("Player")) {
    if (trader.acceptTrade(true)) {
        // Item Name, The Amount that you started with in the inventory, And the amount you are expecting to receive
        if (trader.itemReceivedWithSameItemInInventory("Iron arrow", 100, 200)) {
            log("Item Successfully Received!");
        } else {
            log("Trade Unsuccessful!");
        }
    }
}

//This will be for receiving an item when you don't already have the item in the inventory
//IE You are expecting to receive 100 iron arrows
if (trader.trade("Player")) {
    if (trader.acceptTrade(true)) {
        // Item Name And the amount you are expecting to receive
        if (trader.itemReceived("Iron arrow", 100)) {
            log("Item Successfully Received!");
        } else {
            log("Trade Unsuccessful!");
        }
    }
}

 

Thank you so much, i appreciate it! :)

Link to comment
Share on other sites

32 minutes ago, danielmedvec said:

How do you completely decline the trade?

Say in case there was someone who have declined like 3 times, and i just want to cancel this trade.
How do i make the process stop? Because i tried decline trade, but it still kept trading.

Best regards :)

Do you mean like not trade that person again? If so this will have to be done in your own script logic.

An example would be to add that player to a blacklist after he has declined the trade or keep track of how many times that player has declined the trade and if it exceeds a certain threshold add them to the blacklist to not be traded again.

Example Code Without Threshold (Untested):

Spoiler

private Trader trader;
private ArrayList<String> blackListedNames = new ArrayList<>();

@Override
public void onStart() throws InterruptedException {
    trader = new Trader(this);
}

@Override
public int onLoop() throws InterruptedException {

    String playerName = "PlayerName";

    if (!blackListedNames.contains(playerName) && trader.trade(playerName)) {
        if (trader.offerItem("Iron arrow", 100)) {
            if (trader.acceptTrade(true)) {
                if (trader.itemTradedWithLeftOverItemAmount("Iron arrow", 200, 100)) {
                    log("Item Successfully Traded!");
                } else {
                    log("Trade Unsuccessful!");
                    blackListedNames.add(playerName);
                }
            }
        }
    } else {
        //Set the player name to a different player name.
    }

    return random(800, 3000);
}

Example Code With Threshold (Untested):

Spoiler

private Trader trader;
private HashMap<String, Integer> playerTradeHistory = new HashMap<>();
private int threshold = 3;

@Override
public void onStart() throws InterruptedException {
    trader = new Trader(this);
}

@Override
public int onLoop() throws InterruptedException {

    String playerName = "PlayerName";

    if (playerTradeHistory.containsKey(playerName) && playerTradeHistory.get(playerName) < threshold && trader.trade(playerName)
            || !playerTradeHistory.containsKey(playerName) && trader.trade(playerName)) {
        if (trader.offerItem("Iron arrow", 100)) {
            if (trader.acceptTrade(true)) {
                if (trader.itemTradedWithLeftOverItemAmount("Iron arrow", 200, 100)) {
                    log("Item Successfully Traded!");
                    //This will remove the player if they had declined some trades but finally accepted one of them.
                    if(playerTradeHistory.containsKey(playerName))
                        playerTradeHistory.remove(playerName);
                } else {
                    log("Trade Unsuccessful!");
                    if (playerTradeHistory.containsKey(playerName)) {
                        int oldValue = playerTradeHistory.get(playerName);
                        playerTradeHistory.replace(playerName, oldValue, oldValue + 1);
                    } else {
                        playerTradeHistory.put(playerName, 1);
                    }
                }
            }
        }
    } else {
        //Set the player name to a different player name.
    }

    return random(800, 3000);
}

 

Link to comment
Share on other sites

  • 2 years later...
  • 1 year later...

Can confirm it works, only if on the second trading screen, the player who gets offered the trade accepts before the bot/script running player presses accept, it will fail and indeed give a NPE. 
 

this is because it detects the text on top, when the normal player accepts, the bot gets to see “other player has accepted” and tries to click that instead of the accept trade button. 
 

lovely snippet thanks.

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