forked from BetonQuest/BetonQuest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/betonquest/betonquest/quest/event/stage/StageEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.betonquest.betonquest.quest.event.stage; | ||
|
||
import org.betonquest.betonquest.api.profiles.Profile; | ||
import org.betonquest.betonquest.api.quest.event.Event; | ||
import org.betonquest.betonquest.exceptions.QuestRuntimeException; | ||
|
||
/** | ||
* The StageEvent class to make changes to a player's stage. | ||
*/ | ||
public class StageEvent implements Event { | ||
/** | ||
* The action to perform. | ||
*/ | ||
private final StageAction action; | ||
|
||
/** | ||
* Creates the stage event. | ||
* | ||
* @param action the action to perform | ||
*/ | ||
public StageEvent(final StageAction action) { | ||
this.action = action; | ||
} | ||
|
||
@Override | ||
public void execute(final Profile profile) throws QuestRuntimeException { | ||
action.execute(profile); | ||
} | ||
|
||
/** | ||
* The stage action interface. | ||
*/ | ||
public interface StageAction { | ||
/** | ||
* Execute the action. | ||
* | ||
* @param profile the profile to execute the action for | ||
* @throws QuestRuntimeException when the action fails | ||
*/ | ||
void execute(Profile profile) throws QuestRuntimeException; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/betonquest/betonquest/quest/event/stage/StageEventFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.betonquest.betonquest.quest.event.stage; | ||
|
||
import org.betonquest.betonquest.BetonQuest; | ||
import org.betonquest.betonquest.Instruction; | ||
import org.betonquest.betonquest.VariableNumber; | ||
import org.betonquest.betonquest.VariableString; | ||
import org.betonquest.betonquest.api.quest.event.Event; | ||
import org.betonquest.betonquest.api.quest.event.EventFactory; | ||
import org.betonquest.betonquest.exceptions.InstructionParseException; | ||
import org.betonquest.betonquest.objectives.StageObjective; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Factory to create stage events to modify a StageObjective. | ||
*/ | ||
public class StageEventFactory implements EventFactory { | ||
/** | ||
* BetonQuest instance. | ||
*/ | ||
private final BetonQuest betonQuest; | ||
|
||
/** | ||
* Create the stage event factory. | ||
* | ||
* @param betonQuest BetonQuest instance | ||
*/ | ||
public StageEventFactory(final BetonQuest betonQuest) { | ||
this.betonQuest = betonQuest; | ||
} | ||
|
||
@Override | ||
public Event parseEvent(final Instruction instruction) throws InstructionParseException { | ||
final StageObjective stageObjective; | ||
try { | ||
stageObjective = (StageObjective) betonQuest.getObjective(instruction.getObjective()); | ||
} catch (final ClassCastException e) { | ||
throw new InstructionParseException("Objective '" + instruction.getObjective() + "' is not a stage objective", e); | ||
} | ||
final String action = instruction.next(); | ||
return switch (action.toLowerCase(Locale.ROOT)) { | ||
case "set" -> createSetEvent(instruction, stageObjective); | ||
case "increase" -> createIncreaseEvent(instruction, stageObjective); | ||
case "decrease" -> createDecreaseEvent(instruction, stageObjective); | ||
default -> throw new InstructionParseException("Unknown action '" + action + "'"); | ||
}; | ||
} | ||
|
||
private Event createSetEvent(final Instruction instruction, final StageObjective stageObjective) throws InstructionParseException { | ||
final VariableString variableString = new VariableString(instruction.getPackage(), instruction.next()); | ||
return new StageEvent(profile -> stageObjective.setStage(profile, variableString.getString(profile))); | ||
} | ||
|
||
private Event createIncreaseEvent(final Instruction instruction, final StageObjective stageObjective) throws InstructionParseException { | ||
final VariableNumber amount = instruction.getVarNum(); | ||
return new StageEvent(profile -> stageObjective.increaseStage(profile, amount.getInt(profile))); | ||
} | ||
|
||
private Event createDecreaseEvent(final Instruction instruction, final StageObjective stageObjective) throws InstructionParseException { | ||
final VariableNumber amount = instruction.getVarNum(); | ||
return new StageEvent(profile -> stageObjective.decreaseStage(profile, amount.getInt(profile))); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/betonquest/betonquest/quest/event/stage/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* {@link org.betonquest.betonquest.api.quest.event.Event Event} implementation of the stage event. | ||
*/ | ||
package org.betonquest.betonquest.quest.event.stage; |