src/squwid/util/TransactionInteractions.java
public double buyItem(Player p, MarketObj item, int amount){
        String loc = item.getId();
        List<String> matString = ss.getMarket().getStringList(loc + ".salelist");
        // If there is none of that item on the market
        if (matString.size()==0) {
            return 0;
        }
        List<MarketObj> mktObjects = new ArrayList<>();
        for (int i=0; i<matString.size(); i++){
            String uuid = ss.getMarket().getString(loc + "." + matString.get(i) + ".uuid");
            int objAmount= ss.getMarket().getInt(loc + "." + matString.get(i) + ".amount");
            double price = ss.getMarket().getDouble(loc + "." + matString.get(i) + ".price");
            MarketObj obj = new MarketObj(uuid, objAmount, price, matString.get(i));
            mktObjects.add(obj);
        }
        // Once market objects are in a list, calculate how much each one costs
        double totalPrice = 0;
        int currentItems = 0;
        while(currentItems <= amount){
            if (mktObjects.size() == 0){
                return 0;
            }
            MarketObj cheapest = new MarketObj();
            // This loop gets the cheapest value in the market
            for (int i = 0; i < mktObjects.size(); i++){
                MarketObj current = mktObjects.get(i);
                // 0 would mean uninitialized
                if (cheapest.getPrice() == 0){
                    cheapest = current;
                }
                // if prices are same use the one with biggest amount (Will save looping time)
                else if(cheapest.getPrice() == current.getPrice() && cheapest.getAmount() < current.getPrice()){
                    cheapest = current;
                }
                // if current is cheaper than cheapest
                else if(cheapest.getPrice() > current.getPrice()){
                    cheapest = current;
                }
            }
            //mm.log("CHEAPEST PRICE: " + cheapest.getPrice());
            // If the current cheapest has enough stored in it to receive price
            if (currentItems + cheapest.getAmount() > amount){
                //mm.log("AMOUNT LEFT: " + amountLeft);
                int amountLeft = amount - currentItems;
                totalPrice = totalPrice + (amountLeft * cheapest.getPrice());
                ss.getMarket().set(loc + "." + cheapest.getId() + ".amount", cheapest.getAmount() - amountLeft);
                ss.saveMarket();
                buyBalanceChange(p, (amountLeft * cheapest.getPrice()), item.getId(), amountLeft, cheapest);
                return totalPrice;
            }
            if (currentItems + cheapest.getAmount() == amount){
                totalPrice = totalPrice + (cheapest.getAmount() * cheapest.getPrice());
                ss.getMarket().set(loc + "." + cheapest.getId(), null);
                matString.remove(cheapest.getId());
                ss.getMarket().set(loc + ".salelist", matString);
                ss.saveMarket();
                buyBalanceChange(p, (cheapest.getAmount() * cheapest.getPrice()), item.getId(), cheapest.getAmount(), cheapest);
                return totalPrice;
            }
            currentItems = currentItems + cheapest.getAmount();
            totalPrice = totalPrice + (cheapest.getAmount() * cheapest.getPrice());
            mktObjects.remove(cheapest);
            ss.getMarket().set(loc + "." + cheapest.getId(), null);
            //remove it from the string and save the string back
            buyBalanceChange(p, (cheapest.getAmount() * cheapest.getPrice()), item.getId(), cheapest.getAmount(), cheapest);
            matString.remove(cheapest.getId());
            ss.getMarket().set(loc + ".salelist", matString);
            ss.saveMarket();
        }
        return 0;
    }
//  price is not checked first.
price is not checked first.
price is not checked first .
public double buyItem(Player p, MarketObj item, int amount){
        String loc = item.getId();
        List<String> matString = ss.getMarket().getStringList(loc + ".salelist");
        if (matString.size()==0) {
            return 0;
        }
        List<MarketObj> mktObjects = new ArrayList<>();
        for (int i=0; i<matString.size(); i++){
            String uuid = ss.getMarket().getString(loc + "." + matString.get(i) + ".uuid");
            int objAmount= ss.getMarket().getInt(loc + "." + matString.get(i) + ".amount");
            double price = ss.getMarket().getDouble(loc + "." + matString.get(i) + ".price");
            MarketObj obj = new MarketObj(uuid, objAmount, price, matString.get(i));
            mktObjects.add(obj);
        }
        double totalPrice = 0;
        int currentItems = 0;
        while(currentItems <= amount){
            if (mktObjects.size() == 0){
                return 0;
            }
            MarketObj cheapest = new MarketObj();
            for (int i = 0; i < mktObjects.size(); i++){
                MarketObj current = mktObjects.get(i);
                if (cheapest.getPrice() == 0){
                    cheapest = current;
                }
                else if(cheapest.getPrice() == current.getPrice() && cheapest.getAmount() < current.getPrice()){
                    cheapest = current;
                }
                else if(cheapest.getPrice() > current.getPrice()){
                    cheapest = current;
                }
            }
            if (currentItems + cheapest.getAmount() > amount){
                int amountLeft = amount - currentItems;
                totalPrice = totalPrice + (amountLeft * cheapest.getPrice());
                ss.getMarket().set(loc + "." + cheapest.getId() + ".amount", cheapest.getAmount() - amountLeft);
                ss.saveMarket();
                buyBalanceChange(p, (amountLeft * cheapest.getPrice()), item.getId(), amountLeft, cheapest);
                return totalPrice;
            }
            if (currentItems + cheapest.getAmount() == amount){
                totalPrice = totalPrice + (cheapest.getAmount() * cheapest.getPrice());
                ss.getMarket().set(loc + "." + cheapest.getId(), null);
                matString.remove(cheapest.getId());
                ss.getMarket().set(loc + ".salelist", matString);
                ss.saveMarket();
                buyBalanceChange(p, (cheapest.getAmount() * cheapest.getPrice()), item.getId(), cheapest.getAmount(), cheapest);
                return totalPrice;
            }
            currentItems = currentItems + cheapest.getAmount();
            totalPrice = totalPrice + (cheapest.getAmount() * cheapest.getPrice());
            mktObjects.remove(cheapest);
            ss.getMarket().set(loc + "." + cheapest.getId(), null);
            buyBalanceChange(p, (cheapest.getAmount() * cheapest.getPrice()), item.getId(), cheapest.getAmount(), cheapest);
            matString.remove(cheapest.getId());
            ss.getMarket().set(loc + ".salelist", matString);
            ss.saveMarket();
        }
        return 0;
    }
public double buyItem ( Player p , MarketObj item , int amount ) { String loc = item . getId ( ) ; List < String > matString = ss . getMarket ( ) . getStringList ( loc + ".salelist" ) ; if ( matString . size ( ) == 0 ) { return 0 ; } List < MarketObj > mktObjects = new ArrayList < > ( ) ; for ( int i = 0 ; i < matString . size ( ) ; i ++ ) { String uuid = ss . getMarket ( ) . getString ( loc + "." + matString . get ( i ) + ".uuid" ) ; int objAmount = ss . getMarket ( ) . getInt ( loc + "." + matString . get ( i ) + ".amount" ) ; double price = ss . getMarket ( ) . getDouble ( loc + "." + matString . get ( i ) + ".price" ) ; MarketObj obj = new MarketObj ( uuid , objAmount , price , matString . get ( i ) ) ; mktObjects . add ( obj ) ; } double totalPrice = 0 ; int currentItems = 0 ; while ( currentItems <= amount ) { if ( mktObjects . size ( ) == 0 ) { return 0 ; } MarketObj cheapest = new MarketObj ( ) ; for ( int i = 0 ; i < mktObjects . size ( ) ; i ++ ) { MarketObj current = mktObjects . get ( i ) ; if ( cheapest . getPrice ( ) == 0 ) { cheapest = current ; } else if ( cheapest . getPrice ( ) == current . getPrice ( ) && cheapest . getAmount ( ) < current . getPrice ( ) ) { cheapest = current ; } else if ( cheapest . getPrice ( ) > current . getPrice ( ) ) { cheapest = current ; } } if ( currentItems + cheapest . getAmount ( ) > amount ) { int amountLeft = amount - currentItems ; totalPrice = totalPrice + ( amountLeft * cheapest . getPrice ( ) ) ; ss . getMarket ( ) . set ( loc + "." + cheapest . getId ( ) + ".amount" , cheapest . getAmount ( ) - amountLeft ) ; ss . saveMarket ( ) ; buyBalanceChange ( p , ( amountLeft * cheapest . getPrice ( ) ) , item . getId ( ) , amountLeft , cheapest ) ; return totalPrice ; } if ( currentItems + cheapest . getAmount ( ) == amount ) { totalPrice = totalPrice + ( cheapest . getAmount ( ) * cheapest . getPrice ( ) ) ; ss . getMarket ( ) . set ( loc + "." + cheapest . getId ( ) , null ) ; matString . remove ( cheapest . getId ( ) ) ; ss . getMarket ( ) . set ( loc + ".salelist" , matString ) ; ss . saveMarket ( ) ; buyBalanceChange ( p , ( cheapest . getAmount ( ) * cheapest . getPrice ( ) ) , item . getId ( ) , cheapest . getAmount ( ) , cheapest ) ; return totalPrice ; } currentItems = currentItems + cheapest . getAmount ( ) ; totalPrice = totalPrice + ( cheapest . getAmount ( ) * cheapest . getPrice ( ) ) ; mktObjects . remove ( cheapest ) ; ss . getMarket ( ) . set ( loc + "." + cheapest . getId ( ) , null ) ; buyBalanceChange ( p , ( cheapest . getAmount ( ) * cheapest . getPrice ( ) ) , item . getId ( ) , cheapest . getAmount ( ) , cheapest ) ; matString . remove ( cheapest . getId ( ) ) ; ss . getMarket ( ) . set ( loc + ".salelist" , matString ) ; ss . saveMarket ( ) ; } return 0 ; }
price is not checked first.
price is not checked first .
// If there is none of that item on the market // Once market objects are in a list, calculate how much each one costs // This loop gets the cheapest value in the market // 0 would mean uninitialized // if prices are same use the one with biggest amount (Will save looping time) // if current is cheaper than cheapest //mm.log("CHEAPEST PRICE: " + cheapest.getPrice()); // If the current cheapest has enough stored in it to receive price //mm.log("AMOUNT LEFT: " + amountLeft); //remove it from the string and save the string back