Make MobSkill immutable using builder pattern

This commit is contained in:
P0nk
2022-09-02 08:44:19 +02:00
parent 6e62a6a45d
commit 455d4b2195
2 changed files with 132 additions and 66 deletions

View File

@@ -26,6 +26,7 @@ import client.Disease;
import client.status.MonsterStatus; import client.status.MonsterStatus;
import constants.id.MapId; import constants.id.MapId;
import constants.id.MobId; import constants.id.MobId;
import constants.skills.Bishop;
import net.server.services.task.channel.OverallService; import net.server.services.task.channel.OverallService;
import net.server.services.type.ChannelServices; import net.server.services.type.ChannelServices;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -49,66 +50,130 @@ public class MobSkill {
private final MobSkillType type; private final MobSkillType type;
private final int skillLevel; private final int skillLevel;
private int mpCon; private final int mpCon;
private final List<Integer> toSummon = new ArrayList<>(); private final int spawnEffect;
private int spawnEffect, hp, x, y, count; private final int hp;
private long duration, cooltime; private final int x;
private float prop; private final int y;
private Point lt, rb; private final int count;
private int limit; private final long duration;
private final long cooltime;
private final float prop;
private final Point lt;
private final Point rb;
private final int limit;
private final List<Integer> toSummon;
public MobSkill(MobSkillType type, int level) { private MobSkill(MobSkillType type, int level, int mpCon, int spawnEffect, int hp, int x, int y, int count,
long duration, long cooltime, float prop, Point lt, Point rb, int limit, List<Integer> toSummon) {
this.type = type; this.type = type;
this.skillLevel = level; this.skillLevel = level;
}
public void setMpCon(int mpCon) {
this.mpCon = mpCon; this.mpCon = mpCon;
}
public void addSummons(List<Integer> toSummon) {
this.toSummon.addAll(toSummon);
}
public void setSpawnEffect(int spawnEffect) {
this.spawnEffect = spawnEffect; this.spawnEffect = spawnEffect;
}
public void setHp(int hp) {
this.hp = hp; this.hp = hp;
}
public void setX(int x) {
this.x = x; this.x = x;
}
public void setY(int y) {
this.y = y; this.y = y;
}
public void setCount(int count) {
this.count = count; this.count = count;
}
public void setDuration(long duration) {
this.duration = duration; this.duration = duration;
}
public void setCoolTime(long cooltime) {
this.cooltime = cooltime; this.cooltime = cooltime;
}
public void setProp(float prop) {
this.prop = prop; this.prop = prop;
}
public void setLtRb(Point lt, Point rb) {
this.lt = lt; this.lt = lt;
this.rb = rb; this.rb = rb;
this.limit = limit;
this.toSummon = toSummon;
} }
public void setLimit(int limit) { static class Builder {
this.limit = limit; private final MobSkillType type;
private final int level;
private int mpCon;
private int spawnEffect;
private int hp;
private int x;
private int y;
private int count;
private long duration;
private long cooltime;
private float prop;
private Point lt;
private Point rb;
private int limit;
private List<Integer> toSummon;
public Builder(MobSkillType type, int level) {
this.type = type;
this.level = level;
}
public Builder mpCon(int mpCon) {
this.mpCon = mpCon;
return this;
}
public Builder spawnEffect(int spawnEffect) {
this.spawnEffect = spawnEffect;
return this;
}
public Builder hp(int hp) {
this.hp = hp;
return this;
}
public Builder x(int x) {
this.x = x;
return this;
}
public Builder y(int y) {
this.y = y;
return this;
}
public Builder count(int count) {
this.count = count;
return this;
}
public Builder duration(long duration) {
this.duration = duration;
return this;
}
public Builder cooltime(long cooltime) {
this.cooltime = cooltime;
return this;
}
public Builder prop(float prop) {
this.prop = prop;
return this;
}
public Builder lt(Point lt) {
this.lt = lt;
return this;
}
public Builder rb(Point rb) {
this.rb = rb;
return this;
}
public Builder limit(int limit) {
this.limit = limit;
return this;
}
public Builder toSummon(List<Integer> toSummon) {
this.toSummon = Collections.unmodifiableList(toSummon);
return this;
}
public MobSkill build() {
return new MobSkill(type, level, mpCon, spawnEffect, hp, x, y, count, duration, cooltime, prop, lt, rb,
limit, toSummon);
}
} }
public void applyDelayedEffect(final Character player, final Monster monster, final boolean skill, int animationTime) { public void applyDelayedEffect(final Character player, final Monster monster, final boolean skill, int animationTime) {
@@ -293,7 +358,7 @@ public class MobSkill {
if (lt != null && rb != null && skill) { if (lt != null && rb != null && skill) {
int i = 0; int i = 0;
for (Character character : getPlayersInRange(monster)) { for (Character character : getPlayersInRange(monster)) {
if (!character.hasActiveBuff(2321005)) { // holy shield if (!character.hasActiveBuff(Bishop.HOLY_SHIELD)) {
if (disease.equals(Disease.SEDUCE)) { if (disease.equals(Disease.SEDUCE)) {
if (i < count) { if (i < count) {
character.giveDebuff(Disease.SEDUCE, this); character.giveDebuff(Disease.SEDUCE, this);

View File

@@ -47,12 +47,13 @@ public class MobSkillFactory {
private static final Lock readLock = readWriteLock.readLock(); private static final Lock readLock = readWriteLock.readLock();
private static final Lock writeLock = readWriteLock.writeLock(); private static final Lock writeLock = readWriteLock.writeLock();
// TODO: take in MobSkillType as argument instead of skillId
public static MobSkill getMobSkill(final int skillId, final int level) { // TODO: return Optional public static MobSkill getMobSkill(final int skillId, final int level) { // TODO: return Optional
readLock.lock(); readLock.lock();
try { try {
MobSkill ret = mobSkills.get(createKey(skillId, level)); MobSkill ms = mobSkills.get(createKey(skillId, level));
if (ret != null) { if (ms != null) {
return ret; return ms;
} }
} finally { } finally {
readLock.unlock(); readLock.unlock();
@@ -64,9 +65,9 @@ public class MobSkillFactory {
private static Optional<MobSkill> loadMobSkill(final int skillId, final int level) { private static Optional<MobSkill> loadMobSkill(final int skillId, final int level) {
writeLock.lock(); writeLock.lock();
try { try {
MobSkill ms = mobSkills.get(createKey(skillId, level)); MobSkill existingMs = mobSkills.get(createKey(skillId, level));
if (ms != null) { if (existingMs != null) {
return Optional.of(ms); return Optional.of(existingMs);
} }
Data skillData = skillRoot.getChildByPath(skillId + "/level/" + level); Data skillData = skillRoot.getChildByPath(skillId + "/level/" + level);
@@ -102,22 +103,22 @@ public class MobSkillFactory {
rb = (Point) rbData.getData(); rb = (Point) rbData.getData();
} }
ms = new MobSkill(MobSkillType.from(skillId), level); MobSkill loadedMobSkill = new MobSkill.Builder(MobSkillType.from(skillId), level)
ms.addSummons(toSummon); .toSummon(toSummon)
ms.setCoolTime(cooltime); .cooltime(cooltime)
ms.setDuration(duration); .duration(duration)
ms.setHp(hp); .hp(hp)
ms.setMpCon(mpCon); .x(x)
ms.setSpawnEffect(effect); .y(y)
ms.setX(x); .count(count)
ms.setY(y); .prop(prop)
ms.setCount(count); .limit(limit)
ms.setProp(prop); .lt(lt)
ms.setLimit(limit); .rb(rb)
ms.setLtRb(lt, rb); .build();
mobSkills.put(createKey(skillId, level), ms); mobSkills.put(createKey(skillId, level), loadedMobSkill);
return Optional.of(ms); return Optional.of(loadedMobSkill);
} finally { } finally {
writeLock.unlock(); writeLock.unlock();
} }