Skip to content

Commit

Permalink
added Stage Event
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf2323 committed Nov 3, 2023
1 parent 6406207 commit e1dd928
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/betonquest/betonquest/BetonQuest.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
import org.betonquest.betonquest.quest.event.random.PickRandomEventFactory;
import org.betonquest.betonquest.quest.event.scoreboard.ScoreboardEventFactory;
import org.betonquest.betonquest.quest.event.setblock.SetBlockEventFactory;
import org.betonquest.betonquest.quest.event.stage.StageEventFactory;
import org.betonquest.betonquest.quest.event.sudo.OpSudoEventFactory;
import org.betonquest.betonquest.quest.event.sudo.SudoEventFactory;
import org.betonquest.betonquest.quest.event.tag.TagGlobalEventFactory;
Expand Down Expand Up @@ -926,6 +927,7 @@ public void onEnable() {
registerNonStaticEvent("weather", new WeatherEventFactory(loggerFactory, getServer(), getServer().getScheduler(), this));
registerEvents("folder", FolderEvent.class);
registerEvent("setblock", new SetBlockEventFactory(getServer(), getServer().getScheduler(), this));
registerNonStaticEvent("stage", new StageEventFactory(this));
registerNonStaticEvent("damage", new DamageEventFactory(loggerFactory, getServer(), getServer().getScheduler(), this));
registerNonStaticEvent("party", new PartyEventFactory(loggerFactory));
registerEvents("clear", ClearEvent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ public void setStage(final Profile profile, final String stage) throws QuestRunt
* Increases the stage of a profile.
*
* @param profile the profile
* @param amount the amount to increase
* @throws QuestRuntimeException if the stage is not a valid stage for the objective
*/
public void increaseStage(final Profile profile) throws QuestRuntimeException {
final String stage = getStage(profile);
final String nextStage;
public void increaseStage(final Profile profile, final int amount) throws QuestRuntimeException {
String nextStage = getStage(profile);
try {
nextStage = stageMap.nextStage(stage);
for (int i = 0; i < amount; i++) {
nextStage = stageMap.nextStage(nextStage);
}
} catch (final QuestRuntimeException e) {
if (!preventCompletion) {
completeObjective(profile);
Expand All @@ -160,34 +162,36 @@ public void increaseStage(final Profile profile) throws QuestRuntimeException {
setStage(profile, nextStage);
}

/**
* Returns the index of the stage of a profile.
*
* @param stage the stage
* @return the index of the stage
* @throws QuestRuntimeException if the stage is not a valid stage for the objective
*/
public int getStageIndex(final String stage) throws QuestRuntimeException {
return stageMap.getIndex(stage);
}

/**
* Decreases the stage of a profile.
*
* @param profile the profile
* @param amount the amount to increase
* @throws QuestRuntimeException if the stage is not a valid stage for the objective
*/
public void decreaseStage(final Profile profile) throws QuestRuntimeException {
final String stage = getStage(profile);
final String previousStage;
public void decreaseStage(final Profile profile, final int amount) throws QuestRuntimeException {
String previousStage = getStage(profile);
try {
previousStage = stageMap.previousStage(stage);
for (int i = 0; i < amount; i++) {
previousStage = stageMap.previousStage(previousStage);
}
} catch (final QuestRuntimeException e) {
return;
}
setStage(profile, previousStage);
}

/**
* Returns the index of the stage of a profile.
*
* @param stage the stage
* @return the index of the stage
* @throws QuestRuntimeException if the stage is not a valid stage for the objective
*/
public int getStageIndex(final String stage) throws QuestRuntimeException {
return stageMap.getIndex(stage);
}

/**
* {@link org.betonquest.betonquest.api.Objective.ObjectiveData} for {@link StageObjective}.
*/
Expand Down
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;
}
}
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)));
}
}
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;

0 comments on commit e1dd928

Please sign in to comment.