Example #1
0
  public static ChanceCondition parse(StatsSet set) {
    try {
      TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
      int chance = set.getInteger("activationChance", -1);
      int mindmg = set.getInteger("activationMinDamage", -1);
      String elements = set.getString("activationElements", null);
      boolean pvpOnly = set.getBool("pvpChanceOnly", false);

      if (trigger != null)
        return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), pvpOnly);
    } catch (Exception e) {
      _log.log(Level.WARNING, "", e);
    }
    return null;
  }
  public ItemAuctionInstance(final int instanceId, final AtomicInteger auctionIds, final Node node)
      throws Exception {
    _instanceId = instanceId;
    _auctionIds = auctionIds;
    _auctions = new TIntObjectHashMap<ItemAuction>();
    _items = new ArrayList<AuctionItem>();

    final NamedNodeMap nanode = node.getAttributes();
    final StatsSet generatorConfig = new StatsSet();
    for (int i = nanode.getLength(); i-- > 0; ) {
      final Node n = nanode.item(i);
      if (n != null) generatorConfig.set(n.getNodeName(), n.getNodeValue());
    }

    _dateGenerator = new AuctionDateGenerator(generatorConfig);

    for (Node na = node.getFirstChild(); na != null; na = na.getNextSibling()) {
      try {
        if ("item".equalsIgnoreCase(na.getNodeName())) {
          final NamedNodeMap naa = na.getAttributes();
          final int auctionItemId =
              Integer.parseInt(naa.getNamedItem("auctionItemId").getNodeValue());
          final int auctionLenght =
              Integer.parseInt(naa.getNamedItem("auctionLenght").getNodeValue());
          final long auctionInitBid =
              Integer.parseInt(naa.getNamedItem("auctionInitBid").getNodeValue());

          final int itemId = Integer.parseInt(naa.getNamedItem("itemId").getNodeValue());
          final int itemCount = Integer.parseInt(naa.getNamedItem("itemCount").getNodeValue());

          if (auctionLenght < 1)
            throw new IllegalArgumentException(
                "auctionLenght < 1 for instanceId: " + _instanceId + ", itemId " + itemId);

          final StatsSet itemExtra = new StatsSet();
          final AuctionItem item =
              new AuctionItem(
                  auctionItemId, auctionLenght, auctionInitBid, itemId, itemCount, itemExtra);

          if (!item.checkItemExists())
            throw new IllegalArgumentException("Item with id " + itemId + " not found");

          for (final AuctionItem tmp : _items) {
            if (tmp.getAuctionItemId() == auctionItemId)
              throw new IllegalArgumentException("Dublicated auction item id " + auctionItemId);
          }

          _items.add(item);

          for (Node nb = na.getFirstChild(); nb != null; nb = nb.getNextSibling()) {
            if ("extra".equalsIgnoreCase(nb.getNodeName())) {
              final NamedNodeMap nab = nb.getAttributes();
              for (int i = nab.getLength(); i-- > 0; ) {
                final Node n = nab.item(i);
                if (n != null) itemExtra.set(n.getNodeName(), n.getNodeValue());
              }
            }
          }
        }
      } catch (final IllegalArgumentException e) {
        _log.log(Level.WARNING, "ItemAuctionInstance: Failed loading auction item", e);
      }
    }

    if (_items.isEmpty()) throw new IllegalArgumentException("No items defined");

    Connection con = null;
    try {
      con = L2DatabaseFactory.getInstance().getConnection();
      PreparedStatement statement =
          con.prepareStatement("SELECT auctionId FROM item_auction WHERE instanceId=?");
      statement.setInt(1, _instanceId);
      ResultSet rset = statement.executeQuery();

      while (rset.next()) {
        final int auctionId = rset.getInt(1);
        try {
          final ItemAuction auction = loadAuction(auctionId);
          if (auction != null) {
            _auctions.put(auctionId, auction);
          } else {
            ItemAuctionManager.deleteAuction(auctionId);
          }
        } catch (final SQLException e) {
          _log.log(Level.WARNING, "ItemAuctionInstance: Failed loading auction: " + auctionId, e);
        }
      }
    } catch (final SQLException e) {
      _log.log(Level.SEVERE, "L2ItemAuctionInstance: Failed loading auctions.", e);
      return;
    } finally {
      L2DatabaseFactory.close(con);
    }

    _log.log(
        Level.INFO,
        "L2ItemAuctionInstance: Loaded "
            + _items.size()
            + " item(s) and registered "
            + _auctions.size()
            + " auction(s) for instance "
            + _instanceId
            + ".");
    checkAndSetCurrentAndNextAuction();
  }