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 constants.id.MapId;
import constants.id.MobId;
import constants.skills.Bishop;
import net.server.services.task.channel.OverallService;
import net.server.services.type.ChannelServices;
import org.slf4j.Logger;
@@ -49,66 +50,130 @@ public class MobSkill {
private final MobSkillType type;
private final int skillLevel;
private int mpCon;
private final List<Integer> toSummon = new ArrayList<>();
private int spawnEffect, hp, x, y, count;
private long duration, cooltime;
private float prop;
private Point lt, rb;
private int limit;
private final int mpCon;
private final int spawnEffect;
private final int hp;
private final int x;
private final int y;
private final int count;
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.skillLevel = level;
}
public void setMpCon(int mpCon) {
this.mpCon = mpCon;
}
public void addSummons(List<Integer> toSummon) {
this.toSummon.addAll(toSummon);
}
public void setSpawnEffect(int spawnEffect) {
this.spawnEffect = spawnEffect;
}
public void setHp(int hp) {
this.hp = hp;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setCount(int count) {
this.count = count;
}
public void setDuration(long duration) {
this.duration = duration;
}
public void setCoolTime(long cooltime) {
this.cooltime = cooltime;
}
public void setProp(float prop) {
this.prop = prop;
}
public void setLtRb(Point lt, Point rb) {
this.lt = lt;
this.rb = rb;
this.limit = limit;
this.toSummon = toSummon;
}
public void setLimit(int limit) {
this.limit = limit;
static class Builder {
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) {
@@ -293,7 +358,7 @@ public class MobSkill {
if (lt != null && rb != null && skill) {
int i = 0;
for (Character character : getPlayersInRange(monster)) {
if (!character.hasActiveBuff(2321005)) { // holy shield
if (!character.hasActiveBuff(Bishop.HOLY_SHIELD)) {
if (disease.equals(Disease.SEDUCE)) {
if (i < count) {
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 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
readLock.lock();
try {
MobSkill ret = mobSkills.get(createKey(skillId, level));
if (ret != null) {
return ret;
MobSkill ms = mobSkills.get(createKey(skillId, level));
if (ms != null) {
return ms;
}
} finally {
readLock.unlock();
@@ -64,9 +65,9 @@ public class MobSkillFactory {
private static Optional<MobSkill> loadMobSkill(final int skillId, final int level) {
writeLock.lock();
try {
MobSkill ms = mobSkills.get(createKey(skillId, level));
if (ms != null) {
return Optional.of(ms);
MobSkill existingMs = mobSkills.get(createKey(skillId, level));
if (existingMs != null) {
return Optional.of(existingMs);
}
Data skillData = skillRoot.getChildByPath(skillId + "/level/" + level);
@@ -102,22 +103,22 @@ public class MobSkillFactory {
rb = (Point) rbData.getData();
}
ms = new MobSkill(MobSkillType.from(skillId), level);
ms.addSummons(toSummon);
ms.setCoolTime(cooltime);
ms.setDuration(duration);
ms.setHp(hp);
ms.setMpCon(mpCon);
ms.setSpawnEffect(effect);
ms.setX(x);
ms.setY(y);
ms.setCount(count);
ms.setProp(prop);
ms.setLimit(limit);
ms.setLtRb(lt, rb);
MobSkill loadedMobSkill = new MobSkill.Builder(MobSkillType.from(skillId), level)
.toSummon(toSummon)
.cooltime(cooltime)
.duration(duration)
.hp(hp)
.x(x)
.y(y)
.count(count)
.prop(prop)
.limit(limit)
.lt(lt)
.rb(rb)
.build();
mobSkills.put(createKey(skillId, level), ms);
return Optional.of(ms);
mobSkills.put(createKey(skillId, level), loadedMobSkill);
return Optional.of(loadedMobSkill);
} finally {
writeLock.unlock();
}