public void saveToDB(boolean dc, boolean fromcs) {
if (isClone()) {
return;
}
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = null;
PreparedStatement pse = null;
ResultSet rs = null;
try {
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
con.setAutoCommit(false);
ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, demonMarking = ?, map = ?, meso = ?, hpApUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, pets = ?, subcategory = ?, marriageId = ?, currentrep = ?, totalrep = ?, gachexp = ?, fatigue = ?, charm = ?, charisma = ?, craft = ?, insight = ?, sense = ?, will = ?, totalwins = ?, totallosses = ?, pvpExp = ?, pvpPoints = ?, reborns = ?, apstorage = ?, name = ? WHERE id = ?", DatabaseConnection.RETURN_GENERATED_KEYS);
ps.setInt(1, level);
ps.setInt(2, fame);
ps.setInt(3, stats.getStr());
ps.setInt(4, stats.getDex());
ps.setInt(5, stats.getLuk());
ps.setInt(6, stats.getInt());
ps.setInt(7, (level >= 200 || (GameConstants.isKOC(job) && level >= 200)) && !isIntern() ? 0 : exp);
ps.setInt(8, stats.getHp() < 1 ? 50 : stats.getHp());
ps.setInt(9, stats.getMp());
ps.setInt(10, stats.getMaxHp());
ps.setInt(11, stats.getMaxMp());
final StringBuilder sps = new StringBuilder();
for (int i = 0; i < remainingSp.length; i++) {
sps.append(remainingSp);
sps.append(",");
}
final String sp = sps.toString();
ps.setString(12, sp.substring(0, sp.length() - 1));
ps.setInt(13, remainingAp);
ps.setByte(14, gmLevel);
ps.setByte(15, skinColor);
ps.setByte(16, gender);
ps.setInt(17, job);
ps.setInt(18, hair);
ps.setInt(19, face);
ps.setInt(20, demonMarking);
if (!fromcs && map != null) {
if (map.getForcedReturnId() != 999999999 && map.getForcedReturnMap() != null) {
ps.setInt(21, map.getForcedReturnId());
} else {
ps.setInt(21, stats.getHp() < 1 ? map.getReturnMapId() : map.getId());
}
} else {
ps.setInt(21, mapid);
}
ps.setInt(22, meso);
ps.setShort(23, hpApUsed);
if (map == null) {
ps.setByte(24, (byte) 0);
} else {
final MaplePortal closest = map.findClosestSpawnpoint(getTruePosition());
ps.setByte(24, (byte) (closest != null ? closest.getId() : 0));
}
ps.setInt(25, party == null ? -1 : party.getId());
ps.setShort(26, buddylist.getCapacity());
final StringBuilder petz = new StringBuilder();
int petLength = 0;
for (final MaplePet pet : pets) {
if (pet.getSummoned()) {
pet.saveToDb();
petz.append(pet.getInventoryPosition());
petz.append(",");
petLength++;
}
}
while (petLength < 3) {
petz.append("-1,");
petLength++;
}
final String petstring = petz.toString();
ps.setString(27, petstring.substring(0, petstring.length() - 1));
ps.setByte(28, subcategory);
ps.setInt(29, marriageId);
ps.setInt(30, currentrep);
ps.setInt(31, totalrep);
ps.setInt(32, gachexp);
ps.setShort(33, fatigue);
ps.setInt(34, traits.get(MapleTraitType.charm).getTotalExp());
ps.setInt(35, traits.get(MapleTraitType.charisma).getTotalExp());
ps.setInt(36, traits.get(MapleTraitType.craft).getTotalExp());
ps.setInt(37, traits.get(MapleTraitType.insight).getTotalExp());
ps.setInt(38, traits.get(MapleTraitType.sense).getTotalExp());
ps.setInt(39, traits.get(MapleTraitType.will).getTotalExp());
ps.setInt(40, totalWins);
ps.setInt(41, totalLosses);
ps.setInt(42, pvpExp);
ps.setInt(43, pvpPoints);
/*
* Start of Custom Features
*/
ps.setInt(44, reborns);
ps.setInt(45, apstorage);
/*
* End of Custom Features
*/
ps.setString(46, name);
ps.setInt(47, id);
if (ps.executeUpdate() < 1) {
ps.close();
throw new DatabaseException("Character not in database (" + id + ")");
}
ps.close();
deleteWhereCharacterId(con, "DELETE FROM stolen WHERE characterid = ?");
for (Pair<Integer, Boolean> st : stolenSkills) {
ps = con.prepareStatement("INSERT INTO stolen (characterid, skillid, chosen) VALUES (?, ?, ?)");
ps.setInt(1, id);
ps.setInt(2, st.left);
ps.setInt(3, st.right ? 1 : 0);
ps.execute();
ps.close();
}
if(!customQuests.isEmpty()) {
ps= con.prepareStatement("INSERT INTO `customquests` (questid, characterid, queststage) VALUES (?, ?, ?)");
pse = con.prepareStatement("DELETE FROM `customquests` WHERE `characterid` = ?");
pse.setInt(1, id);
pse.executeUpdate();
pse.close();
for(Integer a : customQuests.keySet()) {
ps.setInt(1, a);
ps.setInt(2, id);
ps.setInt(3, customQuests.get(a));//status
ps.executeUpdate();
} ps.close();
}
if (changed_skillmacros) {
deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?");
for (int i = 0; i < 5; i++) {
final SkillMacro macro = skillMacros;
if (macro != null) {
ps = con.prepareStatement("INSERT INTO skillmacros (characterid, skill1, skill2, skill3, name, shout, position) VALUES (?, ?, ?, ?, ?, ?, ?)");
ps.setInt(1, id);
ps.setInt(2, macro.getSkill1());
ps.setInt(3, macro.getSkill2());
ps.setInt(4, macro.getSkill3());
ps.setString(5, macro.getName());
ps.setInt(6, macro.getShout());
ps.setInt(7, i);
ps.execute();
ps.close();
}
}
}
if (changed_pokemon) {
ps = con.prepareStatement("DELETE FROM pokemon WHERE characterid = ? OR (accountid = ? AND active = 0)");
ps.setInt(1, id);
ps.setInt(2, accountid);
ps.execute();
ps.close();
ps = con.prepareStatement("INSERT INTO pokemon (characterid, level, exp, monsterid, name, nature, active, accountid, itemid, gender, hpiv, atkiv, defiv, spatkiv, spdefiv, speediv, evaiv, acciv, ability) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int i = 0; i < battlers.length; i++) {
final Battler macro = battlers;
if (macro != null) {
ps.setInt(1, id);
ps.setInt(2, macro.getLevel());
ps.setInt(3, macro.getExp());
ps.setInt(4, macro.getMonsterId());
ps.setString(5, macro.getName());
ps.setInt(6, macro.getNature().ordinal());
ps.setInt(7, 1);
ps.setInt(8, accountid);
ps.setInt(9, macro.getItem() == null ? 0 : macro.getItem().id);
ps.setByte(10, macro.getGender());
ps.setByte(11, macro.getIV(PokemonStat.HP));
ps.setByte(12, macro.getIV(PokemonStat.ATK));
ps.setByte(13, macro.getIV(PokemonStat.DEF));
ps.setByte(14, macro.getIV(PokemonStat.SPATK));
ps.setByte(15, macro.getIV(PokemonStat.SPDEF));
ps.setByte(16, macro.getIV(PokemonStat.SPEED));
ps.setByte(17, macro.getIV(PokemonStat.EVA));
ps.setByte(18, macro.getIV(PokemonStat.ACC));
ps.setByte(19, macro.getAbilityIndex());
ps.execute();
}
}
for (Battler macro : boxed) {
ps.setInt(1, id);
ps.setInt(2, macro.getLevel());
ps.setInt(3, macro.getExp());
ps.setInt(4, macro.getMonsterId());
ps.setString(5, macro.getName());
ps.setInt(6, macro.getNature().ordinal());
ps.setInt(7, 0);
ps.setInt(8, accountid);
ps.setInt(9, macro.getItem() == null ? 0 : macro.getItem().id);
ps.setByte(10, macro.getGender());
ps.setByte(11, macro.getIV(PokemonStat.HP));
ps.setByte(12, macro.getIV(PokemonStat.ATK));
ps.setByte(13, macro.getIV(PokemonStat.DEF));
ps.setByte(14, macro.getIV(PokemonStat.SPATK));
ps.setByte(15, macro.getIV(PokemonStat.SPDEF));
ps.setByte(16, macro.getIV(PokemonStat.SPEED));
ps.setByte(17, macro.getIV(PokemonStat.EVA));
ps.setByte(18, macro.getIV(PokemonStat.ACC));
ps.setByte(19, macro.getAbilityIndex());
ps.execute();
}
ps.close();
}
deleteWhereCharacterId(con, "DELETE FROM inventoryslot WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO inventoryslot (characterid, `equip`, `use`, `setup`, `etc`, `cash`) VALUES (?, ?, ?, ?, ?, ?)");
ps.setInt(1, id);
ps.setByte(2, getInventory(MapleInventoryType.EQUIP).getSlotLimit());
ps.setByte(3, getInventory(MapleInventoryType.USE).getSlotLimit());
ps.setByte(4, getInventory(MapleInventoryType.SETUP).getSlotLimit());
ps.setByte(5, getInventory(MapleInventoryType.ETC).getSlotLimit());
ps.setByte(6, getInventory(MapleInventoryType.CASH).getSlotLimit());
ps.execute();
ps.close();
saveInventory(con);
if (changed_questinfo) {
deleteWhereCharacterId(con, "DELETE FROM questinfo WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO questinfo (`characterid`, `quest`, `customData`) VALUES (?, ?, ?)");
ps.setInt(1, id);
for (final Entry<Integer, String> q : questinfo.entrySet()) {
ps.setInt(2, q.getKey());
ps.setString(3, q.getValue());
ps.execute();
}
ps.close();
}
deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO queststatus (`queststatusid`, `characterid`, `quest`, `status`, `time`, `forfeited`, `customData`) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?)", DatabaseConnection.RETURN_GENERATED_KEYS);
pse = con.prepareStatement("INSERT INTO queststatusmobs VALUES (DEFAULT, ?, ?, ?)");
ps.setInt(1, id);
for (final MapleQuestStatus q : quests.values()) {
ps.setInt(2, q.getQuest().getId());
ps.setInt(3, q.getStatus());
ps.setInt(4, (int) (q.getCompletionTime() / 1000));
ps.setInt(5, q.getForfeited());
ps.setString(6, q.getCustomData());
ps.execute();
rs = ps.getGeneratedKeys();
if (q.hasMobKills()) {
rs.next();
for (int mob : q.getMobKills().keySet()) {
pse.setInt(1, rs.getInt(1));
pse.setInt(2, mob);
pse.setInt(3, q.getMobKills(mob));
pse.execute();
}
}
rs.close();
}
ps.close();
pse.close();
if (changed_skills) {
deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel, expiration) VALUES (?, ?, ?, ?, ?)");
ps.setInt(1, id);
for (final Entry<Skill, SkillEntry> skill : skills.entrySet()) {
if (GameConstants.isApplicableSkill(skill.getKey().getId())) { //do not save additional skills
ps.setInt(2, skill.getKey().getId());
ps.setInt(3, skill.getValue().skillevel);
ps.setByte(4, skill.getValue().masterlevel);
ps.setLong(5, skill.getValue().expiration);
ps.execute();
}
}
ps.close();
}
List<MapleCoolDownValueHolder> cd = getCooldowns();
if (dc && cd.size() > 0) {
ps = con.prepareStatement("INSERT INTO skills_cooldowns (charid, SkillID, StartTime, length) VALUES (?, ?, ?, ?)");
ps.setInt(1, getId());
for (final MapleCoolDownValueHolder cooling : cd) {
ps.setInt(2, cooling.skillId);
ps.setLong(3, cooling.startTime);
ps.setLong(4, cooling.length);
ps.execute();
}
ps.close();
}
if (changed_savedlocations) {
deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO savedlocations (characterid, `locationtype`, `map`) VALUES (?, ?, ?)");
ps.setInt(1, id);
for (final SavedLocationType savedLocationType : SavedLocationType.values()) {
if (savedLocations[savedLocationType.getValue()] != -1) {
ps.setInt(2, savedLocationType.getValue());
ps.setInt(3, savedLocations[savedLocationType.getValue()]);
ps.execute();
}
}
ps.close();
}
if (changed_achievements) {
ps = con.prepareStatement("DELETE FROM achievements WHERE accountid = ?");
ps.setInt(1, accountid);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("INSERT INTO achievements(charid, achievementid, accountid) VALUES(?, ?, ?)");
for (Integer achid : finishedAchievements) {
ps.setInt(1, id);
ps.setInt(2, achid);
ps.setInt(3, accountid);
ps.execute();
}
ps.close();
}
if (changed_reports) {
deleteWhereCharacterId(con, "DELETE FROM reports WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO reports VALUES(DEFAULT, ?, ?, ?)");
for (Entry<ReportType, Integer> achid : reports.entrySet()) {
ps.setInt(1, id);
ps.setByte(2, achid.getKey().i);
ps.setInt(3, achid.getValue());
ps.execute();
}
ps.close();
}
if (buddylist.changed()) {
deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, ?)");
ps.setInt(1, id);
for (BuddylistEntry entry : buddylist.getBuddies()) {
ps.setInt(2, entry.getCharacterId());
ps.setInt(3, entry.isVisible() ? 0 : 1);
ps.execute();
}
ps.close();
buddylist.setChanged(false);
}
ps = con.prepareStatement("UPDATE accounts SET `nxCredit` = ?, `ACash` = ?, `mPoints` = ?, `points` = ?,`vpoints` = ? , `dpoints` = ? WHERE id = ?");
ps.setInt(1, nxcredit);
ps.setInt(2, acash);
ps.setInt(3, maplepoints);
ps.setInt(4, points);
ps.setInt(5, vpoints);
ps.setInt(6, dpoints);
ps.setInt(7, client.getAccID());
ps.executeUpdate();
ps.close();
if (storage != null) {
storage.saveToDB();
}
if (cs != null) {
cs.save();
}
//PlayerNPC.updateByCharId(this);
keylayout.saveKeys(id);
mount.saveMount(id);
monsterbook.saveCards(accountid);
deleteWhereCharacterId(con, "DELETE FROM familiars WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO familiars (characterid, expiry, name, fatigue, vitality, familiar) VALUES (?, ?, ?, ?, ?, ?)");
ps.setInt(1, id);
for (MonsterFamiliar f : familiars.values()) {
ps.setLong(2, f.getExpiry());
ps.setString(3, f.getName());
ps.setInt(4, f.getFatigue());
ps.setByte(5, f.getVitality());
ps.setInt(6, f.getFamiliar());
ps.executeUpdate();
}
ps.close();
deleteWhereCharacterId(con, "DELETE FROM imps WHERE characterid = ?");
ps = con.prepareStatement("INSERT INTO imps (characterid, itemid, closeness, fullness, state, level) VALUES (?, ?, ?, ?, ?, ?)");
ps.setInt(1, id);
for (int i = 0; i < imps.length; i++) {
if (imps != null) {
ps.setInt(2, imps.getItemId());
ps.setShort(3, imps.getCloseness());
ps.setShort(4, imps.getFullness());
ps.setByte(5, imps.getState());
ps.setByte(6, imps.getLevel());
ps.executeUpdate();
}
}
ps.close();
if (changed_wishlist) {
deleteWhereCharacterId(con, "DELETE FROM wishlist WHERE characterid = ?");
for (int i = 0; i < getWishlistSize(); i++) {
ps = con.prepareStatement("INSERT INTO wishlist(characterid, sn) VALUES(?, ?) ");
ps.setInt(1, getId());
ps.setInt(2, wishlist);
ps.execute();
ps.close();
}
}
if (changed_trocklocations) {
deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?");
for (int i = 0; i < rocks.length; i++) {
if (rocks != 999999999) {
ps = con.prepareStatement("INSERT INTO trocklocations(characterid, mapid) VALUES(?, ?) ");
ps.setInt(1, getId());
ps.setInt(2, rocks);
ps.execute();
ps.close();
}
}
}
if (changed_regrocklocations) {
deleteWhereCharacterId(con, "DELETE FROM regrocklocations WHERE characterid = ?");
for (int i = 0; i < regrocks.length; i++) {
if (regrocks != 999999999) {
ps = con.prepareStatement("INSERT INTO regrocklocations(characterid, mapid) VALUES(?, ?) ");
ps.setInt(1, getId());
ps.setInt(2, regrocks);
ps.execute();
ps.close();
}
}
}
if (changed_hyperrocklocations) {
deleteWhereCharacterId(con, "DELETE FROM hyperrocklocations WHERE characterid = ?");
for (int i = 0; i < hyperrocks.length; i++) {
if (hyperrocks != 999999999) {
ps = con.prepareStatement("INSERT INTO hyperrocklocations(characterid, mapid) VALUES(?, ?) ");
ps.setInt(1, getId());
ps.setInt(2, hyperrocks);
ps.execute();
ps.close();
}
}
}
if (changed_extendedSlots) {
deleteWhereCharacterId(con, "DELETE FROM extendedSlots WHERE characterid = ?");
for (int i : extendedSlots) {
if (getInventory(MapleInventoryType.ETC).findById(i) != null) { //just in case
ps = con.prepareStatement("INSERT INTO extendedSlots(characterid, itemId) VALUES(?, ?) ");
ps.setInt(1, getId());
ps.setInt(2, i);
ps.execute();
ps.close();
}
}
}
changed_wishlist = false;
changed_trocklocations = false;
changed_regrocklocations = false;
changed_hyperrocklocations = false;
changed_skillmacros = false;
changed_savedlocations = false;
changed_pokemon = false;
changed_questinfo = false;
changed_achievements = false;
changed_extendedSlots = false;
changed_skills = false;
changed_reports = false;
con.commit();
} catch (Exception e) {
FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, e);
e.printStackTrace();
System.err.println(MapleClient.getLogMessage(this, "[charsave] Error saving character data") + e);
try {
con.rollback();
} catch (SQLException ex) {
FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, ex);
System.err.println(MapleClient.getLogMessage(this, "[charsave] Error Rolling Back") + e);
}
} finally {
try {
if (ps != null) {
ps.close();
}
if (pse != null) {
pse.close();
}
if (rs != null) {
rs.close();
}
con.setAutoCommit(true);
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} catch (SQLException e) {
FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, e);
System.err.println(MapleClient.getLogMessage(this, "[charsave] Error going back to autocommit mode") + e);
}
}
}