Skip to content

Commit

Permalink
folder event now has a argument cancelConditions to cancel a runn…
Browse files Browse the repository at this point in the history
…ing folder
  • Loading branch information
Wolf2323 committed Dec 18, 2024
1 parent 5ba8e46 commit ff1f3fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] - ${maven.build.timestamp}
### Added
- A Bukkit event which fires when a player's points change
- `folder` event now has a argument `cancelConditions` to cancel a running folder
### Changed
### Deprecated
### Removed
Expand Down
17 changes: 9 additions & 8 deletions docs/Documentation/Scripting/Building-Blocks/Events-List.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,15 @@ so those events should not be blocked by any conditions!
You can use the `cancelOnLogout` argument to stop the folder executing any remaining events if the player disconnects.


| Parameter | Syntax | Default Value | Explanation |
|------------------|-------------------|------------------------|-------------------------------------------------------------------------------------------------|
| _events to run_ | eventName1,event2 | :octicons-x-circle-16: | One or multiple events to run. Contains event names seperated by commas. |
| _delay_ | Keyword | without delay | The delay before the folder starts executing it's events. |
| _period_ | period:number | without delay | The time between each event of the folder. |
| _time unit_ | Keyword | Seconds | The unit of time to use for delay and period. Either `ticks` or `minutes`. Omit to use seconds. |
| _random_ | random:number | Disabled | Enables "random mode". Will randomly pick the defined amount of events . |
| _cancelOnLogout_ | Keyword | Disabled | If enabled, the folder will stop executing events if the player disconnects. |
| Parameter | Syntax | Default Value | Explanation |
|--------------------|------------------------------|------------------------|-------------------------------------------------------------------------------------------------|
| _events to run_ | eventName1,event2 | :octicons-x-circle-16: | One or multiple events to run. Contains event names seperated by commas. |
| _delay_ | Keyword | without delay | The delay before the folder starts executing it's events. |
| _period_ | period:number | without delay | The time between each event of the folder. |
| _time unit_ | Keyword | Seconds | The unit of time to use for delay and period. Either `ticks` or `minutes`. Omit to use seconds. |
| _random_ | random:number | Disabled | Enables "random mode". Will randomly pick the defined amount of events . |
| _cancelOnLogout_ | Keyword | Disabled | If enabled, the folder will stop executing events if the player disconnects. |
| _cancelConditions_ | cancelConditions:cond1,cond2 | Disabled | If enabled, the folder will stop executing events if the conditions are true. |


```YAML title="Examples"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.betonquest.betonquest.api.profiles.Profile;
import org.betonquest.betonquest.api.quest.event.nullable.NullableEvent;
import org.betonquest.betonquest.exceptions.QuestRuntimeException;
import org.betonquest.betonquest.id.ConditionID;
import org.betonquest.betonquest.id.EventID;
import org.betonquest.betonquest.instruction.variable.VariableNumber;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -77,26 +78,32 @@ public class FolderEvent implements NullableEvent {
private final TimeUnit timeUnit;

/**
* Whether the event should be cancelled on logout.
* Whether the event should be canceled on logout.
*/
private final boolean cancelOnLogout;

/**
* Conditions to check if the event should be canceled.
*/
private final ConditionID[] cancelConditions;

/**
* Create a folder event with the given parameters.
*
* @param betonQuest the BetonQuest instance
* @param log custom logger for this class
* @param pluginManager the plugin manager to register the quit listener
* @param events events to run
* @param delay delay to apply before running the events
* @param period delay to apply between each event
* @param random number of events to run
* @param timeUnit time unit to use for the delay and period
* @param cancelOnLogout whether the event should be cancelled on logout
* @param betonQuest the BetonQuest instance
* @param log custom logger for this class
* @param pluginManager the plugin manager to register the quit listener
* @param events events to run
* @param delay delay to apply before running the events
* @param period delay to apply between each event
* @param random number of events to run
* @param timeUnit time unit to use for the delay and period
* @param cancelOnLogout whether the event should be canceled on logout
* @param cancelConditions conditions to check if the event should be canceled
*/
public FolderEvent(final BetonQuest betonQuest, final BetonQuestLogger log, final PluginManager pluginManager, final EventID[] events, @Nullable final VariableNumber delay,
@Nullable final VariableNumber period, @Nullable final VariableNumber random,
final TimeUnit timeUnit, final boolean cancelOnLogout) {
final TimeUnit timeUnit, final boolean cancelOnLogout, final ConditionID[] cancelConditions) {
this.betonQuest = betonQuest;
this.log = log;
this.pluginManager = pluginManager;
Expand All @@ -106,10 +113,18 @@ public FolderEvent(final BetonQuest betonQuest, final BetonQuestLogger log, fina
this.events = Arrays.copyOf(events, events.length);
this.timeUnit = timeUnit;
this.cancelOnLogout = cancelOnLogout;
this.cancelConditions = cancelConditions;
}

private boolean checkCancelConditions(@Nullable final Profile profile) {
return BetonQuest.conditions(profile, cancelConditions);
}

private void executeAllEvents(@Nullable final Profile profile, final Deque<EventID> chosenList) {
for (final EventID event : chosenList) {
if (checkCancelConditions(profile)) {
return;
}
BetonQuest.event(profile, event);
}
}
Expand All @@ -131,6 +146,9 @@ public void execute(@Nullable final Profile profile) throws QuestRuntimeExceptio
private void handleDelayPeriod(@Nullable final Profile profile, final long delayTicks, final Deque<EventID> chosenList, final long periodTicks) {
if (delayTicks == 0 && !chosenList.isEmpty()) {
final EventID event = chosenList.removeFirst();
if (checkCancelConditions(profile)) {
return;
}
BetonQuest.event(profile, event);
}
if (!chosenList.isEmpty()) {
Expand All @@ -139,7 +157,7 @@ private void handleDelayPeriod(@Nullable final Profile profile, final long delay
@Override
public void run() {
final EventID event = chosenList.pollFirst();
if (eventCanceller.isCancelled() || event == null) {
if (eventCanceller.isCancelled() || event == null || checkCancelConditions(profile)) {
eventCanceller.destroy();
this.cancel();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.betonquest.betonquest.api.quest.event.StaticEventFactory;
import org.betonquest.betonquest.api.quest.event.nullable.NullableEventAdapter;
import org.betonquest.betonquest.exceptions.InstructionParseException;
import org.betonquest.betonquest.id.ConditionID;
import org.betonquest.betonquest.id.EventID;
import org.betonquest.betonquest.instruction.variable.VariableNumber;
import org.bukkit.plugin.PluginManager;
Expand Down Expand Up @@ -63,7 +64,8 @@ private NullableEventAdapter createFolderEvent(final Instruction instruction) th
final VariableNumber random = instruction.getVarNum(instruction.getOptional("random"));
final TimeUnit timeUnit = getTimeUnit(instruction);
final boolean cancelOnLogout = instruction.hasArgument("cancelOnLogout");
return new NullableEventAdapter(new FolderEvent(betonQuest, loggerFactory.create(FolderEvent.class), pluginManager, events, delay, period, random, timeUnit, cancelOnLogout));
final ConditionID[] cancelConditions = instruction.getList(instruction.getOptional("cancelConditions"), instruction::getCondition).toArray(new ConditionID[0]);
return new NullableEventAdapter(new FolderEvent(betonQuest, loggerFactory.create(FolderEvent.class), pluginManager, events, delay, period, random, timeUnit, cancelOnLogout, cancelConditions));
}

private TimeUnit getTimeUnit(final Instruction instruction) {
Expand Down

0 comments on commit ff1f3fc

Please sign in to comment.