Thread: [718] Instant Buy/Sell Grand Exchange at Market Value

Results 1 to 1 of 1
  1. #1 [718] Instant Buy/Sell Grand Exchange at Market Value 
    Registered Member
    Join Date
    May 2013
    Posts
    60
    Thanks given
    18
    Thanks received
    6
    Rep Power
    36
    Hello, it has been a few years since I've been away from the rsps scene, and I was wondering if anyone had any pointers on how to configure the grand exchange system to auto buy/sell items at market value, below I will post the Grand exchange configs, loader and system. If at all anyone could help I would greatly appreciate it.

    Spoiler for Configs:
    package com.rs.game.player.content.grandExchange;

    public class GrandExchangeConfigurations {

    /**
    * The interface id of the main interface
    */
    protected static final int MAIN_INTERFACE = 105;

    /**
    * The interface id of the sell interface
    */
    protected static final int SELL_INTERFACE = 107;

    /**
    * The array of the buttons used to send the buying interfaces
    */
    public static final int[] BUY_BUTTON_IDS = { 31, 82, 101, 47, 63, 120 };

    /**
    * The array of the buttons used to initaliaze the selling process
    */
    public static final int[] SELL_BUTTON_IDS = { 83, 32, 48, 102, 121, 64 };

    public enum Progress {

    BUY_ABORTED(5),
    SELL_ABORTED(-3),
    RESET(0),
    BUY_PROGRESSING(4),
    FINISHED_BUYING(5),
    SELL_PROGRESSING(11),
    FINISHED_SELLING(13);

    Progress(int value) {
    this.value = value;
    }

    public int getValue() {
    return value;
    }

    private final int value;
    }
    }


    Spoiler for Loader:
    package com.rs.game.player.content.grandExchange;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.ListIterator;
    import java.util.logging.Logger;

    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.reflect.TypeToken;
    import com.rs.game.World;
    import com.rs.game.item.Item;
    import com.rs.game.player.Player;

    public class GrandExchangeLoader {

    public static void initialize() {
    List<GrandExchangeOffer> offerList = load();
    if (offerList == null)
    offerList = new ArrayList<GrandExchangeOffer>();
    ListIterator<GrandExchangeOffer> it = offerList.listIterator();
    while(it.hasNext()) {
    GrandExchangeOffer offer = it.next();
    if (offer.getPrimaryAmount() == 0
    && offer.getSecondaryAmount() == 0
    && offer.getExtraCash() == 0) {
    //System.out.println(offer + " had 0 amount so removed");
    it.remove();
    }
    }
    save(offerList);
    for (GrandExchangeOffer offer : offerList) {
    offers.add(offer);
    }
    logger.info("Completely loaded " + offers.size() + " grand exchange offers.");
    }

    public static void addOffer(GrandExchangeOffer offer) {
    offers.add(offer);
    save(offers);
    }

    public static void removeOffer(GrandExchangeOffer offer) {
    ListIterator<GrandExchangeOffer> it = offers.listIterator();
    while(it.hasNext()) {
    GrandExchangeOffer o = it.next();
    if (o.equals(offer))
    it.remove();
    }
    save(offers);
    }

    public static void saveOffer(GrandExchangeOffer offer) {
    ListIterator<GrandExchangeOffer> it = offers.listIterator();
    while(it.hasNext()) {
    GrandExchangeOffer o = it.next();
    if (o.equals(offer)) {
    it.remove();
    offers.add(offer);
    }
    }
    save(offers);
    }

    /**
    * Gets the offers by the player
    * @param owner The player who you are getting the offers of
    * @return A {@code List} {@code Object} with all of the players offers
    */
    public static List<GrandExchangeOffer> getOffersForPlayer(String owner) {
    List<GrandExchangeOffer> playerOffers = new ArrayList<GrandExchangeOffer>();
    ListIterator<GrandExchangeOffer> it = offers.listIterator();
    while(it.hasNext()) {
    GrandExchangeOffer offer = it.next();
    if (offer.getOwner().equals(owner))
    playerOffers.add(offer);
    }
    return playerOffers;
    }

    /**
    * Where the data will save/read from
    * @return An {@code String} object of the location
    */
    private static String getFileLocation() {
    return "data/grandExchange.json";
    }

    /**
    * Populates a list with all of the data in the {@link #getFileLocation()}
    *
    * @return The list with data
    */
    public static List<GrandExchangeOffer> load() {
    List<GrandExchangeOffer> autospawns = null;
    String json = null;
    try {
    File file = new File(getFileLocation());
    if (!file.exists())
    return null;
    FileReader reader = new FileReader(file);
    char[] chars = new char[(int) file.length()];
    reader.read(chars);
    json = new String(chars);
    reader.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    autospawns = gson.fromJson(json, new TypeToken<List<GrandExchangeOffer>>() {
    }.getType());
    return autospawns;
    }

    /**
    * Saves the list to the file
    *
    * @param spawns
    * The list to save
    */
    public static void save(List<GrandExchangeOffer> spawns) {
    try {
    FileWriter fw = new FileWriter(getFileLocation());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(gson.toJson(spawns));
    bw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * The list of offers
    * @return
    */
    public static List<GrandExchangeOffer> getOffers() {
    return offers;
    }

    /**
    * The gson instance
    */
    private static Gson gson = new GsonBuilder().setPrettyPrinting().create();

    private static Logger logger = Logger.getLogger(GrandExchangeLoader.class.getName ());

    private static final List<GrandExchangeOffer> offers = new ArrayList<GrandExchangeOffer>();

    public static class GrandExchangeOffer {

    /**
    * The constructor for the grand exchange offer
    *
    * @param owner
    * The owner of the offer
    * @param slot
    * The slot of the offer
    * @param item
    * The {@code Item} {@code Object} of the offer
    * @param amount
    * The amount of the item you wish to exchange
    * @param type
    * The {@code Type} enum value of the offer
    */
    public GrandExchangeOffer(String owner, int slot, Item item, int amount, int price, Type type) {
    this.owner = owner;
    this.slot = slot;
    this.item = item;
    this.primaryAmount = amount;
    this.setSecondaryAmount(0);
    this.setPrice(price);
    this.type = type;
    }

    public String getOwner() {
    return owner;
    }

    public Item getItem() {
    return item;
    }

    /**
    * The amount the offer was set to buy/sell for. This value does not change!
    * @return
    */
    public int getPrimaryAmount() {
    return primaryAmount;
    }

    /**
    * The amount of the item that has been bought/sold so far. This value changes when the offer updates
    * @return
    */
    public int getSecondaryAmount() {
    return secondaryAmount;
    }

    /**
    * Sets the amount of the item that has been bought/sold so far.
    * @param secondaryAmount The amount to set it to.
    */
    public void setSecondaryAmount(int secondaryAmount) {
    this.secondaryAmount = secondaryAmount;
    }

    public Type getType() {
    return type;
    }

    public boolean isFinished() {
    return secondaryAmount == getPrimaryAmount();
    }

    public int getSlot() {
    return slot;
    }

    public int getPrice() {
    return price;
    }

    public void setPrice(int price) {
    long price2 = (long) primaryAmount * price;
    if (price2 > Integer.MAX_VALUE || price2 == Integer.MAX_VALUE
    || price2 >= Integer.MAX_VALUE || price2 < 0 || price <= 0) {
    //System.out.println("Attempted to set a bad price.");
    return;
    }
    this.price = price;
    }

    public void setPrimaryAmount(int primaryAmount) {
    long price = (long) primaryAmount * (long) this.price;
    if (price > Integer.MAX_VALUE || price == Integer.MAX_VALUE
    || price >= Integer.MAX_VALUE || price < 0 || price <= 0) {
    //System.out.println("Attempted to set a bad amount.");
    return;
    }
    this.primaryAmount = primaryAmount;
    }

    public void forcePrimaryAmount(int primaryAmount) {
    this.primaryAmount = primaryAmount;
    }

    /**
    * The extra cash from the offer, this is incremented if while
    * exchanging, either player is overpaying
    *
    * @return
    */
    public int getExtraCash() {
    return extraCash;
    }

    public void setExtraCash(int extraCash) {
    this.extraCash = extraCash;
    }

    @Override
    public boolean equals(Object o) {
    GrandExchangeOffer o2 = (GrandExchangeOffer) o;
    return owner.equals(o2.getOwner()) && item.getId() == o2.getItem().getId() && slot == o2.getSlot() && price == o2.getPrice() && primaryAmount == o2.getPrimaryAmount() && type == o2.getType();
    }

    public enum Type {
    BUY, SELL
    }

    @Override
    public String toString() {
    return "[owner=" + owner + ", type=" + type + ", slot=" + slot + ", price=" + price + ", primaryAmount=" + getPrimaryAmount() + ", secondaryAmount=" + secondaryAmount + "]";
    }

    public void notifyUpdated() {
    Player player = World.getPlayerByDisplayName(owner);
    if (player != null) {
    if (player.getInterfaceManager().containsInterface(10 5) || player.getInterfaceManager().containsTab(105))
    GrandExchangeSystem.get().display(player);
    player.sendMessage("One or more of your grand exchange offers have been updated!");
    }
    }

    public boolean isAborted() {
    if (isFinished())
    return false;
    return aborted;
    }

    public void setAborted(boolean aborted) {
    this.aborted = aborted;
    }


    public boolean isProcessing() {
    return processing;
    }

    public void setProcessing(boolean processing) {
    this.processing = processing;
    }

    private final String owner;
    private final Item item;
    private final int slot;
    private int price;
    private int primaryAmount;
    private int secondaryAmount;
    private int extraCash;
    private final Type type;
    private boolean aborted;
    private boolean processing;
    }
    }


    Spoiler for System:
    package com.rs.game.player.content.grandExchange;

    import com.rs.cache.loaders.ItemDefinitions;
    import com.rs.game.item.Item;
    import com.rs.game.item.ItemsContainer;
    import com.rs.game.player.Player;
    import com.rs.game.player.content.Commands;
    import com.rs.game.player.content.ItemConstants;
    import com.rs.game.player.content.grandExchange.GrandExch angeLoader.GrandExchangeOffer;
    import com.rs.game.player.content.grandExchange.GrandExch angeLoader.GrandExchangeOffer.Type;
    import com.rs.utils.ItemExamines;
    import com.rs.utils.SerializableFilesManager;
    import com.rs.utils.Utils;

    public class GrandExchangeSystem extends GrandExchangeConfigurations {

    /**
    * Displays the main screen interface, this contains the default
    * configurations, unlocking, aborted information, and progress bar
    * information
    *
    * @param player
    * The player to display the information to
    */
    public void display(final Player player) {
    if (!player.getBank().setLastCount().bln()) {
    if (player.getBank().setLastCount().vkg()) {
    player.getAttributes().put("entering_pin_to_bank", true);
    player.getBank().setLastCount().void5();
    return;
    }
    }
    player.stopAll();
    player.setCloseInterfacesEvent(new Runnable() {

    @Override
    public void run() {
    closeSearchBar(player);
    }
    });
    sendMainScreenConfigs(player);
    player.getInterfaceManager().closeChatBoxInterface ();
    player.getInterfaceManager().closeInventoryInterfa ce();
    player.getInterfaceManager().sendInterface(MAIN_IN TERFACE);
    player.getPackets().sendUnlockIComponentOptionSlot s(MAIN_INTERFACE, 209, -1, -1, 1, 2, 3, 5, 6);
    player.getPackets().sendUnlockIComponentOptionSlot s(MAIN_INTERFACE, 211, -1, -1, 1, 2, 3, 5, 6);
    for (int i = 0; i < 6; i++) {
    player.getPackets().sendGrandExchangeBar(player, i, 0, Progress.RESET, 0, 0, 0);
    }
    for (GrandExchangeOffer offer : GrandExchangeLoader.getOffersForPlayer(player.getD isplayName())) {
    switch(offer.getType()) {
    case BUY:
    if (!offer.isAborted())
    player.getPackets().sendGrandExchangeBar(player, offer.getSlot(), offer.getItem().getId(), offer.isFinished() ? Progress.FINISHED_BUYING : Progress.BUY_PROGRESSING, offer.getPrice(), offer.getSecondaryAmount(), offer.getPrimaryAmount());
    else {
    player.getPackets().sendGrandExchangeBar(player, offer.getSlot(), offer.getItem().getId(), Progress.BUY_ABORTED, offer.getPrice(), offer.getSecondaryAmount(), offer.getPrimaryAmount());
    }
    break;
    case SELL:
    if (!offer.isAborted())
    player.getPackets().sendGrandExchangeBar(player, offer.getSlot(), offer.getItem().getId(), offer.isFinished() ? Progress.FINISHED_SELLING : Progress.SELL_PROGRESSING, offer.getPrice(), offer.getSecondaryAmount(), offer.getPrimaryAmount());
    else {
    player.getPackets().sendGrandExchangeBar(player, offer.getSlot(), offer.getItem().getId(), Progress.SELL_ABORTED, offer.getPrice(), offer.getSecondaryAmount(), offer.getPrimaryAmount());
    }
    break;
    }
    }
    }

    public void closeSearchBar(Player player) {
    player.getPackets().closeInterface(7);
    player.getInterfaceManager().sendTab(player.getInt erfaceManager().getWindowsPane() == 746 ? 21 : 161, 752);
    }

    /**
    * Handles the clicking of a grand exchange button
    *
    * @param player
    * The player clicking
    * @param interfaceId
    * The interface being clicked
    * @param buttonId
    * The id of the button clicked
    * @param packetId
    * The packet id of the button clicked
    * @param slotId
    * The slot id clicked
    * @param slotId2
    * The second slot id, usually item
    * @return {@code true} if successful.
    */
    @SuppressWarnings("unchecked")
    public boolean handleButtonEvent(Player player, int interfaceId, int buttonId, int packetId, int slotId, int slotId2) {
    switch(interfaceId) {
    case MAIN_INTERFACE:
    GrandExchangeOffer offer;
    switch(buttonId) {
    case 200:
    offer = getOfferBySlot(player, (Integer) player.getAttributes().get("grand_exchange_slot_cl icked"));
    if (!offer.isAborted()) {
    if (offer.isProcessing()) {
    //System.out
    // .println(player
    // + " attempted to abort offer while processing.");
    return true;
    }
    if (offer.isFinished()) {
    player.sendMessage("That offer has just been completed! You are too late.");
    return true;
    }
    if (offer.getSecondaryAmount() > 0) {
    player.sendMessage("You need to collect items from the offer before aborting it.");
    return true;
    }
    offer.setAborted(true);
    display(player);
    sendCollectInformation(player, (Integer) player.getAttributes().get("grand_exchange_slot_cl icked"));
    }
    break;
    case 208:
    case 206: // collecting
    boolean noting = false;
    GrandExchangeOffer o = getOfferBySlot(player, (Integer) player.getAttributes().get("grand_exchange_slot_cl icked"));
    if (o == null)
    return true;
    if (o.isAborted()) {
    if (packetId == 67)
    noting = true;
    }
    if (o.isProcessing()) {
    //System.out.println(player + " attempted to abort offer while processing.");
    return true;
    }
    ItemsContainer<Item> ic = (ItemsContainer<Item>) player.getAttributes().get("grand_exchange_items") ;
    Item item = null;
    try {
    item = buttonId == 206 ? ic.get(0) : ic.get(1);
    } catch (Exception e) {
    player.getDialogueManager().startDialogue("SimpleM essage", "You have already received that item!");
    return true;
    }
    if (item == null) {
    player.sendMessage("You have already received that item.");
    return true;
    }
    if (player.getInventory().getFreeSlots() == 0) {
    player.getPackets().sendGameMessage("You don't have enough space in your inventory to collect this item");
    return true;
    }
    if (item.getAmount() > 1 && item.getId() != 995 && packetId == 14)
    noting = true;
    if (noting && item.getDefinitions().getCertId() != -1)
    item.setId(item.getDefinitions().getCertId());
    ic.remove(item);
    player.getInventory().addItem(item);
    player.getPackets().sendItems(523 + o.getSlot(), ic);
    if (o.getType() != Type.SELL && item.getId() == 995)
    o.setExtraCash(0);
    if ((o.isFinished() || o.isAborted()) && ic.getUsedSlots() == 0) {
    GrandExchangeLoader.removeOffer(o);
    display(player);
    } else {
    if (o.isAborted()) {

    } else {
    o.forcePrimaryAmount(o.getPrimaryAmount() - o.getSecondaryAmount());
    o.setSecondaryAmount(0);
    }
    if (ic.getUsedSlots() == 0) { // all of the items have been removed from the container
    player.getAttributes().remove("grand_exchange_item s");
    }
    }
    break;
    case 19:
    case 35:
    case 51:
    case 108:
    case 89:
    case 70:
    player.getAttributes().put("grand_exchange_slot_cl icked", getSlot(buttonId));
    offer = getOfferBySlot(player, getSlot(buttonId));
    if (offer.isProcessing()) {
    //System.out.println(player + " attempted to abort offer while processing.");
    return true;
    }
    if (packetId == 67) {// abort
    if (offer.isFinished()) {
    player.sendMessage("That offer has just been completed! You are too late.");
    return true;
    }
    if (offer.getSecondaryAmount() > 0) {
    player.sendMessage("You need to collect items from the offer before aborting it.");
    return true;
    }
    offer.setAborted(true);
    display(player);
    }
    sendCollectInformation(player, getSlot(buttonId));
    //player.getDialogueManager().startDialogue("GrandEx changeBarter", getSlot(buttonId));
    break;
    case 31:
    case 82:
    case 101:
    case 47:
    case 63:
    case 120:
    player.getAttributes().put("grand_exchange_slot_cl icked", getSlot(buttonId));
    sendBuyScreen(player);
    break;
    case 83:
    case 32:
    case 48:
    case 102:
    case 121:
    case 64:
    player.getAttributes().put("grand_exchange_slot_cl icked", getSlot(buttonId));
    sendSellScreen(player);
    break;
    case 169:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    offer.setPrice(offer.getPrice() - 1);
    player.getPackets().sendConfig(1111, offer.getPrice());
    break;
    case 171:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    offer.setPrice(offer.getPrice() + 1);
    player.getPackets().sendConfig(1111, offer.getPrice());
    break;
    case 179: // +5%
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    offer.setPrice((int) (offer.getPrice() + (offer.getPrice() * 0.05)));
    player.getPackets().sendConfig(1111, offer.getPrice());
    break;
    case 181: // -5%
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    offer.setPrice((int) (offer.getPrice() - (offer.getPrice() * 0.05)));
    player.getPackets().sendConfig(1111, offer.getPrice());
    break;
    case 168: // custom amount
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    player.getPackets().sendRunScript(108, new Object[] { "Enter Amount:" });
    player.getAttributes().put("setting_geoffer_amount ", true);
    break;
    case 177: // custom price
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    player.getPackets().sendRunScript(108, new Object[] { "Enter Price:" });
    player.getAttributes().put("setting_geoffer_price" , true);
    break;
    case 175: //guide price
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    offer.setPrice(ItemDefinitions.getItemDefinitions( offer.getItem().getId()).getValue());
    player.getPackets().sendConfig(1111, offer.getPrice());
    break;
    case 155:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    if (offer.getPrimaryAmount() - 1 > Integer.MAX_VALUE) {
    player.sendMessage("You cannot buy that many...");
    return true;
    }
    offer.setPrimaryAmount(offer.getPrimaryAmount() - 1);
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    break;
    case 157:
    case 160:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    if (offer.getPrimaryAmount() + 1 > Integer.MAX_VALUE) {
    player.sendMessage("You cannot buy that many...");
    return true;
    }
    offer.setPrimaryAmount(offer.getPrimaryAmount() + 1);
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    break;
    case 162:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    if (offer.getPrimaryAmount() + 10 > Integer.MAX_VALUE) {
    player.sendMessage("You cannot buy that many...");
    return true;
    }
    offer.setPrimaryAmount(offer.getPrimaryAmount() + 10);
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    break;
    case 164:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    if (offer.getPrimaryAmount() + 100 > Integer.MAX_VALUE) {
    player.sendMessage("You cannot buy that many...");
    return true;
    }
    offer.setPrimaryAmount(offer.getPrimaryAmount() + 100);
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    break;
    case 166:
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    switch(offer.getType()) {
    case BUY:
    if (offer.getPrimaryAmount() + 1000 > Integer.MAX_VALUE) {
    player.sendMessage("You cannot buy that many...");
    return true;
    }
    offer.setPrimaryAmount(offer.getPrimaryAmount() + 1000);
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    break;
    case SELL:
    int amount = 0;
    if (!player.getInventory().contains(offer.getItem().g etId())) { // means the item is noted
    amount = player.getInventory().getNumerOf(offer.getItem().g etDefinitions().getCertId());
    } else {
    amount = player.getInventory().getNumerOf(offer.getItem().g etId());
    }
    offer.setPrimaryAmount(amount);
    player.getPackets().sendConfig(1110, amount);
    break;
    }
    break;
    case 128: // back
    resetInterfaceConfigs(player);
    display(player);
    player.getInterfaceManager().closeInventory();
    player.getInterfaceManager().sendInventory();
    final int lastGameTab = player.getInterfaceManager().openGameTab(4); // inventory
    final Player player2 = player;
    player.setCloseInterfacesEvent(new Runnable() {
    @Override
    public void run() {
    player2.getInterfaceManager().sendInventory();
    player2.getInventory().unlockInventoryOptions();
    player2.getInterfaceManager().sendEquipment();
    player2.getInterfaceManager().openGameTab(lastGame Tab);
    }
    });
    break;
    case 186: // confirm
    offer = (GrandExchangeOffer) player.getAttributes().get("grand_exchange_offer") ;
    if (offer == null)
    return true;
    int price = offer.getPrimaryAmount() * offer.getPrice();
    if (price > Integer.MAX_VALUE ||
    price == Integer.MAX_VALUE ||
    price >= Integer.MAX_VALUE ||
    price < 0 || price <= 0
    || offer.getPrimaryAmount() == 0
    || offer.getPrice() == 0) {
    player.getPackets().sendGameMessage("Invalid input.");
    return true;
    }
    switch(offer.getType()) {
    case BUY:
    if (player.takeMoney(price)) {
    player.getAttributes().remove("grand_exchange_offe r");
    SerializableFilesManager.savePlayer(player);
    /**
    * Adds the offer to the system so it can be processed
    */
    GrandExchangeLoader.addOffer(offer);
    /**
    * Displays the main offer screen
    */
    display(player);
    } else
    player.sendMessage("You do not have " + Utils.format(price) + " coins to make this exchange.");
    break;
    case SELL:
    int id = player.getInventory().getNumerOf(offer.getItem().g etId()) == offer.getPrimaryAmount() ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId();
    int amt = offer.getPrimaryAmount();
    if (offer.getPrimaryAmount() > player.getInventory().getNumerOf(id)) {
    player.sendMessage("You do not have " + Utils.format(offer.getPrimaryAmount()) + " of this item to sell.");;
    return true;
    }
    player.getInventory().deleteItem(id, amt);
    player.getAttributes().remove("grand_exchange_offe r");
    SerializableFilesManager.savePlayer(player);
    /**
    * Adds the offer to the system so it can be processed
    */
    GrandExchangeLoader.addOffer(offer);
    /**
    * Displays the main offer screen
    */
    display(player);
    break;
    }
    break;
    case 190: //choose item button
    sendSearchScript(player);
    break;
    default:
    //System.out.println("Unhandled button event: [interfaceId=" + interfaceId + ", buttonId=" + buttonId + ", packetId=" + packetId + "]");
    break;
    }
    return true;
    case SELL_INTERFACE:
    Item item = new Item(slotId2);
    if (!ItemConstants.isTradeable(item) || item.getId() == 995) {
    player.sendMessage("You can't sell such an item.");
    return true;
    }
    if (item.getDefinitions().isNoted()) {
    item.setId(item.getDefinitions().getCertId());
    }
    offer = new GrandExchangeOffer(player.getDisplayName(), (int) player.getAttributes().get("grand_exchange_slot_cl icked"), item, getAmountOfItem(player, item), ItemDefinitions.getItemDefinitions(item.getId()).g etValue(), Type.SELL);
    player.getPackets().sendConfig(1109, offer.getItem().getId());
    player.getPackets().sendConfig(1110, offer.getPrimaryAmount());
    player.getPackets().sendConfig(1111, offer.getPrice());
    player.getPackets().sendConfig(1114, offer.getPrice());
    player.getPackets().sendIComponentText(105, 143, ItemExamines.getExamine(item));
    player.getAttributes().put("grand_exchange_offer", offer);
    return true;
    }
    return false;
    }

    /**
    * Gets the amount of an item that the player has, checks for noting in inventory as well
    * @param player The player who is offering the item
    * @param offer The offer that is being checked
    * @return A {@code Integer} {@code Object}
    */
    private int getAmountOfItem(Player player, Item offer) {
    int amount = 0;
    int id = offer.getId();
    if (offer.getDefinitions().isStackable()) {
    amount = player.getInventory().getNumerOf(id);
    } else {
    for (Item item : player.getInventory().getItems().toArray()) {
    if (item == null)
    continue;
    if (item.getId() == id)
    amount++;
    }
    }
    return amount;
    }

    /**
    * Gets the offer on a slot for the player
    * @param player The player to check for
    * @param slot The slot to check in
    * @return
    */
    private GrandExchangeOffer getOfferBySlot(Player player, int slot) {
    for (GrandExchangeOffer offer : GrandExchangeLoader.getOffersForPlayer(player.getD isplayName())) {
    if (offer.getSlot() == slot)
    return offer;
    }
    return null;
    }

    /**
    * Sends the collection interface with two slots, also checks if the offer
    * has been aborted. Items are sent in a {@code ItemsContainer}
    * {@code Object} which is temporarily placed in the player's attributes as
    * "grand_exchange_items"
    *
    * @param player
    * The player to send it to
    * @param slotId
    * The button clicked
    */
    private void sendCollectInformation(Player player, int slotId) {
    player.getPackets().sendConfig(1112, slotId);
    player.getPackets().sendIComponentSettings(105, 206, -1, -1, 6);
    player.getPackets().sendIComponentSettings(105, 208, -1, -1, 6);
    GrandExchangeOffer offer = getOfferBySlot(player, slotId);
    if (offer == null)
    return;
    ItemsContainer<Item> ic = new ItemsContainer<Item>(2, true);
    if (!offer.isAborted()) {
    if (offer.getSecondaryAmount() > 0)
    switch(offer.getType()) {
    case BUY:
    ic.add(new Item(offer.getItem().getId(), offer.getSecondaryAmount()));
    break;
    case SELL:
    ic.add(new Item(995, offer.getSecondaryAmount() * offer.getPrice()));
    break;
    }
    } else {
    switch(offer.getType()) {
    case BUY:
    ic.add(new Item(995, offer.getPrimaryAmount() * offer.getPrice()));
    break;
    case SELL:
    ic.add(new Item(offer.getItem().getId(), offer.getPrimaryAmount()));
    break;
    }
    }
    if (offer.getExtraCash() >= 1) {
    ic.add(new Item(995, offer.getExtraCash()));
    }
    player.getAttributes().put("grand_exchange_items", ic);
    player.getPackets().sendConfig(1113, offer.getType().ordinal());
    player.getPackets().sendItems(523 + slotId, ic);
    }

    /**
    * Sends the main screen configs and sets them to their default value
    * @param player The player to send it to
    */
    private void sendMainScreenConfigs(Player player) {
    player.getPackets().sendConfig(1112, -1);
    player.getPackets().sendConfig(1113, -1);
    player.getPackets().sendConfig(1109, -1);
    player.getPackets().sendConfig(1110, 0);
    player.getPackets().sendConfig(563, 4194304);
    player.getPackets().sendConfig(1112, -1);
    player.getPackets().sendConfig(1113, -1);
    player.getPackets().sendConfig(1114, 0);
    player.getPackets().sendConfig(1109, -1);
    player.getPackets().sendConfig(1110, 0);
    player.getPackets().sendConfig(1111, 1);
    closeSearchBar(player);
    }

    /**
    * Finds the slot of the button you are clicking
    * @param buttonId The button you are clicking
    * @return
    */
    private int getSlot(int buttonId) {
    switch (buttonId) {
    case 31:
    case 32:
    case 19:
    return 0;
    case 47:
    case 35:
    case 48:
    return 1;
    case 63:
    case 51:
    case 64:
    return 2;
    case 82:
    case 83:
    case 70:
    return 3;
    case 101:
    case 102:
    case 89:
    return 4;
    case 120:
    case 108:
    case 121:
    return 5;
    default:
    return -1;
    }
    }

    /**
    * Sends the sell interfaces to the player
    * @param player The player to send it to
    */
    private void sendSellScreen(Player player) {
    int total = 0;
    int reqTotal = 1;
    for (int i = 0; i < 25; i++) {
    total += player.getSkills().getLevel(i);
    }
    if (!player.isOwner() && total < reqTotal) {
    player.sendMessage("No req neededany more.");
    return;
    }
    if (Commands.isAllowedToSpawn(player)) {
    player.getPackets().sendGameMessage("trying to sell.");
    return;
    }
    resetInterfaceConfigs(player);
    player.getPackets().sendConfig(1113, 1);
    player.getInterfaceManager().sendInventoryInterfac e(107);
    final Object[] params = new Object[]{ "", "", "", "", "Offer", -1, 0, 7, 4, 93, 7012370 };
    player.getPackets().sendRunScript(149, params);
    player.getPackets().sendItems(93, player.getInventory().getItems());
    player.getPackets().sendHideIComponent(107, 0, false);
    player.getPackets().sendIComponentSettings(107, 18, 0, 27, 1026);
    player.getPackets().sendConfig(1112, (Integer) player.getAttributes().get("grand_exchange_slot_cl icked"));
    player.getPackets().sendHideIComponent(105, 196, true);
    }

    /**
    * Displays the buy screen to the player
    * @param player The player to display it to.
    */
    public void sendBuyScreen(Player player) {
    if (Commands.isAllowedToSpawn(player)) {
    player.getPackets().sendGameMessage("trying to buy.");
    return;
    }
    resetInterfaceConfigs(player);
    sendSearchScript(player);
    }

    /**
    * Resets interface configurations to prepare for displaying the buy screen
    * @param player The player to reset it for
    */
    private void resetInterfaceConfigs(Player player) {
    player.getPackets().sendConfig2(1109, -1);
    player.getPackets().sendConfig2(1110, 0);
    player.getPackets().sendConfig2(1111, 1);
    player.getPackets().sendConfig2(1112, -1);
    player.getPackets().sendConfig2(1113, 0);
    }

    /**
    * Sends the search script at the bottom of the client box.
    */
    private void sendSearchScript(Player player) {
    player.getPackets().sendConfig1(744, 0);
    player.getPackets().sendConfig(1112, (Integer) player.getAttributes().get("grand_exchange_slot_cl icked"));
    player.getPackets().sendConfig(1113, 0);
    player.getPackets().sendInterface(true, 752, 7, 389);
    player.getPackets().sendRunScript(570, new Object[] { "Grand Exchange Item Search" });
    }

    public static GrandExchangeSystem get() {
    return INSTANCE;
    }

    private static final GrandExchangeSystem INSTANCE = new GrandExchangeSystem();

    }



    At the moment you sell your item(s) and you wait for someone to buy them, or you put in a buy request and wait for someone to sell that item, however this is not viable due to the fact you would need a ton of players for this to get much use, and I was wanting to use it for a quick and easy method for players to be able to buy the items they need or sell the items they don't. Instead of several shops scattered around home, just a few grand exchange npc's and the rest is history.

    Fixed thanks to @Andreas!
    Last edited by cadle; 07-31-2019 at 07:23 PM. Reason: more info
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 52
    Last Post: 08-16-2013, 04:20 PM
  2. [718+] 100% Dupe Free Grand Exchange 2.0 99.9%
    By Mephobia in forum Projects
    Replies: 16
    Last Post: 02-27-2013, 05:05 AM
  3. Buying Full Grand Exchange 718
    By Crypted in forum Buying
    Replies: 0
    Last Post: 09-21-2012, 05:56 AM
  4. Selling Grand Exchange
    By TheLife in forum Selling
    Replies: 17
    Last Post: 08-14-2012, 01:38 AM
  5. Replies: 17
    Last Post: 12-15-2009, 04:02 PM
Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •