refactor: use try-with-resources for alliance db operations
This commit is contained in:
@@ -21,13 +21,6 @@
|
||||
*/
|
||||
package net.server.guild;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import client.MapleCharacter;
|
||||
import client.MapleClient;
|
||||
import net.server.Server;
|
||||
@@ -39,8 +32,14 @@ import net.server.world.MaplePartyCharacter;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.MaplePacketCreator;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author XoticStory
|
||||
* @author Ronan
|
||||
*/
|
||||
@@ -51,7 +50,7 @@ public class MapleAlliance {
|
||||
private int capacity;
|
||||
private String name;
|
||||
private String notice = "";
|
||||
private String rankTitles[] = new String[5];
|
||||
private String[] rankTitles = new String[5];
|
||||
|
||||
public MapleAlliance(String name, int id) {
|
||||
this.name = name;
|
||||
@@ -66,20 +65,17 @@ public class MapleAlliance {
|
||||
if (name.contains(" ") || name.length() > 12) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
ResultSet rs;
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT name FROM alliance WHERE name = ?")) {
|
||||
ps.setString(1, name);
|
||||
rs = ps.executeQuery();
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT name FROM alliance WHERE name = ?")) {
|
||||
ps.setString(1, name);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
ps.close();
|
||||
rs.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
con.close();
|
||||
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -90,7 +86,7 @@ public class MapleAlliance {
|
||||
private static List<MapleCharacter> getPartyGuildMasters(MapleParty party) {
|
||||
List<MapleCharacter> mcl = new LinkedList<>();
|
||||
|
||||
for(MaplePartyCharacter mpc: party.getMembers()) {
|
||||
for (MaplePartyCharacter mpc : party.getMembers()) {
|
||||
MapleCharacter chr = mpc.getPlayer();
|
||||
if (chr != null) {
|
||||
MapleCharacter lchr = party.getLeader().getPlayer();
|
||||
@@ -100,9 +96,9 @@ public class MapleAlliance {
|
||||
}
|
||||
}
|
||||
|
||||
if(!mcl.isEmpty() && !mcl.get(0).isPartyLeader()) {
|
||||
for(int i = 1; i < mcl.size(); i++) {
|
||||
if(mcl.get(i).isPartyLeader()) {
|
||||
if (!mcl.isEmpty() && !mcl.get(0).isPartyLeader()) {
|
||||
for (int i = 1; i < mcl.size(); i++) {
|
||||
if (mcl.get(i).isPartyLeader()) {
|
||||
MapleCharacter temp = mcl.get(0);
|
||||
mcl.set(0, mcl.get(i));
|
||||
mcl.set(i, temp);
|
||||
@@ -115,19 +111,24 @@ public class MapleAlliance {
|
||||
|
||||
public static MapleAlliance createAlliance(MapleParty party, String name) {
|
||||
List<MapleCharacter> guildMasters = getPartyGuildMasters(party);
|
||||
if(guildMasters.size() != 2) return null;
|
||||
if (guildMasters.size() != 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Integer> guilds = new LinkedList<>();
|
||||
for(MapleCharacter mc: guildMasters) guilds.add(mc.getGuildId());
|
||||
for (MapleCharacter mc : guildMasters) {
|
||||
guilds.add(mc.getGuildId());
|
||||
}
|
||||
MapleAlliance alliance = MapleAlliance.createAllianceOnDb(guilds, name);
|
||||
if(alliance != null) {
|
||||
if (alliance != null) {
|
||||
alliance.setCapacity(guilds.size());
|
||||
for(Integer g: guilds)
|
||||
for (Integer g : guilds) {
|
||||
alliance.addGuild(g);
|
||||
}
|
||||
|
||||
int id = alliance.getId();
|
||||
try {
|
||||
for(int i = 0; i < guildMasters.size(); i++) {
|
||||
for (int i = 0; i < guildMasters.size(); i++) {
|
||||
Server.getInstance().setGuildAllianceId(guilds.get(i), id);
|
||||
Server.getInstance().resetAllianceGuildPlayersRank(guilds.get(i));
|
||||
|
||||
@@ -155,35 +156,29 @@ public class MapleAlliance {
|
||||
// will create an alliance, where the first guild listed is the leader and the alliance name MUST BE already checked for unicity.
|
||||
|
||||
int id = -1;
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO `alliance` (`name`) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS);
|
||||
|
||||
ps.setString(1, name);
|
||||
ps.executeUpdate();
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
rs.next();
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
|
||||
for(int i = 0; i < guilds.size(); i++) {
|
||||
int guild = guilds.get(i);
|
||||
|
||||
ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)");
|
||||
ps.setInt(1, id);
|
||||
ps.setInt(2, guild);
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `alliance` (`name`) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||
ps.setString(1, name);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
rs.next();
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
}
|
||||
|
||||
ps.close();
|
||||
con.close();
|
||||
for (int guild : guilds) {
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)")) {
|
||||
ps.setInt(1, id);
|
||||
ps.setInt(2, guild);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return (new MapleAlliance(name, id));
|
||||
return new MapleAlliance(name, id);
|
||||
}
|
||||
|
||||
public static MapleAlliance loadAlliance(int id) {
|
||||
@@ -191,148 +186,107 @@ public class MapleAlliance {
|
||||
return null;
|
||||
}
|
||||
MapleAlliance alliance = new MapleAlliance(null, -1);
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM alliance WHERE id = ?");
|
||||
ps.setInt(1, id);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (!rs.next()) {
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
return null;
|
||||
}
|
||||
alliance.allianceId = id;
|
||||
alliance.capacity = rs.getInt("capacity");
|
||||
alliance.name = rs.getString("name");
|
||||
alliance.notice = rs.getString("notice");
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
|
||||
String ranks[] = new String[5];
|
||||
ranks[0] = rs.getString("rank1");
|
||||
ranks[1] = rs.getString("rank2");
|
||||
ranks[2] = rs.getString("rank3");
|
||||
ranks[3] = rs.getString("rank4");
|
||||
ranks[4] = rs.getString("rank5");
|
||||
alliance.rankTitles = ranks;
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM alliance WHERE id = ?")) {
|
||||
ps.setInt(1, id);
|
||||
|
||||
ps.close();
|
||||
rs.close();
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ps = con.prepareStatement("SELECT guildid FROM allianceguilds WHERE allianceid = ?");
|
||||
ps.setInt(1, id);
|
||||
rs = ps.executeQuery();
|
||||
alliance.allianceId = id;
|
||||
alliance.capacity = rs.getInt("capacity");
|
||||
alliance.name = rs.getString("name");
|
||||
alliance.notice = rs.getString("notice");
|
||||
|
||||
while(rs.next()) {
|
||||
alliance.addGuild(rs.getInt("guildid"));
|
||||
String[] ranks = new String[5];
|
||||
ranks[0] = rs.getString("rank1");
|
||||
ranks[1] = rs.getString("rank2");
|
||||
ranks[2] = rs.getString("rank3");
|
||||
ranks[3] = rs.getString("rank4");
|
||||
ranks[4] = rs.getString("rank5");
|
||||
alliance.rankTitles = ranks;
|
||||
}
|
||||
}
|
||||
|
||||
ps.close();
|
||||
rs.close();
|
||||
con.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT guildid FROM allianceguilds WHERE allianceid = ?")) {
|
||||
ps.setInt(1, id);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
alliance.addGuild(rs.getInt("guildid"));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return alliance;
|
||||
}
|
||||
|
||||
public void saveToDB() {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE `alliance` SET capacity = ?, notice = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ? WHERE id = ?");
|
||||
ps.setInt(1, this.capacity);
|
||||
ps.setString(2, this.notice);
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
|
||||
ps.setString(3, this.rankTitles[0]);
|
||||
ps.setString(4, this.rankTitles[1]);
|
||||
ps.setString(5, this.rankTitles[2]);
|
||||
ps.setString(6, this.rankTitles[3]);
|
||||
ps.setString(7, this.rankTitles[4]);
|
||||
try (PreparedStatement ps = con.prepareStatement("UPDATE `alliance` SET capacity = ?, notice = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ? WHERE id = ?")) {
|
||||
ps.setInt(1, this.capacity);
|
||||
ps.setString(2, this.notice);
|
||||
|
||||
ps.setInt(8, this.allianceId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
ps.setString(3, this.rankTitles[0]);
|
||||
ps.setString(4, this.rankTitles[1]);
|
||||
ps.setString(5, this.rankTitles[2]);
|
||||
ps.setString(6, this.rankTitles[3]);
|
||||
ps.setString(7, this.rankTitles[4]);
|
||||
|
||||
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?");
|
||||
ps.setInt(1, this.allianceId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
for(int i = 0; i < guilds.size(); i++) {
|
||||
int guild = guilds.get(i);
|
||||
|
||||
ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)");
|
||||
ps.setInt(1, this.allianceId);
|
||||
ps.setInt(2, guild);
|
||||
ps.setInt(8, this.allianceId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
}
|
||||
|
||||
con.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?")) {
|
||||
ps.setInt(1, this.allianceId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
|
||||
for (int guild : guilds) {
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)")) {
|
||||
ps.setInt(1, this.allianceId);
|
||||
ps.setInt(2, guild);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void disbandAlliance(int allianceId) {
|
||||
PreparedStatement ps = null;
|
||||
Connection con = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
|
||||
ps = con.prepareStatement("DELETE FROM `alliance` WHERE id = ?");
|
||||
ps.setInt(1, allianceId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `alliance` WHERE id = ?")) {
|
||||
ps.setInt(1, allianceId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
|
||||
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?");
|
||||
ps.setInt(1, allianceId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?")) {
|
||||
ps.setInt(1, allianceId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
|
||||
con.close();
|
||||
Server.getInstance().allianceMessage(allianceId, MaplePacketCreator.disbandAlliance(allianceId), -1, -1);
|
||||
Server.getInstance().disbandAlliance(allianceId);
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null && !ps.isClosed()) {
|
||||
ps.close();
|
||||
}
|
||||
if (con != null && !con.isClosed()) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void removeGuildFromAllianceOnDb(int guildId) {
|
||||
PreparedStatement ps = null;
|
||||
Connection con = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
|
||||
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE guildid = ?");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE guildid = ?")) {
|
||||
ps.setInt(1, guildId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
con.close();
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null && !ps.isClosed()) {
|
||||
ps.close();
|
||||
}
|
||||
if (con != null && !con.isClosed()) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +320,9 @@ public class MapleAlliance {
|
||||
public boolean removeGuild(int gid) {
|
||||
synchronized (guilds) {
|
||||
int index = getGuildIndex(gid);
|
||||
if(index == -1) return false;
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
guilds.remove(index);
|
||||
return true;
|
||||
@@ -375,7 +331,9 @@ public class MapleAlliance {
|
||||
|
||||
public boolean addGuild(int gid) {
|
||||
synchronized (guilds) {
|
||||
if(guilds.size() == capacity || getGuildIndex(gid) > -1) return false;
|
||||
if (guilds.size() == capacity || getGuildIndex(gid) > -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
guilds.add(gid);
|
||||
return true;
|
||||
@@ -402,7 +360,7 @@ public class MapleAlliance {
|
||||
}
|
||||
|
||||
public List<Integer> getGuilds() {
|
||||
synchronized(guilds) {
|
||||
synchronized (guilds) {
|
||||
List<Integer> guilds_ = new LinkedList<>();
|
||||
for (int guild : guilds) {
|
||||
if (guild != -1) {
|
||||
@@ -446,12 +404,14 @@ public class MapleAlliance {
|
||||
}
|
||||
|
||||
public MapleGuildCharacter getLeader() {
|
||||
synchronized(guilds) {
|
||||
for(Integer gId: guilds) {
|
||||
synchronized (guilds) {
|
||||
for (Integer gId : guilds) {
|
||||
MapleGuild guild = Server.getInstance().getGuild(gId);
|
||||
MapleGuildCharacter mgc = guild.getMGC(guild.getLeaderId());
|
||||
|
||||
if(mgc.getAllianceRank() == 1) return mgc;
|
||||
if (mgc.getAllianceRank() == 1) {
|
||||
return mgc;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -463,8 +423,8 @@ public class MapleAlliance {
|
||||
}
|
||||
|
||||
public void dropMessage(int type, String message) {
|
||||
synchronized(guilds) {
|
||||
for(Integer gId: guilds) {
|
||||
synchronized (guilds) {
|
||||
for (Integer gId : guilds) {
|
||||
MapleGuild guild = Server.getInstance().getGuild(gId);
|
||||
guild.dropMessage(type, message);
|
||||
}
|
||||
@@ -477,7 +437,7 @@ public class MapleAlliance {
|
||||
|
||||
public static void sendInvitation(MapleClient c, String targetGuildName, int allianceId) {
|
||||
MapleGuild mg = Server.getInstance().getGuildByName(targetGuildName);
|
||||
if(mg == null) {
|
||||
if (mg == null) {
|
||||
c.getPlayer().dropMessage(5, "The entered guild does not exist.");
|
||||
} else {
|
||||
if (mg.getAllianceId() > 0) {
|
||||
|
||||
Reference in New Issue
Block a user