refactor: use try-with-resources for guild db operations
This commit is contained in:
@@ -24,44 +24,35 @@ package net.server.guild;
|
|||||||
import client.MapleCharacter;
|
import client.MapleCharacter;
|
||||||
import client.MapleClient;
|
import client.MapleClient;
|
||||||
import config.YamlConfig;
|
import config.YamlConfig;
|
||||||
|
import net.server.PlayerStorage;
|
||||||
|
import net.server.Server;
|
||||||
|
import net.server.audit.locks.MonitoredLockType;
|
||||||
|
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||||
|
import net.server.channel.Channel;
|
||||||
|
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
|
||||||
|
import net.server.coordinator.world.MapleInviteCoordinator;
|
||||||
|
import net.server.coordinator.world.MapleInviteCoordinator.InviteType;
|
||||||
|
import net.server.coordinator.world.MapleInviteCoordinator.MapleInviteResult;
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
import tools.MaplePacketCreator;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
|
||||||
|
|
||||||
import net.server.PlayerStorage;
|
|
||||||
import net.server.Server;
|
|
||||||
import net.server.channel.Channel;
|
|
||||||
import tools.DatabaseConnection;
|
|
||||||
import tools.MaplePacketCreator;
|
|
||||||
import net.server.audit.locks.MonitoredLockType;
|
|
||||||
import net.server.coordinator.world.MapleInviteCoordinator;
|
|
||||||
import net.server.coordinator.world.MapleInviteCoordinator.InviteType;
|
|
||||||
import net.server.coordinator.world.MapleInviteCoordinator.MapleInviteResult;
|
|
||||||
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
|
|
||||||
|
|
||||||
public class MapleGuild {
|
public class MapleGuild {
|
||||||
|
|
||||||
private enum BCOp {
|
private enum BCOp {
|
||||||
NONE, DISBAND, EMBLEMCHANGE
|
NONE, DISBAND, EMBLEMCHANGE
|
||||||
}
|
}
|
||||||
|
|
||||||
private final List<MapleGuildCharacter> members;
|
private final List<MapleGuildCharacter> members;
|
||||||
private final Lock membersLock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.GUILD, true);
|
private final Lock membersLock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.GUILD, true);
|
||||||
|
|
||||||
private String rankTitles[] = new String[5]; // 1 = master, 2 = jr, 5 = lowest member
|
private String[] rankTitles = new String[5]; // 1 = master, 2 = jr, 5 = lowest member
|
||||||
private String name, notice;
|
private String name, notice;
|
||||||
private int id, gp, logo, logoColor, leader, capacity, logoBG, logoBGColor, signature, allianceId;
|
private int id, gp, logo, logoColor, leader, capacity, logoBG, logoBGColor, signature, allianceId;
|
||||||
private int world;
|
private int world;
|
||||||
@@ -71,49 +62,46 @@ public class MapleGuild {
|
|||||||
public MapleGuild(int guildid, int world) {
|
public MapleGuild(int guildid, int world) {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
members = new ArrayList<>();
|
members = new ArrayList<>();
|
||||||
Connection con = null;
|
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con = DatabaseConnection.getConnection();
|
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM guilds WHERE guildid = " + guildid);
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM guilds WHERE guildid = " + guildid);
|
ResultSet rs = ps.executeQuery()) {
|
||||||
ResultSet rs = ps.executeQuery();
|
if (!rs.next()) {
|
||||||
if (!rs.next()) {
|
id = -1;
|
||||||
id = -1;
|
ps.close();
|
||||||
ps.close();
|
rs.close();
|
||||||
rs.close();
|
return;
|
||||||
return;
|
}
|
||||||
|
id = guildid;
|
||||||
|
name = rs.getString("name");
|
||||||
|
gp = rs.getInt("GP");
|
||||||
|
logo = rs.getInt("logo");
|
||||||
|
logoColor = rs.getInt("logoColor");
|
||||||
|
logoBG = rs.getInt("logoBG");
|
||||||
|
logoBGColor = rs.getInt("logoBGColor");
|
||||||
|
capacity = rs.getInt("capacity");
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
rankTitles[i - 1] = rs.getString("rank" + i + "title");
|
||||||
|
}
|
||||||
|
leader = rs.getInt("leader");
|
||||||
|
notice = rs.getString("notice");
|
||||||
|
signature = rs.getInt("signature");
|
||||||
|
allianceId = rs.getInt("allianceId");
|
||||||
}
|
}
|
||||||
id = guildid;
|
|
||||||
name = rs.getString("name");
|
try (PreparedStatement ps = con.prepareStatement("SELECT id, name, level, job, guildrank, allianceRank FROM characters WHERE guildid = ? ORDER BY guildrank ASC, name ASC")) {
|
||||||
gp = rs.getInt("GP");
|
ps.setInt(1, guildid);
|
||||||
logo = rs.getInt("logo");
|
|
||||||
logoColor = rs.getInt("logoColor");
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
logoBG = rs.getInt("logoBG");
|
if (!rs.next()) {
|
||||||
logoBGColor = rs.getInt("logoBGColor");
|
return;
|
||||||
capacity = rs.getInt("capacity");
|
}
|
||||||
for (int i = 1; i <= 5; i++) {
|
|
||||||
rankTitles[i - 1] = rs.getString("rank" + i + "title");
|
do {
|
||||||
|
members.add(new MapleGuildCharacter(null, rs.getInt("id"), rs.getInt("level"), rs.getString("name"), (byte) -1, world, rs.getInt("job"), rs.getInt("guildrank"), guildid, false, rs.getInt("allianceRank")));
|
||||||
|
} while (rs.next());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
leader = rs.getInt("leader");
|
|
||||||
notice = rs.getString("notice");
|
|
||||||
signature = rs.getInt("signature");
|
|
||||||
allianceId = rs.getInt("allianceId");
|
|
||||||
ps.close();
|
|
||||||
rs.close();
|
|
||||||
ps = con.prepareStatement("SELECT id, name, level, job, guildrank, allianceRank FROM characters WHERE guildid = ? ORDER BY guildrank ASC, name ASC");
|
|
||||||
ps.setInt(1, guildid);
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
if (!rs.next()) {
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
members.add(new MapleGuildCharacter(null, rs.getInt("id"), rs.getInt("level"), rs.getString("name"), (byte) -1, world, rs.getInt("job"), rs.getInt("guildrank"), guildid, false, rs.getInt("allianceRank")));
|
|
||||||
} while (rs.next());
|
|
||||||
|
|
||||||
ps.close();
|
|
||||||
rs.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
System.out.println("Unable to read guild information from sql: " + se);
|
System.out.println("Unable to read guild information from sql: " + se);
|
||||||
@@ -137,32 +125,33 @@ public class MapleGuild {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
for (MapleGuildCharacter mgc : members) {
|
for (MapleGuildCharacter mgc : members) {
|
||||||
if (!mgc.isOnline()) {
|
if (!mgc.isOnline()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Integer> chl;
|
List<Integer> chl;
|
||||||
synchronized (notifications) {
|
synchronized (notifications) {
|
||||||
chl = notifications.get(mgc.getChannel());
|
chl = notifications.get(mgc.getChannel());
|
||||||
}
|
}
|
||||||
if (chl != null) chl.add(mgc.getId());
|
if (chl != null) {
|
||||||
|
chl.add(mgc.getId());
|
||||||
|
}
|
||||||
//Unable to connect to Channel... error was here
|
//Unable to connect to Channel... error was here
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
bDirty = false;
|
bDirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeToDB(boolean bDisband) {
|
public void writeToDB(boolean bDisband) {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
|
|
||||||
if (!bDisband) {
|
if (!bDisband) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("UPDATE guilds SET GP = ?, logo = ?, logoColor = ?, logoBG = ?, logoBGColor = ?, ");
|
builder.append("UPDATE guilds SET GP = ?, logo = ?, logoColor = ?, logoBG = ?, logoBGColor = ?, ");
|
||||||
@@ -182,18 +171,19 @@ public class MapleGuild {
|
|||||||
ps.setInt(11, capacity);
|
ps.setInt(11, capacity);
|
||||||
ps.setString(12, notice);
|
ps.setString(12, notice);
|
||||||
ps.setInt(13, this.id);
|
ps.setInt(13, this.id);
|
||||||
ps.execute();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PreparedStatement ps = con.prepareStatement("UPDATE characters SET guildid = 0, guildrank = 5 WHERE guildid = ?");
|
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET guildid = 0, guildrank = 5 WHERE guildid = ?")) {
|
||||||
ps.setInt(1, this.id);
|
ps.setInt(1, this.id);
|
||||||
ps.execute();
|
ps.executeUpdate();
|
||||||
ps.close();
|
}
|
||||||
ps = con.prepareStatement("DELETE FROM guilds WHERE guildid = ?");
|
|
||||||
ps.setInt(1, this.id);
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM guilds WHERE guildid = ?")) {
|
||||||
ps.execute();
|
ps.setInt(1, this.id);
|
||||||
ps.close();
|
ps.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(MaplePacketCreator.guildDisband(this.id));
|
this.broadcast(MaplePacketCreator.guildDisband(this.id));
|
||||||
@@ -201,8 +191,6 @@ public class MapleGuild {
|
|||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -215,7 +203,7 @@ public class MapleGuild {
|
|||||||
public int getLeaderId() {
|
public int getLeaderId() {
|
||||||
return leader;
|
return leader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int setLeaderId(int charId) {
|
public int setLeaderId(int charId) {
|
||||||
return leader = charId;
|
return leader = charId;
|
||||||
}
|
}
|
||||||
@@ -286,40 +274,46 @@ public class MapleGuild {
|
|||||||
|
|
||||||
public void broadcastNameChanged() {
|
public void broadcastNameChanged() {
|
||||||
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
||||||
|
|
||||||
for (MapleGuildCharacter mgc : getMembers()) {
|
for (MapleGuildCharacter mgc : getMembers()) {
|
||||||
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
||||||
if (chr == null || !chr.isLoggedinWorld()) continue;
|
if (chr == null || !chr.isLoggedinWorld()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
byte[] packet = MaplePacketCreator.guildNameChanged(chr.getId(), this.getName());
|
byte[] packet = MaplePacketCreator.guildNameChanged(chr.getId(), this.getName());
|
||||||
chr.getMap().broadcastMessage(chr, packet);
|
chr.getMap().broadcastMessage(chr, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastEmblemChanged() {
|
public void broadcastEmblemChanged() {
|
||||||
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
||||||
|
|
||||||
for (MapleGuildCharacter mgc : getMembers()) {
|
for (MapleGuildCharacter mgc : getMembers()) {
|
||||||
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
||||||
if (chr == null || !chr.isLoggedinWorld()) continue;
|
if (chr == null || !chr.isLoggedinWorld()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
byte[] packet = MaplePacketCreator.guildMarkChanged(chr.getId(), this);
|
byte[] packet = MaplePacketCreator.guildMarkChanged(chr.getId(), this);
|
||||||
chr.getMap().broadcastMessage(chr, packet);
|
chr.getMap().broadcastMessage(chr, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastInfoChanged() {
|
public void broadcastInfoChanged() {
|
||||||
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
|
||||||
|
|
||||||
for (MapleGuildCharacter mgc : getMembers()) {
|
for (MapleGuildCharacter mgc : getMembers()) {
|
||||||
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
MapleCharacter chr = ps.getCharacterById(mgc.getId());
|
||||||
if (chr == null || !chr.isLoggedinWorld()) continue;
|
if (chr == null || !chr.isLoggedinWorld()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
byte[] packet = MaplePacketCreator.showGuildInfo(chr);
|
byte[] packet = MaplePacketCreator.showGuildInfo(chr);
|
||||||
chr.announce(packet);
|
chr.announce(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcast(final byte[] packet) {
|
public void broadcast(final byte[] packet) {
|
||||||
broadcast(packet, -1, BCOp.NONE);
|
broadcast(packet, -1, BCOp.NONE);
|
||||||
}
|
}
|
||||||
@@ -372,16 +366,16 @@ public class MapleGuild {
|
|||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dropMessage(String message) {
|
public void dropMessage(String message) {
|
||||||
dropMessage(5, message);
|
dropMessage(5, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dropMessage(int type, String message) {
|
public void dropMessage(int type, String message) {
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
for (MapleGuildCharacter mgc : members) {
|
for (MapleGuildCharacter mgc : members) {
|
||||||
if(mgc.getCharacter() != null) {
|
if (mgc.getCharacter() != null) {
|
||||||
mgc.getCharacter().dropMessage(type, message);
|
mgc.getCharacter().dropMessage(type, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -389,7 +383,7 @@ public class MapleGuild {
|
|||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastMessage(byte[] packet) {
|
public void broadcastMessage(byte[] packet) {
|
||||||
Server.getInstance().guildMessage(id, packet);
|
Server.getInstance().guildMessage(id, packet);
|
||||||
}
|
}
|
||||||
@@ -431,41 +425,40 @@ public class MapleGuild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static int createGuild(int leaderId, String name) {
|
public static int createGuild(int leaderId, String name) {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT guildid FROM guilds WHERE name = ?");
|
try (PreparedStatement ps = con.prepareStatement("SELECT guildid FROM guilds WHERE name = ?")) {
|
||||||
ps.setString(1, name);
|
ps.setString(1, name);
|
||||||
ResultSet rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
ps.close();
|
return 0;
|
||||||
rs.close();
|
}
|
||||||
return 0;
|
}
|
||||||
}
|
}
|
||||||
ps.close();
|
|
||||||
rs.close();
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO guilds (`leader`, `name`, `signature`) VALUES (?, ?, ?)")) {
|
||||||
|
ps.setInt(1, leaderId);
|
||||||
ps = con.prepareStatement("INSERT INTO guilds (`leader`, `name`, `signature`) VALUES (?, ?, ?)");
|
ps.setString(2, name);
|
||||||
ps.setInt(1, leaderId);
|
ps.setInt(3, (int) System.currentTimeMillis());
|
||||||
ps.setString(2, name);
|
ps.executeUpdate();
|
||||||
ps.setInt(3, (int) System.currentTimeMillis());
|
}
|
||||||
ps.execute();
|
|
||||||
ps.close();
|
final int guildId;
|
||||||
|
try (PreparedStatement ps = con.prepareStatement("SELECT guildid FROM guilds WHERE leader = ?")) {
|
||||||
ps = con.prepareStatement("SELECT guildid FROM guilds WHERE leader = ?");
|
ps.setInt(1, leaderId);
|
||||||
ps.setInt(1, leaderId);
|
|
||||||
rs = ps.executeQuery();
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
rs.next();
|
rs.next();
|
||||||
int guildId = rs.getInt("guildid");
|
guildId = rs.getInt("guildid");
|
||||||
rs.close();
|
}
|
||||||
ps.close();
|
}
|
||||||
|
|
||||||
ps = con.prepareStatement("UPDATE characters SET guildid = ? WHERE id = ?");
|
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET guildid = ? WHERE id = ?")) {
|
||||||
ps.setInt(1, guildId);
|
ps.setInt(1, guildId);
|
||||||
ps.setInt(2, leaderId);
|
ps.setInt(2, leaderId);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
ps.close();
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
return guildId;
|
return guildId;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -487,7 +480,7 @@ public class MapleGuild {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.broadcast(MaplePacketCreator.newGuildMember(mgc));
|
this.broadcast(MaplePacketCreator.newGuildMember(mgc));
|
||||||
return 1;
|
return 1;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -530,7 +523,7 @@ public class MapleGuild {
|
|||||||
ps.setLong(4, System.currentTimeMillis());
|
ps.setLong(4, System.currentTimeMillis());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
con.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -564,7 +557,7 @@ public class MapleGuild {
|
|||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeRank(MapleGuildCharacter mgc, int newRank) {
|
public void changeRank(MapleGuildCharacter mgc, int newRank) {
|
||||||
try {
|
try {
|
||||||
if (mgc.isOnline()) {
|
if (mgc.isOnline()) {
|
||||||
@@ -578,7 +571,7 @@ public class MapleGuild {
|
|||||||
re.printStackTrace();
|
re.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(MaplePacketCreator.changeRank(mgc));
|
this.broadcast(MaplePacketCreator.changeRank(mgc));
|
||||||
@@ -586,11 +579,11 @@ public class MapleGuild {
|
|||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGuildNotice(String notice) {
|
public void setGuildNotice(String notice) {
|
||||||
this.notice = notice;
|
this.notice = notice;
|
||||||
this.writeToDB(false);
|
this.writeToDB(false);
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(MaplePacketCreator.guildNotice(this.id, notice));
|
this.broadcast(MaplePacketCreator.guildNotice(this.id, notice));
|
||||||
@@ -634,24 +627,24 @@ public class MapleGuild {
|
|||||||
|
|
||||||
public void changeRankTitle(String[] ranks) {
|
public void changeRankTitle(String[] ranks) {
|
||||||
System.arraycopy(ranks, 0, rankTitles, 0, 5);
|
System.arraycopy(ranks, 0, rankTitles, 0, 5);
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(MaplePacketCreator.rankTitleChange(this.id, ranks));
|
this.broadcast(MaplePacketCreator.rankTitleChange(this.id, ranks));
|
||||||
} finally {
|
} finally {
|
||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.writeToDB(false);
|
this.writeToDB(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disbandGuild() {
|
public void disbandGuild() {
|
||||||
if(allianceId > 0) {
|
if (allianceId > 0) {
|
||||||
if (!MapleAlliance.removeGuildFromAlliance(allianceId, id, world)) {
|
if (!MapleAlliance.removeGuildFromAlliance(allianceId, id, world)) {
|
||||||
MapleAlliance.disbandAlliance(allianceId);
|
MapleAlliance.disbandAlliance(allianceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.writeToDB(true);
|
this.writeToDB(true);
|
||||||
@@ -667,7 +660,7 @@ public class MapleGuild {
|
|||||||
this.logo = logo;
|
this.logo = logo;
|
||||||
this.logoColor = logocolor;
|
this.logoColor = logocolor;
|
||||||
this.writeToDB(false);
|
this.writeToDB(false);
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(null, -1, BCOp.EMBLEMCHANGE);
|
this.broadcast(null, -1, BCOp.EMBLEMCHANGE);
|
||||||
@@ -696,14 +689,14 @@ public class MapleGuild {
|
|||||||
}
|
}
|
||||||
capacity += 5;
|
capacity += 5;
|
||||||
this.writeToDB(false);
|
this.writeToDB(false);
|
||||||
|
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.broadcast(MaplePacketCreator.guildCapacityChange(this.id, this.capacity));
|
this.broadcast(MaplePacketCreator.guildCapacityChange(this.id, this.capacity));
|
||||||
} finally {
|
} finally {
|
||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -713,8 +706,8 @@ public class MapleGuild {
|
|||||||
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
|
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
|
||||||
this.guildMessage(MaplePacketCreator.getGPMessage(amount));
|
this.guildMessage(MaplePacketCreator.getGPMessage(amount));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeGP(int amount){
|
public void removeGP(int amount) {
|
||||||
this.gp -= amount;
|
this.gp -= amount;
|
||||||
this.writeToDB(false);
|
this.writeToDB(false);
|
||||||
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
|
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
|
||||||
@@ -728,7 +721,7 @@ public class MapleGuild {
|
|||||||
if (mc.getGuildId() > 0) {
|
if (mc.getGuildId() > 0) {
|
||||||
return MapleGuildResponse.ALREADY_IN_GUILD;
|
return MapleGuildResponse.ALREADY_IN_GUILD;
|
||||||
}
|
}
|
||||||
|
|
||||||
MapleCharacter sender = c.getPlayer();
|
MapleCharacter sender = c.getPlayer();
|
||||||
if (MapleInviteCoordinator.createInvite(InviteType.GUILD, sender, sender.getGuildId(), mc.getId())) {
|
if (MapleInviteCoordinator.createInvite(InviteType.GUILD, sender, sender.getGuildId(), mc.getId())) {
|
||||||
mc.getClient().announce(MaplePacketCreator.guildInvite(sender.getGuildId(), sender.getName()));
|
mc.getClient().announce(MaplePacketCreator.guildInvite(sender.getGuildId(), sender.getName()));
|
||||||
@@ -737,55 +730,49 @@ public class MapleGuild {
|
|||||||
return MapleGuildResponse.MANAGING_INVITE;
|
return MapleGuildResponse.MANAGING_INVITE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean answerInvitation(int targetId, String targetName, int guildId, boolean answer) {
|
public static boolean answerInvitation(int targetId, String targetName, int guildId, boolean answer) {
|
||||||
MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.GUILD, targetId, guildId, answer);
|
MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.GUILD, targetId, guildId, answer);
|
||||||
|
|
||||||
MapleGuildResponse mgr;
|
MapleGuildResponse mgr;
|
||||||
MapleCharacter sender = res.from;
|
MapleCharacter sender = res.from;
|
||||||
switch (res.result) {
|
switch (res.result) {
|
||||||
case ACCEPTED:
|
case ACCEPTED:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case DENIED:
|
case DENIED:
|
||||||
mgr = MapleGuildResponse.DENIED_INVITE;
|
mgr = MapleGuildResponse.DENIED_INVITE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
mgr = MapleGuildResponse.NOT_FOUND_INVITE;
|
mgr = MapleGuildResponse.NOT_FOUND_INVITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mgr != null && sender != null) {
|
if (mgr != null && sender != null) {
|
||||||
sender.announce(mgr.getPacket(targetName));
|
sender.announce(mgr.getPacket(targetName));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<MapleCharacter> getEligiblePlayersForGuild(MapleCharacter guildLeader) {
|
public static Set<MapleCharacter> getEligiblePlayersForGuild(MapleCharacter guildLeader) {
|
||||||
Set<MapleCharacter> guildMembers = new HashSet<>();
|
Set<MapleCharacter> guildMembers = new HashSet<>();
|
||||||
guildMembers.add(guildLeader);
|
guildMembers.add(guildLeader);
|
||||||
|
|
||||||
MapleMatchCheckerCoordinator mmce = guildLeader.getWorldServer().getMatchCheckerCoordinator();
|
MapleMatchCheckerCoordinator mmce = guildLeader.getWorldServer().getMatchCheckerCoordinator();
|
||||||
for (MapleCharacter chr : guildLeader.getMap().getAllPlayers()) {
|
for (MapleCharacter chr : guildLeader.getMap().getAllPlayers()) {
|
||||||
if (chr.getParty() == null && chr.getGuild() == null && mmce.getMatchConfirmationLeaderid(chr.getId()) == -1) {
|
if (chr.getParty() == null && chr.getGuild() == null && mmce.getMatchConfirmationLeaderid(chr.getId()) == -1) {
|
||||||
guildMembers.add(chr);
|
guildMembers.add(chr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return guildMembers;
|
return guildMembers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void displayGuildRanks(MapleClient c, int npcid) {
|
public static void displayGuildRanks(MapleClient c, int npcid) {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
ResultSet rs;
|
PreparedStatement ps = con.prepareStatement("SELECT `name`, `GP`, `logoBG`, `logoBGColor`, `logo`, `logoColor` FROM guilds ORDER BY `GP` DESC LIMIT 50", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||||
Connection con = DatabaseConnection.getConnection();
|
ResultSet rs = ps.executeQuery()) {
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT `name`, `GP`, `logoBG`, `logoBGColor`, `logo`, `logoColor` FROM guilds ORDER BY `GP` DESC LIMIT 50",
|
c.announce(MaplePacketCreator.showGuildRanks(npcid, rs));
|
||||||
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
c.announce(MaplePacketCreator.showGuildRanks(npcid, rs));
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println("failed to display guild ranks. " + e);
|
System.out.println("failed to display guild ranks. " + e);
|
||||||
@@ -798,41 +785,35 @@ public class MapleGuild {
|
|||||||
|
|
||||||
public void setAllianceId(int aid) {
|
public void setAllianceId(int aid) {
|
||||||
this.allianceId = aid;
|
this.allianceId = aid;
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
Connection con = DatabaseConnection.getConnection();
|
PreparedStatement ps = con.prepareStatement("UPDATE guilds SET allianceId = ? WHERE guildid = ?")) {
|
||||||
try (PreparedStatement ps = con.prepareStatement("UPDATE guilds SET allianceId = ? WHERE guildid = ?")) {
|
ps.setInt(1, aid);
|
||||||
ps.setInt(1, aid);
|
ps.setInt(2, id);
|
||||||
ps.setInt(2, id);
|
ps.executeUpdate();
|
||||||
ps.executeUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetAllianceGuildPlayersRank() {
|
public void resetAllianceGuildPlayersRank() {
|
||||||
try {
|
try {
|
||||||
membersLock.lock();
|
membersLock.lock();
|
||||||
try {
|
try {
|
||||||
for(MapleGuildCharacter mgc: members) {
|
for (MapleGuildCharacter mgc : members) {
|
||||||
if(mgc.isOnline()) {
|
if (mgc.isOnline()) {
|
||||||
mgc.setAllianceRank(5);
|
mgc.setAllianceRank(5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
membersLock.unlock();
|
membersLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET allianceRank = ? WHERE guildid = ?")) {
|
PreparedStatement ps = con.prepareStatement("UPDATE characters SET allianceRank = ? WHERE guildid = ?")) {
|
||||||
ps.setInt(1, 5);
|
ps.setInt(1, 5);
|
||||||
ps.setInt(2, id);
|
ps.setInt(2, id);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -840,7 +821,7 @@ public class MapleGuild {
|
|||||||
|
|
||||||
public static int getIncreaseGuildCost(int size) {
|
public static int getIncreaseGuildCost(int size) {
|
||||||
int cost = YamlConfig.config.server.EXPAND_GUILD_BASE_COST + Math.max(0, (size - 15) / 5) * YamlConfig.config.server.EXPAND_GUILD_TIER_COST;
|
int cost = YamlConfig.config.server.EXPAND_GUILD_BASE_COST + Math.max(0, (size - 15) / 5) * YamlConfig.config.server.EXPAND_GUILD_TIER_COST;
|
||||||
|
|
||||||
if (size > 30) {
|
if (size > 30) {
|
||||||
return Math.min(YamlConfig.config.server.EXPAND_GUILD_MAX_COST, Math.max(cost, 5000000));
|
return Math.min(YamlConfig.config.server.EXPAND_GUILD_MAX_COST, Math.max(cost, 5000000));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user