/**
   * Method saveData.
   *
   * @param cw CursedWeapon
   */
  private void saveData(CursedWeapon cw) {
    Connection con = null;
    PreparedStatement statement = null;

    try {
      con = DatabaseFactory.getInstance().getConnection();
      statement = con.prepareStatement("DELETE FROM cursed_weapons WHERE item_id = ?");
      statement.setInt(1, cw.getItemId());
      statement.executeUpdate();

      if (cw.isActive()) {
        DbUtils.close(statement);
        statement =
            con.prepareStatement(
                "REPLACE INTO cursed_weapons (item_id, player_id, player_karma, player_pkkills, nb_kills, x, y, z, end_time) VALUES (?,?,?,?,?,?,?,?,?)");
        statement.setInt(1, cw.getItemId());
        statement.setInt(2, cw.getPlayerId());
        statement.setInt(3, cw.getPlayerKarma());
        statement.setInt(4, cw.getPlayerPkKills());
        statement.setInt(5, cw.getNbKills());
        statement.setInt(6, cw.getLoc().getX());
        statement.setInt(7, cw.getLoc().getY());
        statement.setInt(8, cw.getLoc().getZ());
        statement.setLong(9, cw.getEndTime() / 1000);
        statement.executeUpdate();
      }
    } catch (SQLException e) {
      _log.error("CursedWeapon: Failed to save data: " + e);
    } finally {
      DbUtils.closeQuietly(con, statement);
    }
  }
Exemple #2
0
 /**
  * Method updateOwnerInDB.
  *
  * @param clan Clan
  */
 private void updateOwnerInDB(Clan clan) {
   _owner = clan;
   Connection con = null;
   PreparedStatement statement = null;
   try {
     con = DatabaseFactory.getInstance().getConnection();
     statement =
         con.prepareStatement("UPDATE clan_data SET hasFortress=0 WHERE hasFortress=? LIMIT 1");
     statement.setInt(1, getId());
     statement.execute();
     DbUtils.close(statement);
     if (clan != null) {
       statement =
           con.prepareStatement("UPDATE clan_data SET hasFortress=? WHERE clan_id=? LIMIT 1");
       statement.setInt(1, getId());
       statement.setInt(2, getOwnerId());
       statement.execute();
       clan.broadcastClanStatus(true, false, false);
     }
   } catch (Exception e) {
     _log.error("", e);
   } finally {
     DbUtils.closeQuietly(con, statement);
   }
 }
  /**
   * Method emptyPlayerCursedWeapon.
   *
   * @param playerId int
   * @param itemId int
   * @param cw CursedWeapon
   */
  private void emptyPlayerCursedWeapon(int playerId, int itemId, CursedWeapon cw) {
    Connection con = null;
    PreparedStatement statement = null;

    try {
      con = DatabaseFactory.getInstance().getConnection();
      statement = con.prepareStatement("DELETE FROM items WHERE owner_id=? AND item_id=?");
      statement.setInt(1, playerId);
      statement.setInt(2, itemId);
      statement.executeUpdate();
      DbUtils.close(statement);
      statement = con.prepareStatement("UPDATE characters SET karma=?, pkkills=? WHERE obj_id=?");
      statement.setInt(1, cw.getPlayerKarma());
      statement.setInt(2, cw.getPlayerPkKills());
      statement.setInt(3, playerId);

      if (statement.executeUpdate() != 1) {
        _log.warn("Error while updating karma & pkkills for userId " + cw.getPlayerId());
      }

      removeFromDb(itemId);
    } catch (SQLException e) {
      _log.error("", e);
    } finally {
      DbUtils.closeQuietly(con, statement);
    }
  }
  /** Method load. */
  public static void load() {
    VoteList.clear();

    try {
      File file = new File(Config.DATAPACK_ROOT, "data/xml/other/vote.xml");
      DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
      factory2.setValidating(false);
      factory2.setIgnoringComments(true);
      Document doc2 = factory2.newDocumentBuilder().parse(file);

      for (Node n2 = doc2.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
        if ("list".equals(n2.getNodeName())) {
          for (Node d2 = n2.getFirstChild(); d2 != null; d2 = d2.getNextSibling()) {
            if ("vote".equals(d2.getNodeName())) {
              Vote v = new Vote();
              v.id = Integer.parseInt(d2.getAttributes().getNamedItem("id").getNodeValue());
              v.maxPerAccount =
                  Integer.parseInt(d2.getAttributes().getNamedItem("maxPerAccount").getNodeValue());
              v.name = d2.getAttributes().getNamedItem("name").getNodeValue();
              v.active =
                  Boolean.parseBoolean(d2.getAttributes().getNamedItem("active").getNodeValue());

              for (Node i = d2.getFirstChild(); i != null; i = i.getNextSibling()) {
                if ("variant".equals(i.getNodeName())) {
                  v.variants.put(
                      Integer.parseInt(i.getAttributes().getNamedItem("id").getNodeValue()),
                      i.getAttributes().getNamedItem("desc").getNodeValue());
                }
              }

              VoteList.put(v.id, v);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    try (Connection con = DatabaseFactory.getInstance().getConnection(); ) {

      PreparedStatement st = con.prepareStatement("SELECT * FROM vote");
      ResultSet rs = st.executeQuery();

      while (rs.next()) {
        Vote v = VoteList.get(rs.getInt("id"));

        if (v != null) {
          String HWID = rs.getString("HWID");
          Integer[] rez = v.results.get(HWID);
          v.results.put(HWID, ArrayUtils.add(rez, rs.getInt("vote")));
        }
      }
      rs.close();
      st.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** Method checkConditions. */
  private void checkConditions() {
    Connection con = null;
    PreparedStatement statement1 = null, statement2 = null;
    ResultSet rset = null;

    try {
      con = DatabaseFactory.getInstance().getConnection();
      statement1 = con.prepareStatement("DELETE FROM character_skills WHERE skill_id=?");
      statement2 = con.prepareStatement("SELECT owner_id FROM items WHERE item_id=?");

      for (CursedWeapon cw : _cursedWeapons) {
        int itemId = cw.getItemId();
        int skillId = cw.getSkillId();
        boolean foundedInItems = false;
        statement1.setInt(1, skillId);
        statement1.executeUpdate();
        statement2.setInt(1, itemId);
        rset = statement2.executeQuery();

        while (rset.next()) {
          int playerId = rset.getInt("owner_id");

          if (!foundedInItems) {
            if ((playerId != cw.getPlayerId()) || (cw.getPlayerId() == 0)) {
              emptyPlayerCursedWeapon(playerId, itemId, cw);
              _log.info(
                  "CursedWeaponsManager[254]: Player "
                      + playerId
                      + " owns the cursed weapon "
                      + itemId
                      + " but he shouldn't.");
            } else {
              foundedInItems = true;
            }
          } else {
            emptyPlayerCursedWeapon(playerId, itemId, cw);
            _log.info(
                "CursedWeaponsManager[262]: Player "
                    + playerId
                    + " owns the cursed weapon "
                    + itemId
                    + " but he shouldn't.");
          }
        }

        if (!foundedInItems && (cw.getPlayerId() != 0)) {
          removeFromDb(cw.getItemId());
          _log.info("CursedWeaponsManager: Unownered weapon, removing from table...");
        }
      }
    } catch (Exception e) {
      _log.warn("CursedWeaponsManager: Could not check cursed_weapons data: " + e);
    } finally {
      DbUtils.closeQuietly(statement1);
      DbUtils.closeQuietly(con, statement2, rset);
    }
  }
  /**
   * Method removeFromDb.
   *
   * @param itemId int
   */
  private void removeFromDb(int itemId) {
    Connection con = null;
    PreparedStatement statement = null;

    try {
      con = DatabaseFactory.getInstance().getConnection();
      statement = con.prepareStatement("DELETE FROM cursed_weapons WHERE item_id = ?");
      statement.setInt(1, itemId);
      statement.executeUpdate();

      if (getCursedWeapon(itemId) != null) {
        getCursedWeapon(itemId).initWeapon();
      }
    } catch (SQLException e) {
      _log.error("CursedWeaponsManager: Failed to remove data: " + e);
    } finally {
      DbUtils.closeQuietly(con, statement);
    }
  }
 /**
  * Method updateRentTime.
  *
  * @param inDebt boolean
  */
 public void updateRentTime(boolean inDebt) {
   setEndTimeInMillis(System.currentTimeMillis() + 86400000);
   Connection con = null;
   PreparedStatement statement = null;
   try {
     con = DatabaseFactory.getInstance().getConnection();
     statement =
         con.prepareStatement(
             "UPDATE residence_functions SET endTime=?, inDebt=? WHERE type=? AND id=?");
     statement.setInt(1, (int) (getEndTimeInMillis() / 1000));
     statement.setInt(2, inDebt ? 1 : 0);
     statement.setInt(3, getType());
     statement.setInt(4, getResidenceId());
     statement.executeUpdate();
   } catch (Exception e) {
     _log.error("", e);
   } finally {
     DbUtils.closeQuietly(con, statement);
   }
 }
  /** Method restore. */
  private void restore() {
    Connection con = null;
    PreparedStatement statement = null;
    ResultSet rset = null;

    try {
      con = DatabaseFactory.getInstance().getConnection();
      statement = con.prepareStatement("SELECT * FROM cursed_weapons");
      rset = statement.executeQuery();

      while (rset.next()) {
        int itemId = rset.getInt("item_id");
        CursedWeapon cw = _cursedWeaponsMap.get(itemId);

        if (cw != null) {
          cw.setPlayerId(rset.getInt("player_id"));
          cw.setPlayerKarma(rset.getInt("player_karma"));
          cw.setPlayerPkKills(rset.getInt("player_pkkills"));
          cw.setNbKills(rset.getInt("nb_kills"));
          cw.setLoc(new Location(rset.getInt("x"), rset.getInt("y"), rset.getInt("z")));
          cw.setEndTime(rset.getLong("end_time") * 1000L);

          if (!cw.reActivate()) {
            endOfLife(cw);
          }
        } else {
          removeFromDb(itemId);
          _log.warn("CursedWeaponsManager: Unknown cursed weapon " + itemId + ", deleted");
        }
      }
    } catch (Exception e) {
      _log.warn("CursedWeaponsManager: Could not restore cursed_weapons data: " + e);
      _log.error("", e);
    } finally {
      DbUtils.closeQuietly(con, statement, rset);
    }
  }
  /**
   * Method endOfLife.
   *
   * @param cw CursedWeapon
   */
  public void endOfLife(CursedWeapon cw) {
    if (cw.isActivated()) {
      Player player = cw.getOnlineOwner();

      if (player != null) {
        _log.info(
            "CursedWeaponsManager: " + cw.getName() + " being removed online from " + player + ".");
        player.abortAttack(true, true);
        player.setKarma(cw.getPlayerKarma());
        player.setPkKills(cw.getPlayerPkKills());
        player.setCursedWeaponEquippedId(0);
        player.setTransformation(0);
        player.setTransformationName(null);
        player.removeSkill(
            SkillTable.getInstance()
                .getInfo(cw.getSkillId(), player.getSkillLevel(cw.getSkillId())),
            false);
        player.getInventory().destroyItemByItemId(cw.getItemId(), 1L);
        player.broadcastCharInfo();
      } else {
        _log.info("CursedWeaponsManager: " + cw.getName() + " being removed offline.");
        Connection con = null;
        PreparedStatement statement = null;

        try {
          con = DatabaseFactory.getInstance().getConnection();
          statement = con.prepareStatement("DELETE FROM items WHERE owner_id=? AND item_id=?");
          statement.setInt(1, cw.getPlayerId());
          statement.setInt(2, cw.getItemId());
          statement.executeUpdate();
          DbUtils.close(statement);
          statement =
              con.prepareStatement(
                  "DELETE FROM character_skills WHERE char_obj_id=? AND skill_id=?");
          statement.setInt(1, cw.getPlayerId());
          statement.setInt(2, cw.getSkillId());
          statement.executeUpdate();
          DbUtils.close(statement);
          statement =
              con.prepareStatement("UPDATE characters SET karma=?, pkkills=? WHERE obj_Id=?");
          statement.setInt(1, cw.getPlayerKarma());
          statement.setInt(2, cw.getPlayerPkKills());
          statement.setInt(3, cw.getPlayerId());
          statement.executeUpdate();
        } catch (SQLException e) {
          _log.warn("CursedWeaponsManager: Could not delete : " + e);
        } finally {
          DbUtils.closeQuietly(con, statement);
        }
      }
    } else if ((cw.getPlayer() != null)
        && (cw.getPlayer().getInventory().getItemByItemId(cw.getItemId()) != null)) {
      Player player = cw.getPlayer();

      if (!cw.getPlayer().getInventory().destroyItemByItemId(cw.getItemId(), 1)) {
        _log.info("CursedWeaponsManager[453]: Error! Cursed weapon not found!!!");
      }

      player.sendChanges();
      player.broadcastUserInfo();
    } else if (cw.getItem() != null) {
      cw.getItem().deleteMe();
      cw.getItem().delete();
      _log.info("CursedWeaponsManager: " + cw.getName() + " item has been removed from World.");
    }

    cw.initWeapon();
    removeFromDb(cw.getItemId());
    announce(new SystemMessage(SystemMessage.S1_HAS_DISAPPEARED_CW).addString(cw.getName()));
  }