Beispiel #1
0
 public boolean canBuy(ItemStockEntry i) throws SQLException, Exception {
   if (tempCache != null && tempCache.ID() == i.itemNum && tempCache.Data() == i.itemSub) {
     // has been retrieved recently
     return tempCache.buy >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.buy >= 0;
 }
Beispiel #2
0
 public boolean isForSale(ItemStockEntry i) throws SQLException, Exception {
   if (tempCache != null && tempCache.ID() == i.itemNum && tempCache.Data() == i.itemSub) {
     // has been retrieved recently
     return tempCache.sell >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.sell >= 0; // getSellPrice(i) >= 0;
 }
Beispiel #3
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;
 }
Beispiel #4
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;
 }
Beispiel #5
0
 public boolean canBuy(ItemStack i) throws SQLException, Exception {
   if (tempCache != null && tempCache.equals(i)) {
     // has been retrieved recently
     return tempCache.buy >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.buy >= 0;
 }
Beispiel #6
0
 public boolean isForSale(ItemStack i) throws SQLException, Exception {
   if (tempCache != null && tempCache.equals(i)) {
     // has been retrieved recently
     return tempCache.sell >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.sell >= 0; // getSellPrice(i) >= 0;
 }
Beispiel #7
0
 public PriceListItem getItemPrice(int id, byte dat) throws SQLException, Exception {
   if (tempCache != null
       && tempCache.ID() == id
       && tempCache.Data() == dat
       && (System.currentTimeMillis() - tempCache.getTime()) / 1000 < tempCacheTTL) {
     // use temp
     return tempCache;
   }
   if (databaseType == DBType.MYSQL && !useCache) {
     tempCache = MySQLpricelist.getItem(id, dat);
   } else {
     updateCache(true);
     PriceListItem f = null;
     for (PriceListItem p : priceList) {
       if (p != null && p.ID() == id && p.Data() == dat) {
         f = p;
         break;
       }
     }
     if (f == null) {
       return null;
     }
     if (tempCache == null) {
       tempCache = new PriceListItem(f);
     } else {
       tempCache.Set(f);
     }
   }
   return tempCache;
 }
Beispiel #8
0
 public int compare(PriceListItem o1, PriceListItem o2) {
   if (sortOrder.size() > 0) {
     int o1i = sortOrder.indexOf(o1.IdDatStr());
     int o2i = sortOrder.indexOf(o2.IdDatStr());
     if (o1i != -1 && o2i != -1) {
       return o2i - o1i;
     } else if (o1i != -1) {
       return -1;
     } else if (o2i != -1) {
       return 1;
     }
   }
   return (o1.ID() * 100 - o2.ID() * 100 + o1.Data() - o2.Data()) * (descending ? 1 : -1);
 }
Beispiel #9
0
 public boolean itemExists(JItem check) throws SQLException, Exception {
   if (check == null) {
     return false;
   }
   updateCache(true);
   if (databaseType == DBType.MYSQL && dbCacheTTL == 0) {
     tempCache = MySQLpricelist.getItem(check);
   } else { // should be in cache
     int i = priceList.indexOf(new PriceListItem(check));
     if (i < 0) {
       return false;
     } // else
     if (tempCache == null) {
       tempCache = new PriceListItem(priceList.get(i));
     } else {
       tempCache.Set(priceList.get(i));
     }
   }
   return tempCache != null;
 }
Beispiel #10
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;
  }
Beispiel #11
0
  public boolean saveFile(File tosave) throws IOException {
    if (tosave != null && !tosave.isDirectory()) {
      if (!tosave.exists()) {
        File dir =
            new File(
                tosave
                    .getAbsolutePath()
                    .substring(0, tosave.getAbsolutePath().lastIndexOf(File.separatorChar)));
        dir.mkdirs();
        try {
          if (!tosave.createNewFile()) {
            return false;
          }
        } catch (Exception e) {
          return false;
        }
      }
      if (tosave.canWrite()) {

        FileWriter fstream = null;
        try {
          fstream = new FileWriter(tosave.getAbsolutePath());
          // System.out.println("writing to " + tosave.getAbsolutePath());
          BufferedWriter out = new BufferedWriter(fstream);
          out.write("id,subdata,buyprice,sellprice,name");
          out.newLine();
          for (PriceListItem i : priceList) {
            // names provided for others to easily edit db
            // out.write(i.ID() + "," + i.Data() + "," + i.buy + "," + i.sell + "," + i.Name());
            out.write(
                String.format(
                    Locale.US, "%d,%d,%.2f,%.2f,%s", i.ID(), i.Data(), i.buy, i.sell, i.Name()));
            out.newLine();
          }
          out.flush();
          out.close();
        } catch (IOException ex) {
          if (!log_nothrow) {
            throw new IOException("Error opening " + tosave.getName() + " for writing", ex);
          }
          logger.log(Level.SEVERE, "Error opening " + tosave.getName() + " for writing", ex);
        } finally {
          if (fstream != null) {
            try {
              fstream.close();
            } catch (IOException ex) {
              /*
              if (!log_nothrow) {
              throw new IOException("Error closing " + tosave.getName(), ex);
              }
              logger.log(Level.SEVERE, "Error closing " + tosave.getName(), ex);*/

            }
          }
        }
        return true;
      }
    } else if (tosave == null) {
      throw new IOException("no file to save to");
    } else if (tosave.isDirectory()) {
      throw new IOException("file to save is a directory");
    }
    return false;
  }