Rename and clean up MapleFamily

This commit is contained in:
P0nk
2021-09-09 21:16:56 +02:00
parent 56c9e0f664
commit 3370152d58
12 changed files with 80 additions and 71 deletions

View File

@@ -1154,7 +1154,7 @@ public class Character extends AbstractCharacterObject {
if (this.guildid > 0) { if (this.guildid > 0) {
getGuild().broadcast(PacketCreator.jobMessage(0, job.getId(), name), this.getId()); getGuild().broadcast(PacketCreator.jobMessage(0, job.getId(), name), this.getId());
} }
MapleFamily family = getFamily(); Family family = getFamily();
if (family != null) { if (family != null) {
family.broadcast(PacketCreator.jobMessage(1, job.getId(), name), this.getId()); family.broadcast(PacketCreator.jobMessage(1, job.getId(), name), this.getId());
} }
@@ -1183,7 +1183,7 @@ public class Character extends AbstractCharacterObject {
public void broadcastAcquaintances(Packet packet) { public void broadcastAcquaintances(Packet packet) {
buddylist.broadcast(packet, getWorldServer().getPlayerStorage()); buddylist.broadcast(packet, getWorldServer().getPlayerStorage());
MapleFamily family = getFamily(); Family family = getFamily();
if (family != null) { if (family != null) {
family.broadcast(packet, id); family.broadcast(packet, id);
} }
@@ -4955,7 +4955,7 @@ public class Character extends AbstractCharacterObject {
return fame; return fame;
} }
public MapleFamily getFamily() { public Family getFamily() {
if (familyEntry != null) { if (familyEntry != null) {
return familyEntry.getFamily(); return familyEntry.getFamily();
} else { } else {
@@ -10419,7 +10419,7 @@ public class Character extends AbstractCharacterObject {
guild.broadcast(PacketCreator.marriageMessage(0, name)); guild.broadcast(PacketCreator.marriageMessage(0, name));
} }
MapleFamily family = this.getFamily(); Family family = this.getFamily();
if (family != null) { if (family != null) {
family.broadcast(PacketCreator.marriageMessage(1, name)); family.broadcast(PacketCreator.marriageMessage(1, name));
} }

View File

@@ -983,7 +983,7 @@ public class Client extends ChannelInboundHandlerAdapter {
} }
/* /*
if (fid > 0) { if (fid > 0) {
final MapleFamily family = worlda.getFamily(fid); final Family family = worlda.getFamily(fid);
family. family.
} }
*/ */

View File

@@ -41,11 +41,10 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
*
* @author Jay Estrella - Mr.Trash :3 * @author Jay Estrella - Mr.Trash :3
* @author Ubaware * @author Ubaware
*/ */
public class MapleFamily { public class Family {
private static final AtomicInteger familyIDCounter = new AtomicInteger(); private static final AtomicInteger familyIDCounter = new AtomicInteger();
@@ -56,11 +55,11 @@ public class MapleFamily {
private String preceptsMessage = ""; private String preceptsMessage = "";
private int totalGenerations; private int totalGenerations;
public MapleFamily(int id, int world) { public Family(int id, int world) {
int newId = id; int newId = id;
if(id == -1) { if (id == -1) {
// get next available family id // get next available family id
while(idInUse(newId = familyIDCounter.incrementAndGet())) { while (idInUse(newId = familyIDCounter.incrementAndGet())) {
} }
} }
this.id = newId; this.id = newId;
@@ -68,8 +67,10 @@ public class MapleFamily {
} }
private static boolean idInUse(int id) { private static boolean idInUse(int id) {
for(World world : Server.getInstance().getWorlds()) { for (World world : Server.getInstance().getWorlds()) {
if(world.getFamily(id) != null) return true; if (world.getFamily(id) != null) {
return true;
}
} }
return false; return false;
} }
@@ -98,11 +99,11 @@ public class MapleFamily {
public int getTotalMembers() { public int getTotalMembers() {
return members.size(); return members.size();
} }
public int getTotalGenerations() { public int getTotalGenerations() {
return totalGenerations; return totalGenerations;
} }
public void setTotalGenerations(int generations) { public void setTotalGenerations(int generations) {
this.totalGenerations = generations; this.totalGenerations = generations;
} }
@@ -113,13 +114,13 @@ public class MapleFamily {
public void setMessage(String message, boolean save) { public void setMessage(String message, boolean save) {
this.preceptsMessage = message; this.preceptsMessage = message;
if(save) { if (save) {
try (Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE family_character SET precepts = ? WHERE cid = ?")) { PreparedStatement ps = con.prepareStatement("UPDATE family_character SET precepts = ? WHERE cid = ?")) {
ps.setString(1, message); ps.setString(1, message);
ps.setInt(2, getLeader().getChrId()); ps.setInt(2, getLeader().getChrId());
ps.executeUpdate(); ps.executeUpdate();
} catch(SQLException e) { } catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not save new precepts for family " + getID() + "."); FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not save new precepts for family " + getID() + ".");
e.printStackTrace(); e.printStackTrace();
} }
@@ -136,15 +137,19 @@ public class MapleFamily {
public void removeEntryBranch(MapleFamilyEntry root) { public void removeEntryBranch(MapleFamilyEntry root) {
members.remove(root.getChrId()); members.remove(root.getChrId());
for(MapleFamilyEntry junior : root.getJuniors()) { for (MapleFamilyEntry junior : root.getJuniors()) {
if(junior != null) removeEntryBranch(junior); if (junior != null) {
removeEntryBranch(junior);
}
} }
} }
public void addEntryTree(MapleFamilyEntry root) { public void addEntryTree(MapleFamilyEntry root) {
members.put(root.getChrId(), root); members.put(root.getChrId(), root);
for(MapleFamilyEntry junior : root.getJuniors()) { for (MapleFamilyEntry junior : root.getJuniors()) {
if(junior != null) addEntryTree(junior); if (junior != null) {
addEntryTree(junior);
}
} }
} }
@@ -157,26 +162,28 @@ public class MapleFamily {
} }
public void broadcast(Packet packet, int ignoreID) { public void broadcast(Packet packet, int ignoreID) {
for(MapleFamilyEntry entry : members.values()) { for (MapleFamilyEntry entry : members.values()) {
Character chr = entry.getChr(); Character chr = entry.getChr();
if(chr != null) { if (chr != null) {
if(chr.getId() == ignoreID) continue; if (chr.getId() == ignoreID) {
continue;
}
chr.sendPacket(packet); chr.sendPacket(packet);
} }
} }
} }
public void broadcastFamilyInfoUpdate() { public void broadcastFamilyInfoUpdate() {
for(MapleFamilyEntry entry : members.values()) { for (MapleFamilyEntry entry : members.values()) {
Character chr = entry.getChr(); Character chr = entry.getChr();
if(chr != null) { if (chr != null) {
chr.sendPacket(PacketCreator.getFamilyInfo(entry)); chr.sendPacket(PacketCreator.getFamilyInfo(entry));
} }
} }
} }
public void resetDailyReps() { public void resetDailyReps() {
for(MapleFamilyEntry entry : members.values()) { for (MapleFamilyEntry entry : members.values()) {
entry.setTodaysRep(0); entry.setTodaysRep(0);
entry.setRepsToSenior(0); entry.setRepsToSenior(0);
entry.resetEntitlementUsages(); entry.resetEntitlementUsages();
@@ -221,9 +228,9 @@ public class MapleFamily {
if (wserv == null) { if (wserv == null) {
continue; continue;
} }
MapleFamily family = wserv.getFamily(familyid); Family family = wserv.getFamily(familyid);
if (family == null) { if (family == null) {
family = new MapleFamily(familyid, world); family = new Family(familyid, world);
Server.getInstance().getWorld(world).addFamily(familyid, family); Server.getInstance().getWorld(world).addFamily(familyid, family);
} }
MapleFamilyEntry familyEntry = new MapleFamilyEntry(family, cid, name, level, MapleJob.getById(jobID)); MapleFamilyEntry familyEntry = new MapleFamilyEntry(family, cid, name, level, MapleJob.getById(jobID));
@@ -271,30 +278,32 @@ public class MapleFamily {
} }
for (World world : Server.getInstance().getWorlds()) { for (World world : Server.getInstance().getWorlds()) {
for (MapleFamily family : world.getFamilies()) { for (Family family : world.getFamilies()) {
family.getLeader().doFullCount(); family.getLeader().doFullCount();
} }
} }
} }
public void saveAllMembersRep() { //was used for autosave task, but character autosave should be enough public void saveAllMembersRep() { //was used for autosave task, but character autosave should be enough
try(Connection con = DatabaseConnection.getConnection()) { try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false); con.setAutoCommit(false);
boolean success = true; boolean success = true;
for(MapleFamilyEntry entry : members.values()) { for (MapleFamilyEntry entry : members.values()) {
success = entry.saveReputation(con); success = entry.saveReputation(con);
if(!success) break; if (!success) {
break;
}
} }
if(!success) { if (!success) {
con.rollback(); con.rollback();
FilePrinter.printError(FilePrinter.FAMILY_ERROR, "Family rep autosave failed for family " + getID() + " on " + Calendar.getInstance().getTime().toString() + "."); FilePrinter.printError(FilePrinter.FAMILY_ERROR, "Family rep autosave failed for family " + getID() + " on " + Calendar.getInstance().getTime() + ".");
} }
con.setAutoCommit(true); con.setAutoCommit(true);
//reset repChanged after successful save //reset repChanged after successful save
for(MapleFamilyEntry entry : members.values()) { for (MapleFamilyEntry entry : members.values()) {
entry.savedSuccessfully(); entry.savedSuccessfully();
} }
} catch(SQLException e) { } catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB."); FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB.");
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -39,7 +39,7 @@ import java.util.List;
public class MapleFamilyEntry { public class MapleFamilyEntry {
private final int characterID; private final int characterID;
private volatile MapleFamily family; private volatile Family family;
private volatile Character character; private volatile Character character;
private volatile MapleFamilyEntry senior; private volatile MapleFamilyEntry senior;
@@ -58,7 +58,7 @@ public class MapleFamilyEntry {
private int level; private int level;
private MapleJob job; private MapleJob job;
public MapleFamilyEntry(MapleFamily family, int characterID, String charName, int level, MapleJob job) { public MapleFamilyEntry(Family family, int characterID, String charName, int level, MapleJob job) {
this.family = family; this.family = family;
this.characterID = characterID; this.characterID = characterID;
this.charName = charName; this.charName = charName;
@@ -86,8 +86,8 @@ public class MapleFamilyEntry {
public synchronized void join(MapleFamilyEntry senior) { public synchronized void join(MapleFamilyEntry senior) {
if(senior == null || getSenior() != null) return; if(senior == null || getSenior() != null) return;
MapleFamily oldFamily = getFamily(); Family oldFamily = getFamily();
MapleFamily newFamily = senior.getFamily(); Family newFamily = senior.getFamily();
setSenior(senior, false); setSenior(senior, false);
addSeniorCount(newFamily.getTotalGenerations(), newFamily); //count will be overwritten by doFullCount() addSeniorCount(newFamily.getTotalGenerations(), newFamily); //count will be overwritten by doFullCount()
newFamily.getLeader().doFullCount(); //easier than keeping track of numbers newFamily.getLeader().doFullCount(); //easier than keeping track of numbers
@@ -117,9 +117,9 @@ public class MapleFamilyEntry {
} }
public synchronized void fork() { public synchronized void fork() {
MapleFamily oldFamily = getFamily(); Family oldFamily = getFamily();
MapleFamilyEntry oldSenior = getSenior(); MapleFamilyEntry oldSenior = getSenior();
family = new MapleFamily(-1, oldFamily.getWorld()); family = new Family(-1, oldFamily.getWorld());
Server.getInstance().getWorld(family.getWorld()).addFamily(family.getID(), family); Server.getInstance().getWorld(family.getWorld()).addFamily(family.getID(), family);
setSenior(null, false); setSenior(null, false);
family.setLeader(this); family.setLeader(this);
@@ -185,7 +185,7 @@ public class MapleFamilyEntry {
return true; return true;
} }
private synchronized void addSeniorCount(int seniorCount, MapleFamily newFamily) { // traverses tree and subtracts seniors and updates family private synchronized void addSeniorCount(int seniorCount, Family newFamily) { // traverses tree and subtracts seniors and updates family
if(newFamily != null) this.family = newFamily; if(newFamily != null) this.family = newFamily;
setTotalSeniors(getTotalSeniors() + seniorCount); setTotalSeniors(getTotalSeniors() + seniorCount);
this.generation += seniorCount; this.generation += seniorCount;
@@ -200,7 +200,7 @@ public class MapleFamilyEntry {
if(senior != null) senior.addJuniorCount(juniorCount); if(senior != null) senior.addJuniorCount(juniorCount);
} }
public MapleFamily getFamily() { public Family getFamily() {
return family; return family;
} }

View File

@@ -23,7 +23,7 @@ package net.server;
import client.Character; import client.Character;
import client.Client; import client.Client;
import client.MapleFamily; import client.Family;
import client.SkillFactory; import client.SkillFactory;
import client.command.CommandsExecutor; import client.command.CommandsExecutor;
import client.inventory.Item; import client.inventory.Item;
@@ -857,7 +857,7 @@ public class Server {
applyAllWorldTransfers(con); applyAllWorldTransfers(con);
if (YamlConfig.config.server.USE_FAMILY_SYSTEM) { if (YamlConfig.config.server.USE_FAMILY_SYSTEM) {
MapleFamily.loadAllFamilies(con); Family.loadAllFamilies(con);
} }
} catch (SQLException sqle) { } catch (SQLException sqle) {
log.error("Failed to run all startup-bound database tasks", sqle); log.error("Failed to run all startup-bound database tasks", sqle);

View File

@@ -23,7 +23,7 @@ package net.server.channel.handlers;
import client.Character; import client.Character;
import client.Client; import client.Client;
import client.MapleFamily; import client.Family;
import client.MapleFamilyEntry; import client.MapleFamilyEntry;
import config.YamlConfig; import config.YamlConfig;
import net.AbstractPacketHandler; import net.AbstractPacketHandler;
@@ -76,7 +76,7 @@ public final class AcceptFamilyHandler extends AbstractPacketHandler {
} }
} else { //absorb target family } else { //absorb target family
MapleFamilyEntry targetEntry = chr.getFamilyEntry(); MapleFamilyEntry targetEntry = chr.getFamilyEntry();
MapleFamily targetFamily = targetEntry.getFamily(); Family targetFamily = targetEntry.getFamily();
if(targetFamily.getLeader() != targetEntry) return; if(targetFamily.getLeader() != targetEntry) return;
if(inviter.getFamily().getTotalGenerations() + targetFamily.getTotalGenerations() <= YamlConfig.config.server.FAMILY_MAX_GENERATIONS) { if(inviter.getFamily().getTotalGenerations() + targetFamily.getTotalGenerations() <= YamlConfig.config.server.FAMILY_MAX_GENERATIONS) {
targetEntry.join(inviter.getFamilyEntry()); targetEntry.join(inviter.getFamilyEntry());
@@ -92,7 +92,7 @@ public final class AcceptFamilyHandler extends AbstractPacketHandler {
chr.sendPacket(PacketCreator.sendFamilyMessage(76, 0)); chr.sendPacket(PacketCreator.sendFamilyMessage(76, 0));
return; return;
} }
MapleFamily newFamily = new MapleFamily(-1, c.getWorld()); Family newFamily = new Family(-1, c.getWorld());
c.getWorldServer().addFamily(newFamily.getID(), newFamily); c.getWorldServer().addFamily(newFamily.getID(), newFamily);
MapleFamilyEntry inviterEntry = new MapleFamilyEntry(newFamily, inviter.getId(), inviter.getName(), inviter.getLevel(), inviter.getJob()); MapleFamilyEntry inviterEntry = new MapleFamilyEntry(newFamily, inviter.getId(), inviter.getName(), inviter.getLevel(), inviter.getJob());
inviterEntry.setCharacter(inviter); inviterEntry.setCharacter(inviter);

View File

@@ -1,7 +1,7 @@
package net.server.channel.handlers; package net.server.channel.handlers;
import client.Client; import client.Client;
import client.MapleFamily; import client.Family;
import net.AbstractPacketHandler; import net.AbstractPacketHandler;
import net.packet.InPacket; import net.packet.InPacket;
import tools.PacketCreator; import tools.PacketCreator;
@@ -10,7 +10,7 @@ public class FamilyPreceptsHandler extends AbstractPacketHandler {
@Override @Override
public void handlePacket(InPacket p, Client c) { public void handlePacket(InPacket p, Client c) {
MapleFamily family = c.getPlayer().getFamily(); Family family = c.getPlayer().getFamily();
if(family == null) return; if(family == null) return;
if(family.getLeader().getChr() != c.getPlayer()) return; //only the leader can set the precepts if(family.getLeader().getChr() != c.getPlayer()) return; //only the leader can set the precepts
String newPrecepts = p.readString(); String newPrecepts = p.readString();

View File

@@ -20,7 +20,7 @@
package net.server.channel.handlers; package net.server.channel.handlers;
import client.Client; import client.Client;
import client.MapleFamily; import client.Family;
import client.MapleFamilyEntry; import client.MapleFamilyEntry;
import config.YamlConfig; import config.YamlConfig;
import net.AbstractPacketHandler; import net.AbstractPacketHandler;
@@ -32,7 +32,7 @@ public class FamilySeparateHandler extends AbstractPacketHandler {
@Override @Override
public void handlePacket(InPacket p, Client c) { public void handlePacket(InPacket p, Client c) {
if(!YamlConfig.config.server.USE_FAMILY_SYSTEM) return; if(!YamlConfig.config.server.USE_FAMILY_SYSTEM) return;
MapleFamily oldFamily = c.getPlayer().getFamily(); Family oldFamily = c.getPlayer().getFamily();
if(oldFamily == null) return; if(oldFamily == null) return;
MapleFamilyEntry forkOn = null; MapleFamilyEntry forkOn = null;
boolean isSenior; boolean isSenior;

View File

@@ -247,7 +247,7 @@ public final class PlayerLoggedinHandler extends AbstractPacketHandler {
c.sendPacket(PacketCreator.loadFamily(player)); c.sendPacket(PacketCreator.loadFamily(player));
if (player.getFamilyId() > 0) { if (player.getFamilyId() > 0) {
MapleFamily f = wserv.getFamily(player.getFamilyId()); Family f = wserv.getFamily(player.getFamilyId());
if (f != null) { if (f != null) {
MapleFamilyEntry familyEntry = f.getEntryByID(player.getId()); MapleFamilyEntry familyEntry = f.getEntryByID(player.getId());
if (familyEntry != null) { if (familyEntry != null) {

View File

@@ -22,7 +22,7 @@
package net.server.handlers.login; package net.server.handlers.login;
import client.Client; import client.Client;
import client.MapleFamily; import client.Family;
import net.AbstractPacketHandler; import net.AbstractPacketHandler;
import net.packet.InPacket; import net.packet.InPacket;
import net.server.Server; import net.server.Server;
@@ -60,7 +60,7 @@ public final class DeleteCharHandler extends AbstractPacketHandler {
c.sendPacket(PacketCreator.deleteCharResponse(cid, 0x16)); c.sendPacket(PacketCreator.deleteCharResponse(cid, 0x16));
return; return;
} else if (familyId != -1) { } else if (familyId != -1) {
MapleFamily family = Server.getInstance().getWorld(world).getFamily(familyId); Family family = Server.getInstance().getWorld(world).getFamily(familyId);
if (family != null && family.getTotalMembers() > 1) { if (family != null && family.getTotalMembers() > 1) {
c.sendPacket(PacketCreator.deleteCharResponse(cid, 0x1D)); c.sendPacket(PacketCreator.deleteCharResponse(cid, 0x1D));
return; return;

View File

@@ -1,15 +1,15 @@
package net.server.task; package net.server.task;
import client.Family;
import net.server.world.World;
import tools.DatabaseConnection;
import tools.FilePrinter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Calendar; import java.util.Calendar;
import client.MapleFamily;
import net.server.world.World;
import tools.DatabaseConnection;
import tools.FilePrinter;
public class FamilyDailyResetTask implements Runnable { public class FamilyDailyResetTask implements Runnable {
private final World world; private final World world;
@@ -21,7 +21,7 @@ public class FamilyDailyResetTask implements Runnable {
@Override @Override
public void run() { public void run() {
resetEntitlementUsage(world); resetEntitlementUsage(world);
for(MapleFamily family : world.getFamilies()) { for(Family family : world.getFamilies()) {
family.resetDailyReps(); family.resetDailyReps();
} }
} }

View File

@@ -26,7 +26,7 @@ import client.BuddyList.BuddyAddResult;
import client.BuddyList.BuddyOperation; import client.BuddyList.BuddyOperation;
import client.BuddylistEntry; import client.BuddylistEntry;
import client.Character; import client.Character;
import client.MapleFamily; import client.Family;
import config.YamlConfig; import config.YamlConfig;
import constants.game.GameConstants; import constants.game.GameConstants;
import net.packet.Packet; import net.packet.Packet;
@@ -82,7 +82,7 @@ public class World {
private Map<Integer, Short> pnpcPodium = new HashMap<>(); private Map<Integer, Short> pnpcPodium = new HashMap<>();
private Map<Integer, MapleMessenger> messengers = new HashMap<>(); private Map<Integer, MapleMessenger> messengers = new HashMap<>();
private AtomicInteger runningMessengerId = new AtomicInteger(); private AtomicInteger runningMessengerId = new AtomicInteger();
private Map<Integer, MapleFamily> families = new LinkedHashMap<>(); private Map<Integer, Family> families = new LinkedHashMap<>();
private Map<Integer, Integer> relationships = new HashMap<>(); private Map<Integer, Integer> relationships = new HashMap<>();
private Map<Integer, Pair<Integer, Integer>> relationshipCouples = new HashMap<>(); private Map<Integer, Pair<Integer, Integer>> relationshipCouples = new HashMap<>();
private Map<Integer, MapleGuildSummary> gsStore = new HashMap<>(); private Map<Integer, MapleGuildSummary> gsStore = new HashMap<>();
@@ -547,7 +547,7 @@ public class World {
return id; return id;
} }
public void addFamily(int id, MapleFamily f) { public void addFamily(int id, Family f) {
synchronized (families) { synchronized (families) {
if (!families.containsKey(id)) { if (!families.containsKey(id)) {
families.put(id, f); families.put(id, f);
@@ -561,7 +561,7 @@ public class World {
} }
} }
public MapleFamily getFamily(int id) { public Family getFamily(int id) {
synchronized (families) { synchronized (families) {
if (families.containsKey(id)) { if (families.containsKey(id)) {
return families.get(id); return families.get(id);
@@ -570,7 +570,7 @@ public class World {
} }
} }
public Collection<MapleFamily> getFamilies() { public Collection<Family> getFamilies() {
synchronized(families) { synchronized(families) {
return Collections.unmodifiableCollection(families.values()); return Collections.unmodifiableCollection(families.values());
} }