Rename and clean up MapleFamilyEntry

This commit is contained in:
P0nk
2021-09-09 21:19:39 +02:00
parent 0c8bef6842
commit ee503eedce
10 changed files with 256 additions and 185 deletions

View File

@@ -115,7 +115,7 @@ public class Character extends AbstractCharacterObject {
private int energybar;
private int gmLevel;
private int ci = 0;
private MapleFamilyEntry familyEntry;
private FamilyEntry familyEntry;
private int familyId;
private int bookCover;
private int battleshipHp = 0;
@@ -4963,11 +4963,11 @@ public class Character extends AbstractCharacterObject {
}
}
public MapleFamilyEntry getFamilyEntry() {
public FamilyEntry getFamilyEntry() {
return familyEntry;
}
public void setFamilyEntry(MapleFamilyEntry entry) {
public void setFamilyEntry(FamilyEntry entry) {
if (entry != null) {
setFamilyId(entry.getFamily().getID());
}
@@ -6495,10 +6495,10 @@ public class Character extends AbstractCharacterObject {
levelUpMessages();
guildUpdate();
MapleFamilyEntry familyEntry = getFamilyEntry();
FamilyEntry familyEntry = getFamilyEntry();
if (familyEntry != null) {
familyEntry.giveReputationToSenior(YamlConfig.config.server.FAMILY_REP_PER_LEVELUP, true);
MapleFamilyEntry senior = familyEntry.getSenior();
FamilyEntry senior = familyEntry.getSenior();
if (senior != null) { //only send the message to direct senior
Character seniorChr = senior.getChr();
if (seniorChr != null) {
@@ -8706,12 +8706,12 @@ public class Character extends AbstractCharacterObject {
}
}
MapleFamilyEntry familyEntry = getFamilyEntry(); //save family rep
FamilyEntry familyEntry = getFamilyEntry(); //save family rep
if (familyEntry != null) {
if (familyEntry.saveReputation(con)) {
familyEntry.savedSuccessfully();
}
MapleFamilyEntry senior = familyEntry.getSenior();
FamilyEntry senior = familyEntry.getSenior();
if (senior != null && senior.getChr() == null) { //only save for offline family members
if (senior.saveReputation(con)) {
senior.savedSuccessfully();
@@ -10529,7 +10529,7 @@ public class Character extends AbstractCharacterObject {
mpc = null;
mgc = null;
party = null;
MapleFamilyEntry familyEntry = getFamilyEntry();
FamilyEntry familyEntry = getFamilyEntry();
if (familyEntry != null) {
familyEntry.setCharacter(null);
setFamilyEntry(null);

View File

@@ -49,8 +49,8 @@ public class Family {
private static final AtomicInteger familyIDCounter = new AtomicInteger();
private final int id, world;
private final Map<Integer, MapleFamilyEntry> members = new ConcurrentHashMap<>();
private MapleFamilyEntry leader;
private final Map<Integer, FamilyEntry> members = new ConcurrentHashMap<>();
private FamilyEntry leader;
private String name;
private String preceptsMessage = "";
private int totalGenerations;
@@ -83,12 +83,12 @@ public class Family {
return world;
}
public void setLeader(MapleFamilyEntry leader) {
public void setLeader(FamilyEntry leader) {
this.leader = leader;
setName(leader.getName());
}
public MapleFamilyEntry getLeader() {
public FamilyEntry getLeader() {
return leader;
}
@@ -131,29 +131,29 @@ public class Family {
return preceptsMessage;
}
public void addEntry(MapleFamilyEntry entry) {
public void addEntry(FamilyEntry entry) {
members.put(entry.getChrId(), entry);
}
public void removeEntryBranch(MapleFamilyEntry root) {
public void removeEntryBranch(FamilyEntry root) {
members.remove(root.getChrId());
for (MapleFamilyEntry junior : root.getJuniors()) {
for (FamilyEntry junior : root.getJuniors()) {
if (junior != null) {
removeEntryBranch(junior);
}
}
}
public void addEntryTree(MapleFamilyEntry root) {
public void addEntryTree(FamilyEntry root) {
members.put(root.getChrId(), root);
for (MapleFamilyEntry junior : root.getJuniors()) {
for (FamilyEntry junior : root.getJuniors()) {
if (junior != null) {
addEntryTree(junior);
}
}
}
public MapleFamilyEntry getEntryByID(int cid) {
public FamilyEntry getEntryByID(int cid) {
return members.get(cid);
}
@@ -162,7 +162,7 @@ public class Family {
}
public void broadcast(Packet packet, int ignoreID) {
for (MapleFamilyEntry entry : members.values()) {
for (FamilyEntry entry : members.values()) {
Character chr = entry.getChr();
if (chr != null) {
if (chr.getId() == ignoreID) {
@@ -174,7 +174,7 @@ public class Family {
}
public void broadcastFamilyInfoUpdate() {
for (MapleFamilyEntry entry : members.values()) {
for (FamilyEntry entry : members.values()) {
Character chr = entry.getChr();
if (chr != null) {
chr.sendPacket(PacketCreator.getFamilyInfo(entry));
@@ -183,7 +183,7 @@ public class Family {
}
public void resetDailyReps() {
for (MapleFamilyEntry entry : members.values()) {
for (FamilyEntry entry : members.values()) {
entry.setTodaysRep(0);
entry.setRepsToSenior(0);
entry.resetEntitlementUsages();
@@ -191,7 +191,7 @@ public class Family {
}
public static void loadAllFamilies(Connection con) {
List<Pair<Pair<Integer, Integer>, MapleFamilyEntry>> unmatchedJuniors = new ArrayList<>(200); // <<world, seniorid> familyEntry>
List<Pair<Pair<Integer, Integer>, FamilyEntry>> unmatchedJuniors = new ArrayList<>(200); // <<world, seniorid> familyEntry>
try (PreparedStatement psEntries = con.prepareStatement("SELECT * FROM family_character")) {
ResultSet rsEntries = psEntries.executeQuery();
while (rsEntries.next()) { // can be optimized
@@ -233,13 +233,13 @@ public class Family {
family = new Family(familyid, world);
Server.getInstance().getWorld(world).addFamily(familyid, family);
}
MapleFamilyEntry familyEntry = new MapleFamilyEntry(family, cid, name, level, MapleJob.getById(jobID));
FamilyEntry familyEntry = new FamilyEntry(family, cid, name, level, MapleJob.getById(jobID));
family.addEntry(familyEntry);
if (seniorid <= 0) {
family.setLeader(familyEntry);
family.setMessage(precepts, false);
}
MapleFamilyEntry senior = family.getEntryByID(seniorid);
FamilyEntry senior = family.getEntryByID(seniorid);
if (senior != null) {
familyEntry.setSenior(family.getEntryByID(seniorid), false);
} else {
@@ -265,11 +265,11 @@ public class Family {
e.printStackTrace();
}
// link missing ones (out of order)
for (Pair<Pair<Integer, Integer>, MapleFamilyEntry> unmatchedJunior : unmatchedJuniors) {
for (Pair<Pair<Integer, Integer>, FamilyEntry> unmatchedJunior : unmatchedJuniors) {
int world = unmatchedJunior.getLeft().getLeft();
int seniorid = unmatchedJunior.getLeft().getRight();
MapleFamilyEntry junior = unmatchedJunior.getRight();
MapleFamilyEntry senior = Server.getInstance().getWorld(world).getFamily(junior.getFamily().getID()).getEntryByID(seniorid);
FamilyEntry junior = unmatchedJunior.getRight();
FamilyEntry senior = Server.getInstance().getWorld(world).getFamily(junior.getFamily().getID()).getEntryByID(seniorid);
if (senior != null) {
junior.setSenior(senior, false);
} else {
@@ -288,7 +288,7 @@ public class Family {
try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
boolean success = true;
for (MapleFamilyEntry entry : members.values()) {
for (FamilyEntry entry : members.values()) {
success = entry.saveReputation(con);
if (!success) {
break;
@@ -300,7 +300,7 @@ public class Family {
}
con.setAutoCommit(true);
//reset repChanged after successful save
for (MapleFamilyEntry entry : members.values()) {
for (FamilyEntry entry : members.values()) {
entry.savedSuccessfully();
}
} catch (SQLException e) {

View File

@@ -37,20 +37,20 @@ import java.util.List;
* @author Ubaware
*/
public class MapleFamilyEntry {
public class FamilyEntry {
private final int characterID;
private volatile Family family;
private volatile Character character;
private volatile MapleFamilyEntry senior;
private final MapleFamilyEntry[] juniors = new MapleFamilyEntry[2];
private volatile FamilyEntry senior;
private final FamilyEntry[] juniors = new FamilyEntry[2];
private final int[] entitlements = new int[11];
private volatile int reputation, totalReputation;
private volatile int todaysRep, repsToSenior; //both are daily values
private volatile int totalJuniors, totalSeniors;
private volatile int generation;
private volatile boolean repChanged; //used to ignore saving unchanged rep values
// cached values for offline players
@@ -58,7 +58,7 @@ public class MapleFamilyEntry {
private int level;
private MapleJob job;
public MapleFamilyEntry(Family family, int characterID, String charName, int level, MapleJob job) {
public FamilyEntry(Family family, int characterID, String charName, int level, MapleJob job) {
this.family = family;
this.characterID = characterID;
this.charName = charName;
@@ -71,21 +71,26 @@ public class MapleFamilyEntry {
}
public void setCharacter(Character newCharacter) {
if(newCharacter == null) cacheOffline(newCharacter);
else newCharacter.setFamilyEntry(this);
if (newCharacter == null) {
cacheOffline(newCharacter);
} else {
newCharacter.setFamilyEntry(this);
}
this.character = newCharacter;
}
private void cacheOffline(Character chr) {
if(chr != null) {
if (chr != null) {
charName = chr.getName();
level = chr.getLevel();
job = chr.getJob();
}
}
public synchronized void join(MapleFamilyEntry senior) {
if(senior == null || getSenior() != null) return;
public synchronized void join(FamilyEntry senior) {
if (senior == null || getSenior() != null) {
return;
}
Family oldFamily = getFamily();
Family newFamily = senior.getFamily();
setSenior(senior, false);
@@ -94,23 +99,25 @@ public class MapleFamilyEntry {
oldFamily.setMessage(null, true);
newFamily.addEntryTree(this);
Server.getInstance().getWorld(oldFamily.getWorld()).removeFamily(oldFamily.getID());
//db
try(Connection con = DatabaseConnection.getConnection()) {
try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
boolean success = updateDBChangeFamily(con, getChrId(), newFamily.getID(), senior.getChrId());
for(MapleFamilyEntry junior : juniors) { // better to duplicate this than the SQL code
if(junior != null) {
for (FamilyEntry junior : juniors) { // better to duplicate this than the SQL code
if (junior != null) {
success = junior.updateNewFamilyDB(con); // recursively updates juniors in db
if(!success) break;
if (!success) {
break;
}
}
}
if(!success) {
if (!success) {
con.rollback();
FilePrinter.printError(FilePrinter.FAMILY_ERROR, "Could not absorb " + oldFamily.getName() + " family into " + newFamily.getName() + " family. (SQL ERROR)");
}
con.setAutoCommit(true);
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB.");
e.printStackTrace();
}
@@ -118,14 +125,14 @@ public class MapleFamilyEntry {
public synchronized void fork() {
Family oldFamily = getFamily();
MapleFamilyEntry oldSenior = getSenior();
FamilyEntry oldSenior = getSenior();
family = new Family(-1, oldFamily.getWorld());
Server.getInstance().getWorld(family.getWorld()).addFamily(family.getID(), family);
setSenior(null, false);
family.setLeader(this);
addSeniorCount(-getTotalSeniors(), family);
setTotalSeniors(0);
if(oldSenior != null) {
if (oldSenior != null) {
oldSenior.addJuniorCount(-getTotalJuniors());
oldSenior.removeJunior(this);
oldFamily.getLeader().doFullCount();
@@ -137,47 +144,55 @@ public class MapleFamilyEntry {
family.setMessage("", true);
doFullCount(); //to make sure all counts are correct
// update db
try(Connection con = DatabaseConnection.getConnection()) {
try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
boolean success = updateDBChangeFamily(con, getChrId(), getFamily().getID(), 0);
for(MapleFamilyEntry junior : juniors) { // better to duplicate this than the SQL code
if(junior != null) {
for (FamilyEntry junior : juniors) { // better to duplicate this than the SQL code
if (junior != null) {
success = junior.updateNewFamilyDB(con); // recursively updates juniors in db
if(!success) break;
if (!success) {
break;
}
}
}
if(!success) {
if (!success) {
con.rollback();
FilePrinter.printError(FilePrinter.FAMILY_ERROR, "Could not fork family with new leader " + getName() + ". (Old senior : " + oldSenior.getName() + ", leader :" + oldFamily.getLeader().getName() + ")");
}
con.setAutoCommit(true);
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB.");
e.printStackTrace();
}
}
private synchronized boolean updateNewFamilyDB(Connection con) {
if(!updateFamilyEntryDB(con, getChrId(), getFamily().getID())) return false;
if(!updateCharacterFamilyDB(con, getChrId(), getFamily().getID(), true)) return false;
if (!updateFamilyEntryDB(con, getChrId(), getFamily().getID())) {
return false;
}
if (!updateCharacterFamilyDB(con, getChrId(), getFamily().getID(), true)) {
return false;
}
for(MapleFamilyEntry junior : juniors) {
if(junior != null) {
if(!junior.updateNewFamilyDB(con)) return false;
for (FamilyEntry junior : juniors) {
if (junior != null) {
if (!junior.updateNewFamilyDB(con)) {
return false;
}
}
}
return true;
}
private static boolean updateFamilyEntryDB(Connection con, int cid, int familyid) {
try(PreparedStatement ps = con.prepareStatement("UPDATE family_character SET familyid = ? WHERE cid = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE family_character SET familyid = ? WHERE cid = ?")) {
ps.setInt(1, familyid);
ps.setInt(2, cid);
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not update family id in 'family_character' for character id " + cid + ". (fork)");
e.printStackTrace();
return false;
@@ -186,18 +201,24 @@ public class MapleFamilyEntry {
}
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);
this.generation += seniorCount;
for(MapleFamilyEntry junior : juniors) {
if(junior != null) junior.addSeniorCount(seniorCount, newFamily);
for (FamilyEntry junior : juniors) {
if (junior != null) {
junior.addSeniorCount(seniorCount, newFamily);
}
}
}
private synchronized void addJuniorCount(int juniorCount) { // climbs tree and adds junior count
setTotalJuniors(getTotalJuniors() + juniorCount);
MapleFamilyEntry senior = getSenior();
if(senior != null) senior.addJuniorCount(juniorCount);
FamilyEntry senior = getSenior();
if (senior != null) {
senior.addJuniorCount(juniorCount);
}
}
public Family getFamily() {
@@ -210,20 +231,29 @@ public class MapleFamilyEntry {
public String getName() {
Character chr = character;
if(chr != null) return chr.getName();
else return charName;
if (chr != null) {
return chr.getName();
} else {
return charName;
}
}
public int getLevel() {
Character chr = character;
if(chr != null) return chr.getLevel();
else return level;
if (chr != null) {
return chr.getLevel();
} else {
return level;
}
}
public MapleJob getJob() {
Character chr = character;
if(chr != null) return chr.getJob();
else return job;
if (chr != null) {
return chr.getJob();
} else {
return job;
}
}
public int getReputation() {
@@ -235,52 +265,64 @@ public class MapleFamilyEntry {
}
public void setReputation(int reputation) {
if(reputation != this.reputation) this.repChanged = true;
if (reputation != this.reputation) {
this.repChanged = true;
}
this.reputation = reputation;
}
public void setTodaysRep(int today) {
if(today != todaysRep) this.repChanged = true;
if (today != todaysRep) {
this.repChanged = true;
}
this.todaysRep = today;
}
public int getRepsToSenior() {
return repsToSenior;
}
public void setRepsToSenior(int reputation) {
if(reputation != this.repsToSenior) this.repChanged = true;
if (reputation != this.repsToSenior) {
this.repChanged = true;
}
this.repsToSenior = reputation;
}
public void gainReputation(int gain, boolean countTowardsTotal) {
gainReputation(gain, countTowardsTotal, this);
}
private void gainReputation(int gain, boolean countTowardsTotal, MapleFamilyEntry from) {
if(gain != 0) repChanged = true;
private void gainReputation(int gain, boolean countTowardsTotal, FamilyEntry from) {
if (gain != 0) {
repChanged = true;
}
this.reputation += gain;
this.todaysRep += gain;
if(gain > 0 && countTowardsTotal) {
if (gain > 0 && countTowardsTotal) {
this.totalReputation += gain;
}
Character chr = getChr();
if(chr != null) chr.sendPacket(PacketCreator.sendGainRep(gain, from != null ? from.getName() : ""));
if (chr != null) {
chr.sendPacket(PacketCreator.sendGainRep(gain, from != null ? from.getName() : ""));
}
}
public void giveReputationToSenior(int gain, boolean includeSuperSenior) {
int actualGain = gain;
MapleFamilyEntry senior = getSenior();
if(senior != null && senior.getLevel() < getLevel() && gain > 0) actualGain /= 2; //don't halve negative values
if(senior != null) {
FamilyEntry senior = getSenior();
if (senior != null && senior.getLevel() < getLevel() && gain > 0) {
actualGain /= 2; //don't halve negative values
}
if (senior != null) {
senior.gainReputation(actualGain, true, this);
if(actualGain > 0) {
if (actualGain > 0) {
this.repsToSenior += actualGain;
this.repChanged = true;
}
if(includeSuperSenior) {
if (includeSuperSenior) {
senior = senior.getSenior();
if(senior != null) {
if (senior != null) {
senior.gainReputation(actualGain, true, this);
}
}
@@ -292,31 +334,37 @@ public class MapleFamilyEntry {
}
public void setTotalReputation(int totalReputation) {
if(totalReputation != this.totalReputation) this.repChanged = true;
if (totalReputation != this.totalReputation) {
this.repChanged = true;
}
this.totalReputation = totalReputation;
}
public MapleFamilyEntry getSenior() {
public FamilyEntry getSenior() {
return senior;
}
public synchronized boolean setSenior(MapleFamilyEntry senior, boolean save) {
if(this.senior == senior) return false;
MapleFamilyEntry oldSenior = this.senior;
public synchronized boolean setSenior(FamilyEntry senior, boolean save) {
if (this.senior == senior) {
return false;
}
FamilyEntry oldSenior = this.senior;
this.senior = senior;
if(senior != null) {
if(senior.addJunior(this)) {
if(save) {
if (senior != null) {
if (senior.addJunior(this)) {
if (save) {
updateDBChangeFamily(getChrId(), senior.getFamily().getID(), senior.getChrId());
}
if(this.repsToSenior != 0) this.repChanged = true;
this.repsToSenior = 0;
if (this.repsToSenior != 0) {
this.repChanged = true;
}
this.repsToSenior = 0;
this.addSeniorCount(1, null);
this.setTotalSeniors(senior.getTotalSeniors() + 1);
return true;
}
} else {
if(oldSenior != null) {
if (oldSenior != null) {
oldSenior.removeJunior(this);
}
}
@@ -324,9 +372,9 @@ public class MapleFamilyEntry {
}
private static boolean updateDBChangeFamily(int cid, int familyid, int seniorid) {
try(Connection con = DatabaseConnection.getConnection()) {
try (Connection con = DatabaseConnection.getConnection()) {
return updateDBChangeFamily(con, cid, familyid, seniorid);
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB.");
e.printStackTrace();
return false;
@@ -334,12 +382,12 @@ public class MapleFamilyEntry {
}
private static boolean updateDBChangeFamily(Connection con, int cid, int familyid, int seniorid) {
try(PreparedStatement ps = con.prepareStatement("UPDATE family_character SET familyid = ?, seniorid = ?, reptosenior = 0 WHERE cid = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE family_character SET familyid = ?, seniorid = ?, reptosenior = 0 WHERE cid = ?")) {
ps.setInt(1, familyid);
ps.setInt(2, seniorid);
ps.setInt(3, cid);
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not update seniorid in 'family_character' for character id " + cid + ".");
e.printStackTrace();
return false;
@@ -348,11 +396,11 @@ public class MapleFamilyEntry {
}
private static boolean updateCharacterFamilyDB(Connection con, int charid, int familyid, boolean fork) {
try(PreparedStatement ps = con.prepareStatement("UPDATE characters SET familyid = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET familyid = ? WHERE id = ?")) {
ps.setInt(1, familyid);
ps.setInt(2, charid);
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not update familyid in 'characters' for character id " + charid + " when changing family. " + (fork ? "(fork)" : ""));
e.printStackTrace();
return false;
@@ -360,26 +408,33 @@ public class MapleFamilyEntry {
return true;
}
public List<MapleFamilyEntry> getJuniors() {
public List<FamilyEntry> getJuniors() {
return Collections.unmodifiableList(Arrays.asList(juniors));
}
public MapleFamilyEntry getOtherJunior(MapleFamilyEntry junior) {
if(juniors[0] == junior) return juniors[1];
else if(juniors[1] == junior) return juniors[0];
public FamilyEntry getOtherJunior(FamilyEntry junior) {
if (juniors[0] == junior) {
return juniors[1];
} else if (juniors[1] == junior) {
return juniors[0];
}
return null;
}
public int getJuniorCount() { //close enough to be relatively consistent to multiple threads (and the result is not vital)
int juniorCount = 0;
if(juniors[0] != null) juniorCount++;
if(juniors[1] != null) juniorCount++;
if (juniors[0] != null) {
juniorCount++;
}
if (juniors[1] != null) {
juniorCount++;
}
return juniorCount;
}
public synchronized boolean addJunior(MapleFamilyEntry newJunior) {
for(int i = 0; i < juniors.length; i++) {
if(juniors[i] == null) { // successfully add new junior to family
public synchronized boolean addJunior(FamilyEntry newJunior) {
for (int i = 0; i < juniors.length; i++) {
if (juniors[i] == null) { // successfully add new junior to family
juniors[i] = newJunior;
addJuniorCount(1);
getFamily().addEntry(newJunior);
@@ -389,15 +444,15 @@ public class MapleFamilyEntry {
return false;
}
public synchronized boolean isJunior(MapleFamilyEntry entry) { //require locking since result accuracy is vital
if(juniors[0] == entry) return true;
else if(juniors[1] == entry) return true;
return false;
public synchronized boolean isJunior(FamilyEntry entry) { //require locking since result accuracy is vital
if (juniors[0] == entry) {
return true;
} else return juniors[1] == entry;
}
public synchronized boolean removeJunior(MapleFamilyEntry junior) {
for(int i = 0; i < juniors.length; i++) {
if(juniors[i] == junior) {
public synchronized boolean removeJunior(FamilyEntry junior) {
for (int i = 0; i < juniors.length; i++) {
if (juniors[i] == junior) {
juniors[i] = null;
return true;
}
@@ -420,29 +475,37 @@ public class MapleFamilyEntry {
public void setTotalJuniors(int totalJuniors) {
this.totalJuniors = totalJuniors;
}
public void announceToSenior(Packet packet, boolean includeSuperSenior) {
MapleFamilyEntry senior = getSenior();
if(senior != null) {
FamilyEntry senior = getSenior();
if (senior != null) {
Character seniorChr = senior.getChr();
if(seniorChr != null) seniorChr.sendPacket(packet);
if (seniorChr != null) {
seniorChr.sendPacket(packet);
}
senior = senior.getSenior();
if(includeSuperSenior && senior != null) {
if (includeSuperSenior && senior != null) {
seniorChr = senior.getChr();
if(seniorChr != null) seniorChr.sendPacket(packet);
if (seniorChr != null) {
seniorChr.sendPacket(packet);
}
}
}
}
public void updateSeniorFamilyInfo(boolean includeSuperSenior) {
MapleFamilyEntry senior = getSenior();
if(senior != null) {
FamilyEntry senior = getSenior();
if (senior != null) {
Character seniorChr = senior.getChr();
if(seniorChr != null) seniorChr.sendPacket(PacketCreator.getFamilyInfo(senior));
if (seniorChr != null) {
seniorChr.sendPacket(PacketCreator.getFamilyInfo(senior));
}
senior = senior.getSenior();
if(includeSuperSenior && senior != null) {
if (includeSuperSenior && senior != null) {
seniorChr = senior.getChr();
if(seniorChr != null) seniorChr.sendPacket(PacketCreator.getFamilyInfo(senior));
if (seniorChr != null) {
seniorChr.sendPacket(PacketCreator.getFamilyInfo(senior));
}
}
}
}
@@ -460,11 +523,13 @@ public class MapleFamilyEntry {
this.generation = seniors;
int juniorCount = 0;
int highestGeneration = this.generation;
for(MapleFamilyEntry entry : juniors) {
if(entry != null) {
for (FamilyEntry entry : juniors) {
if (entry != null) {
Pair<Integer, Integer> counts = entry.traverseAndUpdateCounts(seniors + 1);
juniorCount += counts.getRight(); //total juniors
if(counts.getLeft() > highestGeneration) highestGeneration = counts.getLeft();
if (counts.getLeft() > highestGeneration) {
highestGeneration = counts.getLeft();
}
}
}
setTotalJuniors(juniorCount);
@@ -473,27 +538,29 @@ public class MapleFamilyEntry {
public boolean useEntitlement(FamilyEntitlement entitlement) {
int id = entitlement.ordinal();
if(entitlements[id] >= 1) return false;
try(Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO family_entitlement (entitlementid, charid, timestamp) VALUES (?, ?, ?)")) {
if (entitlements[id] >= 1) {
return false;
}
try (Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO family_entitlement (entitlementid, charid, timestamp) VALUES (?, ?, ?)")) {
ps.setInt(1, id);
ps.setInt(2, getChrId());
ps.setLong(3, System.currentTimeMillis());
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not insert new row in 'family_entitlement' for character " + getName() + ".");
e.printStackTrace();
}
entitlements[id]++;
return true;
}
public boolean refundEntitlement(FamilyEntitlement entitlement) {
int id = entitlement.ordinal();
try(Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("DELETE FROM family_entitlement WHERE entitlementid = ? AND charid = ?")) {
try (Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("DELETE FROM family_entitlement WHERE entitlementid = ? AND charid = ?")) {
ps.setInt(1, id);
ps.setInt(2, getChrId());
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not refund family entitlement \"" + entitlement.getName() + "\" for character " + getName() + ".");
e.printStackTrace();
}
@@ -504,34 +571,38 @@ public class MapleFamilyEntry {
public boolean isEntitlementUsed(FamilyEntitlement entitlement) {
return entitlements[entitlement.ordinal()] >= 1;
}
public int getEntitlementUsageCount(FamilyEntitlement entitlement) {
return entitlements[entitlement.ordinal()];
}
public void setEntitlementUsed(int id) {
entitlements[id]++;
}
public void resetEntitlementUsages() {
for(FamilyEntitlement entitlement : FamilyEntitlement.values()) {
for (FamilyEntitlement entitlement : FamilyEntitlement.values()) {
entitlements[entitlement.ordinal()] = 0;
}
}
public boolean saveReputation() {
if(!repChanged) return true;
try(Connection con = DatabaseConnection.getConnection()) {
if (!repChanged) {
return true;
}
try (Connection con = DatabaseConnection.getConnection()) {
return saveReputation(con);
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Could not get connection to DB.");
e.printStackTrace();
return false;
}
}
public boolean saveReputation(Connection con) {
if(!repChanged) return true;
if (!repChanged) {
return true;
}
try (PreparedStatement ps = con.prepareStatement("UPDATE family_character SET reputation = ?, todaysrep = ?, totalreputation = ?, reptosenior = ? WHERE cid = ?")) {
ps.setInt(1, getReputation());
ps.setInt(2, getTodaysRep());
@@ -539,14 +610,14 @@ public class MapleFamilyEntry {
ps.setInt(4, getRepsToSenior());
ps.setInt(5, getChrId());
ps.executeUpdate();
} catch(SQLException e) {
} catch (SQLException e) {
FilePrinter.printError(FilePrinter.FAMILY_ERROR, e, "Failed to autosave rep to 'family_character' for charid " + getChrId());
e.printStackTrace();
return false;
}
return true;
}
public void savedSuccessfully() {
this.repChanged = false;
}

View File

@@ -24,7 +24,7 @@ package net.server.channel.handlers;
import client.Character;
import client.Client;
import client.Family;
import client.MapleFamilyEntry;
import client.FamilyEntry;
import config.YamlConfig;
import net.AbstractPacketHandler;
import net.packet.InPacket;
@@ -64,7 +64,7 @@ public final class AcceptFamilyHandler extends AbstractPacketHandler {
if(accept) {
if(inviter.getFamily() != null) {
if(chr.getFamily() == null) {
MapleFamilyEntry newEntry = new MapleFamilyEntry(inviter.getFamily(), chr.getId(), chr.getName(), chr.getLevel(), chr.getJob());
FamilyEntry newEntry = new FamilyEntry(inviter.getFamily(), chr.getId(), chr.getName(), chr.getLevel(), chr.getJob());
newEntry.setCharacter(chr);
if(!newEntry.setSenior(inviter.getFamilyEntry(), true)) {
inviter.sendPacket(PacketCreator.sendFamilyMessage(1, 0));
@@ -75,7 +75,7 @@ public final class AcceptFamilyHandler extends AbstractPacketHandler {
insertNewFamilyRecord(chr.getId(), inviter.getFamily().getID(), inviter.getId(), false);
}
} else { //absorb target family
MapleFamilyEntry targetEntry = chr.getFamilyEntry();
FamilyEntry targetEntry = chr.getFamilyEntry();
Family targetFamily = targetEntry.getFamily();
if(targetFamily.getLeader() != targetEntry) return;
if(inviter.getFamily().getTotalGenerations() + targetFamily.getTotalGenerations() <= YamlConfig.config.server.FAMILY_MAX_GENERATIONS) {
@@ -94,12 +94,12 @@ public final class AcceptFamilyHandler extends AbstractPacketHandler {
}
Family newFamily = new Family(-1, c.getWorld());
c.getWorldServer().addFamily(newFamily.getID(), newFamily);
MapleFamilyEntry inviterEntry = new MapleFamilyEntry(newFamily, inviter.getId(), inviter.getName(), inviter.getLevel(), inviter.getJob());
FamilyEntry inviterEntry = new FamilyEntry(newFamily, inviter.getId(), inviter.getName(), inviter.getLevel(), inviter.getJob());
inviterEntry.setCharacter(inviter);
newFamily.setLeader(inviter.getFamilyEntry());
newFamily.addEntry(inviterEntry);
if(chr.getFamily() == null) { //completely new family
MapleFamilyEntry newEntry = new MapleFamilyEntry(newFamily, chr.getId(), chr.getName(), chr.getLevel(), chr.getJob());
FamilyEntry newEntry = new FamilyEntry(newFamily, chr.getId(), chr.getName(), chr.getLevel(), chr.getJob());
newEntry.setCharacter(chr);
newEntry.setSenior(inviterEntry, true);
// save new family

View File

@@ -21,7 +21,7 @@ package net.server.channel.handlers;
import client.Client;
import client.Family;
import client.MapleFamilyEntry;
import client.FamilyEntry;
import config.YamlConfig;
import net.AbstractPacketHandler;
import net.packet.InPacket;
@@ -34,7 +34,7 @@ public class FamilySeparateHandler extends AbstractPacketHandler {
if(!YamlConfig.config.server.USE_FAMILY_SYSTEM) return;
Family oldFamily = c.getPlayer().getFamily();
if(oldFamily == null) return;
MapleFamilyEntry forkOn = null;
FamilyEntry forkOn = null;
boolean isSenior;
if(p.available() > 0) { //packet 0x95 doesn't send id, since there is only one senior
forkOn = c.getPlayer().getFamily().getEntryByID(p.readInt());
@@ -46,7 +46,7 @@ public class FamilySeparateHandler extends AbstractPacketHandler {
}
if(forkOn == null) return;
MapleFamilyEntry senior = forkOn.getSenior();
FamilyEntry senior = forkOn.getSenior();
if(senior == null) return;
int levelDiff = Math.abs(c.getPlayer().getLevel() - senior.getLevel());
int cost = 2500 * levelDiff;
@@ -67,7 +67,7 @@ public class FamilySeparateHandler extends AbstractPacketHandler {
}
private static int separateRepCost(MapleFamilyEntry junior) {
private static int separateRepCost(FamilyEntry junior) {
int level = junior.getLevel();
int ret = level / 20;
ret += 10;

View File

@@ -3,7 +3,7 @@ package net.server.channel.handlers;
import client.Character;
import client.Client;
import client.FamilyEntitlement;
import client.MapleFamilyEntry;
import client.FamilyEntry;
import config.YamlConfig;
import net.AbstractPacketHandler;
import net.packet.InPacket;
@@ -24,7 +24,7 @@ public class FamilySummonResponseHandler extends AbstractPacketHandler {
MapleInviteResult inviteResult = MapleInviteCoordinator.answerInvite(InviteType.FAMILY_SUMMON, c.getPlayer().getId(), c.getPlayer(), accept);
if(inviteResult.result == InviteResult.NOT_FOUND) return;
Character inviter = inviteResult.from;
MapleFamilyEntry inviterEntry = inviter.getFamilyEntry();
FamilyEntry inviterEntry = inviter.getFamilyEntry();
if(inviterEntry == null) return;
MapleMap map = (MapleMap) inviteResult.params[0];
if(accept && inviter.getMap() == map) { //cancel if inviter has changed maps

View File

@@ -24,7 +24,7 @@ package net.server.channel.handlers;
import client.Character;
import client.Client;
import client.FamilyEntitlement;
import client.MapleFamilyEntry;
import client.FamilyEntry;
import config.YamlConfig;
import net.AbstractPacketHandler;
import net.packet.InPacket;
@@ -47,7 +47,7 @@ public final class FamilyUseHandler extends AbstractPacketHandler {
}
FamilyEntitlement type = FamilyEntitlement.values()[p.readInt()];
int cost = type.getRepCost();
MapleFamilyEntry entry = c.getPlayer().getFamilyEntry();
FamilyEntry entry = c.getPlayer().getFamilyEntry();
if(entry.getReputation() < cost || entry.isEntitlementUsed(type)) {
return; // shouldn't even be able to request it
}
@@ -130,7 +130,7 @@ public final class FamilyUseHandler extends AbstractPacketHandler {
}
}
private boolean useEntitlement(MapleFamilyEntry entry, FamilyEntitlement entitlement) {
private boolean useEntitlement(FamilyEntry entry, FamilyEntitlement entitlement) {
if(entry.useEntitlement(entitlement)) {
entry.gainReputation(-entitlement.getRepCost(), false);
entry.getChr().sendPacket(PacketCreator.getFamilyInfo(entry));

View File

@@ -249,7 +249,7 @@ public final class PlayerLoggedinHandler extends AbstractPacketHandler {
if (player.getFamilyId() > 0) {
Family f = wserv.getFamily(player.getFamilyId());
if (f != null) {
MapleFamilyEntry familyEntry = f.getEntryByID(player.getId());
FamilyEntry familyEntry = f.getEntryByID(player.getId());
if (familyEntry != null) {
familyEntry.setCharacter(player);
player.setFamilyEntry(familyEntry);

View File

@@ -943,7 +943,7 @@ public class MapleMonster extends AbstractLoadedMapleLife {
}
}
private void giveFamilyRep(MapleFamilyEntry entry) {
private void giveFamilyRep(FamilyEntry entry) {
if(entry != null) {
int repGain = isBoss() ? YamlConfig.config.server.FAMILY_REP_PER_BOSS_KILL : YamlConfig.config.server.FAMILY_REP_PER_KILL;
if(getMaxHp() <= 1) repGain = 0; //don't count trash mobs

View File

@@ -5801,7 +5801,7 @@ public class PacketCreator {
return p;
}
public static Packet getFamilyInfo(MapleFamilyEntry f) {
public static Packet getFamilyInfo(FamilyEntry f) {
if (f == null) {
return getEmptyFamilyInfo();
}
@@ -5839,10 +5839,10 @@ public class PacketCreator {
return p;
}
public static Packet showPedigree(MapleFamilyEntry entry) {
public static Packet showPedigree(FamilyEntry entry) {
final OutPacket p = OutPacket.create(SendOpcode.FAMILY_CHART_RESULT);
p.writeInt(entry.getChrId()); //ID of viewed player's pedigree, can't be leader?
List<MapleFamilyEntry> superJuniors = new ArrayList<>(4);
List<FamilyEntry> superJuniors = new ArrayList<>(4);
boolean hasOtherJunior = false;
int entryCount = 2; //2 guaranteed, leader and self
entryCount += Math.min(2, entry.getTotalSeniors());
@@ -5853,12 +5853,12 @@ public class PacketCreator {
hasOtherJunior = true;
}
}
for (MapleFamilyEntry junior : entry.getJuniors()) {
for (FamilyEntry junior : entry.getJuniors()) {
if (junior == null) {
continue;
}
entryCount++;
for (MapleFamilyEntry superJunior : junior.getJuniors()) {
for (FamilyEntry superJunior : junior.getJuniors()) {
if (superJunior == null) {
continue;
}
@@ -5881,7 +5881,7 @@ public class PacketCreator {
}
addPedigreeEntry(p, entry);
if (hasOtherJunior) { //must be sent after own entry
MapleFamilyEntry otherJunior = entry.getSenior().getOtherJunior(entry);
FamilyEntry otherJunior = entry.getSenior().getOtherJunior(entry);
if (otherJunior != null) {
addPedigreeEntry(p, otherJunior);
}
@@ -5889,12 +5889,12 @@ public class PacketCreator {
if (missingEntries) {
addPedigreeEntry(p, entry);
}
for (MapleFamilyEntry junior : entry.getJuniors()) {
for (FamilyEntry junior : entry.getJuniors()) {
if (junior == null) {
continue;
}
addPedigreeEntry(p, junior);
for (MapleFamilyEntry superJunior : junior.getJuniors()) {
for (FamilyEntry superJunior : junior.getJuniors()) {
if (superJunior != null) {
addPedigreeEntry(p, superJunior);
}
@@ -5906,7 +5906,7 @@ public class PacketCreator {
p.writeInt(entry.getFamily().getTotalMembers());
p.writeInt(0);
p.writeInt(entry.getTotalSeniors()); //client subtracts provided seniors
for (MapleFamilyEntry superJunior : superJuniors) {
for (FamilyEntry superJunior : superJuniors) {
p.writeInt(superJunior.getChrId());
p.writeInt(superJunior.getTotalJuniors());
}
@@ -5917,7 +5917,7 @@ public class PacketCreator {
return p;
}
private static void addPedigreeEntry(OutPacket p, MapleFamilyEntry entry) {
private static void addPedigreeEntry(OutPacket p, FamilyEntry entry) {
Character chr = entry.getChr();
boolean isOnline = chr != null;
p.writeInt(entry.getChrId()); //ID