Example #1
0
 /**
  * number of items that shop will buy or sell & is legal or can buy illegal
  *
  * @param showIllegal
  * @return
  */
 public int getShopSize(boolean showIllegal) {
   int num = priceList.size();
   for (PriceListItem i : priceList) {
     if ((i.buy < 0 && i.sell < 0) || !(showIllegal || i.IsLegal())) {
       --num;
     }
   }
   return num;
 }
Example #2
0
 /**
  * @param pageNum zero-indexed page to get
  * @param pageSize size of an output page
  * @param showIllegal whether to count illegal items
  * @return
  */
 public int getShopPageStart(int pageNum, int pageSize, boolean showIllegal) {
   if (pageSize <= 0) {
     pageSize = 1;
   }
   int num = 0, showNum = 0;
   for (PriceListItem i : priceList) {
     if (!((i.buy < 0 && i.sell < 0) || !(showIllegal || i.IsLegal()))) {
       if (showNum / pageSize == pageNum) {
         break;
       }
       ++showNum;
     }
     ++num;
   }
   return num;
 }
Example #3
0
  /**
   * returns a page of prices
   *
   * @param pageNum page to lookup (-1 will print all pages)
   * @param isPlayer if should use minecraft font spacing
   * @param pageSize how many on a page
   * @param listing format to output listing with
   * @param header page header (<page> of <pages>)
   * @param footer page footer
   * @param showIllegal whether illegal items should be included in the listing
   * @param showDec whether to round to whole numbers or show 2 decimal places
   * @param stock what to use for stock, if applicable
   * @param discount percentage to mark the price down
   * @return a list of formatted lines
   * @throws SQLException if using MySQL database & there was some database connection error
   * @throws Exception some serious error occurred (details in message)
   */
  public List<String> getShopListPage(
      int pageNum,
      boolean isPlayer,
      int pageSize,
      String listing,
      String header,
      String footer,
      boolean showIllegal,
      boolean showDec,
      ItemStock stock,
      double discount)
      throws SQLException, Exception {
    LinkedList<String> ret = new LinkedList<String>();
    if (databaseType == DBType.MYSQL && !useCache) {
      updateCache(false); // manually update
    } else {
      updateCache();
    }

    int pricelistsize = getShopSize(showIllegal); // priceList.size();

    int pages = (int) Math.ceil((double) pricelistsize / pageSize);

    int pageStart;
    if (pageNum <= 0) {
      // pageNum = 1;
      pageStart = 0;
      pageSize = pricelistsize;
    } else {
      pageStart = getShopPageStart(pageNum - 1, pageSize, showIllegal);
    }

    String listhead =
        header == null || header.length() == 0
            ? ""
            : header
                .replace("<page>", pageNum < 0 ? "(All)" : String.valueOf(pageNum))
                .replace("<pages>", String.valueOf(pages));
    if (pageNum > pages) {
      ret.add("There is no page " + pageNum + ". (" + pages + " pages total)");
    } else {
      if (listhead.length() > 0) {
        ret.add(String.format(listhead, pageNum, pages));
      }
      listing =
          listing
              .replace("<item>", "%1$s")
              .replace("<buyprice>", "%2$s")
              .replace("<sellprice>", "%3$s")
              .replace("<avail>", "%4$s");
      /// for (int i = pageSize * (pageNum - 1), n = 0; n < pageSize && i < priceList.size(); ++i,
      // ++n) {
      for (int i = pageStart, n = 0; n < pageSize && i < priceList.size(); ++i, ++n) {
        PriceListItem it = priceList.get(i);
        if ((!showIllegal && !it.IsLegal()) || (it.buy < 0 && it.sell < 0)) {
          --n;
          continue;
        }
        long st = stock != null ? stock.getItemAmount(it) : -1;
        double buy = it.buy, sell = it.sell;
        if (discount != 0) {
          buy -= buy * discount;
          sell -= sell * discount;
        }
        ret.add(
            String.format(
                listing,
                it.coloredName(),
                String.format(
                    "%5s",
                    buy < 0
                        ? " No "
                        : (showDec
                            ? String.format("%01.2f", buy)
                            : String.valueOf((int) Math.round(buy)))),
                String.format(
                    "%5s",
                    sell < 0
                        ? " No "
                        : (showDec
                            ? String.format("%01.2f", sell)
                            : String.valueOf((int) Math.round(sell)))),
                (st < 0 ? "INF" : String.valueOf(st))));
      }
      if (footer != null && footer.length() > 0) {
        ret.add(footer);
      }
    }
    if (ret.size() > 2) {
      // format spaces
      return MinecraftChatStr.alignTags(ret, isPlayer);
    }
    return ret;
  }