Fix mob using attack with no diseaseSkill causing errors

This commit is contained in:
P0nk
2022-09-12 20:31:38 +02:00
parent 0c6548a36d
commit d31f4806fc
14 changed files with 71 additions and 45 deletions

View File

@@ -192,7 +192,7 @@ public class LifeFactory {
while (monsterSkillInfoData.getChildByPath(Integer.toString(i)) != null) {
int skillId = DataTool.getInt(i + "/skill", monsterSkillInfoData, 0);
int skillLv = DataTool.getInt(i + "/level", monsterSkillInfoData, 0);
MobSkillType type = MobSkillType.from(skillId);
MobSkillType type = MobSkillType.from(skillId).orElseThrow();
skills.add(new MobSkillId(type, skillLv));
Data monsterSkillData = monsterData.getChildByPath("skill" + (i + 1));
@@ -202,7 +202,7 @@ public class LifeFactory {
animationTime += DataTool.getIntConvert("delay", effectEntry, 0);
}
MobSkill skill = MobSkillFactory.getMobSkill(type, skillLv);
MobSkill skill = MobSkillFactory.getMobSkillOrThrow(type, skillLv);
mi.setMobSkillAnimationTime(skill, animationTime);
}

View File

@@ -47,20 +47,24 @@ public class MobSkillFactory {
private static final Lock readLock = readWriteLock.readLock();
private static final Lock writeLock = readWriteLock.writeLock();
public static MobSkill getMobSkill(final MobSkillType type, final int level) {
public static MobSkill getMobSkillOrThrow(MobSkillType type, int level) {
return getMobSkill(type, level).orElseThrow(
() -> new IllegalArgumentException("No MobSkill exists for type %s, level %d".formatted(type, level))
);
}
public static Optional<MobSkill> getMobSkill(final MobSkillType type, final int level) {
readLock.lock();
try {
MobSkill ms = mobSkills.get(createKey(type, level));
if (ms != null) {
return ms;
return Optional.of(ms);
}
} finally {
readLock.unlock();
}
return loadMobSkill(type, level).orElseThrow(
() -> new IllegalArgumentException("No MobSkill exists for type %s, level %d".formatted(type, level))
);
return loadMobSkill(type, level);
}
private static Optional<MobSkill> loadMobSkill(final MobSkillType type, final int level) {

View File

@@ -1,6 +1,7 @@
package server.life;
import java.util.Arrays;
import java.util.Optional;
public enum MobSkillType {
ATTACK_UP(100),
@@ -51,11 +52,18 @@ public enum MobSkillType {
this.id = id;
}
public static MobSkillType from(int id) {
public static Optional<MobSkillType> from(int id) {
if (isOutOfIdRange(id)) {
return Optional.empty();
}
return Arrays.stream(values())
.filter(type -> type.getId() == id)
.findFirst()
.orElseThrow(IllegalArgumentException::new);
.findAny();
}
private static boolean isOutOfIdRange(int id) {
return id < 100 || id > 200;
}
public int getId() {