refactor: use try-with-resources for various server db operations

This commit is contained in:
P0nk
2021-04-04 15:35:17 +02:00
parent 3ca9311cb0
commit e322b05707

View File

@@ -21,28 +21,22 @@
*/ */
package net.server; package net.server;
import java.io.IOException; import client.MapleCharacter;
import java.net.InetSocketAddress; import client.MapleClient;
import java.security.Security; import client.MapleFamily;
import java.sql.Connection; import client.SkillFactory;
import java.sql.PreparedStatement; import client.command.CommandsExecutor;
import java.sql.ResultSet; import client.inventory.Item;
import java.sql.SQLException; import client.inventory.ItemFactory;
import java.util.ArrayList; import client.inventory.manipulator.MapleCashidGenerator;
import java.util.Calendar; import client.newyear.NewYearCardRecord;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import config.YamlConfig; import config.YamlConfig;
import constants.game.GameConstants;
import constants.inventory.ItemConstants;
import constants.net.OpcodeConstants;
import constants.net.ServerConstants;
import net.MapleServerHandler;
import net.mina.MapleCodecFactory;
import net.server.audit.ThreadTracker; import net.server.audit.ThreadTracker;
import net.server.audit.locks.MonitoredLockType; import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.MonitoredReadLock; import net.server.audit.locks.MonitoredReadLock;
@@ -51,28 +45,13 @@ import net.server.audit.locks.MonitoredWriteLock;
import net.server.audit.locks.factory.MonitoredReadLockFactory; import net.server.audit.locks.factory.MonitoredReadLockFactory;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory; import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.audit.locks.factory.MonitoredWriteLockFactory; import net.server.audit.locks.factory.MonitoredWriteLockFactory;
import net.MapleServerHandler;
import net.mina.MapleCodecFactory;
import net.server.channel.Channel; import net.server.channel.Channel;
import net.server.coordinator.session.MapleSessionCoordinator; import net.server.coordinator.session.MapleSessionCoordinator;
import net.server.guild.MapleAlliance; import net.server.guild.MapleAlliance;
import net.server.guild.MapleGuild; import net.server.guild.MapleGuild;
import net.server.guild.MapleGuildCharacter; import net.server.guild.MapleGuildCharacter;
import net.server.task.BossLogTask; import net.server.task.*;
import net.server.task.CharacterDiseaseTask;
import net.server.task.CouponTask;
import net.server.task.EventRecallCoordinatorTask;
import net.server.task.DueyFredrickTask;
import net.server.task.InvitationTask;
import net.server.task.LoginCoordinatorTask;
import net.server.task.LoginStorageTask;
import net.server.task.RankingCommandTask;
import net.server.task.RankingLoginTask;
import net.server.task.ReleaseLockTask;
import net.server.task.RespawnTask;
import net.server.world.World; import net.server.world.World;
import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.buffer.SimpleBufferAllocator; import org.apache.mina.core.buffer.SimpleBufferAllocator;
import org.apache.mina.core.filterchain.IoFilter; import org.apache.mina.core.filterchain.IoFilter;
@@ -80,21 +59,6 @@ import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import client.MapleClient;
import client.MapleFamily;
import client.MapleCharacter;
import client.SkillFactory;
import client.command.CommandsExecutor;
import client.inventory.Item;
import client.inventory.ItemFactory;
import client.inventory.manipulator.MapleCashidGenerator;
import client.newyear.NewYearCardRecord;
import constants.inventory.ItemConstants;
import constants.game.GameConstants;
import constants.net.OpcodeConstants;
import constants.net.ServerConstants;
import java.util.TimeZone;
import server.CashShop.CashItemFactory; import server.CashShop.CashItemFactory;
import server.MapleSkillbookInformationProvider; import server.MapleSkillbookInformationProvider;
import server.ThreadManager; import server.ThreadManager;
@@ -107,6 +71,18 @@ import tools.DatabaseConnection;
import tools.FilePrinter; import tools.FilePrinter;
import tools.Pair; import tools.Pair;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.Security;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
public class Server { public class Server {
private static Server instance = null; private static Server instance = null;
@@ -209,23 +185,23 @@ public class Server {
} }
private void loadPlayerNpcMapStepFromDb() { private void loadPlayerNpcMapStepFromDb() {
try { final List<World> wlist = this.getWorlds();
List<World> wlist = this.getWorlds();
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs_field"); PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs_field");
ResultSet rs = ps.executeQuery()) {
ResultSet rs = ps.executeQuery();
while (rs.next()) { while (rs.next()) {
int world = rs.getInt("world"), map = rs.getInt("map"), step = rs.getInt("step"), podium = rs.getInt("podium"); int world = rs.getInt("world");
int map = rs.getInt("map");
int step = rs.getInt("step");
int podium = rs.getInt("podium");
World w = wlist.get(world); World w = wlist.get(world);
if(w != null) w.setPlayerNpcMapData(map, step, podium); if (w != null) {
w.setPlayerNpcMapData(map, step, podium);
}
} }
rs.close();
ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -339,13 +315,19 @@ public class Server {
wldRLock.lock(); wldRLock.lock();
try { try {
if(worldid >= worlds.size()) return -3; if (worldid >= worlds.size()) {
return -3;
}
channelInfo = channels.get(worldid); channelInfo = channels.get(worldid);
if(channelInfo == null) return -3; if (channelInfo == null) {
return -3;
}
channelid = channelInfo.size(); channelid = channelInfo.size();
if(channelid >= YamlConfig.config.server.CHANNEL_SIZE) return -2; if (channelid >= YamlConfig.config.server.CHANNEL_SIZE) {
return -2;
}
channelid++; channelid++;
world = this.getWorld(worldid); world = this.getWorld(worldid);
@@ -463,7 +445,9 @@ public class Server {
wldRLock.lock(); wldRLock.lock();
try { try {
if(worldid >= worlds.size()) return false; if (worldid >= worlds.size()) {
return false;
}
world = worlds.get(worldid); world = worlds.get(worldid);
} finally { } finally {
wldRLock.unlock(); wldRLock.unlock();
@@ -474,7 +458,9 @@ public class Server {
wldWLock.lock(); wldWLock.lock();
try { try {
Map<Integer, String> m = channels.get(worldid); Map<Integer, String> m = channels.get(worldid);
if(m != null) m.remove(channel); if (m != null) {
m.remove(channel);
}
} finally { } finally {
wldWLock.unlock(); wldWLock.unlock();
} }
@@ -557,36 +543,37 @@ public class Server {
} }
public static void cleanNxcodeCoupons(Connection con) throws SQLException { public static void cleanNxcodeCoupons(Connection con) throws SQLException {
if (!YamlConfig.config.server.USE_CLEAR_OUTDATED_COUPONS) return; if (!YamlConfig.config.server.USE_CLEAR_OUTDATED_COUPONS) {
return;
}
long timeClear = System.currentTimeMillis() - 14 * 24 * 60 * 60 * 1000; long timeClear = System.currentTimeMillis() - 14 * 24 * 60 * 60 * 1000;
PreparedStatement ps = con.prepareStatement("SELECT * FROM nxcode WHERE expiration <= ?"); try (PreparedStatement ps = con.prepareStatement("SELECT * FROM nxcode WHERE expiration <= ?")) {
ps.setLong(1, timeClear); ps.setLong(1, timeClear);
ResultSet rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (!rs.isLast()) { if (!rs.isLast()) {
PreparedStatement ps2 = con.prepareStatement("DELETE FROM nxcode_items WHERE codeid = ?"); try (PreparedStatement ps2 = con.prepareStatement("DELETE FROM nxcode_items WHERE codeid = ?")) {
while (rs.next()) { while (rs.next()) {
ps2.setInt(1, rs.getInt("id")); ps2.setInt(1, rs.getInt("id"));
ps2.addBatch(); ps2.addBatch();
} }
ps2.executeBatch(); ps2.executeBatch();
ps2.close();
ps2 = con.prepareStatement("DELETE FROM nxcode WHERE expiration <= ?");
ps2.setLong(1, timeClear);
ps2.executeUpdate();
ps2.close();
} }
rs.close(); try (PreparedStatement ps2 = con.prepareStatement("DELETE FROM nxcode WHERE expiration <= ?")) {
ps.close(); ps2.setLong(1, timeClear);
ps2.executeUpdate();
}
}
}
}
} }
private void loadCouponRates(Connection c) throws SQLException { private void loadCouponRates(Connection c) throws SQLException {
PreparedStatement ps = c.prepareStatement("SELECT couponid, rate FROM nxcoupons"); try (PreparedStatement ps = c.prepareStatement("SELECT couponid, rate FROM nxcoupons");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
int cid = rs.getInt("couponid"); int cid = rs.getInt("couponid");
@@ -594,9 +581,7 @@ public class Server {
couponRates.put(cid, rate); couponRates.put(cid, rate);
} }
}
rs.close();
ps.close();
} }
public List<Integer> getActiveCoupons() { public List<Integer> getActiveCoupons() {
@@ -608,7 +593,9 @@ public class Server {
public void commitActiveCoupons() { public void commitActiveCoupons() {
for (World world : getWorlds()) { for (World world : getWorlds()) {
for (MapleCharacter chr : world.getPlayerStorage().getAllCharacters()) { for (MapleCharacter chr : world.getPlayerStorage().getAllCharacters()) {
if(!chr.isLoggedin()) continue; if (!chr.isLoggedin()) {
continue;
}
chr.updateCouponRates(); chr.updateCouponRates();
} }
@@ -620,8 +607,7 @@ public class Server {
synchronized (activeCoupons) { synchronized (activeCoupons) {
if (activeCoupons.contains(couponId)) { if (activeCoupons.contains(couponId)) {
activeCoupons.remove(couponId); activeCoupons.remove(couponId);
} } else {
else {
activeCoupons.add(couponId); activeCoupons.add(couponId);
} }
@@ -638,9 +624,7 @@ public class Server {
int weekDay = c.get(Calendar.DAY_OF_WEEK); int weekDay = c.get(Calendar.DAY_OF_WEEK);
int hourDay = c.get(Calendar.HOUR_OF_DAY); int hourDay = c.get(Calendar.HOUR_OF_DAY);
Connection con = null; try (Connection con = DatabaseConnection.getConnection()) {
try {
con = DatabaseConnection.getConnection();
int weekdayMask = (1 << weekDay); int weekdayMask = (1 << weekDay);
PreparedStatement ps = con.prepareStatement("SELECT couponid FROM nxcoupons WHERE (activeday & ?) = ? AND starthour <= ? AND endhour > ?"); PreparedStatement ps = con.prepareStatement("SELECT couponid FROM nxcoupons WHERE (activeday & ?) = ? AND starthour <= ? AND endhour > ?");
@@ -649,25 +633,13 @@ public class Server {
ps.setInt(3, hourDay); ps.setInt(3, hourDay);
ps.setInt(4, hourDay); ps.setInt(4, hourDay);
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
activeCoupons.add(rs.getInt("couponid")); activeCoupons.add(rs.getInt("couponid"));
} }
}
rs.close();
ps.close();
con.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
try {
if(con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex2) {
ex2.printStackTrace();
}
} }
} }
} }
@@ -798,11 +770,7 @@ public class Server {
List<Pair<Integer, List<Pair<String, Integer>>>> rankSystem = new ArrayList<>(); List<Pair<Integer, List<Pair<String, Integer>>>> rankSystem = new ArrayList<>();
List<Pair<String, Integer>> rankUpdate = new ArrayList<>(0); List<Pair<String, Integer>> rankUpdate = new ArrayList<>(0);
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection()) {
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
String worldQuery; String worldQuery;
if (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING) { if (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING) {
@@ -815,8 +783,8 @@ public class Server {
worldQuery = (" AND `characters`.`world` >= 0 AND `characters`.`world` <= " + Math.abs(worldid)); worldQuery = (" AND `characters`.`world` >= 0 AND `characters`.`world` <= " + Math.abs(worldid));
} }
ps = con.prepareStatement("SELECT `characters`.`name`, `characters`.`level`, `characters`.`world` FROM `characters` LEFT JOIN accounts ON accounts.id = characters.accountid WHERE `characters`.`gm` < 2 AND `accounts`.`banned` = '0'" + worldQuery + " ORDER BY " + (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING ? "world, " : "") + "level DESC, exp DESC, lastExpGainTime ASC LIMIT 50"); try (PreparedStatement ps = con.prepareStatement("SELECT `characters`.`name`, `characters`.`level`, `characters`.`world` FROM `characters` LEFT JOIN accounts ON accounts.id = characters.accountid WHERE `characters`.`gm` < 2 AND `accounts`.`banned` = '0'" + worldQuery + " ORDER BY " + (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING ? "world, " : "") + "level DESC, exp DESC, lastExpGainTime ASC LIMIT 50");
rs = ps.executeQuery(); ResultSet rs = ps.executeQuery()) {
if (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING) { if (!YamlConfig.config.server.USE_WHOLE_SERVER_RANKING) {
int currentWorld = -1; int currentWorld = -1;
@@ -838,26 +806,9 @@ public class Server {
rankUpdate.add(new Pair<>(rs.getString("name"), rs.getInt("level"))); rankUpdate.add(new Pair<>(rs.getString("name"), rs.getInt("level")));
} }
} }
}
ps.close();
rs.close();
con.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} finally {
try {
if(ps != null && !ps.isClosed()) {
ps.close();
}
if(rs != null && !rs.isClosed()) {
rs.close();
}
if(con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
return rankSystem; return rankSystem;
@@ -876,24 +827,22 @@ public class Server {
TimeZone.setDefault(TimeZone.getTimeZone(YamlConfig.config.server.TIMEZONE)); TimeZone.setDefault(TimeZone.getTimeZone(YamlConfig.config.server.TIMEZONE));
Connection c = null; try (Connection con = DatabaseConnection.getConnection()) {
try { try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = 0")) {
c = DatabaseConnection.getConnection();
PreparedStatement ps = c.prepareStatement("UPDATE accounts SET loggedin = 0");
ps.executeUpdate(); ps.executeUpdate();
ps.close(); }
ps = c.prepareStatement("UPDATE characters SET HasMerchant = 0");
ps.executeUpdate();
ps.close();
cleanNxcodeCoupons(c); try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0")) {
loadCouponRates(c); ps.executeUpdate();
}
cleanNxcodeCoupons(con);
loadCouponRates(con);
updateActiveCoupons(); updateActiveCoupons();
c.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }
applyAllNameChanges(); // -- name changes can be missed by INSTANT_NAME_CHANGE -- applyAllNameChanges(); // -- name changes can be missed by INSTANT_NAME_CHANGE --
applyAllWorldTransfers(); applyAllWorldTransfers();
//MaplePet.clearMissingPetsFromDb(); // thanks Optimist for noticing this taking too long to run //MaplePet.clearMissingPetsFromDb(); // thanks Optimist for noticing this taking too long to run
@@ -917,7 +866,9 @@ public class Server {
NewYearCardRecord.startPendingNewYearCardRequests(); NewYearCardRecord.startPendingNewYearCardRequests();
if(YamlConfig.config.server.USE_THREAD_TRACKER) ThreadTracker.getInstance().registerThreadTrackerTask(); if (YamlConfig.config.server.USE_THREAD_TRACKER) {
ThreadTracker.getInstance().registerThreadTrackerTask();
}
try { try {
Integer worldCount = Math.min(GameConstants.WORLD_NAMES.length, YamlConfig.config.server.WORLDS); Integer worldCount = Math.min(GameConstants.WORLD_NAMES.length, YamlConfig.config.server.WORLDS);
@@ -1286,7 +1237,9 @@ public class Server {
} }
public void deleteGuildCharacter(MapleGuildCharacter mgc) { public void deleteGuildCharacter(MapleGuildCharacter mgc) {
if(mgc.getCharacter() != null) setGuildMemberOnline(mgc.getCharacter(), false, (byte) -1); if (mgc.getCharacter() != null) {
setGuildMemberOnline(mgc.getCharacter(), false, (byte) -1);
}
if (mgc.getGuildRank() > 1) { if (mgc.getGuildRank() > 1) {
leaveGuild(mgc); leaveGuild(mgc);
} else { } else {
@@ -1401,7 +1354,9 @@ public class Server {
lgnWLock.lock(); lgnWLock.lock();
try { try {
World wserv = this.getWorld(chrView.getWorld()); World wserv = this.getWorld(chrView.getWorld());
if(wserv != null) wserv.registerAccountCharacterView(chrView.getAccountID(), chrView); if (wserv != null) {
wserv.registerAccountCharacterView(chrView.getAccountID(), chrView);
}
} finally { } finally {
lgnWLock.unlock(); lgnWLock.unlock();
} }
@@ -1422,7 +1377,9 @@ public class Server {
MapleCharacter chrView = chr.generateCharacterEntry(); MapleCharacter chrView = chr.generateCharacterEntry();
World wserv = this.getWorld(chrView.getWorld()); World wserv = this.getWorld(chrView.getWorld());
if(wserv != null) wserv.registerAccountCharacterView(chrView.getAccountID(), chrView); if (wserv != null) {
wserv.registerAccountCharacterView(chrView.getAccountID(), chrView);
}
} finally { } finally {
lgnWLock.unlock(); lgnWLock.unlock();
} }
@@ -1439,7 +1396,9 @@ public class Server {
Integer world = worldChars.remove(chrid); Integer world = worldChars.remove(chrid);
if (world != null) { if (world != null) {
World wserv = this.getWorld(world); World wserv = this.getWorld(world);
if(wserv != null) wserv.unregisterAccountCharacterView(accountid, chrid); if (wserv != null) {
wserv.unregisterAccountCharacterView(accountid, chrid);
}
} }
} finally { } finally {
lgnWLock.unlock(); lgnWLock.unlock();
@@ -1452,7 +1411,9 @@ public class Server {
Integer chrid = chr.getId(), accountid = chr.getAccountID(), world = worldChars.get(chr.getId()); Integer chrid = chr.getId(), accountid = chr.getAccountID(), world = worldChars.get(chr.getId());
if (world != null) { if (world != null) {
World wserv = this.getWorld(world); World wserv = this.getWorld(world);
if(wserv != null) wserv.unregisterAccountCharacterView(accountid, chrid); if (wserv != null) {
wserv.unregisterAccountCharacterView(accountid, chrid);
}
} }
worldChars.put(chrid, toWorld); worldChars.put(chrid, toWorld);
@@ -1460,7 +1421,9 @@ public class Server {
MapleCharacter chrView = chr.generateCharacterEntry(); MapleCharacter chrView = chr.generateCharacterEntry();
World wserv = this.getWorld(toWorld); World wserv = this.getWorld(toWorld);
if(wserv != null) wserv.registerAccountCharacterView(chrView.getAccountID(), chrView); if (wserv != null) {
wserv.registerAccountCharacterView(chrView.getAccountID(), chrView);
}
} finally { } finally {
lgnWLock.unlock(); lgnWLock.unlock();
} }
@@ -1485,7 +1448,9 @@ public class Server {
public Pair<Pair<Integer, List<MapleCharacter>>, List<Pair<Integer, List<MapleCharacter>>>> loadAccountCharlist(Integer accountId, int visibleWorlds) { public Pair<Pair<Integer, List<MapleCharacter>>, List<Pair<Integer, List<MapleCharacter>>>> loadAccountCharlist(Integer accountId, int visibleWorlds) {
List<World> wlist = this.getWorlds(); List<World> wlist = this.getWorlds();
if(wlist.size() > visibleWorlds) wlist = wlist.subList(0, visibleWorlds); if (wlist.size() > visibleWorlds) {
wlist = wlist.subList(0, visibleWorlds);
}
List<Pair<Integer, List<MapleCharacter>>> accChars = new ArrayList<>(wlist.size() + 1); List<Pair<Integer, List<MapleCharacter>>> accChars = new ArrayList<>(wlist.size() + 1);
int chrTotal = 0; int chrTotal = 0;
@@ -1517,7 +1482,9 @@ public class Server {
private static Pair<Short, List<List<MapleCharacter>>> loadAccountCharactersViewFromDb(int accId, int wlen) { private static Pair<Short, List<List<MapleCharacter>>> loadAccountCharactersViewFromDb(int accId, int wlen) {
short characterCount = 0; short characterCount = 0;
List<List<MapleCharacter>> wchars = new ArrayList<>(wlen); List<List<MapleCharacter>> wchars = new ArrayList<>(wlen);
for(int i = 0; i < wlen; i++) wchars.add(i, new LinkedList<MapleCharacter>()); for (int i = 0; i < wlen; i++) {
wchars.add(i, new LinkedList<MapleCharacter>());
}
List<MapleCharacter> chars = new LinkedList<>(); List<MapleCharacter> chars = new LinkedList<>();
int curWorld = 0; int curWorld = 0;
@@ -1535,15 +1502,18 @@ public class Server {
playerEquips.add(ae.getLeft()); playerEquips.add(ae.getLeft());
} }
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE accountid = ? ORDER BY world, id")) { try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE accountid = ? ORDER BY world, id")) {
ps.setInt(1, accId); ps.setInt(1, accId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
characterCount++; characterCount++;
int cworld = rs.getByte("world"); int cworld = rs.getByte("world");
if(cworld >= wlen) continue; if (cworld >= wlen) {
continue;
}
if (cworld > curWorld) { if (cworld > curWorld) {
wchars.add(curWorld, chars); wchars.add(curWorld, chars);
@@ -1557,7 +1527,6 @@ public class Server {
} }
} }
} }
con.close();
wchars.add(curWorld, chars); wchars.add(curWorld, chars);
} catch (SQLException sqle) { } catch (SQLException sqle) {
@@ -1568,10 +1537,9 @@ public class Server {
} }
public void loadAllAccountsCharactersView() { public void loadAllAccountsCharactersView() {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT id FROM accounts"); PreparedStatement ps = con.prepareStatement("SELECT id FROM accounts");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
int accountId = rs.getInt("id"); int accountId = rs.getInt("id");
@@ -1579,10 +1547,6 @@ public class Server {
loadAccountCharactersView(accountId, 0, 0); loadAccountCharactersView(accountId, 0, 0);
} }
} }
rs.close();
ps.close();
con.close();
} catch (SQLException se) { } catch (SQLException se) {
se.printStackTrace(); se.printStackTrace();
} }
@@ -1599,8 +1563,8 @@ public class Server {
private static void applyAllNameChanges() { private static void applyAllNameChanges() {
try (Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM namechanges WHERE completionTime IS NULL")) { PreparedStatement ps = con.prepareStatement("SELECT * FROM namechanges WHERE completionTime IS NULL");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery()) {
List<Pair<String, String>> changedNames = new LinkedList<Pair<String, String>>(); //logging only List<Pair<String, String>> changedNames = new LinkedList<Pair<String, String>>(); //logging only
while (rs.next()) { while (rs.next()) {
con.setAutoCommit(false); con.setAutoCommit(false);
@@ -1609,8 +1573,11 @@ public class Server {
String oldName = rs.getString("old"); String oldName = rs.getString("old");
String newName = rs.getString("new"); String newName = rs.getString("new");
boolean success = MapleCharacter.doNameChange(con, characterId, oldName, newName, nameChangeId); boolean success = MapleCharacter.doNameChange(con, characterId, oldName, newName, nameChangeId);
if(!success) con.rollback(); //discard changes if (!success) {
else changedNames.add(new Pair<String, String>(oldName, newName)); con.rollback(); //discard changes
} else {
changedNames.add(new Pair<String, String>(oldName, newName));
}
con.setAutoCommit(true); con.setAutoCommit(true);
} }
//log //log
@@ -1626,8 +1593,8 @@ public class Server {
private static void applyAllWorldTransfers() { private static void applyAllWorldTransfers() {
try (Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM worldtransfers WHERE completionTime IS NULL", PreparedStatement ps = con.prepareStatement("SELECT * FROM worldtransfers WHERE completionTime IS NULL",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery()) {
List<Integer> removedTransfers = new LinkedList<Integer>(); List<Integer> removedTransfers = new LinkedList<Integer>();
while (rs.next()) { while (rs.next()) {
int nameChangeId = rs.getInt("id"); int nameChangeId = rs.getInt("id");
@@ -1652,13 +1619,18 @@ public class Server {
while (rs.next()) { while (rs.next()) {
con.setAutoCommit(false); con.setAutoCommit(false);
int nameChangeId = rs.getInt("id"); int nameChangeId = rs.getInt("id");
if(removedTransfers.contains(nameChangeId)) continue; if (removedTransfers.contains(nameChangeId)) {
continue;
}
int characterId = rs.getInt("characterId"); int characterId = rs.getInt("characterId");
int oldWorld = rs.getInt("from"); int oldWorld = rs.getInt("from");
int newWorld = rs.getInt("to"); int newWorld = rs.getInt("to");
boolean success = MapleCharacter.doWorldTransfer(con, characterId, oldWorld, newWorld, nameChangeId); boolean success = MapleCharacter.doWorldTransfer(con, characterId, oldWorld, newWorld, nameChangeId);
if(!success) con.rollback(); if (!success) {
else worldTransfers.add(new Pair<Integer, Pair<Integer, Integer>>(characterId, new Pair<Integer, Integer>(oldWorld, newWorld))); con.rollback();
} else {
worldTransfers.add(new Pair<Integer, Pair<Integer, Integer>>(characterId, new Pair<Integer, Integer>(oldWorld, newWorld)));
}
con.setAutoCommit(true); con.setAutoCommit(true);
} }
//log //log
@@ -1694,7 +1666,9 @@ public class Server {
if (wserv != null) { if (wserv != null) {
for (MapleCharacter chr : wserv.getAllCharactersView()) { for (MapleCharacter chr : wserv.getAllCharactersView()) {
if (gmLevel < chr.gmLevel()) gmLevel = chr.gmLevel(); if (gmLevel < chr.gmLevel()) {
gmLevel = chr.gmLevel();
}
} }
} }
} }
@@ -1728,7 +1702,9 @@ public class Server {
for (MapleCharacter chr : wchars) { for (MapleCharacter chr : wchars) {
int cid = chr.getId(); int cid = chr.getId();
if (gmLevel < chr.gmLevel()) gmLevel = chr.gmLevel(); if (gmLevel < chr.gmLevel()) {
gmLevel = chr.gmLevel();
}
chars.add(cid); chars.add(cid);
worldChars.put(cid, wid); worldChars.put(cid, wid);
@@ -1897,7 +1873,9 @@ public class Server {
private synchronized void shutdownInternal(boolean restart) { private synchronized void shutdownInternal(boolean restart) {
System.out.println((restart ? "Restarting" : "Shutting down") + " the server!\r\n"); System.out.println((restart ? "Restarting" : "Shutting down") + " the server!\r\n");
if (getWorlds() == null) return;//already shutdown if (getWorlds() == null) {
return;//already shutdown
}
for (World w : getWorlds()) { for (World w : getWorlds()) {
w.shutdown(); w.shutdown();
} }
@@ -1923,7 +1901,9 @@ public class Server {
List<Channel> allChannels = getAllChannels(); List<Channel> allChannels = getAllChannels();
if(YamlConfig.config.server.USE_THREAD_TRACKER) ThreadTracker.getInstance().cancelThreadTrackerTask(); if (YamlConfig.config.server.USE_THREAD_TRACKER) {
ThreadTracker.getInstance().cancelThreadTrackerTask();
}
for (Channel ch : allChannels) { for (Channel ch : allChannels) {
while (!ch.finishedShutdown()) { while (!ch.finishedShutdown()) {