Commit 5d80c1cb authored by Kenix Whisperwind's avatar Kenix Whisperwind

ClanManager is now a non-static class member of World.

parent 8cfb9c91
......@@ -2,7 +2,6 @@ package com.openrsc.server;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.openrsc.server.constants.Constants;
import com.openrsc.server.content.clan.ClanManager;
import com.openrsc.server.event.DelayedEvent;
import com.openrsc.server.event.SingleEvent;
import com.openrsc.server.event.custom.MonitoringEvent;
......@@ -334,7 +333,7 @@ public final class Server implements Runnable {
}
private void saveAndShutdown() {
ClanManager.saveClans();
getWorld().getClanManager().saveClans();
for (Player p : World.getWorld().getPlayers()) {
p.unregister(true, "Server shutting down.");
}
......@@ -371,7 +370,7 @@ public final class Server implements Runnable {
}
private void saveAndRestart() {
ClanManager.saveClans();
getWorld().getClanManager().saveClans();
LOGGER.info("Saving players...");
for (Player p : World.getWorld().getPlayers()) {
p.unregister(true, "Server shutting down.");
......
......@@ -2,6 +2,7 @@ package com.openrsc.server.content.clan;
import com.openrsc.server.constants.Constants;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import com.openrsc.server.net.rsc.ActionSender;
import com.openrsc.server.util.rsc.MessageType;
......@@ -17,6 +18,12 @@ public class Clan {
private int[] clanSetting = new int[3];
private int clanPoints;
private final World world;
public Clan (World world) {
this.world = world;
}
public ClanPlayer addPlayer(Player player) {
if (getPlayers().size() < Constants.MAX_CLAN_SIZE) {
player.setClan(this);
......@@ -36,7 +43,7 @@ public class Clan {
ActionSender.sendClanSetting(clanMember.getPlayerReference());
if (getPlayers().size() > 1) {
ClanManager.saveClanChanges(this);
getWorld().getClanManager().saveClanChanges(this);
}
return clanMember;
}
......@@ -65,9 +72,9 @@ public class Clan {
messageClanInfo("@red@Your clan leader has left the clan!");
messageClanInfo("@yel@" + getLeader().getUsername() + " is the new clan leader!");
}
ClanManager.saveClanChanges(this);
getWorld().getClanManager().saveClanChanges(this);
} else if (getPlayers().size() == 0) {
ClanManager.deleteClan(this);
getWorld().getClanManager().deleteClan(this);
}
updateClanGUI();
}
......@@ -90,7 +97,7 @@ public class Clan {
getLeader().setRank(ClanRank.LEADER);
messageClanInfo("@red@Your clan leader has passed the leadership!");
messageClanInfo("@yel@" + getLeader().getUsername() + " is the new clan leader!");
ClanManager.saveClanChanges(this);
getWorld().getClanManager().saveClanChanges(this);
ActionSender.sendClanSetting(player);
} else {
if (newRank == 2) {
......@@ -99,7 +106,7 @@ public class Clan {
messageClanInfo(member.getUsername() + " has been put back to " + ClanRank.getRankFor(newRank).name().toLowerCase() + " rank.");
}
member.setRank(setRank);
ClanManager.updateClanRankPlayer(member);
getWorld().getClanManager().updateClanRankPlayer(member);
}
updateClanGUI();
if (member.isOnline()) {
......@@ -235,4 +242,8 @@ public class Clan {
public void setClanPoints(int p) {
this.clanPoints = p;
}
public World getWorld() {
return world;
}
}
......@@ -2,6 +2,7 @@ package com.openrsc.server.content.clan;
import com.openrsc.server.Server;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -14,6 +15,20 @@ import java.util.Comparator;
public class ClanManager {
public World getWorld() {
return world;
}
private static class ClanRankComparator implements Comparator<Clan> {
public int compare(Clan o1, Clan o2) {
if (o1.getClanPoints() == o2.getClanPoints()) {
return o1.getClanName().compareTo(o2.getClanName());
}
return o1.getClanPoints() > o2.getClanPoints() ? -1 : 1;
}
}
public final static ClanRankComparator CLAN_COMPERATOR = new ClanRankComparator();
/**
* The asynchronous logger.
......@@ -21,7 +36,13 @@ public class ClanManager {
private static final Logger LOGGER = LogManager.getLogger();
public static ArrayList<Clan> clans = new ArrayList<>();
public static void createClan(Clan clan) {
private final World world;
public ClanManager (World world) {
this.world = world;
}
public void createClan(Clan clan) {
try {
clans.add(clan);
databaseCreateClan(clan);
......@@ -30,7 +51,7 @@ public class ClanManager {
}
}
static void deleteClan(Clan clan) {
public void deleteClan(Clan clan) {
try {
databaseDeleteClan(clan);
clans.remove(clan);
......@@ -39,7 +60,7 @@ public class ClanManager {
}
}
public static void init() {
public void initialize() {
try {
LOGGER.info("Loading Clans...");
loadClans();
......@@ -49,7 +70,7 @@ public class ClanManager {
}
}
public static Clan getClan(String exist) {
public Clan getClan(String exist) {
for (Clan t : clans) {
if (t.getClanName().equalsIgnoreCase(exist))
return t;
......@@ -59,7 +80,7 @@ public class ClanManager {
return null;
}
public static void checkAndAttachToClan(Player player) {
public void checkAndAttachToClan(Player player) {
for (Clan p : clans) {
ClanPlayer clanMember = p.getPlayer(player.getUsername());
if (clanMember != null) {
......@@ -72,11 +93,37 @@ public class ClanManager {
}
}
private static void loadClans() throws SQLException {
public void checkAndUnattachFromClan(Player player) {
for (Clan p : clans) {
ClanPlayer cp = p.getPlayer(player.getUsername());
if (cp != null) {
cp.setPlayerReference(null);
p.updateClanGUI();
break;
}
}
}
public void saveClans() {
for (Clan t : clans) {
saveClanChanges(t);
}
}
public void saveClanChanges(Clan clan) {
updateClan(clan);
deleteClanPlayer(clan);
saveClanPlayer(clan);
//saveBank(team);
}
private void loadClans() throws SQLException {
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement("SELECT `id`, `name`, `tag`, `kick_setting`, `invite_setting`, `allow_search_join`, `clan_points` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan`");
ResultSet result = statement.executeQuery();
while (result.next()) {
Clan clan = new Clan();
Clan clan = new Clan(getWorld());
clan.setClanID(result.getInt("id"));
clan.setClanName(result.getString("name"));
clan.setClanTag(result.getString("tag"));
......@@ -111,7 +158,7 @@ public class ClanManager {
}
}
private static void databaseCreateClan(Clan clan) throws SQLException {
private void databaseCreateClan(Clan clan) throws SQLException {
PreparedStatement statement = Server.getServer().getDatabaseConnection().getConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan`(`name`, `tag`, `leader`) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, clan.getClanName());
......@@ -137,7 +184,7 @@ public class ClanManager {
statement.executeBatch();
}
private static void databaseDeleteClan(Clan clan) throws SQLException {
private void databaseDeleteClan(Clan clan) throws SQLException {
PreparedStatement deleteClan = Server.getServer().getDatabaseConnection().prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan` WHERE `id`=?");
PreparedStatement deleteClanPlayers = Server.getServer().getDatabaseConnection()
.prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` WHERE `clan_id`=?");
......@@ -148,7 +195,7 @@ public class ClanManager {
deleteClanPlayers.executeUpdate();
}
private static void saveClanPlayer(Clan clan) {
private void saveClanPlayer(Clan clan) {
try {
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players`(`clan_id`, `username`, `rank`, `kills`, `deaths`) VALUES (?,?,?,?,?)");
......@@ -167,7 +214,7 @@ public class ClanManager {
}
}
private static void deleteClanPlayer(Clan clan) {
private void deleteClanPlayer(Clan clan) {
try {
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` WHERE `clan_id`=?");
......@@ -179,7 +226,7 @@ public class ClanManager {
}
}
private static void updateClan(Clan clan) {
private void updateClan(Clan clan) {
try {
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("UPDATE `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan` SET `name`=?, `tag`=?, `leader`=?, `kick_setting`=?, `invite_setting`=?, `allow_search_join`=?, `clan_points`=? WHERE `id`=?");
......@@ -200,7 +247,7 @@ public class ClanManager {
}
}
static void updateClanRankPlayer(ClanPlayer cp) {
public void updateClanRankPlayer(ClanPlayer cp) {
try {
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("UPDATE `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` SET `rank`=? WHERE `username`=?");
......@@ -214,38 +261,4 @@ public class ClanManager {
}
public static void checkAndUnattachFromClan(Player player) {
for (Clan p : clans) {
ClanPlayer cp = p.getPlayer(player.getUsername());
if (cp != null) {
cp.setPlayerReference(null);
p.updateClanGUI();
break;
}
}
}
public static void saveClans() {
for (Clan t : clans) {
saveClanChanges(t);
}
}
static void saveClanChanges(Clan clan) {
updateClan(clan);
deleteClanPlayer(clan);
saveClanPlayer(clan);
//saveBank(team);
}
private static class ClanRankComparator implements Comparator<Clan> {
public int compare(Clan o1, Clan o2) {
if (o1.getClanPoints() == o2.getClanPoints()) {
return o1.getClanName().compareTo(o2.getClanName());
}
return o1.getClanPoints() > o2.getClanPoints() ? -1 : 1;
}
}
}
......@@ -120,7 +120,7 @@ public class PartyManager {
return world;
}
/*private static void loadParties() throws SQLException {
/*private void loadParties() throws SQLException {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT `id`, `name`, `tag`, `kick_setting`, `invite_setting`, `allow_search_join`, `party_points` FROM `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party`");
ResultSet result = statement.executeQuery();
while (result.next()) {
......@@ -159,7 +159,7 @@ public class PartyManager {
}
}*/
/*private static void databaseCreateParty(Party party) throws SQLException {
/*private void databaseCreateParty(Party party) throws SQLException {
PreparedStatement statement = DatabaseConnection.getDatabase().getConnection().prepareStatement(
"INSERT INTO `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party`(`name`, `tag`, `leader`) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, party.getPartyName());
......@@ -185,7 +185,7 @@ public class PartyManager {
statement.executeBatch();
}*/
/*private static void databaseDeleteParty(Party party) throws SQLException {
/*private void databaseDeleteParty(Party party) throws SQLException {
PreparedStatement deleteParty = DatabaseConnection.getDatabase().prepareStatement("DELETE FROM `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party` WHERE `id`=?");
PreparedStatement deletePartyPlayers = DatabaseConnection.getDatabase()
.prepareStatement("DELETE FROM `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party_players` WHERE `party_id`=?");
......@@ -196,7 +196,7 @@ public class PartyManager {
deletePartyPlayers.executeUpdate();
}*/
/*private static void savePartyPlayer(Party party) {
/*private void savePartyPlayer(Party party) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
"INSERT INTO `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party_players`(`party_id`, `username`, `rank`, `kills`, `deaths`) VALUES (?,?,?,?,?)");
......@@ -215,7 +215,7 @@ public class PartyManager {
}
}*/
/*private static void deletePartyPlayer(Party party) {
/*private void deletePartyPlayer(Party party) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
.prepareStatement("DELETE FROM `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party_players` WHERE `party_id`=?");
......@@ -227,7 +227,7 @@ public class PartyManager {
}
}*/
/*private static void updateParty(Party party) {
/*private void updateParty(Party party) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
.prepareStatement("UPDATE `" + getServer().getConfig().MYSQL_TABLE_PREFIX + "party` SET `name`=?, `tag`=?, `leader`=?, `kick_setting`=?, `invite_setting`=?, `allow_search_join`=?, `party_points`=? WHERE `id`=?");
......
......@@ -6,7 +6,6 @@ import com.openrsc.server.content.achievement.Achievement;
import com.openrsc.server.content.achievement.AchievementSystem;
import com.openrsc.server.content.clan.Clan;
import com.openrsc.server.content.clan.ClanInvite;
import com.openrsc.server.content.clan.ClanManager;
import com.openrsc.server.content.minigame.fishingtrawler.FishingTrawler;
import com.openrsc.server.content.party.Party;
import com.openrsc.server.content.party.PartyInvite;
......@@ -2014,7 +2013,7 @@ public final class Player extends Mob {
other.getSocial().alertOfLogout(this);
}
ClanManager.checkAndUnattachFromClan(this);
getWorld().getClanManager().checkAndUnattachFromClan(this);
getWorld().getPartyManager().checkAndUnattachFromParty(this);
Server.getServer().getPlayerDataProcessor().addRemoveRequest(this);
......
......@@ -83,6 +83,7 @@ public final class World implements SimpleSubscriber<FishingTrawler> {
private final Map<TrawlerBoat, FishingTrawler> fishingTrawler;
private final TileValue[][] tiles;
private final PartyManager partyManager;
private final ClanManager clanManager;
private final Market market;
private final WorldLoader worldLoader;
......@@ -116,6 +117,7 @@ public final class World implements SimpleSubscriber<FishingTrawler> {
tiles = new TileValue[MAX_WIDTH][MAX_HEIGHT];
worldLoader = new WorldLoader(this);
regionManager = new RegionManager(this);
clanManager = new ClanManager(this);
partyManager = new PartyManager(this);
market = getServer().getConfig().SPAWN_AUCTION_NPCS ? new Market(this) : null;
}
......@@ -404,8 +406,8 @@ public final class World implements SimpleSubscriber<FishingTrawler> {
public void load() {
try {
ClanManager.init();
getWorld().getPartyManager().initialize();
getClanManager().initialize();
getPartyManager().initialize();
worldInstance.getWorldLoader().loadWorld();
worldInstance.getWorldLoader().getWorldPopulator().populateWorld();
......@@ -579,8 +581,8 @@ public final class World implements SimpleSubscriber<FishingTrawler> {
for (Player other : getPlayers()) {
other.getSocial().alertOfLogin(player);
}
ClanManager.checkAndAttachToClan(player);
getWorld().getPartyManager().checkAndAttachToParty(player);
getClanManager().checkAndAttachToClan(player);
getPartyManager().checkAndAttachToParty(player);
LOGGER.info("Registered " + player.getUsername() + " to server");
return true;
}
......@@ -958,4 +960,8 @@ public final class World implements SimpleSubscriber<FishingTrawler> {
public PartyManager getPartyManager() {
return partyManager;
}
public ClanManager getClanManager() {
return clanManager;
}
}
......@@ -281,8 +281,8 @@ public class InterfaceOptionHandler implements PacketHandler {
return;
}
if (ClanManager.getClan(clanName) == null && ClanManager.getClan(clanTag) == null) {
Clan clan = new Clan();
if (player.getWorld().getClanManager().getClan(clanName) == null && player.getWorld().getClanManager().getClan(clanTag) == null) {
Clan clan = new Clan(player.getWorld());
clan.setClanName(clanName);
clan.setClanTag(clanTag);
......@@ -290,7 +290,7 @@ public class InterfaceOptionHandler implements PacketHandler {
clanMember.setRank(ClanRank.LEADER);
clan.setLeader(clanMember);
ClanManager.createClan(clan);
player.getWorld().getClanManager().createClan(clan);
player.message("You have created clan: " + clanName);
} else {
ActionSender.sendBox(player, "There is already a clan with this Clan Name or Clan Tag", false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment