Skip to content

Commit

Permalink
improve stage objective
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf2323 committed Nov 5, 2023
1 parent c296f19 commit f16121d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ protected Double getFirst(final Profile profile) throws QuestRuntimeException {
if (stage.getData(profile) == null) {
return -1.0;
}
return (double) stage.getStageIndex(stage.getStage(profile));
return (double) stage.getStageIndex(stage.getStage(profile)); // TODO return false for invalid stages
}

@Override
protected Double getSecond(final Profile profile) throws QuestRuntimeException {
final StageObjective stage = getStageObjective();
return (double) stage.getStageIndex(second.getString(profile));
return (double) stage.getStageIndex(second.getString(profile)); // TODO return false for invalid stages
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,11 @@ public class StageObjective extends Objective {
*/
private final StageMap stageMap;

/**
* The stage that is used to complete the objective.
*/
private final String completionStage;

/**
* True if the increase of stages should not complete the objective.
*/
private final boolean preventCompletion;

/**
* The fallback stage that is used for the profile if the current stage is not a valid stage.
*/
private final String fallbackStage;

/**
* Creates a new stage objective.
*
Expand All @@ -55,12 +45,7 @@ public StageObjective(final Instruction instruction) throws InstructionParseExce
template = StageData.class;

this.stageMap = new StageMap(instruction.getList(entry -> entry));
this.completionStage = instruction.getOptional("completion");
this.preventCompletion = instruction.hasArgument("preventCompletion");
this.fallbackStage = instruction.getOptional("fallback");
if (fallbackStage != null && !stageMap.isValidStage(fallbackStage)) {
throw new InstructionParseException("Fallback stage must be one of the stages");
}
}

@Override
Expand All @@ -87,7 +72,7 @@ public String getDefaultDataInstruction() {
public String getProperty(final String name, final Profile profile) {
try {
return switch (name.toLowerCase(Locale.ROOT)) {
case "index" -> String.valueOf(stageMap.getIndex(getStage(profile)));
case "index" -> String.valueOf(stageMap.getIndex(getStage(profile) + 1));
case "current" -> getStage(profile);
case "next" -> stageMap.nextStage(getStage(profile));
case "previous" -> stageMap.previousStage(getStage(profile));
Expand All @@ -112,11 +97,6 @@ public String getStage(final Profile profile) throws QuestRuntimeException {
if (stageMap.isValidStage(stage)) {
return stage;
}
if (fallbackStage != null) {
log.warn(instruction.getPackage(), profile + " has invalid stage '" + stage + "' for objective '" + instruction.getID() + "'. Using fallback stage '" + fallbackStage + "'.");
stageData.setStage(fallbackStage);
return fallbackStage;
}
throw new QuestRuntimeException(profile + " has invalid stage '" + stage + "' for objective '" + instruction.getID() + "'.");
}

Expand All @@ -130,11 +110,9 @@ public String getStage(final Profile profile) throws QuestRuntimeException {
public void setStage(final Profile profile, final String stage) throws QuestRuntimeException {
final StageData stageData = (StageObjective.StageData) dataMap.get(profile);
if (stageMap.isValidStage(stage)) {
stageData.setStage(stage);
return;
}
if (stage.equals(completionStage)) {
completeObjective(profile);
if (checkConditions(profile)) {
stageData.setStage(stage);
}
return;
}
throw new QuestRuntimeException("Invalid stage '" + stage + "' for objective '" + instruction.getID() + "'.");
Expand All @@ -151,6 +129,9 @@ public void increaseStage(final Profile profile, final int amount) throws QuestR
String nextStage = getStage(profile);
try {
for (int i = 0; i < amount; i++) {
if (!checkConditions(profile)) {
break;
}
nextStage = stageMap.nextStage(nextStage);
}
} catch (final QuestRuntimeException e) {
Expand Down

0 comments on commit f16121d

Please sign in to comment.