refactor: use try-with-resources for new character db operations

This commit is contained in:
P0nk
2021-04-04 21:21:09 +02:00
parent b56e1d501f
commit 2b0d918401

View File

@@ -8168,27 +8168,26 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
meso.set(recipe.getMeso()); meso.set(recipe.getMeso());
List<Pair<Skill, Integer>> startingSkills = recipe.getStartingSkillLevel(); List<Pair<Skill, Integer>> startingSkills = recipe.getStartingSkillLevel();
for(Pair<Skill, Integer> skEntry : startingSkills) { for (Pair<Skill, Integer> skEntry : startingSkills) {
Skill skill = skEntry.getLeft(); Skill skill = skEntry.getLeft();
this.changeSkillLevel(skill, skEntry.getRight().byteValue(), skill.getMaxLevel(), -1); this.changeSkillLevel(skill, skEntry.getRight().byteValue(), skill.getMaxLevel(), -1);
} }
List<Pair<Item, MapleInventoryType>> itemsWithType = recipe.getStartingItems(); List<Pair<Item, MapleInventoryType>> itemsWithType = recipe.getStartingItems();
for(Pair<Item, MapleInventoryType> itEntry : itemsWithType) { for (Pair<Item, MapleInventoryType> itEntry : itemsWithType) {
this.getInventory(itEntry.getRight()).addItem(itEntry.getLeft()); this.getInventory(itEntry.getRight()).addItem(itEntry.getLeft());
} }
this.events.put("rescueGaga", new RescueGaga(0)); this.events.put("rescueGaga", new RescueGaga(0));
Connection con = null;
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
try { try {
con = DatabaseConnection.getConnection(); // Character info
try (PreparedStatement ps = con.prepareStatement("INSERT INTO characters (str, dex, luk, `int`, gm, skincolor, gender, job, hair, face, map, meso, spawnpoint, accountid, name, world, hp, mp, maxhp, maxmp, level, ap, sp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
con.setAutoCommit(false);
ps = con.prepareStatement("INSERT INTO characters (str, dex, luk, `int`, gm, skincolor, gender, job, hair, face, map, meso, spawnpoint, accountid, name, world, hp, mp, maxhp, maxmp, level, ap, sp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, str); ps.setInt(1, str);
ps.setInt(2, dex); ps.setInt(2, dex);
ps.setInt(3, luk); ps.setInt(3, luk);
@@ -8213,8 +8212,8 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
ps.setInt(22, remainingAp); ps.setInt(22, remainingAp);
StringBuilder sps = new StringBuilder(); StringBuilder sps = new StringBuilder();
for (int i = 0; i < remainingSp.length; i++) { for (int j : remainingSp) {
sps.append(remainingSp[i]); sps.append(j);
sps.append(","); sps.append(",");
} }
String sp = sps.toString(); String sp = sps.toString();
@@ -8222,28 +8221,26 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
int updateRows = ps.executeUpdate(); int updateRows = ps.executeUpdate();
if (updateRows < 1) { if (updateRows < 1) {
ps.close();
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Error trying to insert " + name); FilePrinter.printError(FilePrinter.INSERT_CHAR, "Error trying to insert " + name);
return false; return false;
} }
ResultSet rs = ps.getGeneratedKeys();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) { if (rs.next()) {
this.id = rs.getInt(1); this.id = rs.getInt(1);
rs.close();
ps.close();
} else { } else {
rs.close();
ps.close();
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Inserting char failed " + name); FilePrinter.printError(FilePrinter.INSERT_CHAR, "Inserting char failed " + name);
return false; return false;
} }
}
}
// Select a keybinding method // Select a keybinding method
int[] selectedKey; int[] selectedKey;
int[] selectedType; int[] selectedType;
int[] selectedAction; int[] selectedAction;
if(YamlConfig.config.server.USE_CUSTOM_KEYSET) { if (YamlConfig.config.server.USE_CUSTOM_KEYSET) {
selectedKey = GameConstants.getCustomKey(true); selectedKey = GameConstants.getCustomKey(true);
selectedType = GameConstants.getCustomType(true); selectedType = GameConstants.getCustomType(true);
selectedAction = GameConstants.getCustomAction(true); selectedAction = GameConstants.getCustomAction(true);
@@ -8253,26 +8250,28 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
selectedAction = GameConstants.getCustomAction(false); selectedAction = GameConstants.getCustomAction(false);
} }
ps = con.prepareStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)"); // Key config
try (PreparedStatement ps = con.prepareStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)")) {
ps.setInt(1, id); ps.setInt(1, id);
for (int i = 0; i < selectedKey.length; i++) { for (int i = 0; i < selectedKey.length; i++) {
ps.setInt(2, selectedKey[i]); ps.setInt(2, selectedKey[i]);
ps.setInt(3, selectedType[i]); ps.setInt(3, selectedType[i]);
ps.setInt(4, selectedAction[i]); ps.setInt(4, selectedAction[i]);
ps.execute(); ps.executeUpdate();
}
} }
ps.close();
// No quickslots, or no change. // No quickslots, or no change.
boolean bQuickslotEquals = this.m_pQuickslotKeyMapped == null || (this.m_aQuickslotLoaded != null && Arrays.equals(this.m_pQuickslotKeyMapped.GetKeybindings(), this.m_aQuickslotLoaded)); boolean bQuickslotEquals = this.m_pQuickslotKeyMapped == null || (this.m_aQuickslotLoaded != null && Arrays.equals(this.m_pQuickslotKeyMapped.GetKeybindings(), this.m_aQuickslotLoaded));
if (!bQuickslotEquals) { if (!bQuickslotEquals) {
long nQuickslotKeymapped = LongTool.BytesToLong(this.m_pQuickslotKeyMapped.GetKeybindings()); long nQuickslotKeymapped = LongTool.BytesToLong(this.m_pQuickslotKeyMapped.GetKeybindings());
try (final PreparedStatement pInsertStatement = con.prepareStatement("INSERT INTO quickslotkeymapped (accountid, keymap) VALUES (?, ?) ON DUPLICATE KEY UPDATE keymap = ?;")) { // Quickslot key config
pInsertStatement.setInt(1, this.getAccountID()); try (PreparedStatement ps = con.prepareStatement("INSERT INTO quickslotkeymapped (accountid, keymap) VALUES (?, ?) ON DUPLICATE KEY UPDATE keymap = ?;")) {
pInsertStatement.setLong(2, nQuickslotKeymapped); ps.setInt(1, this.getAccountID());
pInsertStatement.setLong(3, nQuickslotKeymapped); ps.setLong(2, nQuickslotKeymapped);
pInsertStatement.executeUpdate(); ps.setLong(3, nQuickslotKeymapped);
ps.executeUpdate();
} }
} }
@@ -8285,8 +8284,9 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
ItemFactory.INVENTORY.saveItems(itemsWithType, id, con); ItemFactory.INVENTORY.saveItems(itemsWithType, id, con);
if(!skills.isEmpty()) { if (!skills.isEmpty()) {
ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel, expiration) VALUES (?, ?, ?, ?, ?)"); // Skills
try (PreparedStatement ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel, expiration) VALUES (?, ?, ?, ?, ?)")) {
ps.setInt(1, id); ps.setInt(1, id);
for (Entry<Skill, SkillEntry> skill : skills.entrySet()) { for (Entry<Skill, SkillEntry> skill : skills.entrySet()) {
ps.setInt(2, skill.getKey().getId()); ps.setInt(2, skill.getKey().getId());
@@ -8296,31 +8296,23 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
ps.addBatch(); ps.addBatch();
} }
ps.executeBatch(); ps.executeBatch();
ps.close(); }
} }
con.commit(); con.commit();
return true; return true;
} catch (Exception e) {
con.rollback();
throw e;
} finally {
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
con.setAutoCommit(true);
}
} catch (Throwable t) { } catch (Throwable t) {
FilePrinter.printError(FilePrinter.INSERT_CHAR, t, "Error creating " + name + " Level: " + level + " Job: " + job.getId()); FilePrinter.printError(FilePrinter.INSERT_CHAR, t, "Error creating " + name + " Level: " + level + " Job: " + job.getId());
try {
con.rollback();
} catch (SQLException se) {
FilePrinter.printError(FilePrinter.INSERT_CHAR, se, "Error trying to rollback " + name);
} }
return false; return false;
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
con.setAutoCommit(true);
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
public void saveCharToDB() { public void saveCharToDB() {